Skip to content

Commit

Permalink
Add LoaderBuilder to make it easier to configure all the different lo…
Browse files Browse the repository at this point in the history
…aders and normalizers
  • Loading branch information
henrikbjorn committed Jul 28, 2014
1 parent 80baad4 commit b2405d6
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions src/LoaderBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Tacker;

use Symfony\Component\Config\FileLocator;
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\PhpFileLoader;
use Tacker\Loader\YamlFileLoader;
use Tacker\Normalizer\ChainNormalizer;
use Tacker\Normalizer\EnvfileNormalizer;
use Tacker\Normalizer\EnvironmentNormalizer;

/**
* Helps building a Loader with Cache, Normalization etc.
*
* @package Tacker
*/
final class LoaderBuilder
{
private $paths;
private $normalizer;
private $resolver;
private $locator;
private $debug;
private $cacheDir;
private $normalizerConfigured = false;
private $resolverConfigured = false;

public function __construct(array $paths, $cacheDir = null, $debug = false)
{
$this->cacheDir = $cacheDir;
$this->debug = $debug;
$this->locator = new FileLocator($paths);
$this->normalizer = new ChainNormalizer;
$this->resolver = new LoaderResolver;
$this->resources = new ResourceCollection;
}

public static function create(array $paths, $cacheDir = null, $debug = false)
{
return new self($paths, $cacheDir, $debug);
}

public function configureNormalizers(callable $callable)
{
$this->normalizerConfigured = true;

$callable($this->normalizer);
}

public function configureLoaders(callable $callable)
{
$this->resolverConfigured = true;

$callable($this->resolver, $this->locator, $this->resources);
}

public function setCacheDir($cacheDir)
{
$this->cacheDir = $cacheDir;
}

public function setDebug($debug)
{
$this->debug = $debug;
}

public function build()
{
if (false == $this->resolverConfigured) {
$this->addDefaultLoaders();
}

if (false == $this->normalizerConfigured) {
$this->addDefaultNormalizers();
}

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

return $loader;
}

public function addDefaultLoaders()
{
$this->resolverConfigured = true;

if (class_exists('Symfony\Component\Yaml\Yaml')) {
$this->resolver->addLoader(new YamlFileLoader($this->locator, $this->resources));
}

$this->resolver->addLoader(new JsonFileLoader($this->locator, $this->resources));
$this->resolver->addLoader(new PhpFileLoader($this->locator, $this->resources));
$this->resolver->addLoader(new IniFileLoader($this->locator, $this->resources));
}

public function addDefaultNormalizers()
{
$this->normalizerConfigured = true;

$this->normalizer->add(new EnvironmentNormalizer);
$this->normalizer->add(new EnvfileNormalizer($this->locator));
}

private function createNormalizerLoader()
{
return new NormalizerLoader(new DelegatingLoader($this->resolver), $this->normalizer);
}
}

0 comments on commit b2405d6

Please sign in to comment.