Skip to content

Commit 77c17ea

Browse files
committed
Add modules support
1 parent d21b6ce commit 77c17ea

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

‎Flame/Config/Configurator.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public function __construct($containerClass = 'Flame\DI\Container')
3030
'parent' => $containerClass
3131
)
3232
));
33+
34+
$this->registerModulesExtension();
3335
}
3436

3537
/**
@@ -44,4 +46,11 @@ protected function createCompiler()
4446
->addExtension('extensions', new Extensions\ExtensionsExtension);
4547
return $compiler;
4648
}
49+
50+
protected function registerModulesExtension()
51+
{
52+
$this->onCompile[] = function ($configurator, $compiler) {
53+
$compiler->addExtension('modules', new \Flame\Config\Extensions\ModulesExtension);
54+
};
55+
}
4756
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* ModuleExtension.php
4+
*
5+
* @author Jiří Šifalda <sifalda.jiri@gmail.com>
6+
* @date 20.02.13
7+
*/
8+
9+
namespace Flame\Config\Extensions;
10+
11+
use Flame\Utils\Strings;
12+
13+
class ModuleExtension extends \Nette\Config\CompilerExtension
14+
{
15+
16+
/** @var array */
17+
private $configFiles = array();
18+
19+
/**
20+
* @param array $configFiles
21+
*/
22+
public function __construct(array $configFiles = array())
23+
{
24+
$this->configFiles = $configFiles;
25+
}
26+
27+
public function loadConfiguration()
28+
{
29+
$builder = $this->getContainerBuilder();
30+
$config = $this->getConfig();
31+
32+
foreach($this->getConfigFiles() as $configFile){
33+
$services = $this->loadFromFile($configFile);
34+
$this->compiler->parseServices($builder, $services);
35+
if(isset($services['parameters'])){
36+
$builder->parameters = \Nette\Config\Helpers::merge($builder->parameters, $services['parameters']);
37+
}
38+
}
39+
40+
}
41+
42+
/**
43+
* @return array
44+
*/
45+
protected function getConfigFiles()
46+
{
47+
if(count($this->configFiles)){
48+
return array_map(function ($config) {
49+
if(is_array($config))
50+
$config = array_shift($config);
51+
return $config;
52+
}, $this->configFiles);
53+
}else{
54+
return array();
55+
}
56+
}
57+
}
58+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* ModuleExtension.php
4+
*
5+
* @author Jiří Šifalda <sifalda.jiri@gmail.com>
6+
* @date 19.02.13
7+
*/
8+
9+
namespace Flame\Config\Extensions;
10+
11+
class ModulesExtension extends \Nette\Config\CompilerExtension
12+
{
13+
14+
public function loadConfiguration()
15+
{
16+
$config = $this->getConfig();
17+
18+
if(is_array($config)){
19+
if(count($config)){
20+
foreach($config as $name => $extension){
21+
if(!is_string($name))
22+
$name = $this->getExtensionName($extension);
23+
$this->compiler->addExtension($name, $this->invokeExtension($extension));
24+
}
25+
}
26+
}else{
27+
throw new \Nette\InvalidStateException('Modules configuration must be array.' . gettype($config) . ' given');
28+
}
29+
}
30+
31+
/**
32+
* @param $class
33+
* @return string
34+
*/
35+
protected function getExtensionName($class)
36+
{
37+
$ref = \Nette\Reflection\ClassType::from($class);
38+
return $ref->getShortName();
39+
}
40+
41+
/**
42+
* @param $class
43+
* @return mixed
44+
* @throws \Nette\InvalidStateException
45+
*/
46+
protected function invokeExtension($class)
47+
{
48+
$builder = $this->getContainerBuilder();
49+
if(is_object($class)){
50+
$ref = new \ReflectionClass($class->value);
51+
return $ref->newInstance(property_exists($class, 'attributes') ? $builder->expand($class->attributes) : array());
52+
}elseif(is_string($class)){
53+
return new $class;
54+
}else{
55+
throw new \Nette\InvalidStateException('Definition of extension must be valid class (string or object). ' . gettype($class) . ' given.');
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)