Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] HttpRenderer fix + enhancements #6801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/HttpKernelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
Expand All @@ -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())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Fabien Potencier <[email protected]>
*
* @see Symfony\Component\HttpKernel\HttpContentRenderer
* @see Symfony\Component\HttpKernel\HttpContentRendererInterface
* @see Symfony\Component\HttpKernel\RenderingStrategy\RenderingStrategyInterface
*/
class ControllerReference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -31,7 +31,7 @@ class RouterProxyListener implements EventSubscriberInterface
{
private $signer;

public function __construct(UriSigner $signer)
public function __construct(UriSignerInterface $signer)
{
$this->signer = $signer;
}
Expand Down
50 changes: 19 additions & 31 deletions src/Symfony/Component/HttpKernel/HttpContentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author Fabien Potencier <[email protected]>
*/
class HttpContentRenderer implements EventSubscriberInterface
class HttpContentRenderer implements HttpContentRendererInterface, EventSubscriberInterface
{
private $debug;
private $strategies;
Expand All @@ -33,28 +33,37 @@ 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)
{
$this->strategies = array();
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.
*
Expand All @@ -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())
{
Expand All @@ -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
Expand Down
65 changes: 65 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpContentRendererInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
interface HttpContentRendererInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it useful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to implement without extending, what do you think ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not create this interface because I don't see why you would want to have another implementation. And you don't need an interface for extending anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not create this interface because I don't see why you would want to have another implementation.

Why not ? (Sorry I can not think of any better reply)

And you don't need an interface for extending anyway.

If you implement this is an other way, you would probably not be interested to extend ?

But I can revert if you prefer (both ?)

{
/**
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then should it really be part of the interface ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think it is used by external classes (it's public)

*/
public function fixOptions(array $options);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
Expand Down Expand Up @@ -72,6 +74,14 @@ public function render($uri, Request $request = null, array $options = array())
return sprintf('<hx:include src="%s">%s</hx:include>', $uri, $content);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'hinclude';
}

private function templateExists($template)
{
if ($this->templating instanceof EngineInterface) {
Expand All @@ -92,12 +102,4 @@ private function templateExists($template)

return false;
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'hinclude';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Fabien Potencier <[email protected]>
*
* @see Symfony\Component\HttpKernel\HttpContentRenderer
* @see Symfony\Component\HttpKernel\HttpContentRendererInterface
*/
interface RenderingStrategyInterface
{
Expand Down
37 changes: 12 additions & 25 deletions src/Symfony/Component/HttpKernel/UriSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*
* @author Fabien Potencier <[email protected]>
*/
class UriSigner
class UriSigner implements UriSignerInterface
{
private $secret;
protected $secret;

/**
* Constructor.
Expand All @@ -31,44 +31,31 @@ 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)
{
return $uri.(false === (strpos($uri, '?')) ? '?' : '&').'_hash='.$this->computeHash($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)));
}
Expand Down
Loading