-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LoaderBuilder to make it easier to configure all the different lo…
…aders and normalizers
- Loading branch information
1 parent
80baad4
commit b2405d6
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |