PathSubscriber.php 4 KB
Newer Older
1
2
<?php

Crell's avatar
Crell committed
3
4
/**
 * @file
5
 * Definition of Drupal\Core\EventSubscriber\PathSubscriber.
Crell's avatar
Crell committed
6
7
 */

8
9
10
11
12
13
14
15
16
namespace Drupal\Core\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Access subscriber for controller requests.
 */
17
class PathSubscriber extends PathListenerBase implements EventSubscriberInterface {
18
19
20
21

  /**
   * Resolve the system path.
   *
22
23
   * @todo The path system should be objectified to remove the function calls in
   *   this method.
24
   *
25
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
26
27
28
29
30
   *   The Event to process.
   */
  public function onKernelRequestPathResolve(GetResponseEvent $event) {
    $request = $event->getRequest();

31
    $path = $this->extractPath($request);
32

33
34
35
    $path = drupal_get_normal_path($path);

    $this->setPath($request, $path);
36
37
38
39
40
  }

  /**
   * Resolve the front-page default path.
   *
41
42
   * @todo The path system should be objectified to remove the function calls in
   *   this method.
43
   *
44
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
45
46
47
48
49
50
51
52
   *   The Event to process.
   */
  public function onKernelRequestFrontPageResolve(GetResponseEvent $event) {
    $request = $event->getRequest();
    $path = $this->extractPath($request);

    if (empty($path)) {
      // @todo Temporary hack. Fix when configuration is injectable.
53
54
55
56
      $path = config('system.site')->get('page.front');
      if (empty($path)) {
        $path = 'user';
      }
57
    }
58

59
    $this->setPath($request, $path);
60
61
  }

62
  /**
Crell's avatar
Crell committed
63
64
65
   * Decode language information embedded in the request path.
   *
   * @todo Refactor this entire method to inline the relevant portions of
66
   *   drupal_language_initialize(). See the inline comment for more details.
67
   *
68
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
69
70
71
72
73
74
75
76
77
78
79
80
81
82
   *   The Event to process.
   */
  public function onKernelRequestLanguageResolve(GetResponseEvent $event) {
    // drupal_language_initialize() combines:
    // - Determination of language from $request information (e.g., path).
    // - Determination of language from other information (e.g., site default).
    // - Population of determined language into drupal_container().
    // - Removal of language code from _current_path().
    // @todo Decouple the above, but for now, invoke it and update the path
    //   prior to front page and alias resolution. When above is decoupled, also
    //   add 'langcode' (determined from $request only) to $request->attributes.
    drupal_language_initialize();
  }

83
84
85
86
87
  /**
   * Decodes the path of the request.
   *
   * Parameters in the URL sometimes represent code-meaningful strings. It is
   * therefore useful to always urldecode() those values so that individual
88
89
   * controllers need not concern themselves with it. This is Drupal-specific
   * logic and may not be familiar for developers used to other Symfony-family
90
91
92
   * projects.
   *
   * @todo Revisit whether or not this logic is appropriate for here or if
93
94
   *   controllers should be required to implement this logic themselves. If we
   *   decide to keep this code, remove this TODO.
95
   *
96
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
97
98
99
100
101
102
103
104
105
106
107
   *   The Event to process.
   */
  public function onKernelRequestDecodePath(GetResponseEvent $event) {
    $request = $event->getRequest();
    $path = $this->extractPath($request);

    $path = urldecode($path);

    $this->setPath($request, $path);
  }

108
109
110
111
112
113
114
  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
115
116
    $events[KernelEvents::REQUEST][] = array('onKernelRequestDecodePath', 200);
    $events[KernelEvents::REQUEST][] = array('onKernelRequestLanguageResolve', 150);
117
    $events[KernelEvents::REQUEST][] = array('onKernelRequestFrontPageResolve', 101);
118
    $events[KernelEvents::REQUEST][] = array('onKernelRequestPathResolve', 100);
119
120
121
122

    return $events;
  }
}