Issues (75)

src/SymfonyCache/EventDispatchingHttpCache.php (3 issues)

1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCache package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\HttpCache\SymfonyCache;
13
14
use Symfony\Component\EventDispatcher\EventDispatcher;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\HttpKernelInterface;
20
21
/**
22
 * Trait for enhanced Symfony reverse proxy based on the symfony kernel component.
23
 *
24
 * Your kernel needs to implement CacheInvalidatorInterface and redeclare the
25
 * fetch method as public. (The latter is needed because the trait declaring it
26
 * public does not satisfy the interface for whatever reason. See also
27
 * http://stackoverflow.com/questions/31877844/php-trait-exposing-a-method-and-interfaces )
28
 *
29
 * CacheInvalidator kernels support event listeners that can act on the
30
 * events defined in FOS\HttpCache\SymfonyCache\Events and may alter the
31
 * request flow.
32
 *
33
 * If your kernel overwrites any of the methods defined in this trait, make
34
 * sure to also call the trait method. You might get into issues with the order
35
 * of events, in which case you will need to copy event triggering into your
36
 * kernel.
37
 *
38
 * @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS)
39
 * @author David Buchmann <[email protected]>
40
 *
41
 * {@inheritdoc}
42
 */
43
trait EventDispatchingHttpCache
44
{
45
    /**
46
     * @var EventDispatcherInterface
47
     */
48
    private $eventDispatcher;
49
50
    /**
51
     * Get event dispatcher.
52
     *
53
     * @return EventDispatcherInterface
54
     */
55 10
    public function getEventDispatcher()
56
    {
57 10
        if (!$this->eventDispatcher) {
58 10
            $this->eventDispatcher = new EventDispatcher();
59
        }
60
61 10
        return $this->eventDispatcher;
62
    }
63
64
    /**
65
     * Add an event subscriber.
66
     *
67
     * @see EventDispatcherInterface::addSubscriber
68
     */
69 9
    public function addSubscriber(EventSubscriberInterface $subscriber)
70
    {
71 9
        $this->getEventDispatcher()->addSubscriber($subscriber);
72 9
    }
73
74
    /**
75
     * Add an event listener to this HttpCache.
76
     *
77
     * @see EventDispatcherInterface::addListener
78
     */
79 1
    public function addListener($eventName, $listener, $priority = 0)
80
    {
81 1
        $this->getEventDispatcher()->addListener($eventName, $listener, $priority);
82 1
    }
83
84
    /**
85
     * {@inheritdoc}
86
     *
87
     * Adding the Events::PRE_HANDLE and Events::POST_HANDLE events.
88
     */
89 6
    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

89
    public function handle(Request $request, $type = /** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST, $catch = true): Response

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
90
    {
91
        // trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
92 6
        class_exists(CacheEvent::class);
93
94 6
        if ($response = $this->dispatch(Events::PRE_HANDLE, $request, null, $type)) {
95 2
            return $this->dispatch(Events::POST_HANDLE, $request, $response, $type);
96
        }
97
98 4
        $response = parent::handle($request, $type, $catch);
99
100 4
        return $this->dispatch(Events::POST_HANDLE, $request, $response, $type);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     *
106
     * Trigger event to alter response before storing it in the cache.
107
     */
108 2
    protected function store(Request $request, Response $response)
109
    {
110 2
        $response = $this->dispatch(Events::PRE_STORE, $request, $response);
111
112 2
        parent::store($request, $response);
113 2
    }
114
115
    /**
116
     * {@inheritdoc}
117
     *
118
     * Adding the Events::PRE_INVALIDATE event.
119
     */
120 2
    protected function invalidate(Request $request, $catch = false): Response
121
    {
122 2
        if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) {
123 1
            return $response;
124
        }
125
126 1
        return parent::invalidate($request, $catch);
127
    }
128
129
    /**
130
     * Dispatch an event if there are any listeners.
131
     *
132
     * @param string        $name        Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
133
     * @param Response|null $response    If already available
134
     * @param int           $requestType The request type (default HttpKernelInterface::MASTER_REQUEST)
135
     *
136
     * @return Response|null The response to return, which might be provided/altered by a listener
137
     */
138 10
    protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST): ?Response
0 ignored issues
show
Deprecated Code introduced by
The constant Symfony\Component\HttpKe...terface::MASTER_REQUEST has been deprecated: since symfony/http-kernel 5.3, use MAIN_REQUEST instead. To ease the migration, this constant won't be removed until Symfony 7.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

138
    protected function dispatch($name, Request $request, Response $response = null, $requestType = /** @scrutinizer ignore-deprecated */ HttpKernelInterface::MASTER_REQUEST): ?Response

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
139
    {
140 10
        if ($this->getEventDispatcher()->hasListeners($name)) {
141 10
            $event = new CacheEvent($this, $request, $response, $requestType);
0 ignored issues
show
$this of type FOS\HttpCache\SymfonyCac...entDispatchingHttpCache is incompatible with the type FOS\HttpCache\SymfonyCache\CacheInvalidation expected by parameter $kernel of FOS\HttpCache\SymfonyCac...cheEvent::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
            $event = new CacheEvent(/** @scrutinizer ignore-type */ $this, $request, $response, $requestType);
Loading history...
142 10
            $this->getEventDispatcher()->dispatch($event, $name);
143
144 10
            $response = $event->getResponse();
145
        }
146
147 10
        return $response;
148
    }
149
}
150