Development

/branches/1.1/lib/config/sfYamlConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/config/sfYamlConfigHandler.class.php

Revision 17858, 4.4 kB (checked in by FabianLange, 6 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • 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  * sfYamlConfigHandler is a base class for YAML (.yml) configuration handlers. This class
13  * provides a central location for parsing YAML files.
14  *
15  * @package    symfony
16  * @subpackage config
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @version    SVN: $Id$
19  */
20 abstract class sfYamlConfigHandler extends sfConfigHandler
21 {
22   protected
23     $yamlConfig = null;
24
25   /**
26    * Parses an array of YAMLs files and merges them in one configuration array.
27    *
28    * @param array $configFiles An array of configuration file paths
29    *
30    * @return array A merged configuration array
31    */
32   static public function parseYamls($configFiles)
33   {
34     $config = array();
35     foreach ($configFiles as $configFile)
36     {
37       $config = sfToolkit::arrayDeepMerge($config, self::parseYaml($configFile));
38     }
39
40     return $config;
41   }
42
43   /**
44    * Parses a YAML (.yml) configuration file.
45    *
46    * @param string $configFile An absolute filesystem path to a configuration file
47    *
48    * @return string A parsed .yml configuration
49    *
50    * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
51    * @throws sfParseException If a requested configuration file is improperly formatted
52    */
53   static public function parseYaml($configFile)
54   {
55     if (!is_readable($configFile))
56     {
57       // can't read the configuration
58       throw new sfConfigurationException(sprintf('Configuration file "%s" does not exist or is not readable.', $configFile));
59     }
60
61     // parse our config
62     $config = sfYaml::load($configFile);
63
64     if ($config === false)
65     {
66       // configuration couldn't be parsed
67       throw new sfParseException(sprintf('Configuration file "%s" could not be parsed', $configFile));
68     }
69
70     return is_null($config) ? array() : $config;
71   }
72
73   /**
74    * Merges configuration values for a given key and category.
75    *
76    * @param string $keyName  The key name
77    * @param string $category The category name
78    *
79    * @return string The value associated with this key name and category
80    */
81   protected function mergeConfigValue($keyName, $category)
82   {
83     $values = array();
84
85     if (isset($this->yamlConfig['all'][$keyName]) && is_array($this->yamlConfig['all'][$keyName]))
86     {
87       $values = $this->yamlConfig['all'][$keyName];
88     }
89
90     if ($category && isset($this->yamlConfig[$category][$keyName]) && is_array($this->yamlConfig[$category][$keyName]))
91     {
92       $values = array_merge($values, $this->yamlConfig[$category][$keyName]);
93     }
94
95     return $values;
96   }
97
98   /**
99    * Gets a configuration value for a given key and category.
100    *
101    * @param string $keyName      The key name
102    * @param string $category     The category name
103    * @param string $defaultValue The default value
104    *
105    * @return string The value associated with this key name and category
106    */
107   protected function getConfigValue($keyName, $category, $defaultValue = null)
108   {
109     if (isset($this->yamlConfig[$category][$keyName]))
110     {
111       return $this->yamlConfig[$category][$keyName];
112     }
113     else if (isset($this->yamlConfig['all'][$keyName]))
114     {
115       return $this->yamlConfig['all'][$keyName];
116     }
117
118     return $defaultValue;
119   }
120
121   static public function flattenConfiguration($config)
122   {
123     $config['all'] = sfToolkit::arrayDeepMerge(
124       isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
125       isset($config['all']) && is_array($config['all']) ? $config['all'] : array()
126     );
127
128     unset($config['default']);
129
130     return $config;
131   }
132
133   /**
134    * Merges default, all and current environment configurations.
135    *
136    * @param array $config The main configuratino array
137    *
138    * @return array The merged configuration
139    */
140   static public function flattenConfigurationWithEnvironment($config)
141   {
142     return sfToolkit::arrayDeepMerge(
143       isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
144       isset($config['all']) && is_array($config['all']) ? $config['all'] : array(),
145       isset($config[sfConfig::get('sf_environment')]) && is_array($config[sfConfig::get('sf_environment')]) ? $config[sfConfig::get('sf_environment')] : array()
146     );
147   }
148 }
149
Note: See TracBrowser for help on using the browser.