Add a filesystem map service
This commit is contained in:
parent
6db8231e09
commit
3a68f9080b
|
@ -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
41
FilesystemMap.php
Normal 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];
|
||||
}
|
||||
}
|
|
@ -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
|
||||
------------------
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue
Block a user