-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 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
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,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(); | ||
} | ||
} | ||
} | ||
|
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,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.'); | ||
} | ||
} | ||
|
||
} |