Merge pull request #1 from ornicar/addFilesystemMap

Add a filesystem map service
This commit is contained in:
Antoine Hérault 2011-06-19 11:59:53 -07:00
commit 3192da5d04
5 changed files with 75 additions and 24 deletions

View File

@ -45,9 +45,13 @@ class KnplabsGaufretteExtension extends Extension
$adapters[$name] = $this->createAdapter($name, $adapter, $container, $factories); $adapters[$name] = $this->createAdapter($name, $adapter, $container, $factories);
} }
$map = array();
foreach ($config['filesystems'] as $name => $filesystem) { foreach ($config['filesystems'] as $name => $filesystem) {
$this->createFilesystem($name, $filesystem, $container, $adapters); $map[$name] = $this->createFilesystem($name, $filesystem, $container, $adapters);
} }
$container->getDefinition('knplabs_gaufrette.filesystem_map')
->replaceArgument(0, $map);
} }
private function createAdapter($name, array $config, ContainerBuilder $container, array $factories) private function createAdapter($name, array $config, ContainerBuilder $container, array $factories)
@ -65,6 +69,9 @@ class KnplabsGaufretteExtension extends Extension
throw new \LogicException(sprintf('The adapter \'%s\' is not configured.', $name)); throw new \LogicException(sprintf('The adapter \'%s\' is not configured.', $name));
} }
/**
* @return Reference a reference to the created filesystem
*/
private function createFilesystem($name, array $config, ContainerBuilder $container, array $adapters) private function createFilesystem($name, array $config, ContainerBuilder $container, array $adapters)
{ {
if (!array_key_exists($config['adapter'], $adapters)) { if (!array_key_exists($config['adapter'], $adapters)) {
@ -82,6 +89,8 @@ class KnplabsGaufretteExtension extends Extension
if (!empty($config['alias'])) { if (!empty($config['alias'])) {
$container->setAlias($config['alias'], $id); $container->setAlias($config['alias'], $id);
} }
return new Reference($id);
} }
/** /**

41
FilesystemMap.php Normal file
View File

@ -0,0 +1,41 @@
<?php
namespace Knplabs\Bundle\GaufretteBundle;
/**
* Holds references to all declared filesystems
* and allows to access them through their name
*/
class FilesystemMap
{
/**
* Map of filesystems indexed by their name
*
* @var array
*/
private $map;
/**
* Instanciates a new filesystem map
*
* @param array $map
*/
public function __construct(array $map)
{
$this->map = $map;
}
/**
* @param string $name name of a filesystem
* @throw \InvalidArgumentException if the filesystem does not exist
* @return Filesystem
*/
public function get($name)
{
if (!isset($this->map[$name])) {
throw new \InvalidArgumentException(sprintf('No filesystem register for name "%s"', $name));
}
return $this->map[$name];
}
}

View File

@ -14,7 +14,7 @@ Documentation is available the [official page of Gaufrette][gaufrette-homepage].
Installation Installation
------------ ------------
## Prérequisites ## Prerequisites
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. 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.
@ -104,6 +104,18 @@ Each defined filesystem must have an `adapter` with the key of an adapter as val
The filesystem defined above with result in a service with id `gaufrette.bar_filesystem`. The filesystem defined above with result in a service with id `gaufrette.bar_filesystem`.
The `alias` parameter permits to also defines an alias for it. The `alias` parameter permits to also defines an alias for it.
The filesystem map
------------------
You can access to all declared filesystems through the map service.
In the previous exemple, we declared a `bar` filesystem:
``` php
$container->get('knplabs_gaufrette.filesystem_map')->get('bar');
```
Returns the instance of `Gaufrette\Filesystem` for `bar`.
Adapters Reference Adapters Reference
------------------ ------------------

View File

@ -4,24 +4,17 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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> <services>
<service id="knplabs_gaufrette.adapter.factory.in_memory" class="%knplabs_gaufrette.adapter.factory.in_memory.class%"> <service id="knplabs_gaufrette.adapter.factory.in_memory" class="Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\InMemoryAdapterFactory">
<tag name="gaufrette.adapter.factory" /> <tag name="gaufrette.adapter.factory" />
</service> </service>
<service id="knplabs_gaufrette.adapter.factory.service" class="%knplabs_gaufrette.adapter.factory.service.class%"> <service id="knplabs_gaufrette.adapter.factory.service" class="Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\ServiceAdapterFactory">
<tag name="gaufrette.adapter.factory" /> <tag name="gaufrette.adapter.factory" />
</service> </service>
<service id="knplabs_gaufrette.adapter.factory.local" class="%knplabs_gaufrette.adapter.factory.local.class%"> <service id="knplabs_gaufrette.adapter.factory.local" class="Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\LocalAdapterFactory">
<tag name="gaufrette.adapter.factory" /> <tag name="gaufrette.adapter.factory" />
</service> </service>
<service id="knplabs_gaufrette.adapter.factory.safe_local" class="%knplabs_gaufrette.adapter.factory.safe_local.class%"> <service id="knplabs_gaufrette.adapter.factory.safe_local" class="Knplabs\Bundle\GaufretteBundle\DependencyInjection\Factory\SafeLocalAdapterFactory">
<tag name="gaufrette.adapter.factory" /> <tag name="gaufrette.adapter.factory" />
</service> </service>
</services> </services>

View File

@ -4,28 +4,24 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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</parameter>
<parameter key="knplabs_gaufrette.adapter.in_memory.class">Gaufrette\Adapter\InMemory</parameter>
<parameter key="knplabs_gaufrette.adapter.local.class">Gaufrette\Adapter\Local</parameter>
<parameter key="knplabs_gaufrette.adapter.safe_local.class">Gaufrette\Adapter\SafeLocal</parameter>
</parameters>
<services> <services>
<service id="knplabs_gaufrette.filesystem" class="%knplabs_gaufrette.filesystem.class%" abstract="true"> <service id="knplabs_gaufrette.filesystem" class="Gaufrette\Filesystem" abstract="true">
<argument /><!-- The Adapter --> <argument /><!-- The Adapter -->
</service> </service>
<service id="knplabs_gaufrette.adapter.in_memory" class="%knplabs_gaufrette.adapter.in_memory.class%" abstract="true" public="false"> <service id="knplabs_gaufrette.adapter.in_memory" class="Gaufrette\Adapter\InMemory" abstract="true" public="false">
<argument /><!-- Files --> <argument /><!-- Files -->
</service> </service>
<service id="knplabs_gaufrette.adapter.local" class="%knplabs_gaufrette.adapter.local.class%" abstract="true" public="false"> <service id="knplabs_gaufrette.adapter.local" class="Gaufrette\Adapter\Local" abstract="true" public="false">
<argument /><!-- Directory --> <argument /><!-- Directory -->
<argument /><!-- Create --> <argument /><!-- Create -->
</service> </service>
<service id="knplabs_gaufrette.adapter.safe_local" class="%knplabs_gaufrette.adapter.safe_local.class%" abstract="true" public="false"> <service id="knplabs_gaufrette.adapter.safe_local" class="Gaufrette\Adapter\SafeLocal" abstract="true" public="false">
<argument /><!-- Directory --> <argument /><!-- Directory -->
<argument /><!-- Create --> <argument /><!-- Create -->
</service> </service>
<service id="knplabs_gaufrette.filesystem_map" class="Knplabs\Bundle\GaufretteBundle\FilesystemMap">
<argument /> <!-- map of filesystems -->
</service>
</services> </services>
</container> </container>