protected function execute(InputInterface $input, OutputInterface $output) { if (NULL !== ($file = $input->getOption('file'))) { if (is_file($file) && !$input->getOption('overwrite')) { $output->writeln(sprintf('File <info>%s</info> already exists, use option <comment>-o</comment> to overwrite it.', $file)); return; } $fp = Filesystem::openWriteStream($file); foreach (explode("\n", trim($this->config)) as $line) { fwrite($fp, $line . "\n"); } fclose($fp); $output->writeln(sprintf('Config dumped to <info>%s</info>', $file)); } else { foreach (explode("\n", trim($this->config)) as $line) { $output->writeln($line); } } }
public function syncSources() { if (!$this->needsCheck && array_key_exists('loaders', $this->cached)) { $this->dump['loaders'] = $this->cached['loaders']; return; } $this->dump['loaders'] = []; $lock = Filesystem::openWriteStream($this->lockFile, 'wb', 0775, LOCK_EX); try { $manifest = $this->getManifest(); $manifest->beginTransaction(); try { $this->modified = $this->modified || $manifest->syncInternalTypes(); } catch (\Exception $e) { $manifest->rollBack(); throw $e; } $manifest->commit(); foreach ($this->collectLoaders() as $loader) { $key = md5($loader); $hash = hash_init('md5'); $this->collectSources($loader, function ($file) use(&$hash) { hash_update($hash, $file); hash_update($hash, '|' . filemtime($file) . '||'); }); $hashed = hash_final($hash); $this->dump['loaders'][$key] = $hashed; if (isset($this->cached['loaders'][$key]) && $this->cached['loaders'][$key] === $hashed) { // No changes in this loader... skip sync. continue; } $this->modified = true; $manifest->beginTransaction(); try { $inventory = $manifest->getInventory($key); $this->collectSources($loader, function ($file) use($inventory) { $inventory->check($file, filemtime($file)); }); if ($inventory->isModified()) { $manifest->syncInventory($inventory); } } catch (\Exception $e) { $manifest->rollBack(); throw $e; } $manifest->commit(); } $removed = []; if (isset($this->cached['loaders'])) { foreach ($this->cached['loaders'] as $key => $hash) { if (empty($this->dump['loaders'][$key])) { $removed[] = $key; } } } if (!empty($removed)) { $this->modified = true; $manifest->beginTransaction(); try { foreach ($removed as $key) { $manifest->removeByKey($key); } } catch (\Exception $e) { $manifest->rollBack(); throw $e; } $manifest->commit(); } if ($this->modified) { $manifest->beginTransaction(); try { $manifest->syncClassInheritance(); } catch (\Exception $e) { $manifest->rollBack(); throw $e; } $manifest->commit(); } } finally { @fclose($lock); } }
protected function writeFileContents($file, StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Resource content stream must be readable'); } $fp = Filesystem::openWriteStream($file, 'wb', 0755, LOCK_EX); try { while (!$stream->eof()) { fwrite($fp, $stream->read(4096)); } } finally { @fclose($fp); } }