From 3a68f9080bd6eafaf14d4f6b8e534f6341997415 Mon Sep 17 00:00:00 2001 From: ornicar Date: Sat, 18 Jun 2011 11:49:55 -0700 Subject: [PATCH] Add a filesystem map service --- .../KnplabsGaufretteExtension.php | 11 ++++- FilesystemMap.php | 41 +++++++++++++++++++ README.markdown | 14 ++++++- Resources/config/gaufrette.xml | 3 ++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 FilesystemMap.php diff --git a/DependencyInjection/KnplabsGaufretteExtension.php b/DependencyInjection/KnplabsGaufretteExtension.php index 0949791..2551c91 100644 --- a/DependencyInjection/KnplabsGaufretteExtension.php +++ b/DependencyInjection/KnplabsGaufretteExtension.php @@ -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); } /** diff --git a/FilesystemMap.php b/FilesystemMap.php new file mode 100644 index 0000000..78b4b51 --- /dev/null +++ b/FilesystemMap.php @@ -0,0 +1,41 @@ +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]; + } +} diff --git a/README.markdown b/README.markdown index a98dc97..d132d3a 100644 --- a/README.markdown +++ b/README.markdown @@ -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 ------------------ diff --git a/Resources/config/gaufrette.xml b/Resources/config/gaufrette.xml index f4f5653..cf738c7 100644 --- a/Resources/config/gaufrette.xml +++ b/Resources/config/gaufrette.xml @@ -26,6 +26,9 @@ + + +