sfFilterChain.class.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }