1919use Symfony \Component \HttpKernel \HttpKernelInterface ;
2020
2121/**
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.
2323 *
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 )
2528 *
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.
2837 *
2938 * @author Jérôme Vieilledent <[email protected] > (courtesy of eZ Systems AS) 3039 *
@@ -68,17 +77,13 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
6877 */
6978 public function handle (Request $ request , $ type = HttpKernelInterface::MASTER_REQUEST , $ catch = true )
7079 {
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 );
7782 }
7883
7984 $ response = parent ::handle ($ request , $ type , $ catch );
8085
81- return $ this ->dispatchPostHandle ( $ request , $ response );
86+ return $ this ->dispatch (Events:: POST_HANDLE , $ request , $ response );
8287 }
8388
8489 /**
@@ -88,57 +93,40 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
8893 */
8994 protected function store (Request $ request , Response $ response )
9095 {
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 );
9697
9798 parent ::store ($ request , $ response );
9899 }
99100
100101 /**
101- * Dispatch the POST_HANDLE event if needed.
102+ * Dispatch an event if needed.
102103 *
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
105107 *
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.
107109 */
108- private function dispatchPostHandle ( Request $ request , Response $ response )
110+ protected function dispatch ( $ name , Request $ request , Response $ response = null )
109111 {
110- if ($ this ->getEventDispatcher ()->hasListeners (Events:: POST_HANDLE )) {
112+ if ($ this ->getEventDispatcher ()->hasListeners ($ name )) {
111113 $ event = new CacheEvent ($ this , $ request , $ response );
112- $ this ->getEventDispatcher ()->dispatch (Events:: POST_HANDLE , $ event );
114+ $ this ->getEventDispatcher ()->dispatch ($ name , $ event );
113115 $ response = $ event ->getResponse ();
114116 }
115117
116118 return $ response ;
117119 }
118120
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-
129121 /**
130122 * {@inheritDoc}
131123 *
132124 * Adding the Events::PRE_INVALIDATE event.
133125 */
134126 protected function invalidate (Request $ request , $ catch = false )
135127 {
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 ;
142130 }
143131
144132 return parent ::invalidate ($ request , $ catch );
0 commit comments