Skip to content

Commit 5185548

Browse files
authored
MAGETWO-67501: Enable/Disable DB query logging commands #9264
2 parents eca5237 + a483374 commit 5185548

File tree

18 files changed

+839
-21
lines changed

18 files changed

+839
-21
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
/.settings
66
atlassian*
77
/nbproject
8+
/robots.txt
9+
/pub/robots.txt
810
/sitemap
911
/sitemap.xml
12+
/pub/sitemap
13+
/pub/sitemap.xml
1014
/.idea
1115
/.gitattributes
1216
/app/config_sandbox

Diff for: app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
*/
1010
namespace Magento\Config\Model\Config\Backend\Admin;
1111

12-
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
13+
use Magento\Framework\App\ObjectManager;
1314

1415
class Robots extends \Magento\Framework\App\Config\Value
1516
{
@@ -32,6 +33,7 @@ class Robots extends \Magento\Framework\App\Config\Value
3233
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
3334
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
3435
* @param array $data
36+
* @param DocumentRoot $documentRoot
3537
*/
3638
public function __construct(
3739
\Magento\Framework\Model\Context $context,
@@ -41,10 +43,13 @@ public function __construct(
4143
\Magento\Framework\Filesystem $filesystem,
4244
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
4345
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
44-
array $data = []
46+
array $data = [],
47+
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null
4548
) {
4649
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
47-
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
50+
51+
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
52+
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
4853
$this->_file = 'robots.txt';
4954
}
5055

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Config\Model\Config\Reader\Source\Deployed;
8+
9+
use Magento\Framework\Config\ConfigOptionsListConstants;
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\App\DeploymentConfig;
12+
13+
/**
14+
* Class DocumentRoot
15+
* @package Magento\Config\Model\Config\Reader\Source\Deployed
16+
*/
17+
class DocumentRoot
18+
{
19+
/**
20+
* @var DeploymentConfig
21+
*/
22+
private $config;
23+
24+
/**
25+
* DocumentRoot constructor.
26+
* @param DeploymentConfig $config
27+
*/
28+
public function __construct(DeploymentConfig $config)
29+
{
30+
$this->config = $config;
31+
}
32+
33+
/**
34+
* A shortcut to load the document root path from the DirectoryList based on the
35+
* deployment configuration.
36+
*
37+
* @return string
38+
*/
39+
public function getPath()
40+
{
41+
return $this->isPub() ? DirectoryList::PUB : DirectoryList::ROOT;
42+
}
43+
44+
/**
45+
* Returns whether the deployment configuration specifies that the document root is
46+
* in the pub/ folder. This affects ares such as sitemaps and robots.txt (and will
47+
* likely be extended to control other areas).
48+
*
49+
* @return bool
50+
*/
51+
public function isPub()
52+
{
53+
return (bool)$this->config->get(ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB);
54+
}
55+
}
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+
namespace Magento\Config\Test\Unit\Model\Config\Reader\Source\Deployed;
7+
8+
use Magento\Config\Model\Config\Reader;
9+
use Magento\Framework\App\Config;
10+
use Magento\Framework\App\DeploymentConfig;
11+
use Magento\Framework\App\Filesystem\DirectoryList;
12+
use Magento\Framework\Config\ConfigOptionsListConstants;
13+
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
14+
15+
/**
16+
* Test class for checking settings that defined in config file
17+
*/
18+
class DocumentRootTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @var Config|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $configMock;
24+
25+
/**
26+
* @var DocumentRoot
27+
*/
28+
private $documentRoot;
29+
30+
public function setUp()
31+
{
32+
$this->configMock = $this->getMockBuilder(DeploymentConfig::class)
33+
->disableOriginalConstructor()
34+
->getMock();
35+
36+
$this->documentRoot = new DocumentRoot($this->configMock);
37+
}
38+
39+
/**
40+
* Ensures that the path returned matches the pub/ path.
41+
*/
42+
public function testGetPath()
43+
{
44+
$this->configMockSetForDocumentRootIsPub();
45+
46+
$this->assertSame(DirectoryList::PUB, $this->documentRoot->getPath());
47+
}
48+
49+
/**
50+
* Ensures that the deployment configuration returns the mocked value for
51+
* the pub/ folder.
52+
*/
53+
public function testIsPub()
54+
{
55+
$this->configMockSetForDocumentRootIsPub();
56+
57+
$this->assertSame(true, $this->documentRoot->isPub());
58+
}
59+
60+
private function configMockSetForDocumentRootIsPub()
61+
{
62+
$this->configMock->expects($this->any())
63+
->method('get')
64+
->willReturnMap([
65+
[
66+
ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB,
67+
null,
68+
true
69+
],
70+
]);
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
use Magento\Framework\App\DeploymentConfig\Writer;
13+
use Magento\Framework\Config\File\ConfigFilePool;
14+
use Magento\Framework\DB\Logger\LoggerProxy;
15+
16+
class QueryLogDisableCommand extends Command
17+
{
18+
/**
19+
* command name
20+
*/
21+
const COMMAND_NAME = 'dev:query-log:disable';
22+
23+
/**
24+
* Success message
25+
*/
26+
const SUCCESS_MESSAGE = "DB query logging disabled.";
27+
28+
/**
29+
* @var Writer
30+
*/
31+
private $deployConfigWriter;
32+
33+
/**
34+
* QueryLogDisableCommand constructor.
35+
* @param Writer $deployConfigWriter
36+
* @param null $name
37+
*/
38+
public function __construct(
39+
Writer $deployConfigWriter,
40+
$name = null
41+
) {
42+
parent::__construct($name);
43+
$this->deployConfigWriter = $deployConfigWriter;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function configure()
50+
{
51+
$this->setName(self::COMMAND_NAME)
52+
->setDescription('Disable DB query logging');
53+
54+
parent::configure();
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
* @throws \InvalidArgumentException
60+
*/
61+
protected function execute(InputInterface $input, OutputInterface $output)
62+
{
63+
$data = [LoggerProxy::PARAM_ALIAS => LoggerProxy::LOGGER_ALIAS_DISABLED];
64+
$this->deployConfigWriter->saveConfig([ConfigFilePool::APP_ENV => [LoggerProxy::CONF_GROUP_NAME => $data]]);
65+
66+
$output->writeln("<info>". self::SUCCESS_MESSAGE . "</info>");
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)