-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #674 from graundas/master
Flysystem support added.
- Loading branch information
Showing
11 changed files
with
350 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Binary\Loader; | ||
|
||
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface; | ||
use Liip\ImagineBundle\Model\Binary; | ||
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; | ||
use League\Flysystem\FilesystemInterface; | ||
|
||
class FlysystemLoader implements LoaderInterface | ||
{ | ||
/** | ||
* @var FilesystemInterface | ||
*/ | ||
protected $filesystem; | ||
|
||
/** | ||
* @var ExtensionGuesserInterface | ||
*/ | ||
protected $extensionGuesser; | ||
|
||
public function __construct( | ||
ExtensionGuesserInterface $extensionGuesser, | ||
FilesystemInterface $filesystem) | ||
{ | ||
$this->extensionGuesser = $extensionGuesser; | ||
$this->filesystem = $filesystem; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function find($path) | ||
{ | ||
if ($this->filesystem->has($path) === false) { | ||
throw new NotLoadableException(sprintf('Source image "%s" not found.', $path)); | ||
} | ||
|
||
$mimeType = $this->filesystem->getMimetype($path); | ||
|
||
return new Binary( | ||
$this->filesystem->read($path), | ||
$mimeType, | ||
$this->extensionGuesser->guess($mimeType) | ||
); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
DependencyInjection/Factory/Loader/FlysystemLoaderFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\DependencyInjection\Factory\Loader; | ||
|
||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class FlysystemLoaderFactory implements LoaderFactoryInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(ContainerBuilder $container, $loaderName, array $config) | ||
{ | ||
$loaderDefinition = new DefinitionDecorator('liip_imagine.binary.loader.prototype.flysystem'); | ||
$loaderDefinition->replaceArgument(1, new Reference($config['filesystem_service'])); | ||
$loaderDefinition->addTag('liip_imagine.binary.loader', array( | ||
'loader' => $loaderName, | ||
)); | ||
$loaderId = 'liip_imagine.binary.loader.'.$loaderName; | ||
|
||
$container->setDefinition($loaderId, $loaderDefinition); | ||
|
||
return $loaderId; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'flysystem'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addConfiguration(ArrayNodeDefinition $builder) | ||
{ | ||
$builder | ||
->children() | ||
->scalarNode('filesystem_service')->isRequired()->cannotBeEmpty()->end() | ||
->end() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
FlysystemLoader | ||
=============== | ||
|
||
This loader lets you load images from `Flysystem`_ filesystem abstraction layer, | ||
which can be used in Symfony projects by installing, for example, `OneupFlysystemBundle`_. | ||
|
||
Value of ``filesystem_service`` property must be a service, | ||
which returns an instance of League\\Flysystem\\Filesystem. | ||
|
||
For implementation using `OneupFlysystemBundle`_ look below. | ||
|
||
Using factory | ||
------------- | ||
|
||
.. code-block:: yaml | ||
liip_imagine: | ||
loaders: | ||
profile_photos: | ||
flysystem: | ||
filesystem_service: oneup_flysystem.profile_photos_filesystem | ||
oneup_flysystem: | ||
adapters: | ||
profile_photos: | ||
local: | ||
directory: "path/to/profile/photos" | ||
filesystems: | ||
profile_photos: | ||
adapter: profile_photos | ||
.. _`Flysystem`: /~https://github.com/thephpleague/flysystem | ||
.. _`OneupFlysystemBundle`: /~https://github.com/1up-lab/OneupFlysystemBundle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Tests\Binary\Loader; | ||
|
||
use Liip\ImagineBundle\Binary\Loader\FlysystemLoader; | ||
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser; | ||
use Liip\ImagineBundle\Tests\AbstractTest; | ||
|
||
/** | ||
* @requires PHP 5.4 | ||
* @covers Liip\ImagineBundle\Binary\Loader\FlysystemLoader | ||
*/ | ||
class FlysystemLoaderTest extends AbstractTest | ||
{ | ||
private $flyFilesystem; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
if (!class_exists('\League\Flysystem\Filesystem')) { | ||
$this->markTestSkipped( | ||
'The league/flysystem PHP library is not available.' | ||
); | ||
} | ||
|
||
$adapter = new \League\Flysystem\Adapter\Local($this->fixturesDir); | ||
$this->flyFilesystem = new \League\Flysystem\Filesystem($adapter); | ||
} | ||
|
||
public function testShouldImplementLoaderInterface() | ||
{ | ||
$rc = new \ReflectionClass('Liip\ImagineBundle\Binary\Loader\FlysystemLoader'); | ||
|
||
$this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\Binary\Loader\LoaderInterface')); | ||
} | ||
|
||
public function testCouldBeConstructedWithExpectedArguments() | ||
{ | ||
return new FlysystemLoader( | ||
ExtensionGuesser::getInstance(), | ||
$this->flyFilesystem | ||
); | ||
} | ||
|
||
/** | ||
* @depends testCouldBeConstructedWithExpectedArguments | ||
*/ | ||
public function testReturnImageContentOnFind($loader) | ||
{ | ||
$expectedContent = file_get_contents($this->fixturesDir.'/assets/cats.jpeg'); | ||
|
||
$this->assertSame( | ||
$expectedContent, | ||
$loader->find('assets/cats.jpeg')->getContent() | ||
); | ||
} | ||
|
||
/** | ||
* @depends testCouldBeConstructedWithExpectedArguments | ||
*/ | ||
public function testThrowsIfInvalidPathGivenOnFind($loader) | ||
{ | ||
$path = 'invalid.jpeg'; | ||
|
||
$this->setExpectedException( | ||
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException', | ||
sprintf('Source image "%s" not found.', $path) | ||
); | ||
|
||
$loader->find($path); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
Tests/DependencyInjection/Factory/Loader/FlysystemLoaderFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Liip\ImagineBundle\Tests\DependencyInjection\Factory\Loader; | ||
|
||
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory; | ||
use Symfony\Component\Config\Definition\Builder\TreeBuilder; | ||
use Symfony\Component\Config\Definition\Processor; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @requires PHP 5.4 | ||
* @covers Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory<extended> | ||
*/ | ||
class FlysystemLoaderFactoryTest extends \Phpunit_Framework_TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
if (!class_exists('\League\Flysystem\Filesystem')) { | ||
$this->markTestSkipped( | ||
'The league/flysystem PHP library is not available.' | ||
); | ||
} | ||
} | ||
|
||
public function testImplementsLoaderFactoryInterface() | ||
{ | ||
$rc = new \ReflectionClass('Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory'); | ||
|
||
$this->assertTrue($rc->implementsInterface('Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface')); | ||
} | ||
|
||
public function testCouldBeConstructedWithoutAnyArguments() | ||
{ | ||
new FlysystemLoaderFactory(); | ||
} | ||
|
||
public function testReturnExpectedName() | ||
{ | ||
$loader = new FlysystemLoaderFactory(); | ||
|
||
$this->assertEquals('flysystem', $loader->getName()); | ||
} | ||
|
||
public function testCreateLoaderDefinitionOnCreate() | ||
{ | ||
$container = new ContainerBuilder(); | ||
|
||
$loader = new FlysystemLoaderFactory(); | ||
|
||
$loader->create($container, 'theLoaderName', array( | ||
'filesystem_service' => 'flyfilesystemservice', | ||
)); | ||
|
||
$this->assertTrue($container->hasDefinition('liip_imagine.binary.loader.theloadername')); | ||
|
||
$loaderDefinition = $container->getDefinition('liip_imagine.binary.loader.theloadername'); | ||
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $loaderDefinition); | ||
$this->assertEquals('liip_imagine.binary.loader.prototype.flysystem', $loaderDefinition->getParent()); | ||
|
||
$reference = $loaderDefinition->getArgument(1); | ||
$this->assertEquals('flyfilesystemservice', "$reference"); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException | ||
* @expectedExceptionMessage The child node "filesystem_service" at path "flysystem" must be configured. | ||
*/ | ||
public function testThrowIfFileSystemServiceNotSetOnAddConfiguration() | ||
{ | ||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('flysystem', 'array'); | ||
|
||
$resolver = new FlysystemLoaderFactory(); | ||
$resolver->addConfiguration($rootNode); | ||
|
||
$this->processConfigTree($treeBuilder, array()); | ||
} | ||
|
||
public function testProcessCorrectlyOptionsOnAddConfiguration() | ||
{ | ||
$expectedService = 'theService'; | ||
|
||
$treeBuilder = new TreeBuilder(); | ||
$rootNode = $treeBuilder->root('flysystem', 'array'); | ||
|
||
$loader = new FlysystemLoaderFactory(); | ||
$loader->addConfiguration($rootNode); | ||
|
||
$config = $this->processConfigTree($treeBuilder, array( | ||
'flysystem' => array( | ||
'filesystem_service' => $expectedService, | ||
), | ||
)); | ||
|
||
$this->assertArrayHasKey('filesystem_service', $config); | ||
$this->assertEquals($expectedService, $config['filesystem_service']); | ||
} | ||
|
||
/** | ||
* @param TreeBuilder $treeBuilder | ||
* @param array $configs | ||
* | ||
* @return array | ||
*/ | ||
protected function processConfigTree(TreeBuilder $treeBuilder, array $configs) | ||
{ | ||
$processor = new Processor(); | ||
|
||
return $processor->process($treeBuilder->buildTree(), $configs); | ||
} | ||
} |
Oops, something went wrong.