sfFilter.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * sfFilter provides a way for you to intercept incoming requests or outgoing responses.
  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: sfFilter.class.php 9087 2008-05-20 02:00:40Z Carl.Vondrick $
  18. */
  19. abstract class sfFilter
  20. {
  21. protected
  22. $parameterHolder = null,
  23. $context = null;
  24. public static
  25. $filterCalled = array();
  26. /**
  27. * Class constructor.
  28. *
  29. * @see initialize()
  30. */
  31. public function __construct($context, $parameters = array())
  32. {
  33. $this->initialize($context, $parameters);
  34. }
  35. /**
  36. * Initializes this Filter.
  37. *
  38. * @param sfContext $context The current application context
  39. * @param array $parameters An associative array of initialization parameters
  40. *
  41. * @return boolean true, if initialization completes successfully, otherwise false
  42. *
  43. * @throws <b>sfInitializationException</b> If an error occurs while initializing this Filter
  44. */
  45. public function initialize($context, $parameters = array())
  46. {
  47. $this->context = $context;
  48. $this->parameterHolder = new sfParameterHolder();
  49. $this->parameterHolder->add($parameters);
  50. return true;
  51. }
  52. /**
  53. * Returns true if this is the first call to the sfFilter instance.
  54. *
  55. * @return boolean true if this is the first call to the sfFilter instance, false otherwise
  56. */
  57. protected function isFirstCall()
  58. {
  59. $class = get_class($this);
  60. if (isset(self::$filterCalled[$class]))
  61. {
  62. return false;
  63. }
  64. else
  65. {
  66. self::$filterCalled[$class] = true;
  67. return true;
  68. }
  69. }
  70. /**
  71. * Retrieves the current application context.
  72. *
  73. * @return sfContext The current sfContext instance
  74. */
  75. public final function getContext()
  76. {
  77. return $this->context;
  78. }
  79. /**
  80. * Gets the parameter holder for this object.
  81. *
  82. * @return sfParameterHolder A sfParameterHolder instance
  83. */
  84. public function getParameterHolder()
  85. {
  86. return $this->parameterHolder;
  87. }
  88. /**
  89. * Gets the parameter associated with the given key.
  90. *
  91. * This is a shortcut for:
  92. *
  93. * <code>$this->getParameterHolder()->get()</code>
  94. *
  95. * @param string $name The key name
  96. * @param string $default The default value
  97. *
  98. * @return string The value associated with the key
  99. *
  100. * @see sfParameterHolder
  101. */
  102. public function getParameter($name, $default = null)
  103. {
  104. return $this->parameterHolder->get($name, $default);
  105. }
  106. /**
  107. * Returns true if the given key exists in the parameter holder.
  108. *
  109. * This is a shortcut for:
  110. *
  111. * <code>$this->getParameterHolder()->has()</code>
  112. *
  113. * @param string $name The key name
  114. *
  115. * @return boolean true if the given key exists, false otherwise
  116. *
  117. * @see sfParameterHolder
  118. */
  119. public function hasParameter($name)
  120. {
  121. return $this->parameterHolder->has($name);
  122. }
  123. /**
  124. * Sets the value for the given key.
  125. *
  126. * This is a shortcut for:
  127. *
  128. * <code>$this->getParameterHolder()->set()</code>
  129. *
  130. * @param string $name The key name
  131. * @param string $value The value
  132. *
  133. * @see sfParameterHolder
  134. */
  135. public function setParameter($name, $value)
  136. {
  137. return $this->parameterHolder->set($name, $value);
  138. }
  139. }