20c1e41641a1807aea599c35b047f691c08e65d7
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Command / ExportCommand.php
1 <?php
2
3 namespace Drupal\webprofiler\Command;
4
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;
15
16 /**
17  * Class ExportCommand
18  *
19  * @DrupalCommand (
20  *     extension="webprofiler",
21  *     extensionType="module"
22  * )
23  */
24 class ExportCommand extends Command {
25
26   use ContainerAwareCommandTrait;
27
28   /** @var string */
29   private $filename;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function configure() {
35     $this
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');
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function execute(InputInterface $input, OutputInterface $output) {
46     $id = $input->getArgument('id');
47     $directory = $input->getOption('directory');
48
49     /** @var \Drupal\webprofiler\Profiler\Profiler $profiler */
50     $profiler = $this->container->get('profiler');
51
52     try {
53       if ($id) {
54         $this->filename = $this->exportSingle($profiler, $id, $directory);
55       }
56       else {
57         $this->filename = $this->exportAll($profiler, $directory, $output);
58       }
59
60     } catch (\Exception $e) {
61       $output->writeln('<error>' . $e->getMessage() . '</error>');
62     }
63   }
64
65   /**
66    * Exports a single profile.
67    *
68    * @param \Drupal\webprofiler\Profiler\Profiler $profiler
69    * @param int $id
70    * @param string $directory
71    *
72    * @return string
73    *
74    * @throws \Exception
75    */
76   private function exportSingle(Profiler $profiler, $id, $directory) {
77     $profile = $profiler->loadProfile($id);
78     if ($profile) {
79       $data = $profiler->export($profile);
80
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'),
85           $filename));
86       }
87     }
88     else {
89       throw new \Exception(sprintf(
90         $this->trans('commands.webprofiler.export.messages.error_no_profile'),
91         $id));
92     }
93
94     return $filename;
95   }
96
97   /**
98    * Exports all stored profiles (cap limit at 1000 items).
99    *
100    * @param \Drupal\webprofiler\Profiler\Profiler $profiler
101    * @param string $directory
102    * @param \Symfony\Component\Console\Output\OutputInterface $output
103    *
104    * @return string
105    */
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%');
112
113     $files = [];
114     $progress->start();
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();
122     }
123
124     $progress->setMessage($this->trans('commands.webprofiler.export.progress.archive'));
125     $archiver->createModify($files, '', $directory);
126     $progress->advance();
127
128     $progress->setMessage($this->trans('commands.webprofiler.export.progress.delete_tmp'));
129     foreach ($files as $file) {
130       unlink($file);
131     }
132     $progress->advance();
133
134     $progress->setMessage($this->trans('commands.webprofiler.export.progress.done'));
135     $progress->finish();
136     $output->writeln('');
137
138     $output->writeln(sprintf(
139       $this->trans('commands.webprofiler.export.messages.exported_count'),
140       count($profiles)));
141
142     return $filename;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function showMessage($output, $message, $type = 'info') {
149     if (!$this->filename) {
150       return;
151     }
152
153     $completeMessageKey = 'commands.webprofiler.export.messages.success';
154     $completeMessage = sprintf($this->trans($completeMessageKey), $this->filename);
155
156     if ($completeMessage != $completeMessageKey) {
157       parent::showMessage($output, $completeMessage);
158     }
159   }
160 }