implement loading models for Controller Components
This commit is contained in:
parent
1ce1c18590
commit
cf18f8b90c
1 changed files with 76 additions and 0 deletions
|
@ -80,6 +80,82 @@
|
|||
return \nre\configs\AppConfig::$app['namespace']."controllers\\components\\$className";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new (Controller) Component.
|
||||
*
|
||||
* @throws \nre\exceptions\DriverNotFoundException
|
||||
* @throws \nre\exceptions\DriverNotValidException
|
||||
* @throws \nre\exceptions\ModelNotValidException
|
||||
* @throws \nre\exceptions\ModelNotFoundException
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
// Load Models
|
||||
$this->loadModels();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Load the Models of this Component.
|
||||
*
|
||||
* @throws \nre\exceptions\DriverNotFoundException
|
||||
* @throws \nre\exceptions\DriverNotValidException
|
||||
* @throws \nre\exceptions\ModelNotValidException
|
||||
* @throws \nre\exceptions\ModelNotFoundException
|
||||
*/
|
||||
protected function loadModels()
|
||||
{
|
||||
// Determine Models
|
||||
$explicit = false;
|
||||
$models = \nre\core\ClassLoader::stripClassType(\nre\core\ClassLoader::stripNamespace(get_class($this)));
|
||||
if(property_exists($this, 'models'))
|
||||
{
|
||||
$models = $this->models;
|
||||
$explicit = true;
|
||||
}
|
||||
if(!is_array($models)) {
|
||||
$models = array($models);
|
||||
}
|
||||
// Models of parent classes
|
||||
$parent = $this;
|
||||
while($parent = get_parent_class($parent))
|
||||
{
|
||||
$properties = get_class_vars($parent);
|
||||
if(array_key_exists('models', $properties)) {
|
||||
$models = array_merge($models, $properties['models']);
|
||||
}
|
||||
}
|
||||
$models = array_unique($models);
|
||||
|
||||
// Load Models
|
||||
foreach($models as &$model)
|
||||
{
|
||||
try {
|
||||
// Load class
|
||||
Model::load($model);
|
||||
|
||||
// Construct Model
|
||||
$modelName = ucfirst(strtolower($model));
|
||||
$this->$modelName = Model::factory($model);
|
||||
}
|
||||
catch(\nre\exceptions\ModelNotValidException $e) {
|
||||
if($explicit) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
catch(\nre\exceptions\ModelNotFoundException $e) {
|
||||
if($explicit) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Add table
Reference in a new issue