|
| 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\DB\Logger\LoggerProxy; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Input\InputOption; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Magento\Framework\App\DeploymentConfig\Writer; |
| 15 | +use Magento\Framework\Config\File\ConfigFilePool; |
| 16 | + |
| 17 | +class QueryLogEnableCommand extends Command |
| 18 | +{ |
| 19 | + /** |
| 20 | + * input parameter log-all-queries |
| 21 | + */ |
| 22 | + const INPUT_ARG_LOG_ALL_QUERIES = 'include-all-queries'; |
| 23 | + |
| 24 | + /** |
| 25 | + * input parameter log-query-time |
| 26 | + */ |
| 27 | + const INPUT_ARG_LOG_QUERY_TIME = 'query-time-threshold'; |
| 28 | + |
| 29 | + /** |
| 30 | + * input parameter log-call-stack |
| 31 | + */ |
| 32 | + const INPUT_ARG_LOG_CALL_STACK = 'include-call-stack'; |
| 33 | + |
| 34 | + /** |
| 35 | + * command name |
| 36 | + */ |
| 37 | + const COMMAND_NAME = 'dev:query-log:enable'; |
| 38 | + |
| 39 | + /** |
| 40 | + * Success message |
| 41 | + */ |
| 42 | + const SUCCESS_MESSAGE = "DB query logging enabled."; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var Writer |
| 46 | + */ |
| 47 | + private $deployConfigWriter; |
| 48 | + |
| 49 | + /** |
| 50 | + * QueryLogEnableCommand constructor. |
| 51 | + * @param Writer $deployConfigWriter |
| 52 | + * @param null $name |
| 53 | + */ |
| 54 | + public function __construct( |
| 55 | + Writer $deployConfigWriter, |
| 56 | + $name = null |
| 57 | + ) { |
| 58 | + parent::__construct($name); |
| 59 | + $this->deployConfigWriter = $deployConfigWriter; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritdoc} |
| 64 | + */ |
| 65 | + protected function configure() |
| 66 | + { |
| 67 | + $this->setName(self::COMMAND_NAME) |
| 68 | + ->setDescription('Enable DB query logging') |
| 69 | + ->setDefinition( |
| 70 | + [ |
| 71 | + new InputOption( |
| 72 | + self::INPUT_ARG_LOG_ALL_QUERIES, |
| 73 | + null, |
| 74 | + InputOption::VALUE_OPTIONAL, |
| 75 | + 'Log all queries. [true|false]', |
| 76 | + "true" |
| 77 | + ), |
| 78 | + new InputOption( |
| 79 | + self::INPUT_ARG_LOG_QUERY_TIME, |
| 80 | + null, |
| 81 | + InputOption::VALUE_OPTIONAL, |
| 82 | + 'Query time thresholds.', |
| 83 | + "0.001" |
| 84 | + ), |
| 85 | + new InputOption( |
| 86 | + self::INPUT_ARG_LOG_CALL_STACK, |
| 87 | + null, |
| 88 | + InputOption::VALUE_OPTIONAL, |
| 89 | + 'Include call stack. [true|false]', |
| 90 | + "true" |
| 91 | + ), |
| 92 | + ] |
| 93 | + ); |
| 94 | + |
| 95 | + parent::configure(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * {@inheritdoc} |
| 100 | + * @throws \InvalidArgumentException |
| 101 | + */ |
| 102 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 103 | + { |
| 104 | + $data = [LoggerProxy::PARAM_ALIAS => LoggerProxy::LOGGER_ALIAS_FILE]; |
| 105 | + |
| 106 | + $logAllQueries = $input->getOption(self::INPUT_ARG_LOG_ALL_QUERIES); |
| 107 | + $logQueryTime = $input->getOption(self::INPUT_ARG_LOG_QUERY_TIME); |
| 108 | + $logCallStack = $input->getOption(self::INPUT_ARG_LOG_CALL_STACK); |
| 109 | + |
| 110 | + $data[LoggerProxy::PARAM_LOG_ALL] = (int)($logAllQueries != 'false'); |
| 111 | + $data[LoggerProxy::PARAM_QUERY_TIME] = number_format($logQueryTime, 3); |
| 112 | + $data[LoggerProxy::PARAM_CALL_STACK] = (int)($logCallStack != 'false'); |
| 113 | + |
| 114 | + $configGroup[LoggerProxy::CONF_GROUP_NAME] = $data; |
| 115 | + |
| 116 | + $this->deployConfigWriter->saveConfig([ConfigFilePool::APP_ENV => $configGroup]); |
| 117 | + |
| 118 | + $output->writeln("<info>". self::SUCCESS_MESSAGE . "</info>"); |
| 119 | + } |
| 120 | +} |
0 commit comments