ProfilerEnableCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Developer\Console\Command;
  7. use Magento\Framework\Filesystem\Io\File;
  8. use Symfony\Component\Console\Command\Command;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. class ProfilerEnableCommand extends Command
  13. {
  14. /**
  15. * Profiler flag file
  16. */
  17. const PROFILER_FLAG_FILE = 'var/profiler.flag';
  18. /**
  19. * Profiler type default setting
  20. */
  21. const TYPE_DEFAULT = 'html';
  22. /**
  23. * Built in profiler types
  24. */
  25. const BUILT_IN_TYPES = ['html', 'csvfile'];
  26. /**
  27. * Command name
  28. */
  29. const COMMAND_NAME = 'dev:profiler:enable';
  30. /**
  31. * Success message
  32. */
  33. const SUCCESS_MESSAGE = 'Profiler enabled with %s output.';
  34. /**
  35. * @var File
  36. */
  37. private $filesystem;
  38. /**
  39. * Initialize dependencies.
  40. *
  41. * @param File $filesystem
  42. * @internal param ConfigInterface $resourceConfig
  43. */
  44. public function __construct(File $filesystem)
  45. {
  46. parent::__construct();
  47. $this->filesystem = $filesystem;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function configure()
  53. {
  54. $this->setName(self::COMMAND_NAME)
  55. ->setDescription('Enable the profiler.')
  56. ->addArgument('type', InputArgument::OPTIONAL, 'Profiler type');
  57. parent::configure();
  58. }
  59. /**
  60. * {@inheritdoc}
  61. * @throws \InvalidArgumentException
  62. */
  63. protected function execute(InputInterface $input, OutputInterface $output)
  64. {
  65. $type = $input->getArgument('type');
  66. if (!$type) {
  67. $type = self::TYPE_DEFAULT;
  68. }
  69. if (!in_array($type, self::BUILT_IN_TYPES, true)) {
  70. $builtInTypes = implode(', ', self::BUILT_IN_TYPES);
  71. $output->writeln(
  72. '<comment>'
  73. . sprintf('Type %s is not one of the built-in output types (%s).', $type, $builtInTypes) .
  74. '</comment>'
  75. );
  76. }
  77. $this->filesystem->write(BP . '/' . self::PROFILER_FLAG_FILE, $type);
  78. if ($this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
  79. $output->write('<info>'. sprintf(self::SUCCESS_MESSAGE, $type) . '</info>');
  80. if ($type == 'csvfile') {
  81. $output->write(
  82. '<info> ' . sprintf(
  83. 'Output will be saved in %s',
  84. \Magento\Framework\Profiler\Driver\Standard\Output\Csvfile::DEFAULT_FILEPATH
  85. )
  86. . '</info>'
  87. );
  88. }
  89. $output->write(PHP_EOL);
  90. return;
  91. }
  92. $output->writeln('<error>Something went wrong while enabling the profiler.</error>');
  93. }
  94. }