3 namespace Drupal\Core\Path;
4 use Symfony\Component\HttpFoundation\RequestStack;
7 * Represents the current path for the current request.
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.
13 class CurrentPathStack {
16 * Static cache of paths.
18 * @var \SplObjectStorage
25 * @var \Symfony\Component\HttpFoundation\RequestStack
27 protected $requestStack;
30 * Constructs a new CurrentPathStack instance.
32 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
35 public function __construct(RequestStack $request_stack) {
36 $this->requestStack = $request_stack;
37 $this->paths = new \SplObjectStorage();
41 * Returns the path of the current request.
43 * @param \Symfony\Component\HttpFoundation\Request $request
44 * (optional) The request.
47 * Returns the path, without leading slashes.
49 public function getPath($request = NULL) {
50 if (!isset($request)) {
51 $request = $this->requestStack->getCurrentRequest();
53 if (!isset($this->paths[$request])) {
54 $this->paths[$request] = $request->getPathInfo();
57 return $this->paths[$request];
61 * Sets the current path.
65 * @param \Symfony\Component\HttpFoundation\Request $request
66 * (optional) The request.
70 public function setPath($path, $request = NULL) {
71 if (!isset($request)) {
72 $request = $this->requestStack->getCurrentRequest();
74 $this->paths[$request] = $path;