sfFilterConfigHandler.class.php 6.0 KB

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