Skip to content

Commit bd07612

Browse files
authored
Merge pull request #39 from maxhelias/compatibiliy-layer
Bump project with symfony 5 compatibility
2 parents 4519a08 + 8228846 commit bd07612

20 files changed

+257
-330
lines changed

.travis.yml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
language: php
22

33
php:
4-
- 5.5
5-
- 5.6
6-
- 7.0
74
- 7.1
5+
- 7.2
86
- 7.3
97

108
env:
@@ -15,21 +13,18 @@ env:
1513
matrix:
1614
fast_finish: true
1715
include:
18-
- php: 7.0
16+
- php: 7.4
1917
env: TARGET=cs_dry_run
20-
- php: 5.5
21-
env: COMPOSER_FLAGS="--prefer-lowest" SYMFONY_DEPRECATIONS_HELPER=weak
22-
# test 2.7 LTS
23-
- php: 5.6
24-
env: SYMFONY_VERSION=2.7.*
25-
# test 2.8 LTS
26-
- php: 5.6
27-
env: SYMFONY_VERSION=2.8.*
28-
# test the latest stable 3.x release
29-
- php: 5.6
30-
env: SYMFONY_VERSION=^3.0
18+
- php: 7.1
19+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
20+
# test 4.4
21+
- php: 7.4
22+
env: SYMFONY_VERSION=4.4.*
23+
# test the latest stable 5.x release
24+
- php: 7.4
25+
env: SYMFONY_VERSION=^5.0
3126
# test the latest release (including beta releases)
32-
- php: 7.3
27+
- php: 7.4
3328
env: DEPENDENCIES=beta
3429

3530
sudo: false
@@ -41,7 +36,7 @@ cache:
4136
before_install:
4237
- if [ "$DEPENDENCIES" = "beta" ]; then perl -pi -e 's/^}$/,"minimum-stability":"beta"}/' composer.json; fi;
4338
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
44-
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;
39+
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini;
4540

4641
install: composer update --prefer-dist --no-interaction $COMPOSER_FLAGS
4742

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes between versions
22

3+
## 1.6.0 (not release yet)
4+
5+
* Added compatibility with Symfony 5.0
6+
* Dropped compatibility with PHP < 7.1 and Symfony < 4.4
7+
38
## 1.5.0 (2019-03-20)
49

510
* Added more GIFs

Command/GifOptimizerCommand.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ class GifOptimizerCommand extends Command
2424
/**
2525
* @var string The name of the command
2626
*/
27-
const COMMAND_NAME = 'jolicode:gifexception:optimize';
27+
private const COMMAND_NAME = 'jolicode:gifexception:optimize';
2828

2929
/**
3030
* @var string
3131
*/
32-
const DEFAULT_OPTIMIZATION_LEVEL = '-O3';
32+
private const DEFAULT_OPTIMIZATION_LEVEL = '-O3';
3333

3434
/**
3535
* @var int
3636
*/
37-
const DEFAULT_WIDTH = 145;
37+
private const DEFAULT_WIDTH = 145;
3838

3939
/**
4040
* @var Optimizer
@@ -98,7 +98,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
9898
/**
9999
* {@inheritdoc}
100100
*/
101-
protected function execute(InputInterface $input, OutputInterface $output)
101+
protected function execute(InputInterface $input, OutputInterface $output): int
102102
{
103103
$imageDir = $input->getArgument('image_dir');
104104

@@ -126,15 +126,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
126126
$percentage = 100 - (($optimizedFileSize / $originalFileSize) * 100);
127127
$output->writeln(sprintf('<comment>Saving: %s%%</comment>', round($percentage)));
128128
}
129+
130+
return 0;
129131
}
130132

131-
/**
132-
* @param $bytes
133-
* @param bool $useStandard
134-
*
135-
* @return string
136-
*/
137-
private function formatBytes($bytes, $useStandard = true)
133+
private function formatBytes(int $bytes, bool $useStandard = true): string
138134
{
139135
$unit = $useStandard ? 1024 : 1000;
140136
if ($bytes <= $unit) {
@@ -144,6 +140,6 @@ private function formatBytes($bytes, $useStandard = true)
144140
$pre = ($useStandard ? 'kMGTPE' : 'KMGTPE');
145141
$pre = $pre[$exp - 1] . ($useStandard ? '' : 'i');
146142

147-
return sprintf('%.1f %sB', $bytes / pow($unit, $exp), $pre);
143+
return sprintf('%.1f %sB', $bytes / ($unit ** $exp), $pre);
148144
}
149145
}

DependencyInjection/GifExceptionExtension.php

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@
1111

1212
namespace Joli\GifExceptionBundle\DependencyInjection;
1313

14-
use Joli\GifExceptionBundle\Command\GifOptimizerCommand;
15-
use Joli\GifExceptionBundle\EventListener\ReplaceImageListener;
16-
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
14+
use Symfony\Component\Config\FileLocator;
1715
use Symfony\Component\DependencyInjection\ContainerBuilder;
18-
use Symfony\Component\DependencyInjection\Definition;
19-
use Symfony\Component\DependencyInjection\Reference;
16+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2017
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21-
use Symfony\Component\HttpKernel\KernelEvents;
2218

23-
class GifExceptionExtension extends Extension implements CompilerPassInterface
19+
class GifExceptionExtension extends Extension
2420
{
2521
/**
2622
* {@inheritdoc}
@@ -31,17 +27,8 @@ public function load(array $configs, ContainerBuilder $container)
3127
return;
3228
}
3329

34-
$definition = new Definition(GifOptimizerCommand::class);
35-
$definition->addTag('console.command', [
36-
'command' => GifOptimizerCommand::COMMAND_NAME, // Allow lazy loading
37-
]);
38-
$container->setDefinition(GifOptimizerCommand::class, $definition);
39-
40-
$definition = new Definition(ReplaceImageListener::class);
41-
$definition->addTag('kernel.event_listener', [
42-
'event' => KernelEvents::RESPONSE,
43-
'priority' => -1000,
44-
]);
30+
$loader = new XmlFileLoader($container, new FileLocator(\dirname(__DIR__) . '/Resources/config'));
31+
$loader->load('services.xml');
4532

4633
$gifs = [];
4734

@@ -55,44 +42,6 @@ public function load(array $configs, ContainerBuilder $container)
5542
$gifs['other'][] = basename($path);
5643
}
5744

58-
// Set first argument. Next ones will be added by the compiler pass.
59-
$definition->addArgument($gifs);
60-
$container->setDefinition('gif_exception.listener.replace_image', $definition);
61-
}
62-
63-
/**
64-
* This CompilerPassInterface method completes the listener definition with
65-
* the parameter and service coming from other bundles. It allows our
66-
* bundle to be registered anytime before or after others.
67-
*
68-
* {@inheritdoc}
69-
*/
70-
public function process(ContainerBuilder $container)
71-
{
72-
if (!$container->getParameter('kernel.debug')) {
73-
return;
74-
}
75-
76-
$definition = $container->getDefinition('gif_exception.listener.replace_image');
77-
78-
// $container->setArgument($index, ...) was added in Symfony 3.3
79-
$arguments = [
80-
$definition->getArgument(0),
81-
$container->getParameter('twig.exception_listener.controller'),
82-
];
83-
84-
if ($container->has('assets.packages')) {
85-
// New Asset component to generate asset url (SF >=2.8)
86-
$arguments[] = new Reference('assets.packages');
87-
$arguments[] = null;
88-
} elseif ($container->has('templating.helper.assets')) {
89-
// Old way of generating asset url (SF ~2.3)
90-
// To remove when compatibility with Symfony 2.7 is dropped
91-
$arguments[] = null;
92-
$arguments[] = new Reference('templating.helper.assets');
93-
$definition->setScope('request');
94-
}
95-
96-
$definition->setArguments($arguments);
45+
$container->getDefinition('gif_exception.listener.replace_image')->replaceArgument(0, $gifs);
9746
}
9847
}

EventListener/ReplaceImageListener.php

Lines changed: 29 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
namespace Joli\GifExceptionBundle\EventListener;
1313

1414
use 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
}

GifExceptionBundle.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@ class GifExceptionBundle extends Bundle
1919
public function build(ContainerBuilder $container)
2020
{
2121
parent::build($container);
22-
23-
$container->addCompilerPass($this->getContainerExtension());
2422
}
2523
}

0 commit comments

Comments
 (0)