Skip to content

Commit a754fea

Browse files
authored
MCLOUD-9485: Add validator for opcache excluded paths (#103)
1 parent b09c78a commit a754fea

File tree

6 files changed

+417
-0
lines changed

6 files changed

+417
-0
lines changed

scenario/build/generate.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</item>
1515
<item name="warning" xsi:type="array">
1616
<item name="config-file-exists" xsi:type="object" priority="100">Magento\MagentoCloud\Config\Validator\Build\ConfigFileExists</item>
17+
<item name="opcache-exclude-paths" xsi:type="object" priority="100">Magento\MagentoCloud\Config\Validator\Build\OpcacheExcludePaths</item>
1718
<item name="deprecated-ini" xsi:type="object" priority="200">Magento\MagentoCloud\Config\Validator\Build\UnsupportedBuildOptionsIni</item>
1819
<item name="modules-exists" xsi:type="object" priority="300">Magento\MagentoCloud\Config\Validator\Build\ModulesExists</item>
1920
<item name="appropriate-version" xsi:type="object" priority="400">Magento\MagentoCloud\Config\Validator\Build\AppropriateVersion</item>

src/App/Error.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class Error
107107
public const WARN_SCD_OPTIONS_IGNORANCE = 1005;
108108
public const WARN_CONFIGURATION_STATE_NOT_IDEAL = 1006;
109109
public const WARN_BALER_CANNOT_BE_USED = 1007;
110+
public const WARN_WRONG_OPCACHE_CONFIG = 1008;
110111

111112
/**
112113
* Deploy
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MagentoCloud\Config\Validator\Build;
9+
10+
use Magento\MagentoCloud\App\Error;
11+
use Magento\MagentoCloud\Config\Validator;
12+
use Magento\MagentoCloud\Config\ValidatorInterface;
13+
use Magento\MagentoCloud\Filesystem\Driver\File;
14+
use Magento\MagentoCloud\Filesystem\FileList;
15+
16+
/**
17+
* Checks that required paths to exclude for OPCache are present.
18+
*/
19+
class OpcacheExcludePaths implements ValidatorInterface
20+
{
21+
/**
22+
* @var File
23+
*/
24+
private $file;
25+
26+
/**
27+
* @var FileList
28+
*/
29+
private $fileList;
30+
31+
/**
32+
* @var Validator\ResultFactory
33+
*/
34+
private $resultFactory;
35+
36+
/**
37+
* @param File $file
38+
* @param FileList $fileList
39+
* @param Validator\ResultFactory $resultFactory
40+
*/
41+
public function __construct(
42+
File $file,
43+
FileList $fileList,
44+
Validator\ResultFactory $resultFactory
45+
) {
46+
$this->file = $file;
47+
$this->fileList = $fileList;
48+
$this->resultFactory = $resultFactory;
49+
}
50+
51+
/**
52+
* Checks if php.ini and op-exclude.txt are present, and they contain needed configuration
53+
*
54+
* {@inheritdoc}
55+
*/
56+
public function validate(): Validator\ResultInterface
57+
{
58+
$phpIni = $this->fileList->getPhpIni();
59+
$excludeList = $this->fileList->getOpCacheExcludeList();
60+
61+
// Checks if files are present
62+
if (!$this->file->isExists($phpIni) || !$this->file->isExists($excludeList)) {
63+
return $this->resultFactory->error(
64+
'File php.ini or op-exclude.txt does not exist',
65+
'Check if your cloud template contains latest php.ini and op-exclude.txt files',
66+
Error::WARN_WRONG_OPCACHE_CONFIG
67+
);
68+
}
69+
70+
// Checks if the php.ini file contains correct path to the op-exclude.txt file
71+
$parsedPhpIni = (array) $this->file->parseIni($phpIni);
72+
73+
if (!(array_key_exists('opcache.blacklist_filename', $parsedPhpIni)
74+
&& $parsedPhpIni['opcache.blacklist_filename'] == $excludeList)) {
75+
return $this->resultFactory->error(
76+
'File php.ini does not contain opcache.blacklist_filename configuration',
77+
'Check if your cloud template contains latest php.ini configuration file'
78+
. ' https://github.com/magento/magento-cloud/blob/master/php.ini',
79+
Error::WARN_WRONG_OPCACHE_CONFIG
80+
);
81+
}
82+
83+
// Checks if the op-exclude.txt file contains all needed paths to exclude for OPCache
84+
$diff = array_diff(
85+
[
86+
'/app/*/app/etc/config.php',
87+
'/app/*/app/etc/env.php',
88+
'/app/app/etc/config.php',
89+
'/app/app/etc/env.php',
90+
'/app/etc/config.php',
91+
'/app/etc/env.php'
92+
],
93+
explode(PHP_EOL, (string) $this->file->fileGetContents($excludeList))
94+
);
95+
96+
if (!empty($diff)) {
97+
return $this->resultFactory->error(
98+
'File op-exclude.txt does not contain required paths to exclude for OPCache',
99+
'Check if your op-exclude.txt contains the next paths:' . PHP_EOL
100+
. implode(PHP_EOL, $diff),
101+
Error::WARN_WRONG_OPCACHE_CONFIG
102+
);
103+
}
104+
105+
return $this->resultFactory->create(Validator\ResultInterface::SUCCESS);
106+
}
107+
}

src/Filesystem/ConfigFileList.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,20 @@ public function getErrorReportConfig(): string
7373
{
7474
return $this->systemList->getMagentoRoot() . '/pub/errors/local.xml';
7575
}
76+
77+
/**
78+
* @return string
79+
*/
80+
public function getPhpIni(): string
81+
{
82+
return $this->systemList->getMagentoRoot() . '/php.ini';
83+
}
84+
85+
/**
86+
* @return string
87+
*/
88+
public function getOpCacheExcludeList(): string
89+
{
90+
return $this->systemList->getMagentoRoot() . '/op-exclude.txt';
91+
}
7692
}

0 commit comments

Comments
 (0)