Initial commit
This commit is contained in:
commit
ae7440f4d7
35
DependencyInjection/Compiler/AdapterFactoryManagerPass.php
Normal file
35
DependencyInjection/Compiler/AdapterFactoryManagerPass.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* Compiler pass that registers the adapter factories
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class AdapterFactoryManagerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('knplabs_gaufrette.adapter_factory_manager')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$definition = $container->getDefinition('knplabs_gaufrette.adapter_factory_manager');
|
||||
|
||||
$calls = $definition->getMethodCalls();
|
||||
$definition->setMethodCalls(array());
|
||||
|
||||
foreach ($container->findTaggedServiceIds('gaufrette.adapter_factory') as $id => $attributes) {
|
||||
if (!empty($attributes['type'])) {
|
||||
$definition->addMethodCall('set', array($attributes['type'], new Reference($id)));
|
||||
}
|
||||
}
|
||||
|
||||
$definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls));
|
||||
}
|
||||
}
|
34
DependencyInjection/Compiler/AdapterManagerPass.php
Normal file
34
DependencyInjection/Compiler/AdapterManagerPass.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* Compiler pass that registers the adapters
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class AdapterManagerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('knplabs_gaufrette.adapter_manager')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$definition = $container->getDefinition('knplabs_gaufrette.adapter_manager');
|
||||
$calls = $definition->getMethodCalls();
|
||||
$definition->setMethodCalls(array());
|
||||
|
||||
foreach ($container->findTaggedServiceIds('gaufrette.adapter') as $id => $attributes) {
|
||||
if (!empty($attributes['alias'])) {
|
||||
$definition->addMethodCall('set', array($attributes['alias'], new Reference($id)));
|
||||
}
|
||||
}
|
||||
|
||||
$definition->setMethodCalls(array_merge($definition->getMethodCalls(), $calls));
|
||||
}
|
||||
}
|
39
DependencyInjection/Factory/AdapterFactoryInterface.php
Normal file
39
DependencyInjection/Factory/AdapterFactoryInterface.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
/**
|
||||
* Interface that must be implemented by the adapater factories
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
interface AdapterFactoryInterface
|
||||
{
|
||||
/**
|
||||
* Creates the adapter, registers it and returns its id
|
||||
*
|
||||
* @param ContainerBuilder $container
|
||||
* @param string $id
|
||||
* @param array $config
|
||||
*
|
||||
* @return string The Adapter service id in the DIC
|
||||
*/
|
||||
function create(ContainerBuilder $container, $id, array $config);
|
||||
|
||||
/**
|
||||
* Returns the key for the factory configuration
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function getKey();
|
||||
|
||||
/**
|
||||
* Adds configuration nodes for the factory
|
||||
*
|
||||
* @param NodeBuilder $builder
|
||||
*/
|
||||
function addConfiguration(NodeDefinition $builder);
|
||||
}
|
60
DependencyInjection/Factory/InMemoryAdapterFactory.php
Normal file
60
DependencyInjection/Factory/InMemoryAdapterFactory.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
|
||||
/**
|
||||
* In memory adapter factory
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class InMemoryAdapterFactory implements AdapterFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function create(ContainerBuilder $container, $id, array $config)
|
||||
{
|
||||
$adapter = sprintf('knplabs_gaufrette.adapter.local.%s', $id);
|
||||
|
||||
$container
|
||||
->setDefinition($adapter, new DefinitionDecorator('knplabs_gaufrette.adapter.in_memory'))
|
||||
->replaceArgument(0, $config['files'])
|
||||
;
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return 'in_memory';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function addConfiguration(NodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->children()
|
||||
->arrayNode('files')
|
||||
->fixXmlConfig('file')
|
||||
->useAttributeAsKey('filename')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('content')->end()
|
||||
->scalarNode('checksum')->end()
|
||||
->scalarNode('mtime')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
}
|
52
DependencyInjection/Factory/LocalAdapterFactory.php
Normal file
52
DependencyInjection/Factory/LocalAdapterFactory.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
|
||||
/**
|
||||
* Local adapter factory
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class LocalAdapterFactory implements AdapterFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function create(ContainerBuilder $container, $id, array $config)
|
||||
{
|
||||
$adapter = sprintf('knplabs_gaufrette.adapter.local.%s', $id);
|
||||
|
||||
$container
|
||||
->setDefinition($adapter, new DefinitionDecorator('knplabs_gaufrette.adapter.local'))
|
||||
->replaceArgument(0, $config['directory'])
|
||||
->replaceArgument(1, $config['create'])
|
||||
;
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return 'local';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function addConfiguration(NodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->children()
|
||||
->scalarNode('directory')->isRequired()->end()
|
||||
->booleanNode('create')->defaultTrue()->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
}
|
52
DependencyInjection/Factory/SafeLocalAdapterFactory.php
Normal file
52
DependencyInjection/Factory/SafeLocalAdapterFactory.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
|
||||
/**
|
||||
* Safe local adapter factory
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class SafeLocalAdapterFactory implements AdapterFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function create(ContainerBuilder $container, $id, array $config)
|
||||
{
|
||||
$adapter = sprintf('knplabs_gaufrette.adapter.safe_local.%s', $id);
|
||||
|
||||
$container
|
||||
->setDefinition($adapter, new DefinitionDecorator('knplabs_gaufrette.adapter.safe_local'))
|
||||
->replaceArgument(0, $config['directory'])
|
||||
->replaceArgument(1, $config['create'])
|
||||
;
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return 'safe-local';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function addConfiguration(NodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->children()
|
||||
->scalarNode('directory')->isRequired()->end()
|
||||
->booleanNode('create')->defaultTrue()->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
}
|
42
DependencyInjection/Factory/ServiceAdapterFactory.php
Normal file
42
DependencyInjection/Factory/ServiceAdapterFactory.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
/**
|
||||
* Service adapter factory
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class ServiceAdapterFactory implements AdapterFactoryInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function create(ContainerBuilder $container, $id, array $config)
|
||||
{
|
||||
return $config['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return 'service';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function addConfiguration(NodeDefinition $builder)
|
||||
{
|
||||
$builder
|
||||
->children()
|
||||
->scalarNode('id')->isRequired()->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
}
|
38
DependencyInjection/FactoryConfiguration.php
Normal file
38
DependencyInjection/FactoryConfiguration.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
|
||||
/**
|
||||
* Factory configuration for the Gaufrette DIC extension
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class FactoryConfiguration implements ConfigurationInterface
|
||||
{
|
||||
/**
|
||||
* Generates the configuration tree builder
|
||||
*
|
||||
* @return TreeBuilder
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
|
||||
$treeBuilder
|
||||
->root('knplabs_gaufrette')
|
||||
->ignoreExtraKeys()
|
||||
->fixXmlConfig('factory', 'factories')
|
||||
->children()
|
||||
->arrayNode('factories')
|
||||
->prototype('scalar')->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $treeBuilder;
|
||||
}
|
||||
}
|
117
DependencyInjection/KnplabsGaufretteExtension.php
Normal file
117
DependencyInjection/KnplabsGaufretteExtension.php
Normal file
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Processor;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
|
||||
/**
|
||||
* The Gaufrette DIC extension
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class KnplabsGaufretteExtension extends Extension
|
||||
{
|
||||
private $factories = null;
|
||||
|
||||
/**
|
||||
* Loads the extension
|
||||
*
|
||||
* @param array $configs
|
||||
* @param ContainerBuilder $container
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
{
|
||||
$processor = new Processor();
|
||||
|
||||
// first assemble the adapter factories
|
||||
$factoryConfig = new FactoryConfiguration();
|
||||
$config = $processor->processConfiguration($factoryConfig, $configs);
|
||||
$factories = $this->createAdapterFactories($config, $container);
|
||||
|
||||
// then normalize the configs
|
||||
$mainConfig = new MainConfiguration($factories);
|
||||
$config = $processor->processConfiguration($mainConfig, $configs);
|
||||
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('gaufrette.xml');
|
||||
|
||||
foreach ($config['adapters'] as $name => $adapter) {
|
||||
$adapters[$name] = $this->createAdapter($name, $adapter, $container, $factories);
|
||||
}
|
||||
|
||||
foreach ($config['filesystems'] as $name => $filesystem) {
|
||||
$this->createFilesystem($name, $filesystem, $container, $adapters);
|
||||
}
|
||||
}
|
||||
|
||||
private function createAdapter($name, array $config, ContainerBuilder $container, array $factories)
|
||||
{
|
||||
$adapter = null;
|
||||
foreach ($config as $key => $adapter) {
|
||||
if (array_key_exists($key, $factories)) {
|
||||
return $factories[$key]->create($container, $name, $adapter);
|
||||
}
|
||||
}
|
||||
|
||||
throw new \LogicException(sprintf('The adapter \'%s\' is not configured.', $name));
|
||||
}
|
||||
|
||||
private function createFilesystem($name, array $config, ContainerBuilder $container, array $adapters)
|
||||
{
|
||||
if (!array_key_exists($config['adapter'], $adapters)) {
|
||||
throw new \LogicException(sprintf('The adapter \'%s\' is not defined.', $config['adapter']));
|
||||
}
|
||||
|
||||
$adapter = $adapters[$config['adapter']];
|
||||
$id = sprintf('gaufrette.%s_filesystem', $name);
|
||||
|
||||
$container
|
||||
->setDefinition($id, new DefinitionDecorator('knplabs_gaufrette.filesystem'))
|
||||
->replaceArgument(0, new Reference($adapter))
|
||||
;
|
||||
|
||||
if (!empty($config['alias'])) {
|
||||
$container->setAlias($config['alias'], $id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the adapter factories
|
||||
*
|
||||
* @param array $config
|
||||
* @param ContainerBuilder $container
|
||||
*/
|
||||
private function createAdapterFactories($config, ContainerBuilder $container)
|
||||
{
|
||||
if (null !== $this->factories) {
|
||||
return $this->factories;
|
||||
}
|
||||
|
||||
// load bundled adapter factories
|
||||
$tempContainer = new ContainerBuilder();
|
||||
$parameterBag = $container->getParameterBag();
|
||||
$loader = new XmlFileLoader($tempContainer, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
|
||||
$loader->load('adapter_factories.xml');
|
||||
|
||||
// load user-created adapter factories
|
||||
foreach ($config['factories'] as $factory) {
|
||||
$loader->load($parameterBag->resolveValue($factory));
|
||||
}
|
||||
|
||||
$services = $tempContainer->findTaggedServiceIds('gaufrette.adapter.factory');
|
||||
$factories = array();
|
||||
foreach (array_keys($services) as $id) {
|
||||
$factory = $tempContainer->get($id);
|
||||
$factories[str_replace('-', '_', $factory->getKey())] = $factory;
|
||||
}
|
||||
|
||||
return $this->factories = $factories;
|
||||
}
|
||||
}
|
87
DependencyInjection/MainConfiguration.php
Normal file
87
DependencyInjection/MainConfiguration.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\DependencyInjection;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
|
||||
/**
|
||||
* Main configuration for the Gaufrette DIC extension
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class MainConfiguration implements ConfigurationInterface
|
||||
{
|
||||
private $factories;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $factories
|
||||
*/
|
||||
public function __construct(array $factories)
|
||||
{
|
||||
$this->factories = $factories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the configuration tree builder
|
||||
*
|
||||
* @return TreeBuilder
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('knplabs_gaufrette');
|
||||
|
||||
$this->addAdaptersSection($rootNode, $this->factories);
|
||||
$this->addFilesystemsSection($rootNode);
|
||||
|
||||
$rootNode
|
||||
// add a faux-entry for factories, so that no validation error is thrown
|
||||
->fixXmlConfig('factory', 'factories')
|
||||
->children()
|
||||
->arrayNode('factories')->ignoreExtraKeys()->end()
|
||||
->end()
|
||||
;
|
||||
|
||||
return $treeBuilder;
|
||||
}
|
||||
|
||||
private function addAdaptersSection(ArrayNodeDefinition $node, array $factories)
|
||||
{
|
||||
$adapterNodeBuilder = $node
|
||||
->fixXmlConfig('adapter')
|
||||
->children()
|
||||
->arrayNode('adapters')
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
->performNoDeepMerging()
|
||||
->children()
|
||||
;
|
||||
|
||||
foreach ($factories as $name => $factory) {
|
||||
$factoryNode = $adapterNodeBuilder->arrayNode($name)->canBeUnset();
|
||||
|
||||
$factory->addConfiguration($factoryNode);
|
||||
}
|
||||
}
|
||||
|
||||
private function addFilesystemsSection(ArrayNodeDefinition $node)
|
||||
{
|
||||
$node
|
||||
->fixXmlConfig('filesystem')
|
||||
->children()
|
||||
->arrayNode('filesystems')
|
||||
->useAttributeAsKey('name')
|
||||
->prototype('array')
|
||||
->children()
|
||||
->scalarNode('adapter')->isRequired()->end()
|
||||
->scalarNode('alias')->defaultNull()->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
;
|
||||
}
|
||||
}
|
15
KnplabsGaufretteBundle.php
Normal file
15
KnplabsGaufretteBundle.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
|
||||
/**
|
||||
* The Gaufrette Bundle
|
||||
*
|
||||
* @author Antoine Hérault <antoine.herault@gmail.com>
|
||||
*/
|
||||
class KnplabsGaufretteBundle extends Bundle
|
||||
{
|
||||
}
|
19
LICENSE
Normal file
19
LICENSE
Normal file
|
@ -0,0 +1,19 @@
|
|||
Copyright (C) 2011 by Antoine Hérault <antoine.herault@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
187
README.markdown
Normal file
187
README.markdown
Normal file
|
@ -0,0 +1,187 @@
|
|||
Gaufrette Bundle
|
||||
================
|
||||
|
||||
Provides a [Gaufrette][gaufrette-homepage] integration for your Symfony projects.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
## Prérequisites
|
||||
|
||||
As this bundle is an integration for Symfony of the [Gaufrette][gaufrette-homepage] library, it requires you to first install [Gaufrette][gaufrette-homepage] in a Symfony project.
|
||||
|
||||
## Download the bundle
|
||||
|
||||
You can download an archive of the bundle and unpack it in the `vendor/bundles/Knplabs/Bundle/GaufretteBundle` directory of your application.
|
||||
|
||||
If you are versioning your project with git, you had better to embed it as a submodule:
|
||||
|
||||
$ git submodule add https://github.com/knplabs/GaufretteBundle.git vendor/bundles/Knplabs/Bundle/GaufretteBundle
|
||||
|
||||
## Add the namespace in the autoloader
|
||||
|
||||
If the `Knplabs` namespace is not already defined in your autoloader, you must add it:
|
||||
|
||||
``` php
|
||||
<?php
|
||||
|
||||
// app/autoload.php
|
||||
|
||||
$loader->registerNamespaces(array(
|
||||
|
||||
'Knplabs' => __DIR__.'/../vendor/bundles'
|
||||
|
||||
// ...
|
||||
|
||||
));
|
||||
```
|
||||
|
||||
## Register the bundle
|
||||
|
||||
You must register the bundle in your kernel:
|
||||
|
||||
``` php
|
||||
<?php
|
||||
|
||||
// app/AppKernel.php
|
||||
|
||||
public function registerBundles()
|
||||
{
|
||||
$bundles = array(
|
||||
|
||||
// ...
|
||||
|
||||
new Knplabs\Bundle\GaufretteBundle\KnplabsGaufretteBundle()
|
||||
);
|
||||
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
|
||||
The Gaufrette bundle allows you to declare your filesystems as services without having to reach into the famous "Service Container".
|
||||
Indeed, you can do it with the configuration!
|
||||
|
||||
The configuration of the Gaufrette bundle is divided into two parts: the `adapters` and the `filesystems`.
|
||||
|
||||
## Configuring the Adapters
|
||||
|
||||
``` yaml
|
||||
# app/config/config.yml
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
local:
|
||||
directory: /path/to/my/filesystem
|
||||
```
|
||||
|
||||
The defined adapters are usable to create the filesystems.
|
||||
|
||||
## Configuring the Filesystems
|
||||
|
||||
``` yaml
|
||||
# app/config/config.yml
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
# ...
|
||||
filesystems:
|
||||
bar:
|
||||
adapter: foo
|
||||
alias: foo_filesystem
|
||||
```
|
||||
|
||||
Each defined filesystem must have an `adapter` with the key of an adapter as value.
|
||||
The filesystem defined above with result in a service with id `gaufrette.foo_filesystem`.
|
||||
The `alias` parameter permits to also defines an alias for it.
|
||||
|
||||
Adapters Reference
|
||||
------------------
|
||||
|
||||
## Local Adapter
|
||||
|
||||
A simple local filesystem based adapter.
|
||||
|
||||
### Parameters
|
||||
|
||||
* `directory` The directory of the filesystem *(required)*
|
||||
* `create` Whether to create the directory if it does not exist *(default true)*
|
||||
|
||||
### Exemple
|
||||
|
||||
``` yaml
|
||||
# app/config/config.yml
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
local:
|
||||
directory: /path/to/my/filesystem
|
||||
create: true
|
||||
```
|
||||
|
||||
## Safe Local Adapter (safe\_local)
|
||||
|
||||
Almost as simple as the **local** adapter, but it encodes key to avoid having to deal with the directories structure.
|
||||
|
||||
### Parameters
|
||||
|
||||
* `directory` The directory of the filesystem *(required)*
|
||||
* `create` Whether to create the directory if it does not exist *(default true)*
|
||||
|
||||
### Exemple
|
||||
|
||||
``` yaml
|
||||
# app/config/config.yml
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
safe_local:
|
||||
directory: /path/to/my/filesystem
|
||||
create: true
|
||||
```
|
||||
|
||||
## Service (service)
|
||||
|
||||
Allows you to use a user defined adapter service.
|
||||
|
||||
### Parameters
|
||||
|
||||
* `id` The id of the service *(required)*
|
||||
|
||||
### Exemple
|
||||
|
||||
``` yaml
|
||||
# app/config/config.yml
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
service:
|
||||
id: my.adapter.service
|
||||
```
|
||||
|
||||
## In Memory (in\_memory)
|
||||
|
||||
Adapter for test purposes, it stores files in an internal array.
|
||||
|
||||
### Parameters
|
||||
|
||||
* `files` An array of files *(optional)*
|
||||
|
||||
The `files` is an array of files where each file is a sub-array having the `content`, `checksum` and `mtime` optional keys.
|
||||
|
||||
### Exemple
|
||||
|
||||
``` yaml
|
||||
# app/config/config.yml
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
in_memory:
|
||||
files:
|
||||
'file1.txt': ~
|
||||
'file2.txt':
|
||||
content: Some content
|
||||
checksum: abc1efg2hij3
|
||||
mtime: 123456890123
|
||||
```
|
29
Resources/config/adapter_factories.xml
Normal file
29
Resources/config/adapter_factories.xml
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
|
||||
<parameters>
|
||||
<parameter key="knplabs_gaufrette.adapter.factory.in_memory.class">Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\InMemoryAdapterFactory</parameter>
|
||||
<parameter key="knplabs_gaufrette.adapter.factory.service.class">Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\ServiceAdapterFactory</parameter>
|
||||
<parameter key="knplabs_gaufrette.adapter.factory.local.class">Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\LocalAdapterFactory</parameter>
|
||||
<parameter key="knplabs_gaufrette.adapter.factory.safe_local.class">Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\SafeLocalAdapterFactory</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="knplabs_gaufrette.adapter.factory.in_memory" class="%knplabs_gaufrette.adapter.factory.in_memory.class%">
|
||||
<tag name="gaufrette.adapter.factory" />
|
||||
</service>
|
||||
<service id="knplabs_gaufrette.adapter.factory.service" class="%knplabs_gaufrette.adapter.factory.service.class%">
|
||||
<tag name="gaufrette.adapter.factory" />
|
||||
</service>
|
||||
<service id="knplabs_gaufrette.adapter.factory.local" class="%knplabs_gaufrette.adapter.factory.local.class%">
|
||||
<tag name="gaufrette.adapter.factory" />
|
||||
</service>
|
||||
<service id="knplabs_gaufrette.adapter.factory.safe_local" class="%knplabs_gaufrette.adapter.factory.safe_local.class%">
|
||||
<tag name="gaufrette.adapter.factory" />
|
||||
</service>
|
||||
</services>
|
||||
|
||||
</container>
|
31
Resources/config/gaufrette.xml
Normal file
31
Resources/config/gaufrette.xml
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
|
||||
<parameters>
|
||||
<parameter key="knplabs_gaufrette.filesystem.class">Gaufrette\Filesystem\Filesystem</parameter>
|
||||
<parameter key="knplabs_gaufrette.adapter.in_memory.class">Gaufrette\Filesystem\Adapter\InMemory</parameter>
|
||||
<parameter key="knplabs_gaufrette.adapter.local.class">Gaufrette\Filesystem\Adapter\Local</parameter>
|
||||
<parameter key="knplabs_gaufrette.adapter.safe_local.class">Gaufrette\Filesystem\Adapter\SafeLocal</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<service id="knplabs_gaufrette.filesystem" class="%knplabs_gaufrette.filesystem.class%" abstract="true">
|
||||
<argument /><!-- The Adapter -->
|
||||
</service>
|
||||
<service id="knplabs_gaufrette.adapter.in_memory" class="%knplabs_gaufrette.adapter.in_memory.class%" abstract="true">
|
||||
<argument /><!-- Files -->
|
||||
</service>
|
||||
<service id="knplabs_gaufrette.adapter.local" class="%knplabs_gaufrette.adapter.local.class%" abstract="true">
|
||||
<argument /><!-- Directory -->
|
||||
<argument /><!-- Create -->
|
||||
</service>
|
||||
<service id="knplabs_gaufrette.adapter.safe_local" class="%knplabs_gaufrette.adapter.safe_local.class%" abstract="true">
|
||||
<argument /><!-- Directory -->
|
||||
<argument /><!-- Create -->
|
||||
</service>
|
||||
</services>
|
||||
|
||||
</container>
|
1
Resources/views/Default/index.html.twig
Normal file
1
Resources/views/Default/index.html.twig
Normal file
|
@ -0,0 +1 @@
|
|||
Hello!
|
66
Tests/FunctionalTest.php
Normal file
66
Tests/FunctionalTest.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\Tests;
|
||||
|
||||
use Symfony\Component\HttpKernel\Util\Filesystem;
|
||||
|
||||
class FunctionalTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
$this->cacheDir = __DIR__.'/Resources/cache';
|
||||
if (file_exists($this->cacheDir)) {
|
||||
$filesystem = new Filesystem();
|
||||
$filesystem->remove($this->cacheDir);
|
||||
}
|
||||
|
||||
mkdir($this->cacheDir, 0777, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getConfigurationData
|
||||
*/
|
||||
public function testConfiguration($env, array $filesystems)
|
||||
{
|
||||
$kernel = new TestKernel($env, false);
|
||||
$kernel->boot();
|
||||
|
||||
$container = $kernel->getContainer();
|
||||
|
||||
foreach ($filesystems as $id => $adapterClass) {
|
||||
$this->assertTrue($container->has($id), sprintf('Filesystem service \'%s\' exists.', $id));
|
||||
|
||||
$filesystem = $container->get($id);
|
||||
$this->assertInstanceOf('Gaufrette\Filesystem\Filesystem', $filesystem);
|
||||
|
||||
$reflProperty = new \ReflectionProperty($filesystem, 'adapter');
|
||||
$reflProperty->setAccessible(true);
|
||||
|
||||
$adapter = $reflProperty->getValue($filesystem);
|
||||
|
||||
$reflProperty->setAccessible(false);
|
||||
|
||||
$this->assertInstanceOf($adapterClass, $adapter);
|
||||
}
|
||||
}
|
||||
|
||||
public function getConfigurationData()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
'dev',
|
||||
array(
|
||||
'gaufrette.foo_filesystem' => 'Gaufrette\Filesystem\Adapter\Local',
|
||||
'foo_filesystem' => 'Gaufrette\Filesystem\Adapter\Local',
|
||||
)
|
||||
),
|
||||
array(
|
||||
'test',
|
||||
array(
|
||||
'gaufrette.foo_filesystem' => 'Gaufrette\Filesystem\Adapter\InMemory',
|
||||
'foo_filesystem' => 'Gaufrette\Filesystem\Adapter\InMemory',
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
69
Tests/Resources/cache/test/ResourcesTestProjectContainer.php
vendored
Normal file
69
Tests/Resources/cache/test/ResourcesTestProjectContainer.php
vendored
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\Parameter;
|
||||
class ResourcesTestProjectContainer extends Container
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->parameters = $this->getDefaultParameters();
|
||||
$this->services =
|
||||
$this->scopedServices =
|
||||
$this->scopeStacks = array();
|
||||
$this->set('service_container', $this);
|
||||
$this->scopes = array();
|
||||
$this->scopeChildren = array();
|
||||
}
|
||||
protected function getGaufrette_FooFilesystemService()
|
||||
{
|
||||
return $this->services['gaufrette.foo_filesystem'] = new \Gaufrette\Filesystem\Filesystem($this->get('knplabs_gaufrette.adapter.local.foo'));
|
||||
}
|
||||
protected function getKnplabsGaufrette_Adapter_Local_FooService()
|
||||
{
|
||||
return $this->services['knplabs_gaufrette.adapter.local.foo'] = new \Gaufrette\Filesystem\Adapter\InMemory(array());
|
||||
}
|
||||
protected function getFooFilesystemService()
|
||||
{
|
||||
return $this->get('gaufrette.foo_filesystem');
|
||||
}
|
||||
public function getParameter($name)
|
||||
{
|
||||
$name = strtolower($name);
|
||||
if (!array_key_exists($name, $this->parameters)) {
|
||||
throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
|
||||
}
|
||||
return $this->parameters[$name];
|
||||
}
|
||||
public function hasParameter($name)
|
||||
{
|
||||
return array_key_exists(strtolower($name), $this->parameters);
|
||||
}
|
||||
public function setParameter($name, $value)
|
||||
{
|
||||
throw new \LogicException('Impossible to call set() on a frozen ParameterBag.');
|
||||
}
|
||||
protected function getDefaultParameters()
|
||||
{
|
||||
return array(
|
||||
'kernel.root_dir' => '/home/antoine/htdocs/symfony/vendor/bundles/Knplabs/Bundle/GaufretteBundle/Tests/Resources',
|
||||
'kernel.environment' => 'test',
|
||||
'kernel.debug' => false,
|
||||
'kernel.name' => 'Resources',
|
||||
'kernel.cache_dir' => '/home/antoine/htdocs/symfony/vendor/bundles/Knplabs/Bundle/GaufretteBundle/Tests/Resources/cache/test',
|
||||
'kernel.logs_dir' => '/home/antoine/htdocs/symfony/vendor/bundles/Knplabs/Bundle/GaufretteBundle/Tests/Resources/logs',
|
||||
'kernel.bundles' => array(
|
||||
'KnplabsGaufretteBundle' => 'Knplabs\\Bundle\\GaufretteBundle\\KnplabsGaufretteBundle',
|
||||
),
|
||||
'kernel.charset' => 'UTF-8',
|
||||
'kernel.container_class' => 'ResourcesTestProjectContainer',
|
||||
'knplabs_gaufrette.filesystem.class' => 'Gaufrette\\Filesystem\\Filesystem',
|
||||
'knplabs_gaufrette.adapter.in_memory.class' => 'Gaufrette\\Filesystem\\Adapter\\InMemory',
|
||||
'knplabs_gaufrette.adapter.local.class' => 'Gaufrette\\Filesystem\\Adapter\\Local',
|
||||
'knplabs_gaufrette.adapter.safe_local.class' => 'Gaufrette\\Filesystem\\Adapter\\SafeLocal',
|
||||
'kernel.compiled_classes' => array(
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
11
Tests/Resources/config/config.yml
Normal file
11
Tests/Resources/config/config.yml
Normal file
|
@ -0,0 +1,11 @@
|
|||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
local:
|
||||
directory: %kernel.root_dir%
|
||||
create: true
|
||||
|
||||
filesystems:
|
||||
foo:
|
||||
adapter: foo
|
||||
alias: foo_filesystem
|
2
Tests/Resources/config/config_dev.yml
Normal file
2
Tests/Resources/config/config_dev.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
imports:
|
||||
- { resource: config.yml }
|
7
Tests/Resources/config/config_test.yml
Normal file
7
Tests/Resources/config/config_test.yml
Normal file
|
@ -0,0 +1,7 @@
|
|||
imports:
|
||||
- { resource: config.yml }
|
||||
|
||||
knplabs_gaufrette:
|
||||
adapters:
|
||||
foo:
|
||||
in_memory: ~
|
26
Tests/TestKernel.php
Normal file
26
Tests/TestKernel.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Knplabs\Bundle\GaufretteBundle\Tests;
|
||||
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
class TestKernel extends Kernel
|
||||
{
|
||||
public function getRootDir()
|
||||
{
|
||||
return __DIR__.'/Resources';
|
||||
}
|
||||
|
||||
public function registerBundles()
|
||||
{
|
||||
return array(
|
||||
new \Knplabs\Bundle\GaufretteBundle\KnplabsGaufretteBundle(),
|
||||
);
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(__DIR__.'/Resources/config/config_'.$this->getEnvironment().'.yml');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user