Skip to content

Commit

Permalink
Merge pull request #1726 from magento-engcom/2.2-develop-prs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Oleksii Korshenko committed Nov 15, 2017
2 parents e18f1d2 + 0976cad commit 255fecc
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,17 @@
unset($_SERVER['ORIG_PATH_INFO']);
}

if (!empty($_SERVER['MAGE_PROFILER'])
if (
(!empty($_SERVER['MAGE_PROFILER']) || file_exists(BP . '/var/profiler.flag'))
&& isset($_SERVER['HTTP_ACCEPT'])
&& strpos($_SERVER['HTTP_ACCEPT'], 'text/html') !== false
) {
$profilerFlag = isset($_SERVER['MAGE_PROFILER']) && strlen($_SERVER['MAGE_PROFILER'])
? $_SERVER['MAGE_PROFILER']
: trim(file_get_contents(BP . '/var/profiler.flag'));

\Magento\Framework\Profiler::applyConfig(
$_SERVER['MAGE_PROFILER'],
$profilerFlag,
BP,
!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Developer\Console\Command;

use Magento\Framework\Filesystem\Io\File;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* CLI Command to disable Magento profiler.
*/
class ProfilerDisableCommand extends Command
{
/**
* Profiler flag file
*/
const PROFILER_FLAG_FILE = 'var/profiler.flag';

/**
* Command name
*/
const COMMAND_NAME = 'dev:profiler:disable';

/**
* Success message
*/
const SUCCESS_MESSAGE = 'Profiler disabled.';

/**
* @var File
*/
private $filesystem;

/**
* Initialize dependencies.
*
* @param File $filesystem
* @param string|null $name The name of the command; passing null means it must be set in configure()
* @internal param ConfigInterface $resourceConfig
*/
public function __construct(File $filesystem, $name = null)
{
parent::__construct($name ?: self::COMMAND_NAME);
$this->filesystem = $filesystem;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setDescription('Disable the profiler.');
parent::configure();
}

/**
* {@inheritdoc}
* @throws \InvalidArgumentException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->filesystem->rm(BP . '/' . self::PROFILER_FLAG_FILE);
if (!$this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
$output->writeln('<info>'. self::SUCCESS_MESSAGE . '</info>');
return;
}
$output->writeln('<error>Something went wrong while disabling the profiler.</error>');
}
}
107 changes: 107 additions & 0 deletions app/code/Magento/Developer/Console/Command/ProfilerEnableCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Developer\Console\Command;

use Magento\Framework\Filesystem\Io\File;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
* CLI Command to enable Magento profiler.
*/
class ProfilerEnableCommand extends Command
{
/**
* Profiler flag file
*/
const PROFILER_FLAG_FILE = 'var/profiler.flag';

/**
* Profiler type default setting
*/
const TYPE_DEFAULT = 'html';

/**
* Built in profiler types
*/
const BUILT_IN_TYPES = ['html', 'csvfile'];

/**
* Command name
*/
const COMMAND_NAME = 'dev:profiler:enable';

/**
* Success message
*/
const SUCCESS_MESSAGE = 'Profiler enabled with %s output.';

/**
* @var File
*/
private $filesystem;

/**
* Initialize dependencies.
*
* @param File $filesystem
* @param string|null $name The name of the command; passing null means it must be set in configure()
* @internal param ConfigInterface $resourceConfig
*/
public function __construct(File $filesystem, $name = null)
{
parent::__construct($name ?: self::COMMAND_NAME);
$this->filesystem = $filesystem;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setDescription('Enable the profiler.')
->addArgument('type', InputArgument::OPTIONAL, 'Profiler type', self::TYPE_DEFAULT);

parent::configure();
}

/**
* {@inheritdoc}
* @throws \InvalidArgumentException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$type = $input->getArgument('type');
if (!in_array($type, self::BUILT_IN_TYPES)) {
$builtInTypes = implode(', ', self::BUILT_IN_TYPES);
$output->writeln(
'<comment>' . sprintf('Type %s is not one of the built-in output types (%s).', $type) .
sprintf('Make sure the necessary class exists.', $type, $builtInTypes) . '</comment>'
);
}

$this->filesystem->write(BP . '/' . self::PROFILER_FLAG_FILE, $type);
if ($this->filesystem->fileExists(BP . '/' . self::PROFILER_FLAG_FILE)) {
$output->write('<info>'. sprintf(self::SUCCESS_MESSAGE, $type) . '</info>');
if ($type == 'csvfile') {
$output->write(
'<info> ' . sprintf(
'Output will be saved in %s',
\Magento\Framework\Profiler\Driver\Standard\Output\Csvfile::DEFAULT_FILEPATH
)
. '</info>'
);
}
$output->write(PHP_EOL);
return;
}

$output->writeln('<error>Something went wrong while enabling the profiler.</error>');
}
}
2 changes: 2 additions & 0 deletions app/code/Magento/Developer/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
<item name="dev_query_log_disable" xsi:type="object">Magento\Developer\Console\Command\QueryLogDisableCommand</item>
<item name="dev_template_hints_disable" xsi:type="object">Magento\Developer\Console\Command\TemplateHintsDisableCommand</item>
<item name="dev_template_hints_enable" xsi:type="object">Magento\Developer\Console\Command\TemplateHintsEnableCommand</item>
<item name="dev_profiler_disable" xsi:type="object">Magento\Developer\Console\Command\ProfilerDisableCommand</item>
<item name="dev_profiler_enable" xsi:type="object">Magento\Developer\Console\Command\ProfilerEnableCommand</item>
</argument>
</arguments>
</type>
Expand Down

0 comments on commit 255fecc

Please sign in to comment.