Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
RouterListener.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpKernel\EventListener;
013
014 use Psr\Log\LoggerInterface;
015 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
016 use Symfony\Component\HttpFoundation\Request;
017 use Symfony\Component\HttpFoundation\RequestStack;
018 use Symfony\Component\HttpFoundation\Response;
019 use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
020 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
021 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
022 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
023 use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
024 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
025 use Symfony\Component\HttpKernel\Kernel;
026 use Symfony\Component\HttpKernel\KernelEvents;
027 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
028 use Symfony\Component\Routing\Exception\NoConfigurationException;
029 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
030 use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
031 use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
032 use Symfony\Component\Routing\RequestContext;
033 use Symfony\Component\Routing\RequestContextAwareInterface;
034
035 /**
036 * Initializes the context from the request and sets request attributes based on a matching route.
037 *
038 * @author Fabien Potencier <fabien@symfony.com>
039 * @author Yonel Ceruto <yonelceruto@gmail.com>
040 */
041 class RouterListener implements EventSubscriberInterface
042 {
043 private $matcher;
044 private $context;
045 private $logger;
046 private $requestStack;
047 private $projectDir;
048 private $debug;
049
050 /**
051 * @param UrlMatcherInterface|RequestMatcherInterface $matcher The Url or Request matcher
052 * @param RequestStack $requestStack A RequestStack instance
053 * @param RequestContext|null $context The RequestContext (can be null when $matcher implements RequestContextAwareInterface)
054 * @param LoggerInterface|null $logger The logger
055 * @param string $projectDir
056 * @param bool $debug
057 *
058 * @throws \InvalidArgumentException
059 */
060 public function __construct($matcher, RequestStack $requestStack, RequestContext $context = null, LoggerInterface $logger = null, $projectDir = null, $debug = true)
061 {
062 if (!$matcher instanceof UrlMatcherInterface && !$matcher instanceof RequestMatcherInterface) {
063 throw new \InvalidArgumentException('Matcher must either implement UrlMatcherInterface or RequestMatcherInterface.');
064 }
065
066 if (null === $context && !$matcher instanceof RequestContextAwareInterface) {
067 throw new \InvalidArgumentException('You must either pass a RequestContext or the matcher must implement RequestContextAwareInterface.');
068 }
069
070 $this->matcher = $matcher;
071 $this->context = $context ?: $matcher->getContext();
072 $this->requestStack = $requestStack;
073 $this->logger = $logger;
074 $this->projectDir = $projectDir;
075 $this->debug = $debug;
076 }
077
078 private function setCurrentRequest(Request $request = null)
079 {
080 if (null !== $request) {
081 try {
082 $this->context->fromRequest($request);
083 } catch (\UnexpectedValueException $e) {
084 throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode());
085 }
086 }
087 }
088
089 /**
090 * After a sub-request is done, we need to reset the routing context to the parent request so that the URL generator
091 * operates on the correct context again.
092 */
093 public function onKernelFinishRequest(FinishRequestEvent $event)
094 {
095 $this->setCurrentRequest($this->requestStack->getParentRequest());
096 }
097
098 public function onKernelRequest(GetResponseEvent $event)
099 {
100 $request = $event->getRequest();
101
102 $this->setCurrentRequest($request);
103
104 if ($request->attributes->has('_controller')) {
105 // routing is already done
106 return;
107 }
108
109 // add attributes based on the request (routing)
110 try {
111 // matching a request is more powerful than matching a URL path + context, so try that first
112 if ($this->matcher instanceof RequestMatcherInterface) {
113 $parameters = $this->matcher->matchRequest($request);
114 } else {
115 $parameters = $this->matcher->match($request->getPathInfo());
116 }
117
118 if (null !== $this->logger) {
119 $this->logger->info('Matched route "{route}".', [
120 'route' => isset($parameters['_route']) ? $parameters['_route'] : 'n/a',
121 'route_parameters' => $parameters,
122 'request_uri' => $request->getUri(),
123 'method' => $request->getMethod(),
124 ]);
125 }
126
127 $request->attributes->add($parameters);
128 unset($parameters['_route'], $parameters['_controller']);
129 $request->attributes->set('_route_params', $parameters);
130 } catch (ResourceNotFoundException $e) {
131 $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo());
132
133 if ($referer = $request->headers->get('referer')) {
134 $message .= sprintf(' (from "%s")', $referer);
135 }
136
137 throw new NotFoundHttpException($message, $e);
138 } catch (MethodNotAllowedException $e) {
139 $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods()));
140
141 throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e);
142 }
143 }
144
145 public function onKernelException(GetResponseForExceptionEvent $event)
146 {
147 if (!$this->debug || !($e = $event->getException()) instanceof NotFoundHttpException) {
148 return;
149 }
150
151 if ($e->getPrevious() instanceof NoConfigurationException) {
152 $event->setResponse($this->createWelcomeResponse());
153 }
154 }
155
156 public static function getSubscribedEvents()
157 {
158 return [
159 KernelEvents::REQUEST => [['onKernelRequest', 32]],
160 KernelEvents::FINISH_REQUEST => [['onKernelFinishRequest', 0]],
161 KernelEvents::EXCEPTION => ['onKernelException', -64],
162 ];
163 }
164
165 private function createWelcomeResponse()
166 {
167 $version = Kernel::VERSION;
168 $baseDir = realpath($this->projectDir).\DIRECTORY_SEPARATOR;
169 $docVersion = substr(Kernel::VERSION, 0, 3);
170
171 ob_start();
172 include __DIR__.'/../Resources/welcome.html.php';
173
174 return new Response(ob_get_clean(), Response::HTTP_NOT_FOUND);
175 }
176 }
177