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

Commit 2c1a23f

Browse files
Merge forwardport of magento/magento2#11407 to 2.3-develop branch
Applied pull request patch https://github.com/magento/magento2/pull/11407.patch (created by @peterjaap) based on commit(s): 1. 4c26acf Fixed GitHub Issues in 2.3-develop branch: - magento/magento2#9277: Create new CLI command: enable/disable Magento Profiler (reported by @vrann)
2 parents fd2f731 + dc3bb78 commit 2c1a23f

File tree

6 files changed

+386
-2
lines changed

6 files changed

+386
-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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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, true)) {
83+
$builtInTypes = implode(', ', self::BUILT_IN_TYPES);
84+
$output->writeln(
85+
'<comment>'
86+
. sprintf('Type %s is not one of the built-in output types (%s).', $type, $builtInTypes) .
87+
'</comment>'
88+
);
89+
}
90+
91+
$this->filesystem->write(BP . '/' . self::PROFILER_FLAG_FILE, $type);
92+
if ($this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
93+
$output->write('<info>'. sprintf(self::SUCCESS_MESSAGE, $type) . '</info>');
94+
if ($type == 'csvfile') {
95+
$output->write(
96+
'<info> ' . sprintf(
97+
'Output will be saved in %s',
98+
\Magento\Framework\Profiler\Driver\Standard\Output\Csvfile::DEFAULT_FILEPATH
99+
)
100+
. '</info>'
101+
);
102+
}
103+
$output->write(PHP_EOL);
104+
105+
return;
106+
}
107+
$output->writeln('<error>Something went wrong while enabling the profiler.</error>');
108+
}
109+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Test\Unit\Console\Command;
8+
9+
use Magento\Developer\Console\Command\ProfilerDisableCommand;
10+
use Magento\Framework\Filesystem\Io\File;
11+
use PHPUnit\Framework\TestCase;
12+
use Symfony\Component\Console\Tester\CommandTester;
13+
14+
15+
/**
16+
* Class ProfilerDisableCommandTest
17+
*
18+
* Tests dev:profiler:disable command.
19+
*/
20+
class ProfilerDisableCommandTest extends TestCase
21+
{
22+
/**
23+
* @var File|\PHPUnit_Framework_MockObject_MockObject
24+
*/
25+
private $filesystemMock;
26+
27+
/**
28+
* Test disabling the profiler by command.
29+
*
30+
* @param bool $fileExists
31+
* @param string $expectedOutput
32+
* @dataProvider commandDataProvider
33+
*/
34+
public function testCommand(bool $fileExists, string $expectedOutput)
35+
{
36+
$this->filesystemMock
37+
->expects($this->once())
38+
->method('rm')
39+
->with(BP . '/' . ProfilerDisableCommand::PROFILER_FLAG_FILE);
40+
$this->filesystemMock
41+
->expects($this->once())
42+
->method('fileExists')
43+
->with(BP . '/' . ProfilerDisableCommand::PROFILER_FLAG_FILE)
44+
->willReturn($fileExists);
45+
/** @var ProfilerDisableCommand $command */
46+
$command = new ProfilerDisableCommand($this->filesystemMock);
47+
$commandTester = new CommandTester($command);
48+
$commandTester->execute([]);
49+
50+
self::assertEquals(
51+
$expectedOutput,
52+
trim(str_replace(PHP_EOL, ' ', $commandTester->getDisplay()))
53+
);
54+
}
55+
56+
/**
57+
* Data provider for testCommand.
58+
*
59+
* @return array
60+
*/
61+
public function commandDataProvider()
62+
{
63+
return [
64+
[true, 'Something went wrong while disabling the profiler.'],
65+
[false, 'Profiler disabled.'],
66+
];
67+
}
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
protected function setUp()
73+
{
74+
$this->filesystemMock = $this->getMockBuilder(File::class)
75+
->disableOriginalConstructor()
76+
->getMock();
77+
}
78+
}

0 commit comments

Comments
 (0)