setOptions($options); } public function setOptions(array $options) { if (array_key_exists('options', $options)) { unset($options['options']); } foreach ($options as $name => $value) { $method = 'set' . ucfirst($name); if (method_exists($this, $method)) { $this->$method($value); } else { trigger_error('"' . $name . '" config is not supported'); } } return $this; } /** * @param $name * @param null $field * @param null $default * @return bool|array|string */ public function getSoftware($name = null, $field = null, $default = null) { if (null === $name) { return $this->software; } if (!array_key_exists($name, $this->software)) { return $default; } if (null !== $field) { if (!array_key_exists($field, $this->software[$name])) { return $default; } return $this->software[$name][$field]; } return $this->software[$name]; } public function setSoftware(array $software) { $this->software = $software; return $this; } public function run() { $this->valid = true; foreach (array_keys($this->getSoftware()) as $name) { $this->testSoftware($name); } return $this->valid; } protected function testSoftware($name) { $version = $this->getSoftwareVersion($name); $requiredVersion = $this->getSoftwareRequiredVersion($name); $this->addItem( $this->getSoftware($name, 'title'), $this->isValidSoftware($name), $requiredVersion ? $requiredVersion : '-', $version ? $version : '-' ); foreach ($this->getSoftware($name, 'subTests', array()) as $method) { $this->$method($this->getSoftware($name)); } return $this; } protected function isValidSoftware($name) { if (($validator = $this->getSoftware($name, 'validator'))) { return $this->$validator(); } if (false === ($version = $this->getSoftwareVersion($name))) { return false; } return $this->isValidVersion($version, $this->getSoftware($name, 'rules')); } public function getSoftwareVersion($name) { if (($versionIdentificator = $this->getSoftware($name, 'versionIdentificator'))) { return $this->$versionIdentificator(); } return false; } public function getSoftwareRequiredVersion($name) { $rules = $this->getSoftware($name, 'rules', array()); return $this->getRulesVersion($rules); } protected function getRulesVersion(array $rules) { $count = count($rules); if (1 == $count) { $rule = current($rules); $version = array_shift($rule); $operator = array_shift($rule); if ($operator == '>=') { return $version . ' or higher'; } return 'higher ' . $version; } if (2 == $count) { $minRule = current($rules); $maxRule = next($rules); $minVersion = array_shift($minRule); $maxVersion = array_shift($maxRule); $minOperator = array_shift($minRule); $maxOperator = array_shift($maxRule); return $minVersion . ' ' . $minOperator . ' and ' . $maxOperator . ' ' . $maxVersion; } $result = array(); foreach ($rules as $rule) { $version = array_shift($rule); $operator = array_shift($rule); $result[] = $operator . ' ' . $version; } return implode(' and ', $result); } public function getImageMagickVersion() { return $this->_getCommandVersion('convert --version', '/ImageMagick\x20([\d.-]+)\x20/'); } protected function isValidVersion($version, array $rules) { foreach ($rules as $rule) { array_unshift($rule, $version); if (false === call_user_func_array('version_compare', $rule)) { return false; } } return true; } public function testModRewrite() { return !empty($_SERVER['HTTP_MOD_REWRITE']); } public function getPhpVersion() { return phpversion(); } public function getCurlVersion() { if (!function_exists('curl_version')) { return false; } if (($info = curl_version())) { return $info['version']; } return false; } protected function _getCommandVersion($command, $pattern) { static $versions = []; if (empty($versions[$command])) { $output = ''; $return = 0; exec($command, $output, $return); if (0 !== $return) { $versions[$command] = false; } else { $matches = array(); if (!preg_match($pattern, $output[0], $matches)) { trigger_error('Can\'t parse version'); $versions[$command] = false; } else { $versions[$command] = $matches[1]; } } } return $versions[$command]; } protected function testPhpExtensions($php) { $loadedExtensions = get_loaded_extensions(); foreach ($php['extensions'] as $extension) { $requiredVersion = $currentVersion = '-'; if (is_array($extension)) { $name = $extension['name']; $isValid = in_array($extension['name'], $loadedExtensions); if (array_key_exists('versionIdentificator', $extension) && array_key_exists('rules', $extension)) { $requiredVersion = $this->getRulesVersion($extension['rules']); $currentVersion = $this->{$extension['versionIdentificator']}(); $isValid = $isValid && $this->isValidVersion($currentVersion, $extension['rules']); } } else { $name = $extension; $isValid = in_array($extension, $loadedExtensions); } $this->addItem('PHP Extension: ' . $name, $isValid, $requiredVersion, $currentVersion); } return $this; } protected function addItem($title, $isValid, $requiredVersion = '-', $currentVersion = '-') { if (false === $isValid) { $this->valid = false; } $this->items[] = compact('title', 'isValid', 'requiredVersion', 'currentVersion'); return $this; } public function isValid() { return $this->valid; } public function getItems() { return $this->items; } public function getMySqlVersion() { return $this->_getCommandVersion('mysql -V', '/Distrib\x20([\d.-]+),/'); } }