Skip to content

Commit

Permalink
add loader that processes configuration as symfony does
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikbjorn committed Aug 14, 2014
1 parent ea0f98c commit 7912fb4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
51 changes: 51 additions & 0 deletions spec/Tacker/Loader/ProcessorLoaderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace spec\Tacker\Loader;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;

class ProcessorLoaderSpec extends \PhpSpec\ObjectBehavior
{
/**
* @param Symfony\Component\Config\Loader\LoaderInterface $loader
* @param Symfony\Component\Config\Definition\ConfigurationInterface $configuration
*/
function let($loader, $configuration)
{
$this->beConstructedWith($loader, $configuration);
}

function it_is_initializable()
{
$this->shouldHaveType('Tacker\Loader\ProcessorLoader');
}

function it_processes_configuration($loader, $configuration)
{
$builder = new TreeBuilder;
$root = $builder->root('');
$root
->children()
->booleanNode('auto_connect')
->defaultTrue()
->end()
->end()
;


$configuration->getConfigTreeBuilder()->willReturn($builder);

$loader->load('file', null)->willReturn([]);

$this->load('file')->shouldReturn(['auto_connect' => true]);
}

function it_delegates_supports_to_loader($loader)
{
$loader->supports('php', null)->willReturn(false)->shouldBeCalled();
$loader->supports('yml', null)->willReturn(true)->shouldBeCalled();

$this->supports('php')->shouldReturn(false);
$this->supports('yml')->shouldReturn(true);
}
}
38 changes: 38 additions & 0 deletions src/Loader/ProcessorLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tacker\Loader;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\Loader\LoaderInterface;

class ProcessorLoader extends \Symfony\Component\Config\Loader\Loader
{
private $loader;
private $configuration;

public function __construct(LoaderInterface $loader, ConfigurationInterface $configuration)
{
$this->loader = $loader;
$this->configuration = $configuration;
}

public function load($resource, $type = null)
{
$parameters = $this->loader->load($resource, $type);

return $this->processConfiguration($parameters);
}

public function supports($resource, $type = null)
{
return $this->loader->supports($resource, $type);
}

private function processConfiguration(array $parameters)
{
$processor = new Processor;

return $processor->processConfiguration($this->configuration, [$parameters]);
}
}
16 changes: 15 additions & 1 deletion src/LoaderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Tacker;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Tacker\Loader\CacheLoader;
use Tacker\Loader\IniFileLoader;
use Tacker\Loader\JsonFileLoader;
use Tacker\Loader\NormalizerLoader;
use Tacker\Loader\ProcessorLoader;
use Tacker\Loader\PhpFileLoader;
use Tacker\Loader\YamlFileLoader;
use Tacker\Normalizer\ChainNormalizer;
Expand All @@ -26,6 +28,7 @@ final class LoaderBuilder
private $normalizer;
private $resolver;
private $resources;
private $configuration;
private $locator;
private $debug;
private $cacheDir;
Expand Down Expand Up @@ -79,6 +82,11 @@ public function setDebug($debug)
return $this;
}

public function setConfiguration(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}

public function build()
{
if (false == $this->resolverConfigured) {
Expand All @@ -89,7 +97,13 @@ public function build()
$this->addDefaultNormalizers();
}

$loader = new CacheLoader($this->createNormalizerLoader(), $this->resources);
$loader = $this->createNormalizerLoader();

if ($this->configuration) {
$loader = new ProcessorLoader($loader, $this->configuration);
}

$loader = new CacheLoader($loader, $this->resources);
$loader->setCacheDir($this->cacheDir);
$loader->setDebug($this->debug);

Expand Down

0 comments on commit 7912fb4

Please sign in to comment.