Development

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

You must first sign up to be able to contribute.

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

Revision 17858, 3.7 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  *
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 $configFiles 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     $config = self::getConfiguration($configFiles);
43
44     $values = array();
45     foreach ($config 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 $prefix   The prefix name
73    * @param string $category The category name
74    * @param mixed  $keys      The key/value array
75    *
76    * @return 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 $category The category name
105    * @param string $key      The key name
106    * @param string $value    The value
107    *
108    * @return string Return the new key and value
109    */
110   protected function fixCategoryValue($category, $key, $value)
111   {
112     return array($category.$key, $value);
113   }
114
115   /**
116    * Fixes the category name.
117    *
118    * @param string $category The category name
119    * @param string $prefix   The prefix
120    *
121    * @return string The fixed category name
122    */
123   protected function fixCategoryName($category, $prefix)
124   {
125     // categories starting without a period will be prepended to the key
126     if ($category[0] != '.')
127     {
128       $category = $prefix.$category.'_';
129     }
130     else
131     {
132       $category = $prefix;
133     }
134
135     return $category;
136   }
137
138   /**
139    * @see sfConfigHandler
140    */
141   static public function getConfiguration(array $configFiles)
142   {
143     return self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
144   }
145 }
146
Note: See TracBrowser for help on using the browser.