Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Routing / UrlGeneratorTrait.php
1 <?php
2
3 namespace Drupal\Core\Routing;
4
5 use Symfony\Component\HttpFoundation\RedirectResponse;
6
7 /**
8  * Wrapper methods for the Url Generator.
9  *
10  * This utility trait should only be used in application-level code, such as
11  * classes that would implement ContainerInjectionInterface. Services registered
12  * in the Container should not use this trait but inject the appropriate service
13  * directly for easier testing.
14  *
15  * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
16  *   Use \Drupal\Core\Url instead.
17  */
18 trait UrlGeneratorTrait {
19
20   /**
21    * The url generator.
22    *
23    * @var \Drupal\Core\Routing\UrlGeneratorInterface
24    */
25   protected $urlGenerator;
26
27   /**
28    * Generates a URL or path for a specific route based on the given parameters.
29    *
30    * For details on the arguments, usage, and possible exceptions see
31    * \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute().
32    *
33    * @return string
34    *   The generated URL for the given route.
35    *
36    * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0.
37    *   Use \Drupal\Core\Url instead.
38    *
39    * @see \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute()
40    */
41   protected function url($route_name, $route_parameters = [], $options = []) {
42     return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
43   }
44
45   /**
46    * Returns a redirect response object for the specified route.
47    *
48    * @param string $route_name
49    *   The name of the route to which to redirect.
50    * @param array $route_parameters
51    *   (optional) Parameters for the route.
52    * @param array $options
53    *   (optional) An associative array of additional options.
54    * @param int $status
55    *   (optional) The HTTP redirect status code for the redirect. The default is
56    *   302 Found.
57    *
58    * @return \Symfony\Component\HttpFoundation\RedirectResponse
59    *   A redirect response object that may be returned by the controller.
60    */
61   protected function redirect($route_name, array $route_parameters = [], array $options = [], $status = 302) {
62     $options['absolute'] = TRUE;
63     $url = $this->url($route_name, $route_parameters, $options);
64     return new RedirectResponse($url, $status);
65   }
66
67   /**
68    * Returns the URL generator service.
69    *
70    * @return \Drupal\Core\Routing\UrlGeneratorInterface
71    *   The URL generator service.
72    */
73   protected function getUrlGenerator() {
74     if (!$this->urlGenerator) {
75       $this->urlGenerator = \Drupal::service('url_generator');
76     }
77     return $this->urlGenerator;
78   }
79
80   /**
81    * Sets the URL generator service.
82    *
83    * @param \Drupal\Core\Routing\UrlGeneratorInterface $generator
84    *   The url generator service.
85    *
86    * @return $this
87    */
88   public function setUrlGenerator(UrlGeneratorInterface $generator) {
89     $this->urlGenerator = $generator;
90
91     return $this;
92   }
93
94 }