diff --git a/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php b/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php index cb18b5f6433b9..c28010b3c866d 100644 --- a/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php @@ -11,7 +11,7 @@ namespace Symfony\Bridge\Twig\Extension; -use Symfony\Component\HttpKernel\HttpContentRenderer; +use Symfony\Component\HttpKernel\HttpContentRendererInterface; use Symfony\Component\HttpKernel\Controller\ControllerReference; /** @@ -26,9 +26,9 @@ class HttpKernelExtension extends \Twig_Extension /** * Constructor. * - * @param HttpContentRenderer $renderer A HttpContentRenderer instance + * @param HttpContentRendererInterface $renderer A HttpContentRendererInterface instance */ - public function __construct(HttpContentRenderer $renderer) + public function __construct(HttpContentRendererInterface $renderer) { $this->renderer = $renderer; } @@ -50,7 +50,7 @@ public function getFunctions() * * @return string The Response content * - * @see Symfony\Component\HttpKernel\HttpContentRenderer::render() + * @see Symfony\Component\HttpKernel\HttpContentRendererInterface::render() */ public function render($uri, $options = array()) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php index f07adb2ce2052..56de6bebcb4ba 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php @@ -12,7 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Templating\Helper; use Symfony\Component\Templating\Helper\Helper; -use Symfony\Component\HttpKernel\HttpContentRenderer; +use Symfony\Component\HttpKernel\HttpContentRendererInterface; use Symfony\Component\HttpKernel\Controller\ControllerReference; /** @@ -27,9 +27,9 @@ class ActionsHelper extends Helper /** * Constructor. * - * @param HttpContentRenderer $renderer A HttpContentRenderer instance + * @param HttpContentRendererInterface $renderer A HttpContentRendererInterface instance */ - public function __construct(HttpContentRenderer $renderer) + public function __construct(HttpContentRendererInterface $renderer) { $this->renderer = $renderer; } diff --git a/src/Symfony/Component/HttpKernel/Controller/ControllerReference.php b/src/Symfony/Component/HttpKernel/Controller/ControllerReference.php index 905e89f5dc00b..b91afc049f893 100644 --- a/src/Symfony/Component/HttpKernel/Controller/ControllerReference.php +++ b/src/Symfony/Component/HttpKernel/Controller/ControllerReference.php @@ -20,7 +20,7 @@ * * @author Fabien Potencier * - * @see Symfony\Component\HttpKernel\HttpContentRenderer + * @see Symfony\Component\HttpKernel\HttpContentRendererInterface * @see Symfony\Component\HttpKernel\RenderingStrategy\RenderingStrategyInterface */ class ControllerReference diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php index b88350c20fa32..ef7e03c803b9b 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterProxyListener.php @@ -16,7 +16,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; -use Symfony\Component\HttpKernel\UriSigner; +use Symfony\Component\HttpKernel\UriSignerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** @@ -31,7 +31,7 @@ class RouterProxyListener implements EventSubscriberInterface { private $signer; - public function __construct(UriSigner $signer) + public function __construct(UriSignerInterface $signer) { $this->signer = $signer; } diff --git a/src/Symfony/Component/HttpKernel/HttpContentRenderer.php b/src/Symfony/Component/HttpKernel/HttpContentRenderer.php index ca35cd711ffd5..5fc6dbc0502e2 100644 --- a/src/Symfony/Component/HttpKernel/HttpContentRenderer.php +++ b/src/Symfony/Component/HttpKernel/HttpContentRenderer.php @@ -23,7 +23,7 @@ * * @author Fabien Potencier */ -class HttpContentRenderer implements EventSubscriberInterface +class HttpContentRenderer implements HttpContentRendererInterface, EventSubscriberInterface { private $debug; private $strategies; @@ -33,7 +33,7 @@ class HttpContentRenderer implements EventSubscriberInterface * Constructor. * * @param RenderingStrategyInterface[] $strategies An array of RenderingStrategyInterface instances - * @param Boolean $debug Whether the debug mode is enabled or not + * @param Boolean $debug Whether the debug mode is enabled */ public function __construct(array $strategies = array(), $debug = false) { @@ -41,20 +41,29 @@ public function __construct(array $strategies = array(), $debug = false) foreach ($strategies as $strategy) { $this->addStrategy($strategy); } - $this->debug = $debug; + $this->debug = (Boolean) $debug; $this->requests = array(); } /** - * Adds a rendering strategy. - * - * @param RenderingStrategyInterface $strategy A RenderingStrategyInterface instance + * {@inheritdoc} */ public function addStrategy(RenderingStrategyInterface $strategy) { $this->strategies[$strategy->getName()] = $strategy; } + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() + { + return array( + KernelEvents::REQUEST => 'onKernelRequest', + KernelEvents::RESPONSE => 'onKernelResponse', + ); + } + /** * Stores the Request object. * @@ -76,22 +85,7 @@ public function onKernelResponse(FilterResponseEvent $event) } /** - * Renders a URI and returns the Response content. - * - * When the Response is a StreamedResponse, the content is streamed immediately - * instead of being returned. - * - * Available options: - * - * * ignore_errors: true to return an empty string in case of an error - * - * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance - * @param string $strategy The strategy to use for the rendering - * @param array $options An array of options - * - * @return string|null The Response content or null when the Response is streamed - * - * @throws \InvalidArgumentException when the strategy does not exist + * {@inheritdoc} */ public function render($uri, $strategy = 'default', array $options = array()) { @@ -106,15 +100,9 @@ public function render($uri, $strategy = 'default', array $options = array()) return $this->strategies[$strategy]->render($uri, $this->requests ? $this->requests[0] : null, $options); } - public static function getSubscribedEvents() - { - return array( - KernelEvents::REQUEST => 'onKernelRequest', - KernelEvents::RESPONSE => 'onKernelResponse', - ); - } - - // to be removed in 2.3 + /** + * {@inheritdoc} + */ public function fixOptions(array $options) { // support for the standalone option is @deprecated in 2.2 and replaced with the strategy option diff --git a/src/Symfony/Component/HttpKernel/HttpContentRendererInterface.php b/src/Symfony/Component/HttpKernel/HttpContentRendererInterface.php new file mode 100644 index 0000000000000..72785ced7dfec --- /dev/null +++ b/src/Symfony/Component/HttpKernel/HttpContentRendererInterface.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel; + +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Controller\ControllerReference; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\Event\FilterResponseEvent; +use Symfony\Component\HttpKernel\RenderingStrategy\RenderingStrategyInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Interface to be implemented by Http content renderers. + * + * @author Fabien Potencier + */ +interface HttpContentRendererInterface +{ + /** + * Adds a rendering strategy. + * + * @param RenderingStrategyInterface $strategy A RenderingStrategyInterface instance + */ + public function addStrategy(RenderingStrategyInterface $strategy); + + /** + * Renders a URI and returns the Response content. + * + * When the Response is a StreamedResponse, the content is streamed immediately + * instead of being returned. + * + * Available options: + * + * * ignore_errors: true to return an empty string in case of an error + * + * @param string|ControllerReference $uri A URI as a string or a ControllerReference instance + * @param string $strategy The strategy to use for the rendering + * @param array $options An array of options + * + * @return string|null The Response content or null when the Response is streamed + * + * @throws \InvalidArgumentException when the strategy does not exist + */ + public function render($uri, $strategy = 'default', array $options = array()); + + /** + * BC support + * + * @param array $options + * + * @return array + * + * @deprecated fixOptions will be removed in 2.3 + */ + public function fixOptions(array $options); +} diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php index a5ba272f81e9b..e34b9714f0d39 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/GeneratorAwareRenderingStrategy.php @@ -43,7 +43,7 @@ public function setUrlGenerator(UrlGeneratorInterface $generator) * placeholders. * * @param ControllerReference $reference A ControllerReference instance - * @param Request $request A Request instance + * @param Request $request A Request instance * * @return string A proxy URI * diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php index 82abf3f71f248..86d332dff264a 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/HIncludeRenderingStrategy.php @@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Templating\EngineInterface; use Symfony\Component\HttpKernel\Controller\ControllerReference; -use Symfony\Component\HttpKernel\UriSigner; +use Symfony\Component\HttpKernel\UriSignerInterface; /** * Implements the Hinclude rendering strategy. @@ -31,10 +31,12 @@ class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy * Constructor. * * @param EngineInterface|\Twig_Environment $templating An EngineInterface or a \Twig_Environment instance - * @param UriSigner $signer A UriSigner instance + * @param UriSignerInterface $signer A UriSignerInterface instance * @param string $globalDefaultTemplate The global default content (it can be a template name or the content) + * + * @throws \InvalidArgumentException When an invalid $templating is given */ - public function __construct($templating = null, UriSigner $signer = null, $globalDefaultTemplate = null) + public function __construct($templating = null, UriSignerInterface $signer = null, $globalDefaultTemplate = null) { if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof \Twig_Environment) { throw new \InvalidArgumentException('The hinclude rendering strategy needs an instance of \Twig_Environment or Symfony\Component\Templating\EngineInterface'); @@ -72,6 +74,14 @@ public function render($uri, Request $request = null, array $options = array()) return sprintf('%s', $uri, $content); } + /** + * {@inheritdoc} + */ + public function getName() + { + return 'hinclude'; + } + private function templateExists($template) { if ($this->templating instanceof EngineInterface) { @@ -92,12 +102,4 @@ private function templateExists($template) return false; } - - /** - * {@inheritdoc} - */ - public function getName() - { - return 'hinclude'; - } } diff --git a/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php b/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php index 36419c3792f94..660bb98dab215 100644 --- a/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php +++ b/src/Symfony/Component/HttpKernel/RenderingStrategy/RenderingStrategyInterface.php @@ -19,7 +19,7 @@ * * @author Fabien Potencier * - * @see Symfony\Component\HttpKernel\HttpContentRenderer + * @see Symfony\Component\HttpKernel\HttpContentRendererInterface */ interface RenderingStrategyInterface { diff --git a/src/Symfony/Component/HttpKernel/UriSigner.php b/src/Symfony/Component/HttpKernel/UriSigner.php index 45825fe246052..4cc19b8368a62 100644 --- a/src/Symfony/Component/HttpKernel/UriSigner.php +++ b/src/Symfony/Component/HttpKernel/UriSigner.php @@ -16,9 +16,9 @@ * * @author Fabien Potencier */ -class UriSigner +class UriSigner implements UriSignerInterface { - private $secret; + protected $secret; /** * Constructor. @@ -31,14 +31,7 @@ public function __construct($secret) } /** - * Signs a URI. - * - * The given URI is signed by adding a _hash query string parameter - * which value depends on the URI and the secret. - * - * @param string $uri A URI to sign - * - * @return string The signed URI + * {@inheritdoc} */ public function sign($uri) { @@ -46,29 +39,23 @@ public function sign($uri) } /** - * Checks that a URI contains the correct hash. - * - * The _hash query string parameter must be the last one - * (as it is generated that way by the sign() method, it should - * never be a problem). - * - * @param string $uri A signed URI - * - * @return Boolean True if the URI is signed correctly, false otherwise + * {@inheritdoc} */ public function check($uri) { - if (!preg_match('/(\?|&)_hash=(.+?)$/', $uri, $matches, PREG_OFFSET_CAPTURE)) { + if (!preg_match('/(.*)(?:\?|&)_hash=(.+?)$/', $uri, $matches)) { return false; } - // the naked URI is the URI without the _hash parameter (we need to keep the ? if there is some other parameters after) - $nakedUri = substr($uri, 0, $matches[0][1]).substr($uri, $matches[0][1] + strlen($matches[0][0])); - - return $this->computeHash($nakedUri) === $matches[2][0]; + return $this->computeHash($matches[1]) === $matches[2]; } - private function computeHash($uri) + /** + * @param string $uri + * + * @return string A signature + */ + protected function computeHash($uri) { return urlencode(base64_encode(hash_hmac('sha1', $uri, $this->secret, true))); } diff --git a/src/Symfony/Component/HttpKernel/UriSignerInterface.php b/src/Symfony/Component/HttpKernel/UriSignerInterface.php new file mode 100644 index 0000000000000..818ae423ce143 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/UriSignerInterface.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel; + +/** + * Interface to be implemented by URI signers. + * + * @author Fabien Potencier + */ +interface UriSignerInterface +{ + /** + * Signs a URI. + * + * The given URI is signed by adding a _hash query string parameter + * which value depends on the URI and the secret. + * + * @param string $uri A URI to sign + * + * @return string The signed URI + */ + public function sign($uri); + + /** + * Checks that a URI contains the correct hash. + * + * The _hash query string parameter must be the last one + * (as it is generated that way by the sign() method, it should + * never be a problem). + * + * @param string $uri A signed URI + * + * @return Boolean Whether the signature is verified + */ + public function check($uri); +}