PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/SymfonyRequirements.php

https://github.com/kosssi/cmf-sandbox
PHP | 572 lines | 320 code | 72 blank | 180 comment | 27 complexity | 15a62596d567ec6e90ba5390aec7e87e MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Represents a single PHP requirement, e.g. an installed extension.
  12. * It can be a mandatory requirement or an optional recommendation.
  13. * There is a special subclass, named PhpIniRequirement, to check a php.ini configuration.
  14. *
  15. * @author Tobias Schultze <http://tobion.de>
  16. */
  17. class Requirement
  18. {
  19. private $fulfilled;
  20. private $testMessage;
  21. private $helpText;
  22. private $helpHtml;
  23. private $optional;
  24. /**
  25. * Constructor that initializes the requirement.
  26. *
  27. * @param Boolean $fulfilled Whether the requirement is fulfilled
  28. * @param string $testMessage The message for testing the requirement
  29. * @param string $helpHtml The help text formatted in HTML for resolving the problem
  30. * @param string|null $helpText The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
  31. * @param Boolean $optional Whether this is only an optional recommendation not a mandatory requirement
  32. */
  33. public function __construct($fulfilled, $testMessage, $helpHtml, $helpText = null, $optional = false)
  34. {
  35. $this->fulfilled = (Boolean) $fulfilled;
  36. $this->testMessage = (string) $testMessage;
  37. $this->helpHtml = (string) $helpHtml;
  38. $this->helpText = null === $helpText ? strip_tags($this->helpHtml) : (string) $helpText;
  39. $this->optional = (Boolean) $optional;
  40. }
  41. /**
  42. * Returns whether the requirement is fulfilled.
  43. *
  44. * @return Boolean true if fulfilled, otherwise false
  45. */
  46. public function isFulfilled()
  47. {
  48. return $this->fulfilled;
  49. }
  50. /**
  51. * Returns the message for testing the requirement.
  52. *
  53. * @return string The test message
  54. */
  55. public function getTestMessage()
  56. {
  57. return $this->testMessage;
  58. }
  59. /**
  60. * Returns the help text for resolving the problem
  61. *
  62. * @return string The help text
  63. */
  64. public function getHelpText()
  65. {
  66. return $this->helpText;
  67. }
  68. /**
  69. * Returns the help text formatted in HTML.
  70. *
  71. * @return string The HTML help
  72. */
  73. public function getHelpHtml()
  74. {
  75. return $this->helpHtml;
  76. }
  77. /**
  78. * Returns whether this is only an optional recommendation and not a mandatory requirement.
  79. *
  80. * @return Boolean true if optional, false if mandatory
  81. */
  82. public function isOptional()
  83. {
  84. return $this->optional;
  85. }
  86. }
  87. /**
  88. * Represents a PHP requirement in form of a php.ini configuration.
  89. *
  90. * @author Tobias Schultze <http://tobion.de>
  91. */
  92. class PhpIniRequirement extends Requirement
  93. {
  94. /**
  95. * Constructor that initializes the requirement.
  96. *
  97. * @param string $cfgName The configuration name used for ini_get()
  98. * @param Boolean|callback $evaluation Either a Boolean indicating whether the configuration should evaluate to true or false,
  99. or a callback function receiving the configuration value as parameter to determine the fulfillment of the requirement
  100. * @param Boolean $approveCfgAbsence If true the Requirement will be fulfilled even if the configuration option does not exist, i.e. ini_get() returns false.
  101. This is helpful for abandoned configs in later PHP versions or configs of an optional extension, like Suhosin.
  102. Example: You require a config to be true but PHP later removes this config and defaults it to true internally.
  103. * @param string $testMessage The message for testing the requirement (when null and $evaluation is a Boolean a default message is derived)
  104. * @param string $helpHtml The help text formatted in HTML for resolving the problem (when null and $evaluation is a Boolean a default help is derived)
  105. * @param string|null $helpText The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
  106. * @param Boolean $optional Whether this is only an optional recommendation not a mandatory requirement
  107. */
  108. public function __construct($cfgName, $evaluation, $approveCfgAbsence = false, $testMessage = null, $helpHtml = null, $helpText = null, $optional = false)
  109. {
  110. $cfgValue = ini_get($cfgName);
  111. if (is_callable($evaluation)) {
  112. if (null === $testMessage || null === $helpHtml) {
  113. throw new InvalidArgumentException('You must provide the parameters testMessage and helpHtml for a callback evaluation.');
  114. }
  115. $fulfilled = call_user_func($evaluation, $cfgValue);
  116. } else {
  117. if (null === $testMessage) {
  118. $testMessage = sprintf('%s %s be %s in php.ini',
  119. $cfgName,
  120. $optional ? 'should' : 'must',
  121. $evaluation ? 'enabled' : 'disabled'
  122. );
  123. }
  124. if (null === $helpHtml) {
  125. $helpHtml = sprintf('Set <strong>%s</strong> to <strong>%s</strong> in php.ini<a href="#phpini">*</a>.',
  126. $cfgName,
  127. $evaluation ? 'on' : 'off'
  128. );
  129. }
  130. $fulfilled = $evaluation == $cfgValue;
  131. }
  132. parent::__construct($fulfilled || ($approveCfgAbsence && false === $cfgValue), $testMessage, $helpHtml, $helpText, $optional);
  133. }
  134. }
  135. /**
  136. * A RequirementCollection represents a set of Requirement instances.
  137. *
  138. * Users of PHP 5.2 should be able to run the requirements checks.
  139. * This is why the class must be compatible with PHP 5.2
  140. * (e.g. not using namespaces and closures).
  141. *
  142. * @author Tobias Schultze <http://tobion.de>
  143. */
  144. class RequirementCollection implements IteratorAggregate
  145. {
  146. private $requirements = array();
  147. /**
  148. * Gets the current RequirementCollection as an Iterator.
  149. *
  150. * @return Traversable A Traversable interface
  151. */
  152. public function getIterator()
  153. {
  154. return new ArrayIterator($this->requirements);
  155. }
  156. /**
  157. * Adds a Requirement.
  158. *
  159. * @param Requirement $requirement A Requirement instance
  160. */
  161. public function add(Requirement $requirement)
  162. {
  163. $this->requirements[] = $requirement;
  164. }
  165. /**
  166. * Adds a mandatory requirement.
  167. *
  168. * @param Boolean $fulfilled Whether the requirement is fulfilled
  169. * @param string $testMessage The message for testing the requirement
  170. * @param string $helpHtml The help text formatted in HTML for resolving the problem
  171. * @param string|null $helpText The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
  172. */
  173. public function addRequirement($fulfilled, $testMessage, $helpHtml, $helpText = null)
  174. {
  175. $this->add(new Requirement($fulfilled, $testMessage, $helpHtml, $helpText, false));
  176. }
  177. /**
  178. * Adds an optional recommendation.
  179. *
  180. * @param Boolean $fulfilled Whether the recommendation is fulfilled
  181. * @param string $testMessage The message for testing the recommendation
  182. * @param string $helpHtml The help text formatted in HTML for resolving the problem
  183. * @param string|null $helpText The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
  184. */
  185. public function addRecommendation($fulfilled, $testMessage, $helpHtml, $helpText = null)
  186. {
  187. $this->add(new Requirement($fulfilled, $testMessage, $helpHtml, $helpText, true));
  188. }
  189. /**
  190. * Adds a mandatory requirement in form of a php.ini configuration.
  191. *
  192. * @param string $cfgName The configuration name used for ini_get()
  193. * @param Boolean|callback $evaluation Either a Boolean indicating whether the configuration should evaluate to true or false,
  194. or a callback function receiving the configuration value as parameter to determine the fulfillment of the requirement
  195. * @param Boolean $approveCfgAbsence If true the Requirement will be fulfilled even if the configuration option does not exist, i.e. ini_get() returns false.
  196. This is helpful for abandoned configs in later PHP versions or configs of an optional extension, like Suhosin.
  197. Example: You require a config to be true but PHP later removes this config and defaults it to true internally.
  198. * @param string $testMessage The message for testing the requirement (when null and $evaluation is a Boolean a default message is derived)
  199. * @param string $helpHtml The help text formatted in HTML for resolving the problem (when null and $evaluation is a Boolean a default help is derived)
  200. * @param string|null $helpText The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
  201. */
  202. public function addPhpIniRequirement($cfgName, $evaluation, $approveCfgAbsence = false, $testMessage = null, $helpHtml = null, $helpText = null)
  203. {
  204. $this->add(new PhpIniRequirement($cfgName, $evaluation, $approveCfgAbsence, $testMessage, $helpHtml, $helpText, false));
  205. }
  206. /**
  207. * Adds an optional recommendation in form of a php.ini configuration.
  208. *
  209. * @param string $cfgName The configuration name used for ini_get()
  210. * @param Boolean|callback $evaluation Either a Boolean indicating whether the configuration should evaluate to true or false,
  211. or a callback function receiving the configuration value as parameter to determine the fulfillment of the requirement
  212. * @param Boolean $approveCfgAbsence If true the Requirement will be fulfilled even if the configuration option does not exist, i.e. ini_get() returns false.
  213. This is helpful for abandoned configs in later PHP versions or configs of an optional extension, like Suhosin.
  214. Example: You require a config to be true but PHP later removes this config and defaults it to true internally.
  215. * @param string $testMessage The message for testing the requirement (when null and $evaluation is a Boolean a default message is derived)
  216. * @param string $helpHtml The help text formatted in HTML for resolving the problem (when null and $evaluation is a Boolean a default help is derived)
  217. * @param string|null $helpText The help text (when null, it will be inferred from $helpHtml, i.e. stripped from HTML tags)
  218. */
  219. public function addPhpIniRecommendation($cfgName, $evaluation, $approveCfgAbsence = false, $testMessage = null, $helpHtml = null, $helpText = null)
  220. {
  221. $this->add(new PhpIniRequirement($cfgName, $evaluation, $approveCfgAbsence, $testMessage, $helpHtml, $helpText, true));
  222. }
  223. /**
  224. * Adds a requirement collection to the current set of requirements.
  225. *
  226. * @param RequirementCollection $collection A RequirementCollection instance
  227. */
  228. public function addCollection(RequirementCollection $collection)
  229. {
  230. $this->requirements = array_merge($this->requirements, $collection->all());
  231. }
  232. /**
  233. * Returns both requirements and recommendations.
  234. *
  235. * @return array Array of Requirement instances
  236. */
  237. public function all()
  238. {
  239. return $this->requirements;
  240. }
  241. /**
  242. * Returns all mandatory requirements.
  243. *
  244. * @return array Array of Requirement instances
  245. */
  246. public function getRequirements()
  247. {
  248. $array = array();
  249. foreach ($this->requirements as $req) {
  250. if (!$req->isOptional()) {
  251. $array[] = $req;
  252. }
  253. }
  254. return $array;
  255. }
  256. /**
  257. * Returns the mandatory requirements that were not met.
  258. *
  259. * @return array Array of Requirement instances
  260. */
  261. public function getFailedRequirements()
  262. {
  263. $array = array();
  264. foreach ($this->requirements as $req) {
  265. if (!$req->isFulfilled() && !$req->isOptional()) {
  266. $array[] = $req;
  267. }
  268. }
  269. return $array;
  270. }
  271. /**
  272. * Returns all optional recommmendations.
  273. *
  274. * @return array Array of Requirement instances
  275. */
  276. public function getRecommendations()
  277. {
  278. $array = array();
  279. foreach ($this->requirements as $req) {
  280. if ($req->isOptional()) {
  281. $array[] = $req;
  282. }
  283. }
  284. return $array;
  285. }
  286. /**
  287. * Returns the recommendations that were not met.
  288. *
  289. * @return array Array of Requirement instances
  290. */
  291. public function getFailedRecommendations()
  292. {
  293. $array = array();
  294. foreach ($this->requirements as $req) {
  295. if (!$req->isFulfilled() && $req->isOptional()) {
  296. $array[] = $req;
  297. }
  298. }
  299. return $array;
  300. }
  301. /**
  302. * Returns whether a php.ini configuration is not correct.
  303. *
  304. * @return Boolean php.ini configuration problem?
  305. */
  306. public function hasPhpIniConfigIssue()
  307. {
  308. foreach ($this->requirements as $req) {
  309. if (!$req->isFulfilled() && $req instanceof PhpIniRequirement) {
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. /**
  316. * Returns the PHP configuration file (php.ini) path.
  317. *
  318. * @return string|false php.ini file path
  319. */
  320. public function getPhpIniConfigPath()
  321. {
  322. return get_cfg_var('cfg_file_path');
  323. }
  324. }
  325. /**
  326. * This class specifies all requirements and optional recommendations that
  327. * are necessary to run the Symfony Standard Edition.
  328. *
  329. * @author Tobias Schultze <http://tobion.de>
  330. */
  331. class SymfonyRequirements extends RequirementCollection
  332. {
  333. const REQUIRED_PHP_VERSION = '5.3.3';
  334. /**
  335. * Constructor that initializes the requirements.
  336. */
  337. public function __construct()
  338. {
  339. /* mandatory requirements follow */
  340. $installedPhpVersion = phpversion();
  341. $this->addRequirement(
  342. version_compare($installedPhpVersion, self::REQUIRED_PHP_VERSION, '>='),
  343. sprintf('PHP version must be at least %s (%s installed)', self::REQUIRED_PHP_VERSION, $installedPhpVersion),
  344. sprintf('You are running PHP version "<strong>%s</strong>", but Symfony needs at least PHP "<strong>%s</strong>" to run.
  345. Before using Symfony, upgrade your PHP installation, preferably to the latest version.',
  346. $installedPhpVersion, self::REQUIRED_PHP_VERSION),
  347. sprintf('Install PHP %s or newer (installed version is %s)', self::REQUIRED_PHP_VERSION, $installedPhpVersion)
  348. );
  349. $this->addRequirement(
  350. is_dir(__DIR__.'/../vendor/symfony'),
  351. 'Vendor libraries must be installed',
  352. 'Vendor libraries are missing. Install composer following instructions from <a href="http://getcomposer.org/">http://getcomposer.org/</a>. ' .
  353. 'Then run "<strong>php composer.phar install</strong>" to install them.'
  354. );
  355. $baseDir = basename(__DIR__);
  356. $this->addRequirement(
  357. is_writable(__DIR__.'/cache'),
  358. "$baseDir/cache/ directory must be writable",
  359. "Change the permissions of the \"<strong>$baseDir/cache/</strong>\" directory so that the web server can write into it."
  360. );
  361. $this->addRequirement(
  362. is_writable(__DIR__.'/logs'),
  363. "$baseDir/logs/ directory must be writable",
  364. "Change the permissions of the \"<strong>$baseDir/logs/</strong>\" directory so that the web server can write into it."
  365. );
  366. $this->addPhpIniRequirement(
  367. 'date.timezone', true, false,
  368. 'date.timezone setting must be set',
  369. 'Set the "<strong>date.timezone</strong>" setting in php.ini<a href="#phpini">*</a> (like Europe/Paris).'
  370. );
  371. $this->addRequirement(
  372. function_exists('json_encode'),
  373. 'json_encode() must be available',
  374. 'Install and enable the <strong>JSON</strong> extension.'
  375. );
  376. $this->addRequirement(
  377. function_exists('session_start'),
  378. 'session_start() must be available',
  379. 'Install and enable the <strong>session</strong> extension.'
  380. );
  381. $this->addRequirement(
  382. function_exists('ctype_alpha'),
  383. 'ctype_alpha() must be available',
  384. 'Install and enable the <strong>ctype</strong> extension.'
  385. );
  386. $this->addRequirement(
  387. function_exists('token_get_all'),
  388. 'token_get_all() must be available',
  389. 'Install and enable the <strong>Tokenizer</strong> extension.'
  390. );
  391. $this->addRequirement(
  392. function_exists('simplexml_import_dom'),
  393. 'simplexml_import_dom() must be available',
  394. 'Install and enable the <strong>SimpleXML</strong> extension.'
  395. );
  396. $this->addRequirement(
  397. !(function_exists('apc_store') && ini_get('apc.enabled')) || version_compare(phpversion('apc'), '3.0.17', '>='),
  398. 'APC version must be at least 3.0.17',
  399. 'Upgrade your <strong>APC</strong> extension (3.0.17+)'
  400. );
  401. $this->addPhpIniRequirement('detect_unicode', false);
  402. $this->addPhpIniRequirement(
  403. 'suhosin.executor.include.whitelist',
  404. create_function('$cfgValue', 'return false !== stripos($cfgValue, "phar");'),
  405. true,
  406. 'suhosin.executor.include.whitelist must be configured correctly in php.ini',
  407. 'Add "<strong>phar</strong>" to <strong>suhosin.executor.include.whitelist</strong> in php.ini<a href="#phpini">*</a>.'
  408. );
  409. $pcreVersion = defined('PCRE_VERSION') ? (float) PCRE_VERSION : null;
  410. $this->addRequirement(
  411. null !== $pcreVersion && $pcreVersion > 8.0,
  412. sprintf('PCRE extension must be available and at least 8.0 (%s installed)', $pcreVersion ? $pcreVersion : 'not'),
  413. 'Upgrade your <strong>PCRE</strong> extension (8.0+)'
  414. );
  415. /* optional recommendations follow */
  416. $this->addRecommendation(
  417. version_compare($installedPhpVersion, '5.3.8', '>='),
  418. sprintf('Annotations might not work properly due to the PHP bug #55156 before PHP 5.3.8 (%s installed)', $installedPhpVersion),
  419. 'Install PHP 5.3.8 or newer if your project uses annotations'
  420. );
  421. $this->addRecommendation(
  422. class_exists('DomDocument'),
  423. 'PHP-XML module should be installed',
  424. 'Install and enable the <strong>PHP-XML</strong> module.'
  425. );
  426. $this->addRecommendation(
  427. function_exists('mb_strlen'),
  428. 'mb_strlen() should be available',
  429. 'Install and enable the <strong>mbstring</strong> extension.'
  430. );
  431. $this->addRecommendation(
  432. function_exists('iconv'),
  433. 'iconv() should be available',
  434. 'Install and enable the <strong>iconv</strong> extension.'
  435. );
  436. $this->addRecommendation(
  437. function_exists('utf8_decode'),
  438. 'utf8_decode() should be available',
  439. 'Install and enable the <strong>XML</strong> extension.'
  440. );
  441. if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
  442. $this->addRecommendation(
  443. function_exists('posix_isatty'),
  444. 'posix_isatty() should be available',
  445. 'Install and enable the <strong>php_posix</strong> extension (used to colorize the CLI output).'
  446. );
  447. }
  448. $this->addRecommendation(
  449. class_exists('Locale'),
  450. 'intl extension should be available',
  451. 'Install and enable the <strong>intl</strong> extension (used for validators).'
  452. );
  453. if (class_exists('Locale')) {
  454. if (defined('INTL_ICU_VERSION')) {
  455. $version = INTL_ICU_VERSION;
  456. } else {
  457. $reflector = new ReflectionExtension('intl');
  458. ob_start();
  459. $reflector->info();
  460. $output = strip_tags(ob_get_clean());
  461. preg_match('/^ICU version +(?:=> )?(.*)$/m', $output, $matches);
  462. $version = $matches[1];
  463. }
  464. $this->addRecommendation(
  465. version_compare($version, '4.0', '>='),
  466. 'intl ICU version should be at least 4+',
  467. 'Upgrade your <strong>intl</strong> extension with a newer ICU version (4+).'
  468. );
  469. }
  470. $accelerator =
  471. (function_exists('apc_store') && ini_get('apc.enabled'))
  472. ||
  473. function_exists('eaccelerator_put') && ini_get('eaccelerator.enable')
  474. ||
  475. function_exists('xcache_set')
  476. ;
  477. $this->addRecommendation(
  478. $accelerator,
  479. 'a PHP accelerator should be installed',
  480. 'Install and enable a <strong>PHP accelerator</strong> like APC (highly recommended).'
  481. );
  482. $this->addPhpIniRecommendation('short_open_tag', false);
  483. $this->addPhpIniRecommendation('magic_quotes_gpc', false, true);
  484. $this->addPhpIniRecommendation('register_globals', false, true);
  485. $this->addPhpIniRecommendation('session.auto_start', false);
  486. $this->addRecommendation(
  487. class_exists('PDO'),
  488. 'PDO should be installed',
  489. 'Install <strong>PDO</strong> (mandatory for Doctrine).'
  490. );
  491. if (class_exists('PDO')) {
  492. $drivers = PDO::getAvailableDrivers();
  493. $this->addRecommendation(
  494. count($drivers),
  495. sprintf('PDO should have some drivers installed (currently available: %s)', count($drivers) ? implode(', ', $drivers) : 'none'),
  496. 'Install <strong>PDO drivers</strong> (mandatory for Doctrine).'
  497. );
  498. }
  499. }
  500. }