3 namespace Drupal\Core\Path;
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\RequestStack;
9 * Represents the current path for the current request.
11 * Note: You should not rely on paths but rather on route names / parameters or
12 * other indicators like context. For some fundamental parts, like routing or
13 * path processing, there is unfortunately no way around dealing with paths.
15 class CurrentPathStack {
18 * Static cache of paths.
20 * @var \SplObjectStorage
27 * @var \Symfony\Component\HttpFoundation\RequestStack
29 protected $requestStack;
32 * Constructs a new CurrentPathStack instance.
34 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
37 public function __construct(RequestStack $request_stack) {
38 $this->requestStack = $request_stack;
39 $this->paths = new \SplObjectStorage();
43 * Returns the path of the current request.
45 * @param \Symfony\Component\HttpFoundation\Request $request
46 * (optional) The request.
49 * Returns the path, without leading slashes.
51 public function getPath(Request $request = NULL) {
52 if (!isset($request)) {
53 $request = $this->requestStack->getCurrentRequest();
55 if (!isset($this->paths[$request])) {
56 $this->paths[$request] = $request->getPathInfo();
59 return $this->paths[$request];
63 * Sets the current path.
67 * @param \Symfony\Component\HttpFoundation\Request $request
68 * (optional) The request.
72 public function setPath($path, Request $request = NULL) {
73 if (!isset($request)) {
74 $request = $this->requestStack->getCurrentRequest();
76 $this->paths[$request] = $path;