Development

/branches/1.2/lib/config/sfFilterConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/config/sfFilterConfigHandler.class.php

Revision 17858, 6.0 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  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfFilterConfigHandler allows you to register filters with the system.
14  *
15  * @package    symfony
16  * @subpackage config
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 class sfFilterConfigHandler extends sfYamlConfigHandler
22 {
23   /**
24    * Executes this configuration handler
25    *
26    * @param array $configFiles An array of absolute filesystem path to a configuration file
27    *
28    * @return string Data to be written to a cache file
29    *
30    * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
31    * @throws sfParseException If a requested configuration file is improperly formatted
32    */
33   public function execute($configFiles)
34   {
35     // parse the yaml
36     $config = self::getConfiguration($configFiles);
37
38     // init our data and includes arrays
39     $data     = array();
40     $includes = array();
41
42     $execution = false;
43     $rendering = false;
44
45     // let's do our fancy work
46     foreach ($config as $category => $keys)
47     {
48       if (isset($keys['enabled']) && !$keys['enabled'])
49       {
50         continue;
51       }
52
53       if (!isset($keys['class']))
54       {
55         // missing class key
56         throw new sfParseException(sprintf('Configuration file "%s" specifies category "%s" with missing class key.', $configFiles[0], $category));
57       }
58
59       $class = $keys['class'];
60
61       if (isset($keys['file']))
62       {
63         if (!is_readable($keys['file']))
64         {
65           // filter file doesn't exist
66           throw new sfParseException(sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s".', $configFiles[0], $class, $keys['file']));
67         }
68
69         // append our data
70         $includes[] = sprintf("require_once('%s');\n", $keys['file']);
71       }
72
73       $condition = true;
74       if (isset($keys['param']['condition']))
75       {
76         $condition = $keys['param']['condition'];
77         unset($keys['param']['condition']);
78       }
79
80       $type = isset($keys['param']['type']) ? $keys['param']['type'] : null;
81       unset($keys['param']['type']);
82
83       if ($condition)
84       {
85         // parse parameters
86         $parameters = isset($keys['param']) ? var_export($keys['param'], true) : 'null';
87
88         // append new data
89         if ('security' == $type)
90         {
91           $data[] = $this->addSecurityFilter($category, $class, $parameters);
92         }
93         else
94         {
95           $data[] = $this->addFilter($category, $class, $parameters);
96         }
97
98         if ('rendering' == $type)
99         {
100           $rendering = true;
101         }
102
103         if ('execution' == $type)
104         {
105           $execution = true;
106         }
107       }
108     }
109
110     if (!$rendering)
111     {
112       throw new sfParseException(sprintf('Configuration file "%s" must register a filter of type "rendering".', $configFiles[0]));
113     }
114
115     if (!$execution)
116     {
117       throw new sfParseException(sprintf('Configuration file "%s" must register a filter of type "execution".', $configFiles[0]));
118     }
119
120     // compile data
121     $retval = sprintf("<?php\n".
122                       "// auto-generated by sfFilterConfigHandler\n".
123                       "// date: %s\n%s\n%s\n\n", date('Y/m/d H:i:s'),
124                       implode("\n", $includes), implode("\n", $data));
125
126     return $retval;
127   }
128
129   /**
130    * Adds a filter statement to the data.
131    *
132    * @param string $category   The category name
133    * @param string $class      The filter class name
134    * @param array  $parameters Filter default parameters
135    *
136    * @return string The PHP statement
137    */
138   protected function addFilter($category, $class, $parameters)
139   {
140     return sprintf("\nlist(\$class, \$parameters) = (array) sfConfig::get('sf_%s_filter', array('%s', %s));\n".
141                       "\$filter = new \$class(sfContext::getInstance(), \$parameters);\n".
142                       "\$this->register(\$filter);",
143                       $category, $class, $parameters);
144   }
145
146   /**
147    * Adds a security filter statement to the data.
148    *
149    * @param string $category   The category name
150    * @param string $class      The filter class name
151    * @param array  $parameters Filter default parameters
152    *
153    * @return string The PHP statement
154    */
155   protected function addSecurityFilter($category, $class, $parameters)
156   {
157     return <<<EOF
158
159 // does this action require security?
160 if (\$actionInstance->isSecure())
161 {
162   {$this->addFilter($category, $class, $parameters)}
163 }
164 EOF;
165   }
166
167   /**
168    * @see sfConfigHandler
169    */
170   static public function getConfiguration(array $configFiles)
171   {
172     $config = self::parseYaml($configFiles[0]);
173     foreach (array_slice($configFiles, 1) as $i => $configFile)
174     {
175       // we get the order of the new file and merge with the previous configurations
176       $previous = $config;
177
178       $config = array();
179       foreach (self::parseYaml($configFile) as $key => $value)
180       {
181         $value = (array) $value;
182         $config[$key] = isset($previous[$key]) ? sfToolkit::arrayDeepMerge($previous[$key], $value) : $value;
183       }
184
185       // check that every key in previous array is still present (to avoid problem when upgrading)
186       foreach (array_keys($previous) as $key)
187       {
188         if (!isset($config[$key]))
189         {
190           throw new sfConfigurationException(sprintf('The filter name "%s" is defined in "%s" but not present in "%s" file. To disable a filter, add a "enabled" key with a false value.', $key, $configFiles[$i], $configFile));
191         }
192       }
193     }
194
195     $config = self::replaceConstants($config);
196
197     foreach ($config as $category => $keys)
198     {
199       if (isset($keys['file']))
200       {
201         $config[$category]['file'] = self::replacePath($keys['file']);
202       }
203     }
204
205     return $config;
206   }
207 }
208
Note: See TracBrowser for help on using the browser.