Skip to content

Commit

Permalink
Merge pull request #16 from hrevert/feature/simple-image-loader-factory
Browse files Browse the repository at this point in the history
New way to load images
  • Loading branch information
ojhaujjwal committed Aug 24, 2014
2 parents 656e673 + a8230ad commit 28bb25b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/image-loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ $imageLoader = new HtImgModule\Imagine\Loader\SimpleFileSystemLoader('data/path/
$binary = $imageLoader->load('my-image.png');
```

This module can be used to load images by configuring like this:

```php
return [
'htimg' => [
'filters' => [
'my_filter' => [
'type' => 'filter_type',
'options' => [
// .....
'image_loader' => 'simple',
'loader_options' => [
'root_path' => 'data/images/uploads',
]
]
]
]
]
]
```

#### HtImgModule\Imagine\Loader\FileSystemLoader
This image loaders resolves full path of image using image resolver and loads the image.
```php
Expand Down
33 changes: 33 additions & 0 deletions src/Factory/Imagine/Loader/SimpleFileSystemLoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace HtImgModule\Factory\Imagine\Loader;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use HtImgModule\Imagine\Loader\SimpleFileSystemLoader;
use Zend\ServiceManager\MutableCreationOptionsInterface;
use HtImgModule\Exception;

class SimpleFileSystemLoaderFactory implements FactoryInterface, MutableCreationOptionsInterface
{
/**
* @var array
*/
protected $options = [];

/**
* {@inheritDoc}
*/
public function setCreationOptions(array $options)
{
$this->options = $options;
}

public function createService(ServiceLocatorInterface $loaders)
{
if (!isset($this->options['root_path'])) {
throw new Exception\InvalidArgumentException('Missing "root_path" in options array');
}

return new SimpleFileSystemLoader($this->options['root_path']);
}
}
5 changes: 5 additions & 0 deletions src/Imagine/Loader/LoaderPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ class LoaderPluginManager extends AbstractPluginManager
{
protected $factories = [
'filesystem' => 'HtImgModule\Factory\Imagine\Loader\FileSystemLoaderFactory',
'simple' => 'HtImgModule\Factory\Imagine\Loader\SimpleFileSystemLoaderFactory',
];

protected $shared = [
'simple' => false
];

public function validatePlugin($plugin)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace HtImgModuleTest\Factory\Imagine\Loader;

use Zend\ServiceManager\ServiceManager;
use HtImgModule\Factory\Imagine\Loader\SimpleFileSystemLoaderFactory;

class SimpleFileSystemLoaderFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateService()
{
$serviceManager = new ServiceManager();
$factory = new SimpleFileSystemLoaderFactory;
$factory->setCreationOptions(['root_path' => 'data/images']);
$this->assertInstanceOf('HtImgModule\Imagine\Loader\SimpleFileSystemLoader', $factory->createService($serviceManager));
}

public function testGetExceptionWhenRootPathOptionIsMissing()
{
$serviceManager = new ServiceManager();
$factory = new SimpleFileSystemLoaderFactory;
$this->setExpectedException('HtImgModule\Exception\InvalidArgumentException');
$factory->createService($serviceManager);
}
}

0 comments on commit 28bb25b

Please sign in to comment.