Skip to content

Commit

Permalink
Add option to activate sql profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
vpietri committed Dec 7, 2023
1 parent 5530258 commit 8fe807a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
38 changes: 35 additions & 3 deletions Console/Command/AbstractStatusToolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\App\Cache\Manager;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
use Magento\Framework\Stdlib\ArrayManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -20,6 +23,9 @@ abstract class AbstractStatusToolbar extends \Symfony\Component\Console\Command\
{

const CLEAN_HTML="clear-front-cache";

const ACTIVATE_SQL_PROFILER="sql-profiler";

/**
* @var string
*/
Expand All @@ -44,16 +50,22 @@ abstract class AbstractStatusToolbar extends \Symfony\Component\Console\Command\
protected $resourceConfig;
private $cacheManager;
private $eventManager;
private $writer;
private $arrayManager;

public function __construct(Config $resourceConfig,
Manager $cacheManager,
EventManagerInterface $eventManager,
Writer $writer,
ArrayManager $arrayManager,
string $name = null)
{
parent::__construct($name);
$this->resourceConfig = $resourceConfig;
$this->cacheManager = $cacheManager;
$this->eventManager = $eventManager;
$this->writer = $writer;
$this->arrayManager = $arrayManager;
}

/**
Expand All @@ -69,6 +81,12 @@ protected function configure()
InputOption::VALUE_NONE,
'Clear front cache block_html & full_page'
);
$this->addOption(
self::ACTIVATE_SQL_PROFILER,
null,
InputOption::VALUE_NONE,
'Activate/deactivate SQL profiler'
);
}

/**
Expand All @@ -81,14 +99,28 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>" . $this->message . "</info>");

$this->eventManager->dispatch('adminhtml_cache_flush_system');
$this->cacheManager->clean(['config']);



$cachesToClear=['config'];
//TODO: Conditionner
if ($input->getOption(self::CLEAN_HTML)) {
$output->writeln("<info>Front cache block_html & full_page cleared</info>");
$cachesToClear = array_merge($cachesToClear, ['block_html', 'full_page']);
}

//$this->cacheManager->clean(['config', 'block_html', 'full_page']);
$lockTargetPath = ConfigFilePool::APP_ENV;
if ($input->getOption(self::ACTIVATE_SQL_PROFILER)) {
$this->writer->saveConfig(
[$lockTargetPath => $this->arrayManager->set('db/connection/default/profiler', [], $this->status)],
false
);
$output->writeln("<info>SQL profiler is enabled in env.php</info>");
}

$this->cacheManager->clean($cachesToClear);
$output->writeln("<info>Cache cleared: ".implode(",", $cachesToClear)."</info>");


return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
}
}
4 changes: 3 additions & 1 deletion view/base/templates/tab/sql.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@

<?php else:?>
SQL profiler is not active<br/>
You should set a new key for $config array in file app/etc/env.php<br/>
You can use command line
<pre>bin/magento dev:quickdevbar:enable --sql-profiler</pre>
or set a new key for $config array in file app/etc/env.php<br/>
<pre>$config[db][connection][default][profiler] = 1</pre>
<?php endif ?>

0 comments on commit 8fe807a

Please sign in to comment.