Skip to content

Commit 255fecc

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1726 from magento-engcom/2.2-develop-prs
Public Pull Requests #11407 Added CLI command to enable and disable the Profiler by @peterjaap Fixed Public Issues #9277 Create new CLI command: enable/disable Magento Profiler
2 parents e18f1d2 + 0976cad commit 255fecc

File tree

4 files changed

+190
-2
lines changed

4 files changed

+190
-2
lines changed

app/bootstrap.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@
4949
unset($_SERVER['ORIG_PATH_INFO']);
5050
}
5151

52-
if (!empty($_SERVER['MAGE_PROFILER'])
52+
if (
53+
(!empty($_SERVER['MAGE_PROFILER']) || file_exists(BP . '/var/profiler.flag'))
5354
&& isset($_SERVER['HTTP_ACCEPT'])
5455
&& strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false
5556
) {
57+
$profilerFlag = isset($_SERVER['MAGE_PROFILER']) && strlen($_SERVER['MAGE_PROFILER'])
58+
? $_SERVER['MAGE_PROFILER']
59+
: trim(file_get_contents(BP . '/var/profiler.flag'));
60+
5661
\Magento\Framework\Profiler::applyConfig(
57-
$_SERVER['MAGE_PROFILER'],
62+
$profilerFlag,
5863
BP,
5964
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
6065
);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Console\Command;
8+
9+
use Magento\Framework\Filesystem\Io\File;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
/**
15+
* CLI Command to disable Magento profiler.
16+
*/
17+
class ProfilerDisableCommand extends Command
18+
{
19+
/**
20+
* Profiler flag file
21+
*/
22+
const PROFILER_FLAG_FILE = 'var/profiler.flag';
23+
24+
/**
25+
* Command name
26+
*/
27+
const COMMAND_NAME = 'dev:profiler:disable';
28+
29+
/**
30+
* Success message
31+
*/
32+
const SUCCESS_MESSAGE = 'Profiler disabled.';
33+
34+
/**
35+
* @var File
36+
*/
37+
private $filesystem;
38+
39+
/**
40+
* Initialize dependencies.
41+
*
42+
* @param File $filesystem
43+
* @param string|null $name The name of the command; passing null means it must be set in configure()
44+
* @internal param ConfigInterface $resourceConfig
45+
*/
46+
public function __construct(File $filesystem, $name = null)
47+
{
48+
parent::__construct($name ?: self::COMMAND_NAME);
49+
$this->filesystem = $filesystem;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function configure()
56+
{
57+
$this->setDescription('Disable the profiler.');
58+
parent::configure();
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
* @throws \InvalidArgumentException
64+
*/
65+
protected function execute(InputInterface $input, OutputInterface $output)
66+
{
67+
$this->filesystem->rm(BP . '/' . self::PROFILER_FLAG_FILE);
68+
if (!$this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
69+
$output->writeln('<info>'. self::SUCCESS_MESSAGE . '</info>');
70+
return;
71+
}
72+
$output->writeln('<error>Something went wrong while disabling the profiler.</error>');
73+
}
74+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Console\Command;
8+
9+
use Magento\Framework\Filesystem\Io\File;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
use Symfony\Component\Console\Input\InputArgument;
14+
15+
/**
16+
* CLI Command to enable Magento profiler.
17+
*/
18+
class ProfilerEnableCommand extends Command
19+
{
20+
/**
21+
* Profiler flag file
22+
*/
23+
const PROFILER_FLAG_FILE = 'var/profiler.flag';
24+
25+
/**
26+
* Profiler type default setting
27+
*/
28+
const TYPE_DEFAULT = 'html';
29+
30+
/**
31+
* Built in profiler types
32+
*/
33+
const BUILT_IN_TYPES = ['html', 'csvfile'];
34+
35+
/**
36+
* Command name
37+
*/
38+
const COMMAND_NAME = 'dev:profiler:enable';
39+
40+
/**
41+
* Success message
42+
*/
43+
const SUCCESS_MESSAGE = 'Profiler enabled with %s output.';
44+
45+
/**
46+
* @var File
47+
*/
48+
private $filesystem;
49+
50+
/**
51+
* Initialize dependencies.
52+
*
53+
* @param File $filesystem
54+
* @param string|null $name The name of the command; passing null means it must be set in configure()
55+
* @internal param ConfigInterface $resourceConfig
56+
*/
57+
public function __construct(File $filesystem, $name = null)
58+
{
59+
parent::__construct($name ?: self::COMMAND_NAME);
60+
$this->filesystem = $filesystem;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
protected function configure()
67+
{
68+
$this->setDescription('Enable the profiler.')
69+
->addArgument('type', InputArgument::OPTIONAL, 'Profiler type', self::TYPE_DEFAULT);
70+
71+
parent::configure();
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
* @throws \InvalidArgumentException
77+
*/
78+
protected function execute(InputInterface $input, OutputInterface $output)
79+
{
80+
$type = $input->getArgument('type');
81+
if (!in_array($type, self::BUILT_IN_TYPES)) {
82+
$builtInTypes = implode(', ', self::BUILT_IN_TYPES);
83+
$output->writeln(
84+
'<comment>' . sprintf('Type %s is not one of the built-in output types (%s).', $type) .
85+
sprintf('Make sure the necessary class exists.', $type, $builtInTypes) . '</comment>'
86+
);
87+
}
88+
89+
$this->filesystem->write(BP . '/' . self::PROFILER_FLAG_FILE, $type);
90+
if ($this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
91+
$output->write('<info>'. sprintf(self::SUCCESS_MESSAGE, $type) . '</info>');
92+
if ($type == 'csvfile') {
93+
$output->write(
94+
'<info> ' . sprintf(
95+
'Output will be saved in %s',
96+
\Magento\Framework\Profiler\Driver\Standard\Output\Csvfile::DEFAULT_FILEPATH
97+
)
98+
. '</info>'
99+
);
100+
}
101+
$output->write(PHP_EOL);
102+
return;
103+
}
104+
105+
$output->writeln('<error>Something went wrong while enabling the profiler.</error>');
106+
}
107+
}

app/code/Magento/Developer/etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
<item name="dev_query_log_disable" xsi:type="object">Magento\Developer\Console\Command\QueryLogDisableCommand</item>
103103
<item name="dev_template_hints_disable" xsi:type="object">Magento\Developer\Console\Command\TemplateHintsDisableCommand</item>
104104
<item name="dev_template_hints_enable" xsi:type="object">Magento\Developer\Console\Command\TemplateHintsEnableCommand</item>
105+
<item name="dev_profiler_disable" xsi:type="object">Magento\Developer\Console\Command\ProfilerDisableCommand</item>
106+
<item name="dev_profiler_enable" xsi:type="object">Magento\Developer\Console\Command\ProfilerEnableCommand</item>
105107
</argument>
106108
</arguments>
107109
</type>

0 commit comments

Comments
 (0)