Add a filesystem map service

This commit is contained in:
ornicar 2011-06-18 11:49:55 -07:00
parent 6db8231e09
commit 3a68f9080b
4 changed files with 67 additions and 2 deletions

View File

@ -45,9 +45,13 @@ class KnplabsGaufretteExtension extends Extension
$adapters[$name] = $this->createAdapter($name, $adapter, $container, $factories);
}
$map = array();
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)
@ -65,6 +69,9 @@ class KnplabsGaufretteExtension extends Extension
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)
{
if (!array_key_exists($config['adapter'], $adapters)) {
@ -82,6 +89,8 @@ class KnplabsGaufretteExtension extends Extension
if (!empty($config['alias'])) {
$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
------------
## 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.
@ -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 `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
------------------

View File

@ -26,6 +26,9 @@
<argument /><!-- Directory -->
<argument /><!-- Create -->
</service>
<service id="knplabs_gaufrette.filesystem_map" class="Gaufrette\FilesystemMap">
<argument /> <!-- map of filesystems -->
</service>
</services>
</container>