Development

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

You must first sign up to be able to contribute.

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

Revision 7791, 6.0 kB (checked in by fabien, 6 years ago)

updated Sean Kerr email address

  • 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 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 = $this->parseYaml($configFiles[0]);
37     foreach (array_slice($configFiles, 1) as $i => $configFile)
38     {
39       // we get the order of the new file and merge with the previous configurations
40       $previous = $config;
41
42       $config = array();
43       foreach ($this->parseYaml($configFile) as $key => $value)
44       {
45         $value = (array) $value;
46         $config[$key] = isset($previous[$key]) ? sfToolkit::arrayDeepMerge($previous[$key], $value) : $value;
47       }
48
49       // check that every key in previous array is still present (to avoid problem when upgrading)
50       foreach (array_keys($previous) as $key)
51       {
52         if (!isset($config[$key]))
53         {
54           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));
55         }
56       }
57     }
58
59     // init our data and includes arrays
60     $data     = array();
61     $includes = array();
62
63     $execution = false;
64     $rendering = false;
65
66     // let's do our fancy work
67     foreach ($config as $category => $keys)
68     {
69       if (isset($keys['enabled']) && !$keys['enabled'])
70       {
71         continue;
72       }
73
74       if (!isset($keys['class']))
75       {
76         // missing class key
77         $error = 'Configuration file "%s" specifies category "%s" with missing class key';
78         $error = sprintf($error, $configFiles[0], $category);
79
80         throw new sfParseException($error);
81       }
82
83       $class = $keys['class'];
84
85       if (isset($keys['file']))
86       {
87         // we have a file to include
88         $file = $this->replaceConstants($keys['file']);
89         $file = $this->replacePath($file);
90
91         if (!is_readable($file))
92         {
93           // filter file doesn't exist
94           $error = sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s"', $configFiles[0], $class, $file);
95
96           throw new sfParseException($error);
97         }
98
99         // append our data
100         $includes[] = sprintf("require_once('%s');\n", $file);
101       }
102
103       $condition = true;
104       if (isset($keys['param']['condition']))
105       {
106         $condition = $this->replaceConstants($keys['param']['condition']);
107         unset($keys['param']['condition']);
108       }
109
110       $type = isset($keys['param']['type']) ? $keys['param']['type'] : null;
111       unset($keys['param']['type']);
112
113       if ($condition)
114       {
115         // parse parameters
116         $parameters = isset($keys['param']) ? var_export($keys['param'], true) : 'null';
117
118         // append new data
119         if ('security' == $type)
120         {
121           $data[] = $this->addSecurityFilter($category, $class, $parameters);
122         }
123         else
124         {
125           $data[] = $this->addFilter($category, $class, $parameters);
126         }
127
128         if ('rendering' == $type)
129         {
130           $rendering = true;
131         }
132
133         if ('execution' == $type)
134         {
135           $execution = true;
136         }
137       }
138     }
139
140     if (!$rendering)
141     {
142       $error = sprintf('Configuration file "%s" must register a filter of type "rendering"', $configFiles[0]);
143
144       throw new sfParseException($error);
145     }
146
147     if (!$execution)
148     {
149       $error = sprintf('Configuration file "%s" must register a filter of type "execution"', $configFiles[0]);
150
151       throw new sfParseException($error);
152     }
153
154     // compile data
155     $retval = sprintf("<?php\n".
156                       "// auto-generated by sfFilterConfigHandler\n".
157                       "// date: %s%s\n%s\n\n", date('Y/m/d H:i:s'),
158                       implode("\n", $includes), implode("\n", $data));
159
160     return $retval;
161   }
162
163   /**
164    * Adds a filter statement to the data.
165    *
166    * @param string The category name
167    * @param string The filter class name
168    * @param array  Filter default parameters
169    *
170    * @return string The PHP statement
171    */
172   protected function addFilter($category, $class, $parameters)
173   {
174     return sprintf("\nlist(\$class, \$parameters) = (array) sfConfig::get('sf_%s_filter', array('%s', %s));\n".
175                       "\$filter = new \$class();\n".
176                       "\$filter->initialize(\$this->context, \$parameters);\n".
177                       "\$filterChain->register(\$filter);",
178                       $category, $class, $parameters);
179   }
180
181   /**
182    * Adds a security filter statement to the data.
183    *
184    * @param string The category name
185    * @param string The filter class name
186    * @param array  Filter default parameters
187    *
188    * @return string The PHP statement
189    */
190   protected function addSecurityFilter($category, $class, $parameters)
191   {
192     return <<<EOF
193
194 // does this action require security?
195 if (\$actionInstance->isSecure())
196 {
197   if (!in_array('sfSecurityUser', class_implements(\$this->context->getUser())))
198   {
199     \$error = 'Security is enabled, but your sfUser implementation does not implement sfSecurityUser interface';
200     throw new sfSecurityException(\$error);
201   }
202   {$this->addFilter($category, $class, $parameters)}
203 }
204 EOF;
205   }
206 }
207
Note: See TracBrowser for help on using the browser.