[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/config/ -> sfPhpConfigHandler.class.php (source)

   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: sfPhpConfigHandler.class.php 3203 2007-01-09 18:32:54Z fabien $
  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          if ($configs[$key]['access'] != 7)
  61          {
  62            $error = sprintf('Configuration file "%s" specifies key "%s" which cannot be overrided', $configFiles[0], $key);
  63            throw new sfParseException($error);
  64          }
  65  
  66          // escape value
  67          $value = str_replace("'", "\\'", $value);
  68  
  69          $data[] = sprintf("ini_set('%s', '%s');", $key, $value);
  70        }
  71      }
  72  
  73      // check some php.ini settings
  74      if (isset($config['check']))
  75      {
  76        foreach ($config['check'] as $key => $value)
  77        {
  78          $key = strtolower($key);
  79  
  80          // key exists?
  81          if (!array_key_exists($key, $configs))
  82          {
  83            $error = sprintf('Configuration file "%s" specifies key "%s" which is not a php.ini directive [err0002]', $configFiles[0], $key);
  84            throw new sfParseException($error);
  85          }
  86  
  87          if (ini_get($key) != $value)
  88          {
  89            $error = sprintf('Configuration file "%s" specifies that php.ini "%s" key must be set to "%s". The current value is "%s" (%s). [err0001]', $configFiles[0], $key, var_export($value, true), var_export(ini_get($key), true), $this->get_ini_path());
  90            throw new sfInitializationException($error);
  91          }
  92        }
  93      }
  94  
  95      // warn about some php.ini settings
  96      if (isset($config['warn']))
  97      {
  98        foreach ($config['warn'] as $key => $value)
  99        {
 100          $key = strtolower($key);
 101  
 102          // key exists?
 103          if (!array_key_exists($key, $configs))
 104          {
 105            $error = sprintf('Configuration file "%s" specifies key "%s" which is not a php.ini directive [err0002]', $configFiles[0], $key);
 106            throw new sfParseException($error);
 107          }
 108  
 109          $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());
 110          $data[] = sprintf("if (ini_get('%s') != %s)\n{\n  sfLogger::getInstance()->warning('%s');\n}\n", $key, var_export($value, true), str_replace("'", "\\'", $warning));
 111        }
 112      }
 113  
 114      // check for some extensions
 115      if (isset($config['extensions']))
 116      {
 117        foreach ($config['extensions'] as $extension_name)
 118        {
 119          if (!extension_loaded($extension_name))
 120          {
 121            $error = sprintf('Configuration file "%s" specifies that the PHP extension "%s" should be loaded. (%s)', $configFiles[0], $extension_name, $this->get_ini_path());
 122            throw new sfInitializationException($error);
 123          }
 124        }
 125      }
 126  
 127      // compile data
 128      $retval = sprintf("<?php\n".
 129                        "// auto-generated by sfPhpConfigHandler\n".
 130                        "// date: %s\n%s\n", date('Y/m/d H:i:s'), implode("\n", $data));
 131  
 132      return $retval;
 133    }
 134  
 135    /**
 136     * Gets the php.ini path used by PHP.
 137     *
 138     * @return string the php.ini path
 139     */
 140    protected function get_ini_path()
 141    {
 142      $cfg_path = get_cfg_var('cfg_file_path');
 143      if ($cfg_path == '')
 144      {
 145        $ini_path = 'WARNING: system is not using a php.ini file';
 146      }
 147      else
 148      {
 149        $ini_path = sprintf('php.ini location: "%s"', $cfg_path);
 150      }
 151  
 152      return $ini_path;
 153    }
 154  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7