Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drush / drush / src / Preflight / PreflightVerify.php
1 <?php
2 namespace Drush\Preflight;
3
4 use Drush\Drush;
5 use Drush\Config\Environment;
6 use Drush\Utils\StringUtils;
7
8 /**
9  * Helper methods to verify preflight state.
10  */
11 class PreflightVerify
12 {
13     /**
14      * Throw an exception if the environment is not right for running Drush.
15      *
16      * @param Environment $environment
17      */
18     public function verify(Environment $environment)
19     {
20         // Fail fast if the PHP version is not at least 5.6.0.
21         // We'll come back and check this again later, in case someone
22         // set a higher value in a configuration file.
23         $this->confirmPhpVersion('5.6.0');
24
25         // Fail if this is not a CLI php
26         $this->confirmUsingCLI($environment);
27
28         // Fail if any mandatory functions have been disabled, or any
29         // illegal options have been set in php.ini.
30         $this->checkPhpIni();
31     }
32
33     /**
34      * Fail fast if the php version does not meet the minimum requirements.
35      *
36      * @param string $minimumPhpVersion
37      *   The minimum allowable php version
38      */
39     public function confirmPhpVersion($minimumPhpVersion)
40     {
41         if (version_compare(phpversion(), $minimumPhpVersion) < 0 && !getenv('DRUSH_NO_MIN_PHP')) {
42             throw new \Exception(StringUtils::interpolate('Your command line PHP installation is too old. Drush requires at least PHP {version}. To suppress this check, set the environment variable DRUSH_NO_MIN_PHP=1', ['version' => $minimumPhpVersion]));
43         }
44     }
45
46     /**
47      * Fail if not being run from the command line.
48      *
49      * @param Environment $environment
50      */
51     protected function confirmUsingCLI(Environment $environment)
52     {
53         if (!$environment->verifyCLI()) {
54             throw new \Exception(StringUtils::interpolate('Drush is designed to run via the command line.'));
55         }
56     }
57
58     /**
59      * Evaluate the environment before command bootstrapping
60      * begins.  If the php environment is too restrictive, then
61      * notify the user that a setting change is needed and abort.
62      */
63     protected function checkPhpIni()
64     {
65         $ini_checks = ['safe_mode' => '', 'open_basedir' => '', 'disable_functions' => ['exec', 'system'], 'disable_classes' => ''];
66
67         // Test to insure that certain php ini restrictions have not been enabled
68         $prohibited_list = [];
69         foreach ($ini_checks as $prohibited_mode => $disallowed_value) {
70             $ini_value = ini_get($prohibited_mode);
71             if ($this->invalidIniValue($ini_value, $disallowed_value)) {
72                 $prohibited_list[] = $prohibited_mode;
73             }
74         }
75         if (!empty($prohibited_list)) {
76             throw new \Exception(StringUtils::interpolate('The following restricted PHP modes have non-empty values: {prohibited_list}. This configuration is incompatible with drush.  {php_ini_msg}', ['prohibited_list' => implode(' and ', $prohibited_list), 'php_ini_msg' => $this->loadedPhpIniMessage()]));
77         }
78     }
79
80     /**
81      * Determine whether an ini value is valid based on the criteria.
82      *
83      * @param string $ini_value
84      *   The value of the ini setting being tested.
85      * @param string|string[] $disallowed_value
86      *   The value that the ini seting cannot be, or a list of disallowed
87      *   values that cannot appear in the setting.
88      * @return bool
89      */
90     protected function invalidIniValue($ini_value, $disallowed_value)
91     {
92         if (empty($disallowed_value)) {
93             return !empty($ini_value) && (strcasecmp($ini_value, 'off') != 0);
94         } else {
95             foreach ($disallowed_value as $test_value) {
96                 if (preg_match('/(^|,)' . $test_value . '(,|$)/', $ini_value)) {
97                     return true;
98                 }
99             }
100         }
101         return false;
102     }
103
104     /**
105      * Returns a localizable message about php.ini that
106      * varies depending on whether the php_ini_loaded_file()
107      * is available or not.
108      */
109     protected function loadedPhpIniMessage()
110     {
111         if (function_exists('php_ini_loaded_file')) {
112             return StringUtils::interpolate('Please check your configuration settings in !phpini or in your drush.ini file; see examples/example.drush.ini for details.', ['!phpini' => php_ini_loaded_file()]);
113         } else {
114             return StringUtils::interpolate('Please check your configuration settings in your php.ini file or in your drush.ini file; see examples/example.drush.ini for details.');
115         }
116     }
117 }