3 namespace Drupal\webprofiler\Command;
5 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
6 use Drupal\Core\Archiver\ArchiveTar;
7 use Drupal\webprofiler\Profiler\Profiler;
8 use Symfony\Component\Console\Helper\ProgressBar;
9 use Symfony\Component\Console\Input\InputArgument;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Output\OutputInterface;
14 use Drupal\Console\Annotations\DrupalCommand;
20 * extension="webprofiler",
21 * extensionType="module"
24 class ExportCommand extends Command {
26 use ContainerAwareCommandTrait;
34 protected function configure() {
36 ->setName('webprofiler:export')
37 ->setDescription($this->trans('commands.webprofiler.export.description'))
38 ->addArgument('id', InputArgument::OPTIONAL, $this->trans('commands.webprofiler.export.arguments.id'))
39 ->addOption('directory', NULL, InputOption::VALUE_REQUIRED, $this->trans('commands.webprofiler.export.options.directory'), '/tmp');
45 protected function execute(InputInterface $input, OutputInterface $output) {
46 $id = $input->getArgument('id');
47 $directory = $input->getOption('directory');
49 /** @var \Drupal\webprofiler\Profiler\Profiler $profiler */
50 $profiler = $this->container->get('profiler');
54 $this->filename = $this->exportSingle($profiler, $id, $directory);
57 $this->filename = $this->exportAll($profiler, $directory, $output);
60 } catch (\Exception $e) {
61 $output->writeln('<error>' . $e->getMessage() . '</error>');
66 * Exports a single profile.
68 * @param \Drupal\webprofiler\Profiler\Profiler $profiler
70 * @param string $directory
76 private function exportSingle(Profiler $profiler, $id, $directory) {
77 $profile = $profiler->loadProfile($id);
79 $data = $profiler->export($profile);
81 $filename = $directory . DIRECTORY_SEPARATOR . $id . '.txt';
82 if (file_put_contents($filename, $data) === FALSE) {
83 throw new \Exception(sprintf(
84 $this->trans('commands.webprofiler.export.messages.error_writing'),
89 throw new \Exception(sprintf(
90 $this->trans('commands.webprofiler.export.messages.error_no_profile'),
98 * Exports all stored profiles (cap limit at 1000 items).
100 * @param \Drupal\webprofiler\Profiler\Profiler $profiler
101 * @param string $directory
102 * @param \Symfony\Component\Console\Output\OutputInterface $output
106 private function exportAll(Profiler $profiler, $directory, $output) {
107 $filename = $directory . DIRECTORY_SEPARATOR . 'profiles_' . time() . '.tar.gz';
108 $archiver = new ArchiveTar($filename, 'gz');
109 $profiles = $profiler->find(NULL, NULL, 1000, NULL, '', '');
110 $progress = new ProgressBar($output, count($profiles) + 2);
111 $progress->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%');
115 $progress->setMessage($this->trans('commands.webprofiler.export.progress.exporting'));
116 foreach ($profiles as $profile) {
117 $data = $profiler->export($profiler->loadProfile($profile['token']));
118 $profileFilename = $directory . "/{$profile['token']}.txt";
119 file_put_contents($profileFilename, $data);
120 $files[] = $profileFilename;
121 $progress->advance();
124 $progress->setMessage($this->trans('commands.webprofiler.export.progress.archive'));
125 $archiver->createModify($files, '', $directory);
126 $progress->advance();
128 $progress->setMessage($this->trans('commands.webprofiler.export.progress.delete_tmp'));
129 foreach ($files as $file) {
132 $progress->advance();
134 $progress->setMessage($this->trans('commands.webprofiler.export.progress.done'));
136 $output->writeln('');
138 $output->writeln(sprintf(
139 $this->trans('commands.webprofiler.export.messages.exported_count'),
148 public function showMessage($output, $message, $type = 'info') {
149 if (!$this->filename) {
153 $completeMessageKey = 'commands.webprofiler.export.messages.success';
154 $completeMessage = sprintf($this->trans($completeMessageKey), $this->filename);
156 if ($completeMessage != $completeMessageKey) {
157 parent::showMessage($output, $completeMessage);