Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 02620b0

Browse files
⏫ Forwardport of magento/magento2#11407 to 2.3-develop branch
1 parent 8c705bf commit 02620b0

File tree

4 files changed

+185
-2
lines changed

4 files changed

+185
-2
lines changed

app/bootstrap.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@
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
) {
5657
\Magento\Framework\Profiler::applyConfig(
57-
$_SERVER['MAGE_PROFILER'],
58+
(isset($_SERVER['MAGE_PROFILER']) && strlen($_SERVER['MAGE_PROFILER'])) ? $_SERVER['MAGE_PROFILER'] : trim(file_get_contents(BP . '/var/profiler.flag')),
5859
BP,
5960
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
6061
);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
class ProfilerDisableCommand extends Command
15+
{
16+
/**
17+
* Profiler flag file
18+
*/
19+
const PROFILER_FLAG_FILE = 'var/profiler.flag';
20+
21+
/**
22+
* Command name
23+
*/
24+
const COMMAND_NAME = 'dev:profiler:disable';
25+
26+
/**
27+
* Success message
28+
*/
29+
const SUCCESS_MESSAGE = 'Profiler disabled.';
30+
31+
/**
32+
* @var File
33+
*/
34+
protected $filesystem;
35+
36+
/**
37+
* Initialize dependencies.
38+
*
39+
* @param File $filesystem
40+
* @internal param ConfigInterface $resourceConfig
41+
*/
42+
public function __construct(File $filesystem)
43+
{
44+
parent::__construct();
45+
$this->filesystem = $filesystem;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function configure()
52+
{
53+
$this->setName(self::COMMAND_NAME)
54+
->setDescription('Disable the profiler.');
55+
56+
parent::configure();
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
* @throws \InvalidArgumentException
62+
*/
63+
protected function execute(InputInterface $input, OutputInterface $output)
64+
{
65+
$this->filesystem->rm(BP . '/' . self::PROFILER_FLAG_FILE);
66+
if (!$this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
67+
$output->writeln('<info>'. self::SUCCESS_MESSAGE . '</info>');
68+
return;
69+
}
70+
$output->writeln('<error>Something went wrong while disabling the profiler.</error>');
71+
}
72+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
class ProfilerEnableCommand extends Command
16+
{
17+
/**
18+
* Profiler flag file
19+
*/
20+
const PROFILER_FLAG_FILE = 'var/profiler.flag';
21+
22+
/**
23+
* Profiler type default setting
24+
*/
25+
const TYPE_DEFAULT = 'html';
26+
27+
/**
28+
* Built in profiler types
29+
*/
30+
const BUILT_IN_TYPES = ['html', 'csvfile'];
31+
32+
/**
33+
* Command name
34+
*/
35+
const COMMAND_NAME = 'dev:profiler:enable';
36+
37+
/**
38+
* Success message
39+
*/
40+
const SUCCESS_MESSAGE = 'Profiler enabled with %s output.';
41+
42+
/**
43+
* @var File
44+
*/
45+
protected $filesystem;
46+
47+
/**
48+
* Initialize dependencies.
49+
*
50+
* @param File $filesystem
51+
* @internal param ConfigInterface $resourceConfig
52+
*/
53+
public function __construct(File $filesystem)
54+
{
55+
parent::__construct();
56+
$this->filesystem = $filesystem;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
protected function configure()
63+
{
64+
$this->setName(self::COMMAND_NAME)
65+
->setDescription('Enable the profiler.')
66+
->addArgument('type', InputArgument::OPTIONAL, 'Profiler type');
67+
68+
parent::configure();
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
* @throws \InvalidArgumentException
74+
*/
75+
protected function execute(InputInterface $input, OutputInterface $output)
76+
{
77+
$type = $input->getArgument('type');
78+
if (!$type) {
79+
$type = self::TYPE_DEFAULT;
80+
}
81+
82+
if (!in_array($type, self::BUILT_IN_TYPES)) {
83+
$builtInTypes = implode(', ', self::BUILT_IN_TYPES);
84+
$output->writeln(
85+
'<comment>' . sprintf('Type %s is not one of the built-in output types (%s).', $type) .
86+
sprintf('Make sure the necessary class exists.', $type, $builtInTypes) . '</comment>'
87+
);
88+
}
89+
90+
$this->filesystem->write(BP . '/' . self::PROFILER_FLAG_FILE, $type);
91+
if ($this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
92+
$output->write('<info>'. sprintf(self::SUCCESS_MESSAGE, $type) . '</info>');
93+
if ($type == 'csvfile') {
94+
$output->write(
95+
'<info> ' . sprintf(
96+
'Output will be saved in %s',
97+
\Magento\Framework\Profiler\Driver\Standard\Output\Csvfile::DEFAULT_FILEPATH
98+
)
99+
. '</info>'
100+
);
101+
}
102+
$output->write(PHP_EOL);
103+
return;
104+
}
105+
106+
$output->writeln('<error>Something went wrong while enabling the profiler.</error>');
107+
}
108+
}

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)