Skip to content

Commit

Permalink
Merge pull request #58 from jolicode/refactore-optimize-command
Browse files Browse the repository at this point in the history
Refactore gif optimizer command
  • Loading branch information
pyrech authored Jun 14, 2023
2 parents 414a64d + 5270053 commit d1ac32e
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 57 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes between versions

## Not released yet

* Updated GIF optimization command

## 1.8.1 (2023-06-07)

* Fixed CI
Expand Down
2 changes: 1 addition & 1 deletion Command/GifOptimizerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/GifExceptionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = [];

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the GifExceptionBundle project.
*
* (c) JoliCode <[email protected]>
*
* 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')
;
};
19 changes: 0 additions & 19 deletions Resources/config/services.xml

This file was deleted.

46 changes: 17 additions & 29 deletions bin/optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
6 changes: 3 additions & 3 deletions tests/Command/GifOptimizerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
}
Expand All @@ -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');
}
Expand Down

0 comments on commit d1ac32e

Please sign in to comment.