/** * Writes a file in two phase to the filesystem. * * First write the data to a temporary file (in the same directory) and than renames the temporary file. If the file * already exists and its content is equal to the data that must be written no action is taken. This has the * following advantages: * * In case of some write error (e.g. disk full) the original file is kept in tact and no file with partially data * is written. * * Renaming a file is atomic. So, running processes will never read a partially written data. * * @param string $filename The name of the file were the data must be stored. * @param string $data The data that must be written. * @param StratumStyle $io The output decorator. */ static function writeTwoPhases($filename, $data, $io) { $write_flag = true; if (file_exists($filename)) { $old_data = file_get_contents($filename); if ($data == $old_data) { $write_flag = false; } } if ($write_flag) { $tmp_filename = $filename . '.tmp'; file_put_contents($tmp_filename, $data); rename($tmp_filename, $filename); $io->text(sprintf('Wrote <fso>%s</fso>', OutputFormatter::escape($filename))); } else { $io->text(sprintf('File <fso>%s</fso> is up to date', OutputFormatter::escape($filename))); } }
/** * Outputs information about removed records. * * @param $record */ private function writeDeletions($record) { $this->io->text(sprintf('<sql>%s</sql>', OutputFormatter::escape('Deleted:'))); $output = "("; foreach ($record as $field_name => $field) { $output = $output . " {$field},"; } $output = rtrim($output, ", "); $output = $output . " )"; $this->io->text(sprintf('%s', $output)); }
/** * * @param array $unknown The unknown placeholders. */ private function logUnknownPlaceholders($unknown) { // Return immediately if there are no unknown placeholders. if (empty($unknown)) { return; } sort($unknown); $this->io->error('Unknown placeholder found'); $this->io->listing($unknown); $replace = []; foreach ($unknown as $placeholder) { $replace[$placeholder] = '<error>' . $placeholder . '</error>'; } $code = strtr(OutputFormatter::escape($this->routineSourceCode), $replace); $this->io->text(explode(PHP_EOL, $code)); }