-
Notifications
You must be signed in to change notification settings - Fork 380
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
Flysystem support added. #674
Changes from 10 commits
322e9e3
48069d1
d87364e
7f7cc71
6714020
0e8f3c6
fe25f18
7112fe0
25e2c04
4cc2914
91af895
cc8c1b7
f5341df
bf3f931
3647634
4dfc6b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
); | ||
} | ||
} |
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() | ||
; | ||
} | ||
} |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace this:
by this (backticks must be doubled):
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @javiereguiluz does the doc looks ok? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added some minor comments, but most of it is correct. |
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); | ||
} | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be as long as the preceding heading.