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

Debug toolbar