1212namespace Joli \GifExceptionBundle \EventListener ;
1313
1414use Symfony \Component \Asset \Packages ;
15- use Symfony \Component \HttpKernel \ Event \ FilterResponseEvent ;
16- use Symfony \Component \HttpKernel \Kernel ;
17- use Symfony \Component \Templating \ Helper \ CoreAssetsHelper ;
15+ use Symfony \Component \EventDispatcher \ EventSubscriberInterface ;
16+ use Symfony \Component \HttpKernel \Event \ ResponseEvent ;
17+ use Symfony \Component \HttpKernel \ KernelEvents ;
1818
19- class ReplaceImageListener
19+ class ReplaceImageListener implements EventSubscriberInterface
2020{
21- const IMAGES_DIR = '../../Resources/public/images ' ;
21+ private const IMAGES_DIR = '../../Resources/public/images ' ;
2222
2323 /** @var string[][] */
2424 private $ gifs ;
@@ -29,29 +29,20 @@ class ReplaceImageListener
2929 /** @var Packages */
3030 private $ packages ;
3131
32- /** @var CoreAssetsHelper */
33- private $ coreAssetsHelper ;
34-
3532 /**
36- * @param string[][] $gifs
37- * @param string $exceptionController
38- * @param Packages $packages
39- * @param CoreAssetsHelper $coreAssetsHelper
33+ * @param string[][] $gifs
4034 */
41- public function __construct (array $ gifs , $ exceptionController , Packages $ packages = null , CoreAssetsHelper $ coreAssetsHelper = null )
35+ public function __construct (array $ gifs , string $ exceptionController , Packages $ packages = null )
4236 {
4337 $ this ->gifs = $ gifs ;
4438 $ this ->exceptionController = $ exceptionController ;
4539 $ this ->packages = $ packages ;
46- $ this ->coreAssetsHelper = $ coreAssetsHelper ;
4740 }
4841
4942 /**
5043 * Handle the response for exception and replace the little Phantom by a random Gif.
51- *
52- * @param FilterResponseEvent $event
5344 */
54- public function onKernelResponse (FilterResponseEvent $ event )
45+ public function onKernelResponse (ResponseEvent $ event )
5546 {
5647 if ($ event ->isMasterRequest ()
5748 || $ event ->getRequest ()->attributes ->get ('_controller ' ) !== $ this ->exceptionController ) {
@@ -69,37 +60,29 @@ public function onKernelResponse(FilterResponseEvent $event)
6960
7061 $ content = $ event ->getResponse ()->getContent ();
7162
72- if (version_compare (Kernel::VERSION , '3.2 ' , '< ' )) {
73- $ content = preg_replace (
74- '/<img alt="Exception detected!" src=".*" \/>/ ' ,
75- sprintf ('<img alt="Exception detected!" src="%s" data-gif style="width:145px" /> ' , $ url ),
76- $ content
77- );
78- } elseif (version_compare (Kernel::VERSION , '3.3 ' , '< ' )) {
79- $ content = preg_replace (
80- '@<svg xmlns="http://www.w3.org/2000/svg" width="112"(.*?)</svg>@ims ' ,
81- sprintf ('<img alt="Exception detected!" src="%s" data-gif style="width:145px" /> ' , $ url ),
82- $ content
83- );
84- } else {
85- $ content = preg_replace (
86- '@<div class="exception-illustration hidden-xs-down">(.*?)</div>@ims ' ,
87- sprintf ('<div class="exception-illustration hidden-xs-down" style="opacity:1"><img alt="Exception detected!" src="%s" data-gif style="height:66px" /></div> ' , $ url ),
88- $ content
89- );
90- }
63+ $ content = preg_replace (
64+ '@<div class="exception-illustration hidden-xs-down">(.*?)</div>@ims ' ,
65+ sprintf ('<div class="exception-illustration hidden-xs-down" style="opacity:1"><img alt="Exception detected!" src="%s" data-gif style="height:66px" /></div> ' , $ url ),
66+ $ content
67+ );
9168
9269 $ event ->getResponse ()->setContent ($ content );
9370 }
9471
72+ /**
73+ * {@inheritdoc}
74+ */
75+ public static function getSubscribedEvents ()
76+ {
77+ return [
78+ KernelEvents::RESPONSE => ['onKernelResponse ' , -1000 ],
79+ ];
80+ }
81+
9582 /**
9683 * Return the gif folder for the given status code.
97- *
98- * @param int $statusCode
99- *
100- * @return string
10184 */
102- private function getGifDir ($ statusCode )
85+ private function getGifDir (int $ statusCode ): string
10386 {
10487 if (\array_key_exists ($ statusCode , $ this ->gifs ) && \count ($ this ->gifs [$ statusCode ]) > 0 ) {
10588 return $ statusCode ;
@@ -110,49 +93,31 @@ private function getGifDir($statusCode)
11093
11194 /**
11295 * Return a random gif name for the given directory.
113- *
114- * @param string $dir
115- *
116- * @return string
11796 */
118- private function getRandomGif ($ dir )
97+ private function getRandomGif (string $ dir ): string
11998 {
120- $ imageIndex = mt_rand (0 , \count ($ this ->gifs [$ dir ]) - 1 );
99+ $ imageIndex = random_int (0 , \count ($ this ->gifs [$ dir ]) - 1 );
121100
122101 return $ this ->gifs [$ dir ][$ imageIndex ];
123102 }
124103
125104 /**
126105 * Return a the url of given gif in the given directory.
127- *
128- * @param string $dir
129- * @param string $gif
130- *
131- * @return string
132106 */
133- private function getGifUrl ($ dir , $ gif )
107+ private function getGifUrl (string $ dir , string $ gif ): string
134108 {
135109 return $ this ->generateUrl (sprintf ('bundles/gifexception/images/%s/%s ' , $ dir , $ gif ));
136110 }
137111
138112 /**
139113 * Generate an url in both Symfony 2 and Symfony 3+ compatible ways.
140- *
141- * @param string $url
142- *
143- * @return string
144114 */
145- private function generateUrl ($ url )
115+ private function generateUrl (string $ url ): string
146116 {
147- if ($ this ->packages ) {
117+ if (null !== $ this ->packages ) {
148118 return $ this ->packages ->getUrl ($ url );
149119 }
150120
151- if ($ this ->coreAssetsHelper ) {
152- // To remove when compatibility with Symfony 2.7 is dropped
153- return $ this ->coreAssetsHelper ->getUrl ($ url );
154- }
155-
156121 return $ url ;
157122 }
158123}
0 commit comments