Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow service container loader to be overridden #3

Merged
merged 2 commits into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/ServiceContainer/DefaultLoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the ServiceContainerExtension package.
*
* (c) FriendsOfBehat
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FriendsOfBehat\ServiceContainerExtension\ServiceContainer;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

/**
* @author Kamil Kokot <kamil.kokot@lakion.com>
*/
final class DefaultLoaderFactory implements LoaderFactory
{
/**
* {@inheritdoc}
*/
public function createLoader(ContainerBuilder $container, array $config)
{
$fileLocator = new FileLocator($container->getParameter('paths.base'));

return new DelegatingLoader(new LoaderResolver([
new XmlFileLoader($container, $fileLocator),
new YamlFileLoader($container, $fileLocator),
new PhpFileLoader($container, $fileLocator),
]));
}
}
29 changes: 29 additions & 0 deletions src/ServiceContainer/LoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the ServiceContainerExtension package.
*
* (c) FriendsOfBehat
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FriendsOfBehat\ServiceContainerExtension\ServiceContainer;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Kamil Kokot <kamil.kokot@lakion.com>
*/
interface LoaderFactory
{
/**
* @param ContainerBuilder $container
* @param array $config
*
* @return LoaderInterface
*/
public function createLoader(ContainerBuilder $container, array $config);
}
39 changes: 32 additions & 7 deletions src/ServiceContainer/ServiceContainerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
Expand All @@ -28,6 +29,28 @@
final class ServiceContainerExtension implements Extension
{
/**
* @var LoaderFactory
*/
private $loaderFactory;

public function __construct()
{
$this->loaderFactory = new DefaultLoaderFactory();
}

/**
* @api
*
* @param LoaderFactory $loaderFactory
*/
public function setLoaderCallable(LoaderFactory $loaderFactory)
{
$this->loaderFactory = $loaderFactory;
}

/**
* @internal
*
* {@inheritdoc}
*/
public function getConfigKey()
Expand All @@ -36,6 +59,8 @@ public function getConfigKey()
}

/**
* @internal
*
* {@inheritdoc}
*/
public function initialize(ExtensionManager $extensionManager)
Expand All @@ -44,6 +69,8 @@ public function initialize(ExtensionManager $extensionManager)
}

/**
* @internal
*
* {@inheritdoc}
*/
public function configure(ArrayNodeDefinition $builder)
Expand All @@ -57,24 +84,22 @@ public function configure(ArrayNodeDefinition $builder)
}

/**
* @internal
*
* {@inheritdoc}
*/
public function load(ContainerBuilder $container, array $config)
{
$fileLocator = new FileLocator($container->getParameter('paths.base'));

$loader = new DelegatingLoader(new LoaderResolver([
new XmlFileLoader($container, $fileLocator),
new YamlFileLoader($container, $fileLocator),
new PhpFileLoader($container, $fileLocator),
]));
$loader = $this->loaderFactory->createLoader($container, $config);

foreach ($config['imports'] as $file) {
$loader->load($file);
}
}

/**
* @internal
*
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
Expand Down