Development

/branches/1.0/lib/config/sfPhpConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/config/sfPhpConfigHandler.class.php

Revision 11377, 5.1 kB (checked in by hartym, 6 years ago)

[1.0] added links to php bugs that implied a hack for symfony 1.0 to work with php 5.2.6

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.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 /**
12  * sfPhpConfigHandler allows you to override php.ini configuration at runtime.
13  *
14  * @package    symfony
15  * @subpackage config
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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     // parse the yaml
37     $config = $this->parseYamls($configFiles);
38
39     // init our data array
40     $data = array();
41
42     // get all php.ini configuration
43     $configs = ini_get_all();
44
45     // set some php.ini keys
46     if (isset($config['set']))
47     {
48       foreach ($config['set'] as $key => $value)
49       {
50         $key = strtolower($key);
51
52         // key exists?
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         // key is overridable?
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         // escape value
70         $value = str_replace("'", "\\'", $value);
71
72         $data[] = sprintf("ini_set('%s', '%s');", $key, $value);
73       }
74     }
75
76     // check some php.ini settings
77     if (isset($config['check']))
78     {
79       foreach ($config['check'] as $key => $value)
80       {
81         $key = strtolower($key);
82
83         // key exists?
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     // warn about some php.ini settings
99     if (isset($config['warn']))
100     {
101       foreach ($config['warn'] as $key => $value)
102       {
103         $key = strtolower($key);
104
105         // key exists?
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     // check for some extensions
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     // compile data
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
Note: See TracBrowser for help on using the browser.