Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Path / CurrentPathStack.php
1 <?php
2
3 namespace Drupal\Core\Path;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\RequestStack;
7
8 /**
9  * Represents the current path for the current request.
10  *
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.
14  */
15 class CurrentPathStack {
16
17   /**
18    * Static cache of paths.
19    *
20    * @var \SplObjectStorage
21    */
22   protected $paths;
23
24   /**
25    * The request stack.
26    *
27    * @var \Symfony\Component\HttpFoundation\RequestStack
28    */
29   protected $requestStack;
30
31   /**
32    * Constructs a new CurrentPathStack instance.
33    *
34    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
35    *   The request stack.
36    */
37   public function __construct(RequestStack $request_stack) {
38     $this->requestStack = $request_stack;
39     $this->paths = new \SplObjectStorage();
40   }
41
42   /**
43    * Returns the path of the current request.
44    *
45    * @param \Symfony\Component\HttpFoundation\Request $request
46    *   (optional) The request.
47    *
48    * @return string
49    *   Returns the path, without leading slashes.
50    */
51   public function getPath(Request $request = NULL) {
52     if (!isset($request)) {
53       $request = $this->requestStack->getCurrentRequest();
54     }
55     if (!isset($this->paths[$request])) {
56       $this->paths[$request] = $request->getPathInfo();
57     }
58
59     return $this->paths[$request];
60   }
61
62   /**
63    * Sets the current path.
64    *
65    * @param string $path
66    *   The path.
67    * @param \Symfony\Component\HttpFoundation\Request $request
68    *   (optional) The request.
69    *
70    * @return $this
71    */
72   public function setPath($path, Request $request = NULL) {
73     if (!isset($request)) {
74       $request = $this->requestStack->getCurrentRequest();
75     }
76     $this->paths[$request] = $path;
77
78     return $this;
79   }
80
81 }