Skip to content

Commit

Permalink
Add modules support
Browse files Browse the repository at this point in the history
  • Loading branch information
jsifalda committed Feb 20, 2013
1 parent d21b6ce commit 77c17ea
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Flame/Config/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function __construct($containerClass = 'Flame\DI\Container')
'parent' => $containerClass
)
));

$this->registerModulesExtension();
}

/**
Expand All @@ -44,4 +46,11 @@ protected function createCompiler()
->addExtension('extensions', new Extensions\ExtensionsExtension);
return $compiler;
}

protected function registerModulesExtension()
{
$this->onCompile[] = function ($configurator, $compiler) {
$compiler->addExtension('modules', new \Flame\Config\Extensions\ModulesExtension);
};
}
}
58 changes: 58 additions & 0 deletions Flame/Config/Extensions/ModuleExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* ModuleExtension.php
*
* @author Jiří Šifalda <sifalda.jiri@gmail.com>
* @date 20.02.13
*/

namespace Flame\Config\Extensions;

use Flame\Utils\Strings;

class ModuleExtension extends \Nette\Config\CompilerExtension
{

/** @var array */
private $configFiles = array();

/**
* @param array $configFiles
*/
public function __construct(array $configFiles = array())
{
$this->configFiles = $configFiles;
}

public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = $this->getConfig();

foreach($this->getConfigFiles() as $configFile){
$services = $this->loadFromFile($configFile);
$this->compiler->parseServices($builder, $services);
if(isset($services['parameters'])){
$builder->parameters = \Nette\Config\Helpers::merge($builder->parameters, $services['parameters']);
}
}

}

/**
* @return array
*/
protected function getConfigFiles()
{
if(count($this->configFiles)){
return array_map(function ($config) {
if(is_array($config))
$config = array_shift($config);
return $config;
}, $this->configFiles);
}else{
return array();
}
}
}

59 changes: 59 additions & 0 deletions Flame/Config/Extensions/ModulesExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* ModuleExtension.php
*
* @author Jiří Šifalda <sifalda.jiri@gmail.com>
* @date 19.02.13
*/

namespace Flame\Config\Extensions;

class ModulesExtension extends \Nette\Config\CompilerExtension
{

public function loadConfiguration()
{
$config = $this->getConfig();

if(is_array($config)){
if(count($config)){
foreach($config as $name => $extension){
if(!is_string($name))
$name = $this->getExtensionName($extension);
$this->compiler->addExtension($name, $this->invokeExtension($extension));
}
}
}else{
throw new \Nette\InvalidStateException('Modules configuration must be array.' . gettype($config) . ' given');
}
}

/**
* @param $class
* @return string
*/
protected function getExtensionName($class)
{
$ref = \Nette\Reflection\ClassType::from($class);
return $ref->getShortName();
}

/**
* @param $class
* @return mixed
* @throws \Nette\InvalidStateException
*/
protected function invokeExtension($class)
{
$builder = $this->getContainerBuilder();
if(is_object($class)){
$ref = new \ReflectionClass($class->value);
return $ref->newInstance(property_exists($class, 'attributes') ? $builder->expand($class->attributes) : array());
}elseif(is_string($class)){
return new $class;
}else{
throw new \Nette\InvalidStateException('Definition of extension must be valid class (string or object). ' . gettype($class) . ' given.');
}
}

}

0 comments on commit 77c17ea

Please sign in to comment.