Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Path / CurrentPathStack.php
1 <?php
2
3 namespace Drupal\Core\Path;
4 use Symfony\Component\HttpFoundation\RequestStack;
5
6 /**
7  * Represents the current path for the current request.
8  *
9  * Note: You should not rely on paths but rather on route names / parameters or
10  *   other indicators like context. For some fundamental parts, like routing or
11  *   path processing, there is unfortunately no way around dealing with paths.
12  */
13 class CurrentPathStack {
14
15   /**
16    * Static cache of paths.
17    *
18    * @var \SplObjectStorage
19    */
20   protected $paths;
21
22   /**
23    * The request stack.
24    *
25    * @var \Symfony\Component\HttpFoundation\RequestStack
26    */
27   protected $requestStack;
28
29   /**
30    * Constructs a new CurrentPathStack instance.
31    *
32    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
33    *   The request stack.
34    */
35   public function __construct(RequestStack $request_stack) {
36     $this->requestStack = $request_stack;
37     $this->paths = new \SplObjectStorage();
38   }
39
40   /**
41    * Returns the path of the current request.
42    *
43    * @param \Symfony\Component\HttpFoundation\Request $request
44    *   (optional) The request.
45    *
46    * @return string
47    *   Returns the path, without leading slashes.
48    */
49   public function getPath($request = NULL) {
50     if (!isset($request)) {
51       $request = $this->requestStack->getCurrentRequest();
52     }
53     if (!isset($this->paths[$request])) {
54       $this->paths[$request] = $request->getPathInfo();
55     }
56
57     return $this->paths[$request];
58   }
59
60   /**
61    * Sets the current path.
62    *
63    * @param string $path
64    *   The path.
65    * @param \Symfony\Component\HttpFoundation\Request $request
66    *   (optional) The request.
67    *
68    * @return $this
69    */
70   public function setPath($path, $request = NULL) {
71     if (!isset($request)) {
72       $request = $this->requestStack->getCurrentRequest();
73     }
74     $this->paths[$request] = $path;
75
76     return $this;
77   }
78
79 }