summaryrefslogtreecommitdiffstats
path: root/Http/HttpUtils.php
diff options
context:
space:
mode:
authorFabien Potencier <fabien.potencier@gmail.com>2011-06-20 07:08:46 +0200
committerFabien Potencier <fabien.potencier@gmail.com>2011-06-22 14:47:19 +0200
commit100bc023abb4c1908b0d468dda6869e47494e355 (patch)
tree2780db9d5ab963bd0c8216622c647771f343fc64 /Http/HttpUtils.php
parent2ebfd97612d3d5ef4fc6f632b27a242eed8a1f0f (diff)
downloadsymfony-security-100bc023abb4c1908b0d468dda6869e47494e355.zip
symfony-security-100bc023abb4c1908b0d468dda6869e47494e355.tar.gz
symfony-security-100bc023abb4c1908b0d468dda6869e47494e355.tar.bz2
[Security] added an HttpUtils class to manage logic related to Requests and Responses
This change removes the need for the {_locale} hack. Now, all paths in the Security component can be: * An absolute path (/login) * An absolute URL (http://symfony.com/login) * A route name (login) So, if you want to use a path that includes a global parameter (like _locale), use a route instead of a path.
Diffstat (limited to 'Http/HttpUtils.php')
-rw-r--r--Http/HttpUtils.php99
1 files changed, 99 insertions, 0 deletions
diff --git a/Http/HttpUtils.php b/Http/HttpUtils.php
new file mode 100644
index 0000000..6b674aa
--- /dev/null
+++ b/Http/HttpUtils.php
@@ -0,0 +1,99 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Http;
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
+
+/**
+ * Encapsulates the logic needed to create sub-requests, redirect the user, and match URLs.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class HttpUtils
+{
+ private $urlGenerator;
+
+ /**
+ * Constructor.
+ *
+ * @param UrlGeneratorInterface $urlGenerator An UrlGeneratorInterface instance
+ */
+ public function __construct(UrlGeneratorInterface $urlGenerator = null)
+ {
+ $this->urlGenerator = $urlGenerator;
+ }
+
+ /**
+ * Creates a redirect Response.
+ *
+ * @param Request $request A Request instance
+ * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
+ * @param integer $status The status code
+ *
+ * @return Response A RedirectResponse instance
+ */
+ public function createRedirectResponse(Request $request, $path, $status = 302)
+ {
+ if (0 === strpos($path, '/')) {
+ $path = $request->getUriForPath($path);
+ } elseif (0 !== strpos($path, 'http')) {
+ $path = $this->generateUrl($path, true);
+ }
+
+ return new RedirectResponse($path, 302);
+ }
+
+ /**
+ * Creates a Request.
+ *
+ * @param Request $request The current Request instance
+ * @param string $path A path (an absolute path (/foo), an absolute URL (http://...), or a route name (foo))
+ *
+ * @return Request A Request instance
+ */
+ public function createRequest(Request $request, $path)
+ {
+ if ($path && '/' !== $path[0] && 0 !== strpos($path, 'http')) {
+ $path = $this->generateUrl($path, true);
+ }
+
+ return Request::create($path, 'get', array(), $request->cookies->all(), array(), $request->server->all());
+ }
+
+ /**
+ * Checks that a given path matches the Request.
+ *
+ * @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 Boolean true if the path is the same as the one from the Request, false otherwise
+ */
+ public function checkRequestPath(Request $request, $path)
+ {
+ if ('/' !== $path[0]) {
+ $path = preg_replace('#'.preg_quote($request->getBaseUrl(), '#').'#', '', $this->generateUrl($path));
+ }
+
+ return $path === $request->getPathInfo();
+ }
+
+ private function generateUrl($route, $absolute = false)
+ {
+ if (null === $this->urlGenerator) {
+ throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
+ }
+
+ return $this->urlGenerator->generate($route, array(), $absolute);
+ }
+}