diff --git a/CHANGELOG.md b/CHANGELOG.md index abd537c..4404a36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changes between versions +## Not released yet + +* Updated GIF optimization command + ## 1.8.1 (2023-06-07) * Fixed CI diff --git a/Command/GifOptimizerCommand.php b/Command/GifOptimizerCommand.php index 1bb6d4d..fd7f78d 100644 --- a/Command/GifOptimizerCommand.php +++ b/Command/GifOptimizerCommand.php @@ -20,7 +20,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -#[AsCommand(name: 'jolicode:gifexception:optimize')] +#[AsCommand(name: 'gifexception:optimize', description: 'Optimize gifs')] class GifOptimizerCommand extends Command { private const DEFAULT_OPTIMIZATION_LEVEL = '-O3'; diff --git a/DependencyInjection/GifExceptionExtension.php b/DependencyInjection/GifExceptionExtension.php index dd4f043..f52c453 100644 --- a/DependencyInjection/GifExceptionExtension.php +++ b/DependencyInjection/GifExceptionExtension.php @@ -13,7 +13,7 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; +use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\Extension; class GifExceptionExtension extends Extension @@ -24,8 +24,8 @@ public function load(array $configs, ContainerBuilder $container): void return; } - $loader = new XmlFileLoader($container, new FileLocator(\dirname(__DIR__) . '/Resources/config')); - $loader->load('services.xml'); + $loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__) . '/Resources/config')); + $loader->load('services.php'); $gifs = []; diff --git a/README.md b/README.md index 6eebac7..81840fb 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ If you are adding gifs we strongly recommend optimizing them. There is a tool to bin/optimizer.php ``` -You can also run this from a Symfony project if you have this as an installed bundle via: +You can also run this command directly from your Symfony application: ``` -bin/console jolicode:gifexception:optimize +bin/console gifexception:optimize ``` Although it would probably make more sense to use the former and push up the optimized gifs. diff --git a/Resources/config/services.php b/Resources/config/services.php new file mode 100644 index 0000000..16de822 --- /dev/null +++ b/Resources/config/services.php @@ -0,0 +1,36 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Joli\GifExceptionBundle\DependencyInjection\Loader\Configurator; + +use Joli\GifExceptionBundle\Command\GifOptimizerCommand; +use Joli\GifExceptionBundle\EventListener\ReplaceImageListener; +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; + +use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg; +use function Symfony\Component\DependencyInjection\Loader\Configurator\param; +use function Symfony\Component\DependencyInjection\Loader\Configurator\service; + +return static function (ContainerConfigurator $container) { + $container->services() + ->set('gif_exception.listener.replace_image', ReplaceImageListener::class) + ->args([ + abstract_arg('gif paths'), + param('kernel.error_controller'), + service('assets.packages')->nullOnInvalid(), + ]) + ->tag('kernel.event_subscriber') + ; + $container->services() + ->set(GifOptimizerCommand::class) + ->tag('console.command') + ; +}; diff --git a/Resources/config/services.xml b/Resources/config/services.xml deleted file mode 100644 index f307692..0000000 --- a/Resources/config/services.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - %kernel.error_controller% - - - - - diff --git a/bin/optimizer.php b/bin/optimizer.php index 5e91c81..b5b46f0 100755 --- a/bin/optimizer.php +++ b/bin/optimizer.php @@ -10,32 +10,20 @@ * file that was distributed with this source code. */ -use Joli\GifExceptionBundle\Tests\app\src\Kernel; -use Symfony\Bundle\FrameworkBundle\Console\Application; -use Symfony\Component\Console\Input\ArgvInput; - -// if you don't want to setup permissions the proper way, just uncomment the following PHP line -// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information -// umask(0000); -set_time_limit(0); - -/** - * @var Composer\Autoload\ClassLoader - */ -$loader = require __DIR__ . '/../vendor/autoload.php'; - -$args = $_SERVER['argv']; - -// Strip application name -array_shift($args); - -// Prepend command name -array_unshift($args, 'jolicode:gifexception:optimize'); - -// Prepend application name (ArgvInput strips it again so needs to be here) -array_unshift($args, __DIR__ . '/optimizer.php'); - -$input = new ArgvInput($args); -$kernel = new Kernel('dev', false); -$application = new Application($kernel); -$application->run($input); +use Joli\GifExceptionBundle\Command\GifOptimizerCommand; +use Symfony\Component\Console\Application; + +if (file_exists(__DIR__ . '/../vendor/autoload.php')) { + $loader = require __DIR__ . '/../vendor/autoload.php'; +} elseif (file_exists(__DIR__ . '/../../../../vendor/autoload.php')) { + $loader = require __DIR__ . '/../../../../vendor/autoload.php'; +} else { + throw new \RuntimeException('Unable to load autoloader.'); +} + +$application = new Application('gifexception'); +$application->add(new GifOptimizerCommand()); +$application + ->setDefaultCommand('gifexception:optimize', true) + ->run() +; diff --git a/tests/Command/GifOptimizerCommandTest.php b/tests/Command/GifOptimizerCommandTest.php index b4d9f88..5db3d00 100644 --- a/tests/Command/GifOptimizerCommandTest.php +++ b/tests/Command/GifOptimizerCommandTest.php @@ -37,7 +37,7 @@ public function testExceptionRaisedForInvalidImageDir(): void { $this->expectException(\RuntimeException::class); - $this->getOutputForCommand('jolicode:gifexception:optimize', ['image_dir' => 'foobar']); + $this->getOutputForCommand('gifexception:optimize', ['image_dir' => 'foobar']); } public function testGifIsResizedToExpectedWidth(): void @@ -49,7 +49,7 @@ public function testGifIsResizedToExpectedWidth(): void $options = ['--resize_width' => $expectedWidth]; try { - $this->getOutputForCommand('jolicode:gifexception:optimize', $args, $options); + $this->getOutputForCommand('gifexception:optimize', $args, $options); } catch (CommandNotFound $e) { $this->markTestSkipped('Gif optimizer tool is not executable'); } @@ -69,7 +69,7 @@ public function testGifIsSmallerFileSize(): void $args = ['image_dir' => __DIR__]; try { - $this->getOutputForCommand('jolicode:gifexception:optimize', $args); + $this->getOutputForCommand('gifexception:optimize', $args); } catch (CommandNotFound $e) { $this->markTestSkipped('Gif optimizer tool is not executable'); }