Development

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

You must first sign up to be able to contribute.

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

Revision 3254, 4.1 kB (checked in by fabien, 8 years ago)

-

  • 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  *
13  * @package    symfony
14  * @subpackage config
15  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
16  * @version    SVN: $Id$
17  */
18 class sfDefineEnvironmentConfigHandler extends sfYamlConfigHandler
19 {
20   /**
21    * Executes this configuration handler.
22    *
23    * @param  string An absolute filesystem path to a configuration file
24    *
25    * @return string Data to be written to a cache file
26    *
27    * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
28    * @throws sfParseException If a requested configuration file is improperly formatted
29    */
30   public function execute($configFiles)
31   {
32     // get our prefix
33     $prefix = strtolower($this->getParameterHolder()->get('prefix', ''));
34
35     // add dynamic prefix if needed
36     if ($this->getParameterHolder()->get('module', false))
37     {
38       $prefix .= "'.strtolower(\$moduleName).'_";
39     }
40
41     // parse the yaml
42     $myConfig = $this->mergeEnvironment($this->parseYamls($configFiles));
43
44     $values = array();
45     foreach ($myConfig as $category => $keys)
46     {
47       $values = array_merge($values, $this->getValues($prefix, $category, $keys));
48     }
49
50     $data = '';
51     foreach ($values as $key => $value)
52     {
53       $data .= sprintf("  '%s' => %s,\n", $key, var_export($value, true));
54     }
55
56     // compile data
57     $retval = '';
58     if ($values)
59     {
60       $retval = "<?php\n".
61                 "// auto-generated by sfDefineEnvironmentConfigHandler\n".
62                 "// date: %s\nsfConfig::add(array(\n%s));\n";
63       $retval = sprintf($retval, date('Y/m/d H:i:s'), $data);
64     }
65
66     return $retval;
67   }
68
69   /**
70    * Gets values from the configuration array.
71    *
72    * @param string The prefix name
73    * @param string The category name
74    * @param mixed  The key/value array
75    *
76    * @param array The new key/value array
77    */
78   protected function getValues($prefix, $category, $keys)
79   {
80     if (!is_array($keys))
81     {
82       list($key, $value) = $this->fixCategoryValue($prefix.strtolower($category), '', $keys);
83
84       return array($key => $value);
85     }
86
87     $values = array();
88
89     $category = $this->fixCategoryName($category, $prefix);
90
91     // loop through all key/value pairs
92     foreach ($keys as $key => $value)
93     {
94       list($key, $value) = $this->fixCategoryValue($category, $key, $value);
95       $values[$key] = $value;
96     }
97
98     return $values;
99   }
100
101   /**
102    * Fixes the category name and replaces constants in the value.
103    *
104    * @param string The category name
105    * @param string The key name
106    * @param string The value
107    *
108    * @param string Return the new key and value
109    */
110   protected function fixCategoryValue($category, $key, $value)
111   {
112     // prefix the key
113     $key = $category.$key;
114
115     // replace constant values
116     $value = $this->replaceConstants($value);
117
118     return array($key, $value);
119   }
120
121   /**
122    * Fixes the category name.
123    *
124    * @param string The category name
125    * @param string The prefix
126    *
127    * @return string The fixed category name
128    */
129   protected function fixCategoryName($category, $prefix)
130   {
131     // categories starting without a period will be prepended to the key
132     if ($category[0] != '.')
133     {
134       $category = $prefix.$category.'_';
135     }
136     else
137     {
138       $category = $prefix;
139     }
140
141     return $category;
142   }
143
144   /**
145    * Merges default, all and current environment configurations.
146    *
147    * @param array The main configuratino array
148    *
149    * @param array The merged configuration
150    */
151   protected function mergeEnvironment($config)
152   {
153     return sfToolkit::arrayDeepMerge(
154       isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
155       isset($config['all']) && is_array($config['all']) ? $config['all'] : array(),
156       isset($config[sfConfig::get('sf_environment')]) && is_array($config[sfConfig::get('sf_environment')]) ? $config[sfConfig::get('sf_environment')] : array()
157     );
158   }
159 }
160
Note: See TracBrowser for help on using the browser.