X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fdevel%2Fwebprofiler%2Fsrc%2FCommand%2FExportCommand.php;fp=web%2Fmodules%2Fcontrib%2Fdevel%2Fwebprofiler%2Fsrc%2FCommand%2FExportCommand.php;h=20c1e41641a1807aea599c35b047f691c08e65d7;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/devel/webprofiler/src/Command/ExportCommand.php b/web/modules/contrib/devel/webprofiler/src/Command/ExportCommand.php new file mode 100644 index 000000000..20c1e4164 --- /dev/null +++ b/web/modules/contrib/devel/webprofiler/src/Command/ExportCommand.php @@ -0,0 +1,160 @@ +setName('webprofiler:export') + ->setDescription($this->trans('commands.webprofiler.export.description')) + ->addArgument('id', InputArgument::OPTIONAL, $this->trans('commands.webprofiler.export.arguments.id')) + ->addOption('directory', NULL, InputOption::VALUE_REQUIRED, $this->trans('commands.webprofiler.export.options.directory'), '/tmp'); + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $id = $input->getArgument('id'); + $directory = $input->getOption('directory'); + + /** @var \Drupal\webprofiler\Profiler\Profiler $profiler */ + $profiler = $this->container->get('profiler'); + + try { + if ($id) { + $this->filename = $this->exportSingle($profiler, $id, $directory); + } + else { + $this->filename = $this->exportAll($profiler, $directory, $output); + } + + } catch (\Exception $e) { + $output->writeln('' . $e->getMessage() . ''); + } + } + + /** + * Exports a single profile. + * + * @param \Drupal\webprofiler\Profiler\Profiler $profiler + * @param int $id + * @param string $directory + * + * @return string + * + * @throws \Exception + */ + private function exportSingle(Profiler $profiler, $id, $directory) { + $profile = $profiler->loadProfile($id); + if ($profile) { + $data = $profiler->export($profile); + + $filename = $directory . DIRECTORY_SEPARATOR . $id . '.txt'; + if (file_put_contents($filename, $data) === FALSE) { + throw new \Exception(sprintf( + $this->trans('commands.webprofiler.export.messages.error_writing'), + $filename)); + } + } + else { + throw new \Exception(sprintf( + $this->trans('commands.webprofiler.export.messages.error_no_profile'), + $id)); + } + + return $filename; + } + + /** + * Exports all stored profiles (cap limit at 1000 items). + * + * @param \Drupal\webprofiler\Profiler\Profiler $profiler + * @param string $directory + * @param \Symfony\Component\Console\Output\OutputInterface $output + * + * @return string + */ + private function exportAll(Profiler $profiler, $directory, $output) { + $filename = $directory . DIRECTORY_SEPARATOR . 'profiles_' . time() . '.tar.gz'; + $archiver = new ArchiveTar($filename, 'gz'); + $profiles = $profiler->find(NULL, NULL, 1000, NULL, '', ''); + $progress = new ProgressBar($output, count($profiles) + 2); + $progress->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%'); + + $files = []; + $progress->start(); + $progress->setMessage($this->trans('commands.webprofiler.export.progress.exporting')); + foreach ($profiles as $profile) { + $data = $profiler->export($profiler->loadProfile($profile['token'])); + $profileFilename = $directory . "/{$profile['token']}.txt"; + file_put_contents($profileFilename, $data); + $files[] = $profileFilename; + $progress->advance(); + } + + $progress->setMessage($this->trans('commands.webprofiler.export.progress.archive')); + $archiver->createModify($files, '', $directory); + $progress->advance(); + + $progress->setMessage($this->trans('commands.webprofiler.export.progress.delete_tmp')); + foreach ($files as $file) { + unlink($file); + } + $progress->advance(); + + $progress->setMessage($this->trans('commands.webprofiler.export.progress.done')); + $progress->finish(); + $output->writeln(''); + + $output->writeln(sprintf( + $this->trans('commands.webprofiler.export.messages.exported_count'), + count($profiles))); + + return $filename; + } + + /** + * {@inheritdoc} + */ + public function showMessage($output, $message, $type = 'info') { + if (!$this->filename) { + return; + } + + $completeMessageKey = 'commands.webprofiler.export.messages.success'; + $completeMessage = sprintf($this->trans($completeMessageKey), $this->filename); + + if ($completeMessage != $completeMessageKey) { + parent::showMessage($output, $completeMessage); + } + } +}