summaryrefslogtreecommitdiffstats
path: root/Http/HttpUtils.php
diff options
context:
space:
mode:
Diffstat (limited to 'Http/HttpUtils.php')
-rw-r--r--Http/HttpUtils.php84
1 files changed, 41 insertions, 43 deletions
diff --git a/Http/HttpUtils.php b/Http/HttpUtils.php
index 78bfb85..746b217 100644
--- a/Http/HttpUtils.php
+++ b/Http/HttpUtils.php
@@ -15,7 +15,9 @@ use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
-use Symfony\Component\Routing\RouterInterface;
+use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
+use Symfony\Component\HttpFoundation\RequestMatcherInterface;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
@@ -26,16 +28,22 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException;
*/
class HttpUtils
{
- private $router;
+ private $urlGenerator;
+ private $urlMatcher;
/**
* Constructor.
*
- * @param RouterInterface $router An RouterInterface instance
+ * @param UrlGeneratorInterface $urlGenerator A UrlGeneratorInterface instance
+ * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
*/
- public function __construct(RouterInterface $router = null)
+ public function __construct(UrlGeneratorInterface $urlGenerator = null, $urlMatcher = null)
{
- $this->router = $router;
+ $this->urlGenerator = $urlGenerator;
+ if ($urlMatcher !== null && !$urlMatcher instanceof UrlMatcherInterface && !$urlMatcher instanceof RequestMatcherInterface) {
+ throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
+ }
+ $this->urlMatcher = $urlMatcher;
}
/**
@@ -49,14 +57,7 @@ class HttpUtils
*/
public function createRedirectResponse(Request $request, $path, $status = 302)
{
- if ('/' === $path[0]) {
- $path = $request->getUriForPath($path);
- } elseif (0 !== strpos($path, 'http')) {
- $this->resetLocale($request);
- $path = $this->generateUrl($path, true);
- }
-
- return new RedirectResponse($path, $status);
+ return new RedirectResponse($this->generateUri($request, $path), $status);
}
/**
@@ -69,15 +70,7 @@ class HttpUtils
*/
public function createRequest(Request $request, $path)
{
- if ($path && '/' !== $path[0] && 0 !== strpos($path, 'http')) {
- $this->resetLocale($request);
- $path = $this->generateUrl($path, true);
- }
- if (0 !== strpos($path, 'http')) {
- $path = $request->getUriForPath($path);
- }
-
- $newRequest = Request::create($path, 'get', array(), $request->cookies->all(), array(), $request->server->all());
+ $newRequest = Request::create($this->generateUri($request, $path), 'get', array(), $request->cookies->all(), array(), $request->server->all());
if ($session = $request->getSession()) {
$newRequest->setSession($session);
}
@@ -99,7 +92,7 @@ class HttpUtils
* Checks that a given path matches the Request.
*
* @param Request $request A Request instance
- * @param string $path A path (an absolute path (/foo) or a route name (foo))
+ * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
*
* @return Boolean true if the path is the same as the one from the Request, false otherwise
*/
@@ -107,7 +100,12 @@ class HttpUtils
{
if ('/' !== $path[0]) {
try {
- $parameters = $this->router->match(urlencode($request->getPathInfo()));
+ // matching a request is more powerful than matching a URL path + context, so try that first
+ if ($this->urlMatcher instanceof RequestMatcherInterface) {
+ $parameters = $this->urlMatcher->matchRequest($request);
+ } else {
+ $parameters = $this->urlMatcher->match($request->getPathInfo());
+ }
return $path === $parameters['_route'];
} catch (MethodNotAllowedException $e) {
@@ -117,36 +115,36 @@ class HttpUtils
}
}
- return $path === $request->getPathInfo();
+ return $path === rawurldecode($request->getPathInfo());
}
- // hack (don't have a better solution for now)
- private function resetLocale(Request $request)
+ /**
+ * Generates a URI, based on the given path or absolute URL.
+ *
+ * @param Request $request A Request instance
+ * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
+ *
+ * @return string An absolute URL
+ */
+ public function generateUri($request, $path)
{
- $context = $this->router->getContext();
- if ($context->getParameter('_locale')) {
- return;
+ if (0 === strpos($path, 'http') || !$path) {
+ return $path;
}
- try {
- $parameters = $this->router->match(urlencode($request->getPathInfo()));
-
- if (isset($parameters['_locale'])) {
- $context->setParameter('_locale', $parameters['_locale']);
- } elseif ($session = $request->getSession()) {
- $context->setParameter('_locale', $session->getLocale());
- }
- } catch (\Exception $e) {
- // let's hope user doesn't use the locale in the path
+ if ('/' === $path[0]) {
+ return $request->getUriForPath($path);
}
+
+ return $this->generateUrl($path, true);
}
private function generateUrl($route, $absolute = false)
{
- if (null === $this->router) {
- throw new \LogicException('You must provide a RouterInterface instance to be able to use routes.');
+ if (null === $this->urlGenerator) {
+ throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
}
- return $this->router->generate($route, array(), $absolute);
+ return $this->urlGenerator->generate($route, array(), $absolute);
}
}