| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfPhpConfigHandler extends sfYamlConfigHandler |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
* Executes this configuration handler |
|---|
| 23 |
* |
|---|
| 24 |
* @param array An array of absolute filesystem path to a configuration file |
|---|
| 25 |
* |
|---|
| 26 |
* @return string Data to be written to a cache file |
|---|
| 27 |
* |
|---|
| 28 |
* @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable |
|---|
| 29 |
* @throws <b>sfParseException</b> If a requested configuration file is improperly formatted |
|---|
| 30 |
* @throws <b>sfInitializationException</b> If a php.yml key check fails |
|---|
| 31 |
*/ |
|---|
| 32 |
public function execute($configFiles) |
|---|
| 33 |
{ |
|---|
| 34 |
$this->initialize(); |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
$config = $this->parseYamls($configFiles); |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
$data = array(); |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
$configs = ini_get_all(); |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
if (isset($config['set'])) |
|---|
| 47 |
{ |
|---|
| 48 |
foreach ($config['set'] as $key => $value) |
|---|
| 49 |
{ |
|---|
| 50 |
$key = strtolower($key); |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
if (!array_key_exists($key, $configs)) |
|---|
| 54 |
{ |
|---|
| 55 |
$error = sprintf('Configuration file "%s" specifies key "%s" which is not a php.ini directive.', $configFiles[0], $key); |
|---|
| 56 |
throw new sfParseException($error); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
// 63 is returned by PHP 5.2.6 instead of 7 when a php.ini key is changed several times per script |
|---|
| 61 |
// PHP bug: http://bugs.php.net/bug.php?id=44936 |
|---|
| 62 |
// Resolution diff: http://cvs.php.net/viewvc.cgi/ZendEngine2/zend_ini.c?r1=1.39.2.2.2.26&r2=1.39.2.2.2.27&pathrev=PHP_5_2 |
|---|
| 63 |
if ($configs[$key]['access'] != 7 && $configs[$key]['access'] != 63) |
|---|
| 64 |
{ |
|---|
| 65 |
$error = sprintf('Configuration file "%s" specifies key "%s" which cannot be overrided.', $configFiles[0], $key); |
|---|
| 66 |
throw new sfParseException($error); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
$value = str_replace("'", "\\'", $value); |
|---|
| 71 |
|
|---|
| 72 |
$data[] = sprintf("ini_set('%s', '%s');", $key, $value); |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
if (isset($config['check'])) |
|---|
| 78 |
{ |
|---|
| 79 |
foreach ($config['check'] as $key => $value) |
|---|
| 80 |
{ |
|---|
| 81 |
$key = strtolower($key); |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
if (!array_key_exists($key, $configs)) |
|---|
| 85 |
{ |
|---|
| 86 |
$error = sprintf('Configuration file "%s" specifies key "%s" which is not a php.ini directive.', $configFiles[0], $key); |
|---|
| 87 |
throw new sfParseException($error); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
if (ini_get($key) != $value) |
|---|
| 91 |
{ |
|---|
| 92 |
$error = sprintf('Configuration file "%s" specifies that php.ini "%s" key must be set to "%s". The current value is "%s" (%s).', $configFiles[0], $key, var_export($value, true), var_export(ini_get($key), true), $this->get_ini_path()); |
|---|
| 93 |
throw new sfInitializationException($error); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
if (isset($config['warn'])) |
|---|
| 100 |
{ |
|---|
| 101 |
foreach ($config['warn'] as $key => $value) |
|---|
| 102 |
{ |
|---|
| 103 |
$key = strtolower($key); |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
if (!array_key_exists($key, $configs)) |
|---|
| 107 |
{ |
|---|
| 108 |
$error = sprintf('Configuration file "%s" specifies key "%s" which is not a php.ini directive.', $configFiles[0], $key); |
|---|
| 109 |
throw new sfParseException($error); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
$warning = sprintf('{sfPhpConfigHandler} php.ini "%s" key is better set to "%s" (current value is "%s" - %s).', $key, var_export($value, true), var_export(ini_get($key), true), $this->get_ini_path()); |
|---|
| 113 |
$data[] = sprintf("if (ini_get('%s') != %s)\n{\n sfLogger::getInstance()->warning('%s');\n}\n", $key, var_export($value, true), str_replace("'", "\\'", $warning)); |
|---|
| 114 |
} |
|---|
| 115 |
} |
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
if (isset($config['extensions'])) |
|---|
| 119 |
{ |
|---|
| 120 |
foreach ($config['extensions'] as $extension_name) |
|---|
| 121 |
{ |
|---|
| 122 |
if (!extension_loaded($extension_name)) |
|---|
| 123 |
{ |
|---|
| 124 |
$error = sprintf('Configuration file "%s" specifies that the PHP extension "%s" should be loaded. (%s).', $configFiles[0], $extension_name, $this->get_ini_path()); |
|---|
| 125 |
throw new sfInitializationException($error); |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
$retval = sprintf("<?php\n". |
|---|
| 132 |
"// auto-generated by sfPhpConfigHandler\n". |
|---|
| 133 |
"// date: %s\n%s\n", date('Y/m/d H:i:s'), implode("\n", $data)); |
|---|
| 134 |
|
|---|
| 135 |
return $retval; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
* Gets the php.ini path used by PHP. |
|---|
| 140 |
* |
|---|
| 141 |
* @return string the php.ini path |
|---|
| 142 |
*/ |
|---|
| 143 |
protected function get_ini_path() |
|---|
| 144 |
{ |
|---|
| 145 |
$cfg_path = get_cfg_var('cfg_file_path'); |
|---|
| 146 |
if ($cfg_path == '') |
|---|
| 147 |
{ |
|---|
| 148 |
$ini_path = 'WARNING: system is not using a php.ini file'; |
|---|
| 149 |
} |
|---|
| 150 |
else |
|---|
| 151 |
{ |
|---|
| 152 |
$ini_path = sprintf('php.ini location: "%s"', $cfg_path); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
return $ini_path; |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|