diff options
author | simivar <simivar@gmail.com> | 2018-08-07 01:13:44 +0300 |
---|---|---|
committer | diosmosis <diosmosis@users.noreply.github.com> | 2018-08-07 01:13:44 +0300 |
commit | bb1b1b4b068fc40da896e7eedbad3b2914dc2468 (patch) | |
tree | 15cef1f94283c295bfc460145a4bdd259fac4541 /plugins/Diagnostics | |
parent | 43d3acb076a401ca64df9ee46f38596ce8786386 (diff) |
New system check diagnostic for max_execution_time (#11050) (#12576)
* New system check diagnostic for max_execution_time (#11050)
* PhpSettingsCheck: getRequiredSettings() returns array instead building it in property
* Rename PhpSettingsCheckService to RequiredPhpSetting and swap || with OR in string
* PhpSettingsCheck: method to set whether setting should raise an error or warning
Diffstat (limited to 'plugins/Diagnostics')
-rw-r--r-- | plugins/Diagnostics/Diagnostic/PhpSettingsCheck.php | 28 | ||||
-rw-r--r-- | plugins/Diagnostics/Diagnostic/RequiredPhpSetting.php | 104 |
2 files changed, 117 insertions, 15 deletions
diff --git a/plugins/Diagnostics/Diagnostic/PhpSettingsCheck.php b/plugins/Diagnostics/Diagnostic/PhpSettingsCheck.php index 41142214f1..cf108ec12f 100644 --- a/plugins/Diagnostics/Diagnostic/PhpSettingsCheck.php +++ b/plugins/Diagnostics/Diagnostic/PhpSettingsCheck.php @@ -29,14 +29,10 @@ class PhpSettingsCheck implements Diagnostic $label = $this->translator->translate('Installation_SystemCheckSettings'); $result = new DiagnosticResult($label); - + foreach ($this->getRequiredSettings() as $setting) { - list($settingName, $requiredValue) = explode('=', $setting); - - $currentValue = (int) ini_get($settingName); - - if ($currentValue != $requiredValue) { - $status = DiagnosticResult::STATUS_ERROR; + if (!$setting->check()) { + $status = $setting->getErrorResult(); $comment = sprintf( '%s <br/><br/><em>%s</em><br/><em>%s</em><br/>', $setting, @@ -55,22 +51,23 @@ class PhpSettingsCheck implements Diagnostic } /** - * @return string[] + * @return RequiredPhpSetting[] */ private function getRequiredSettings() { - $requiredSettings = array( - // setting = required value - // Note: value must be an integer only - 'session.auto_start=0', - ); + $requiredSettings[] = new RequiredPhpSetting('session.auto_start', 0); + + $maxExecutionTime = new RequiredPhpSetting('max_execution_time', 0); + $maxExecutionTime->addRequiredValue(30, '>='); + $maxExecutionTime->setErrorResult(DiagnosticResult::STATUS_WARNING); + $requiredSettings[] = $maxExecutionTime; if ($this->isPhpVersionAtLeast56() && ! defined("HHVM_VERSION") && !$this->isPhpVersionAtLeast70()) { // always_populate_raw_post_data must be -1 // removed in PHP 7 - $requiredSettings[] = 'always_populate_raw_post_data=-1'; + $requiredSettings[] = new RequiredPhpSetting('always_populate_raw_post_data', -1); } - + return $requiredSettings; } @@ -83,4 +80,5 @@ class PhpSettingsCheck implements Diagnostic { return version_compare(PHP_VERSION, '7.0.0-dev', '>='); } + } diff --git a/plugins/Diagnostics/Diagnostic/RequiredPhpSetting.php b/plugins/Diagnostics/Diagnostic/RequiredPhpSetting.php new file mode 100644 index 0000000000..c05e2c5ea9 --- /dev/null +++ b/plugins/Diagnostics/Diagnostic/RequiredPhpSetting.php @@ -0,0 +1,104 @@ +<?php + +namespace Piwik\Plugins\Diagnostics\Diagnostic; + +class RequiredPhpSetting +{ + + /** @var string */ + private $setting; + + /** @var array */ + private $requiredValues; + + /** @var string */ + private $errorResult = DiagnosticResult::STATUS_ERROR; + + /** + * @param string $setting + * @param int $requiredValue + * @param string $operator + */ + public function __construct($setting, $requiredValue, $operator = '=') + { + $this->setting = $setting; + $this->addRequiredValue($requiredValue, $operator); + } + + /** + * @param int $requiredValue + * @param string $operator + * + * @return $this + */ + public function addRequiredValue($requiredValue, $operator) + { + if(!is_int($requiredValue)){ + throw new \InvalidArgumentException('Required value must be an integer.'); + } + + $this->requiredValues[] = array( + 'requiredValue' => $requiredValue, + 'operator' => $operator, + 'isValid' => null, + ); + + return $this; + } + + /** + * @param $errorResult + * + * @return $this + */ + public function setErrorResult($errorResult) + { + if ($errorResult !== DiagnosticResult::STATUS_WARNING && $errorResult !== DiagnosticResult::STATUS_ERROR) { + throw new \InvalidArgumentException('Error result must be either DiagnosticResult::STATUS_WARNING or DiagnosticResult::STATUS_ERROR.'); + } + + $this->errorResult = $errorResult; + + return $this; + } + + /** + * @return string + */ + public function getErrorResult() + { + return $this->errorResult; + } + + /** + * Checks required values against php.ini value. + * + * @return bool + */ + public function check() + { + $currentValue = (int) ini_get($this->setting); + + $return = false; + foreach($this->requiredValues as $key => $requiredValue){ + $this->requiredValues[$key]['isValid'] = version_compare($currentValue, $requiredValue['requiredValue'], $requiredValue['operator']); + + if($this->requiredValues[$key]['isValid']){ + $return = true; + } + } + + return $return; + } + + public function __toString() + { + $checks = array(); + foreach($this->requiredValues as $requiredValue){ + $checks[] = $requiredValue['operator'] . ' ' . $requiredValue['requiredValue']; + } + + return $this->setting . ' ' . implode(' OR ', $checks); + } + +} |