19
19
use Symfony \Component \HttpKernel \HttpKernelInterface ;
20
20
21
21
/**
22
- * Trait for enhanced Symfony reverse proxy based on the symfony component.
22
+ * Trait for enhanced Symfony reverse proxy based on the symfony kernel component.
23
23
*
24
- * <b>When using FOSHttpCacheBundle, look at FOS\HttpCacheBundle\HttpCache instead.</b>
24
+ * Your kernel needs to implement CacheInvalidatorInterface and redeclare the
25
+ * fetch method as public. (The later 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 )
25
28
*
26
- * This kernel supports event subscribers that can act on the events defined in
27
- * FOS\HttpCache\SymfonyCache\Events and may alter the request flow.
29
+ * CacheInvalidator kernels support event subscribers 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.
28
37
*
29
38
* @author Jérôme Vieilledent <[email protected] > (courtesy of eZ Systems AS)
30
39
*
@@ -68,17 +77,13 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
68
77
*/
69
78
public function handle (Request $ request , $ type = HttpKernelInterface::MASTER_REQUEST , $ catch = true )
70
79
{
71
- if ($ this ->getEventDispatcher ()->hasListeners (Events::PRE_HANDLE )) {
72
- $ event = new CacheEvent ($ this , $ request );
73
- $ this ->getEventDispatcher ()->dispatch (Events::PRE_HANDLE , $ event );
74
- if ($ event ->getResponse ()) {
75
- return $ this ->dispatchPostHandle ($ request , $ event ->getResponse ());
76
- }
80
+ if ($ response = $ this ->dispatch (Events::PRE_HANDLE , $ request )) {
81
+ return $ this ->dispatch (Events::POST_HANDLE , $ request , $ response );
77
82
}
78
83
79
84
$ response = parent ::handle ($ request , $ type , $ catch );
80
85
81
- return $ this ->dispatchPostHandle ( $ request , $ response );
86
+ return $ this ->dispatch (Events:: POST_HANDLE , $ request , $ response );
82
87
}
83
88
84
89
/**
@@ -88,57 +93,40 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
88
93
*/
89
94
protected function store (Request $ request , Response $ response )
90
95
{
91
- if ($ this ->getEventDispatcher ()->hasListeners (Events::PRE_STORE )) {
92
- $ event = new CacheEvent ($ this , $ request , $ response );
93
- $ this ->getEventDispatcher ()->dispatch (Events::PRE_STORE , $ event );
94
- $ response = $ event ->getResponse ();
95
- }
96
+ $ response = $ this ->dispatch (Events::PRE_STORE , $ request , $ response );
96
97
97
98
parent ::store ($ request , $ response );
98
99
}
99
100
100
101
/**
101
- * Dispatch the POST_HANDLE event if needed.
102
+ * Dispatch an event if needed.
102
103
*
103
- * @param Request $request
104
- * @param Response $response
104
+ * @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
105
+ * @param Request $request
106
+ * @param Response|null $response If already available
105
107
*
106
- * @return Response The response to return which might be altered by a POST_HANDLE listener.
108
+ * @return Response The response to return, which might be provided/ altered by a listener.
107
109
*/
108
- private function dispatchPostHandle ( Request $ request , Response $ response )
110
+ protected function dispatch ( $ name , Request $ request , Response $ response = null )
109
111
{
110
- if ($ this ->getEventDispatcher ()->hasListeners (Events:: POST_HANDLE )) {
112
+ if ($ this ->getEventDispatcher ()->hasListeners ($ name )) {
111
113
$ event = new CacheEvent ($ this , $ request , $ response );
112
- $ this ->getEventDispatcher ()->dispatch (Events:: POST_HANDLE , $ event );
114
+ $ this ->getEventDispatcher ()->dispatch ($ name , $ event );
113
115
$ response = $ event ->getResponse ();
114
116
}
115
117
116
118
return $ response ;
117
119
}
118
120
119
- /**
120
- * Made public to allow event subscribers to do refresh operations.
121
- *
122
- * {@inheritDoc}
123
- */
124
- public function fetch (Request $ request , $ catch = false )
125
- {
126
- return parent ::fetch ($ request , $ catch );
127
- }
128
-
129
121
/**
130
122
* {@inheritDoc}
131
123
*
132
124
* Adding the Events::PRE_INVALIDATE event.
133
125
*/
134
126
protected function invalidate (Request $ request , $ catch = false )
135
127
{
136
- if ($ this ->getEventDispatcher ()->hasListeners (Events::PRE_INVALIDATE )) {
137
- $ event = new CacheEvent ($ this , $ request );
138
- $ this ->getEventDispatcher ()->dispatch (Events::PRE_INVALIDATE , $ event );
139
- if ($ event ->getResponse ()) {
140
- return $ event ->getResponse ();
141
- }
128
+ if ($ response = $ this ->dispatch (Events::PRE_INVALIDATE , $ request )) {
129
+ return $ response ;
142
130
}
143
131
144
132
return parent ::invalidate ($ request , $ catch );
0 commit comments