sfFilterChain.class.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * sfFilterChain manages registered filters for a specific context.
  12. *
  13. * @package symfony
  14. * @subpackage filter
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Sean Kerr <sean@code-box.org>
  17. * @version SVN: $Id: sfFilterChain.class.php 9087 2008-05-20 02:00:40Z Carl.Vondrick $
  18. */
  19. class sfFilterChain
  20. {
  21. protected
  22. $chain = array(),
  23. $index = -1;
  24. /**
  25. * Loads filters configuration for a given action instance.
  26. *
  27. * @param sfComponent $actionInstance A sfComponent instance
  28. */
  29. public function loadConfiguration($actionInstance)
  30. {
  31. require(sfContext::getInstance()->getConfigCache()->checkConfig('modules/'.$actionInstance->getModuleName().'/config/filters.yml'));
  32. }
  33. /**
  34. * Executes the next filter in this chain.
  35. */
  36. public function execute()
  37. {
  38. // skip to the next filter
  39. ++$this->index;
  40. if ($this->index < count($this->chain))
  41. {
  42. if (sfConfig::get('sf_logging_enabled'))
  43. {
  44. sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array(sprintf('Executing filter "%s"', get_class($this->chain[$this->index])))));
  45. }
  46. // execute the next filter
  47. $this->chain[$this->index]->execute($this);
  48. }
  49. }
  50. /**
  51. * Returns true if the filter chain contains a filter of a given class.
  52. *
  53. * @param string $class The class name of the filter
  54. *
  55. * @return boolean true if the filter exists, false otherwise
  56. */
  57. public function hasFilter($class)
  58. {
  59. foreach ($this->chain as $filter)
  60. {
  61. if ($filter instanceof $class)
  62. {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * Registers a filter with this chain.
  70. *
  71. * @param sfFilter $filter A sfFilter implementation instance.
  72. */
  73. public function register($filter)
  74. {
  75. $this->chain[] = $filter;
  76. }
  77. }