Permalink
Browse files

Add modules support

  • Loading branch information...
jsifalda committed Feb 20, 2013
1 parent d21b6ce commit 77c17ea2a04876f6f84f87ffe06f223a50912728
@@ -30,6 +30,8 @@ public function __construct($containerClass = 'Flame\DI\Container')
'parent' => $containerClass
)
));
+
+ $this->registerModulesExtension();
}
/**
@@ -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);
+ };
+ }
}
@@ -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();
+ }
+ }
+}
+
@@ -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.