class ExportCommand in Devel 4.x
Same name and namespace in other branches
- 8.3 webprofiler/src/Command/ExportCommand.php \Drupal\webprofiler\Command\ExportCommand
- 8 webprofiler/src/Command/ExportCommand.php \Drupal\webprofiler\Command\ExportCommand
- 8.2 webprofiler/src/Command/ExportCommand.php \Drupal\webprofiler\Command\ExportCommand
Class ExportCommand.
@DrupalCommand ( extension="webprofiler", extensionType="module" )
Hierarchy
- class \Drupal\webprofiler\Command\ExportCommand extends \Symfony\Component\Console\Command\Command uses \Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait
Expanded class hierarchy of ExportCommand
1 string reference to 'ExportCommand'
- console.services.yml in webprofiler/
console.services.yml - webprofiler/console.services.yml
1 service uses ExportCommand
File
- webprofiler/
src/ Command/ ExportCommand.php, line 24
Namespace
Drupal\webprofiler\CommandView source
class ExportCommand extends Command {
use ContainerAwareCommandTrait;
/**
* @var string
*/
private $filename;
/**
* {@inheritdoc}
*/
protected function configure() {
$this
->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('<error>' . $e
->getMessage() . '</error>');
}
}
/**
* 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);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExportCommand:: |
private | property | ||
ExportCommand:: |
protected | function | ||
ExportCommand:: |
protected | function | ||
ExportCommand:: |
private | function | Exports all stored profiles (cap limit at 1000 items). | |
ExportCommand:: |
private | function | Exports a single profile. | |
ExportCommand:: |
public | function |