| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 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 |
|
|---|
| 36 |
$config = self::getConfiguration($configFiles); |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
$data = array(); |
|---|
| 40 |
$includes = array(); |
|---|
| 41 |
|
|---|
| 42 |
$execution = false; |
|---|
| 43 |
$rendering = false; |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 86 |
$parameters = isset($keys['param']) ? var_export($keys['param'], true) : 'null'; |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|