Development

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

You must first sign up to be able to contribute.

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

Revision 6367, 3.7 kB (checked in by fabien, 6 years ago)

fixed typo (closes #2070)

  • 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 and detecting required categories.
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 An array of configuration file paths
29    *
30    * @return array A merged configuration array
31    */
32   protected function parseYamls($configFiles)
33   {
34     $config = array();
35     foreach ($configFiles as $configFile)
36     {
37       $config = sfToolkit::arrayDeepMerge($config, $this->parseYaml($configFile));
38     }
39
40     return $config;
41   }
42
43   /**
44    * Parses a YAML (.yml) configuration file.
45    *
46    * @param string 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   protected function parseYaml($configFile)
54   {
55     if (!is_readable($configFile))
56     {
57       // can't read the configuration
58       $error = sprintf('Configuration file "%s" does not exist or is not readable', $configFile);
59
60       throw new sfConfigurationException($error);
61     }
62
63     // parse our config
64     $config = sfYaml::load($configFile);
65
66     if ($config === false || $config === null)
67     {
68       // configuration couldn't be parsed
69       $error = sprintf('Configuration file "%s" could not be parsed', $configFile);
70       throw new sfParseException($error);
71     }
72
73     // get a list of the required categories
74     $categories = $this->getParameterHolder()->get('required_categories', array());
75     foreach ($categories as $category)
76     {
77       if (!isset($config[$category]))
78       {
79         $error = sprintf('Configuration file "%s" is missing "%s" category', $configFile, $category);
80         throw new sfParseException($error);
81       }
82     }
83
84     return $config;
85   }
86
87   /**
88    * Merges configuration values for a given key and category.
89    *
90    * @param string The key name
91    * @param string The category name
92    *
93    * @return string The value associated with this key name and category
94    */
95   protected function mergeConfigValue($keyName, $category)
96   {
97     $values = array();
98
99     if (isset($this->yamlConfig['all'][$keyName]) && is_array($this->yamlConfig['all'][$keyName]))
100     {
101       $values = $this->yamlConfig['all'][$keyName];
102     }
103
104     if ($category && isset($this->yamlConfig[$category][$keyName]) && is_array($this->yamlConfig[$category][$keyName]))
105     {
106       $values = array_merge($values, $this->yamlConfig[$category][$keyName]);
107     }
108
109     return $values;
110   }
111
112   /**
113    * Gets a configuration value for a given key and category.
114    *
115    * @param string The key name
116    * @param string The category name
117    * @param string The default value
118    *
119    * @return string The value associated with this key name and category
120    */
121   protected function getConfigValue($keyName, $category, $defaultValue = null)
122   {
123     if (isset($this->yamlConfig[$category][$keyName]))
124     {
125       return $this->yamlConfig[$category][$keyName];
126     }
127     else if (isset($this->yamlConfig['all'][$keyName]))
128     {
129       return $this->yamlConfig['all'][$keyName];
130     }
131
132     return $defaultValue;
133   }
134 }
135
Note: See TracBrowser for help on using the browser.