2011-05-08 21:04:37 +00:00
|
|
|
<?php
|
|
|
|
|
2011-07-03 09:38:38 +00:00
|
|
|
namespace Knp\Bundle\GaufretteBundle\DependencyInjection;
|
2011-05-08 21:04:37 +00:00
|
|
|
|
|
|
|
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>
|
|
|
|
*/
|
2011-07-03 09:38:38 +00:00
|
|
|
class KnpGaufretteExtension extends Extension
|
2011-05-08 21:04:37 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-06-18 18:49:55 +00:00
|
|
|
$map = array();
|
2011-05-08 21:04:37 +00:00
|
|
|
foreach ($config['filesystems'] as $name => $filesystem) {
|
2011-06-18 18:49:55 +00:00
|
|
|
$map[$name] = $this->createFilesystem($name, $filesystem, $container, $adapters);
|
2011-05-08 21:04:37 +00:00
|
|
|
}
|
2011-06-18 18:49:55 +00:00
|
|
|
|
2011-07-03 09:38:38 +00:00
|
|
|
$container->getDefinition('knp_gaufrette.filesystem_map')
|
2011-06-18 18:49:55 +00:00
|
|
|
->replaceArgument(0, $map);
|
2012-10-10 15:32:37 +00:00
|
|
|
|
|
|
|
if (isset($config['stream_wrapper'])) {
|
|
|
|
$container->setParameter('knp_gaufrette.stream_wrapper.protocol', $config['stream_wrapper']['protocol']);
|
|
|
|
$container->setParameter('knp_gaufrette.stream_wrapper.filesystems', $config['stream_wrapper']['filesystems']);
|
|
|
|
}
|
2011-05-08 21:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createAdapter($name, array $config, ContainerBuilder $container, array $factories)
|
|
|
|
{
|
|
|
|
$adapter = null;
|
|
|
|
foreach ($config as $key => $adapter) {
|
|
|
|
if (array_key_exists($key, $factories)) {
|
2011-05-13 05:11:59 +00:00
|
|
|
$id = sprintf('gaufrette.%s_adapter', $name);
|
|
|
|
$factories[$key]->create($container, $id, $adapter);
|
|
|
|
|
|
|
|
return $id;
|
2011-05-08 21:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \LogicException(sprintf('The adapter \'%s\' is not configured.', $name));
|
|
|
|
}
|
|
|
|
|
2011-06-18 18:49:55 +00:00
|
|
|
/**
|
|
|
|
* @return Reference a reference to the created filesystem
|
|
|
|
*/
|
2011-05-08 21:04:37 +00:00
|
|
|
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
|
2011-07-03 09:38:38 +00:00
|
|
|
->setDefinition($id, new DefinitionDecorator('knp_gaufrette.filesystem'))
|
2011-05-08 21:04:37 +00:00
|
|
|
->replaceArgument(0, new Reference($adapter))
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!empty($config['alias'])) {
|
2011-09-27 08:50:58 +00:00
|
|
|
$container->getDefinition($id)->setPublic(false);
|
2011-05-08 21:04:37 +00:00
|
|
|
$container->setAlias($config['alias'], $id);
|
|
|
|
}
|
2011-06-18 18:49:55 +00:00
|
|
|
|
|
|
|
return new Reference($id);
|
2011-05-08 21:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|