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