Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpKernel\DataCollector;
13
 
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\HttpKernel\Event\ControllerEvent;
18
 
19
/**
20
 * @author Fabien Potencier <fabien@symfony.com>
21
 */
22
class RouterDataCollector extends DataCollector
23
{
24
    /**
25
     * @var \SplObjectStorage<Request, callable>
26
     */
27
    protected $controllers;
28
 
29
    public function __construct()
30
    {
31
        $this->reset();
32
    }
33
 
34
    /**
35
     * @final
36
     */
37
    public function collect(Request $request, Response $response, \Throwable $exception = null)
38
    {
39
        if ($response instanceof RedirectResponse) {
40
            $this->data['redirect'] = true;
41
            $this->data['url'] = $response->getTargetUrl();
42
 
43
            if ($this->controllers->contains($request)) {
44
                $this->data['route'] = $this->guessRoute($request, $this->controllers[$request]);
45
            }
46
        }
47
 
48
        unset($this->controllers[$request]);
49
    }
50
 
51
    public function reset()
52
    {
53
        $this->controllers = new \SplObjectStorage();
54
 
55
        $this->data = [
56
            'redirect' => false,
57
            'url' => null,
58
            'route' => null,
59
        ];
60
    }
61
 
62
    protected function guessRoute(Request $request, string|object|array $controller)
63
    {
64
        return 'n/a';
65
    }
66
 
67
    /**
68
     * Remembers the controller associated to each request.
69
     */
70
    public function onKernelController(ControllerEvent $event)
71
    {
72
        $this->controllers[$event->getRequest()] = $event->getController();
73
    }
74
 
75
    /**
76
     * @return bool Whether this request will result in a redirect
77
     */
78
    public function getRedirect(): bool
79
    {
80
        return $this->data['redirect'];
81
    }
82
 
83
    public function getTargetUrl(): ?string
84
    {
85
        return $this->data['url'];
86
    }
87
 
88
    public function getTargetRoute(): ?string
89
    {
90
        return $this->data['route'];
91
    }
92
 
93
    public function getName(): string
94
    {
95
        return 'router';
96
    }
97
}