Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for configuring adapter #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getConfigTreeBuilder()
->booleanNode('throw_exception')->defaultFalse()->end()
->scalarNode('fallback_image')->defaultNull()->end()
->scalarNode('web_dir')->defaultValue('%kernel.root_dir%/../web')->end()
->scalarNode('adapter')->defaultValue('gd')->end()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a class name and will not work.

I think it would be better to create services of adapters and configure the service name instead.

BTW, maybe can you write some tests using this library? https://packagist.org/packages/matthiasnoback/symfony-dependency-injection-test

->end()
;

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/GregwarImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('gregwar_image.throw_exception', $config['throw_exception']);
$container->setParameter('gregwar_image.fallback_image', $config['fallback_image']);
$container->setParameter('gregwar_image.web_dir', $config['web_dir']);
$container->setParameter('gregwar_image.adapter', $config['adapter']);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ you can set the `web_dir` to your new Web path:

gregwar_image:
web_dir: %kernel.root_dir%/../../public_html


There is currently only one adapter (GD) included with
[Gregwar's Image](http://github.com/Gregwar/Image), however if you have a custom
adapter you can configure it like so:

gregwar_image:
adapter: "My\Custom\Adapter"

Usage
=====
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ services:
- '@file_locator'
- '%gregwar_image.throw_exception%'
- '%gregwar_image.fallback_image%'
- '%gregwar_image.adapter%'

# Helper Twig
twig.extension.image:
Expand Down
14 changes: 13 additions & 1 deletion Services/ImageHandling.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class ImageHandling
*/
private $throwException;

/**
* @var string
*/
private $adapter;

/**
* @param string $cacheDirectory
* @param int $cacheDirMode
Expand All @@ -53,8 +58,9 @@ class ImageHandling
* @param KernelInterface|FileLocatorInterface $fileLocator
* @param bool $throwException
* @param string $fallbackImage
* @param string $adapter
*/
public function __construct($cacheDirectory, $cacheDirMode, $handlerClass, ContainerInterface $container, $fileLocator, $throwException, $fallbackImage)
public function __construct($cacheDirectory, $cacheDirMode, $handlerClass, ContainerInterface $container, $fileLocator, $throwException, $fallbackImage, $adapter)
{
if (!$fileLocator instanceof FileLocatorInterface && $fileLocator instanceof KernelInterface) {
throw new \InvalidArgumentException(
Expand All @@ -79,6 +85,7 @@ public function __construct($cacheDirectory, $cacheDirMode, $handlerClass, Conta
$this->fileLocator = $fileLocator;
$this->throwException = $throwException;
$this->fallbackImage = $fallbackImage;
$this->adapter = $adapter;
}

/**
Expand Down Expand Up @@ -129,6 +136,11 @@ private function createInstance($file, $w = null, $h = null)
/** @var ImageHandler $image */
$image = new $handlerClass($file, $w, $h, $this->throwException, $this->fallbackImage);

if (class_exists($this->adapter)) {
$this->adapter = new $this->adapter();
}

$image->setAdapter($this->adapter);
$image->setCacheDir($this->cacheDirectory);
$image->setCacheDirMode($this->cacheDirMode);
$image->setActualCacheDir($webDir.'/'.$this->cacheDirectory);
Expand Down