Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 0d2a2a7

Browse files
committed
ENH: refs #255. Add component loader class (just like our ModelLoader)
1 parent c205c9e commit 0d2a2a7

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

core/ComponentLoader.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
13+
/**
14+
* \class MIDAS_ComponentLoader
15+
* \brief Create component object
16+
*/
17+
class MIDAS_ComponentLoader
18+
{
19+
/**
20+
* \fn public loadComponents()
21+
* \brief Loads components (array or string)
22+
*/
23+
public function loadComponents($components, $module = '')
24+
{
25+
if(is_string($components))
26+
{
27+
$this->loadComponent($components, $module);
28+
}
29+
elseif(is_array($components))
30+
{
31+
foreach($components as $component)
32+
{
33+
$this->loadComponent($component, $module);
34+
}
35+
}
36+
}
37+
38+
/**
39+
* \fn public loadComponent()
40+
* \brief Loads a component
41+
*/
42+
public function loadComponent($component, $module = '')
43+
{
44+
$components = Zend_Registry::get('components');
45+
if(!isset($components[$module.$component]))
46+
{
47+
if($module == '')
48+
{
49+
include_once BASE_PATH.'/core/controllers/components/'.$component.'Component.php';
50+
$name = $component . 'Component';
51+
}
52+
else
53+
{
54+
include_once BASE_PATH.'/modules/'.$module.'/controllers/components/'.$component.'Component.php';
55+
$name = ucfirst($module).'_'.$component.'Component';
56+
}
57+
if(class_exists($name))
58+
{
59+
$components[$module.$component] = new $name;
60+
Zend_Registry::set('components', $components);
61+
}
62+
else
63+
{
64+
throw new Zend_Exception('Unable to load class '.$name);
65+
}
66+
}
67+
return $components[$module.$component];
68+
}
69+
}

core/include.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
require_once BASE_PATH . '/core/models/MIDASDatabasePdo.php';
2323
require_once BASE_PATH . '/core/models/MIDASDatabaseCassandra.php';
2424
require_once BASE_PATH . '/core/models/MIDASDatabaseMongo.php';
25-
require_once BASE_PATH . '/core/models//GlobalDao.php';
25+
require_once BASE_PATH . '/core/models/GlobalDao.php';
2626

2727
include_once BASE_PATH . '/core/constant/datatype.php';
2828
require_once BASE_PATH . '/core/models/ModelLoader.php';
29+
require_once BASE_PATH . '/core/ComponentLoader.php';
2930

3031
require_once BASE_PATH.'/core/AppController.php';
3132
require_once BASE_PATH.'/core/AppComponent.php';

core/models/ModelLoader.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public function loadModel($model, $module = '')
4545
$models = Zend_Registry::get('models');
4646
if(!isset($models[$module.$model]))
4747
{
48-
if($module == "")
48+
if($module == '')
4949
{
50-
include_once BASE_PATH."/core/models/" . $databaseType."/".$model.'Model.php';
50+
include_once BASE_PATH.'/core/models/'.$databaseType.'/'.$model.'Model.php';
5151
$name = $model . 'Model';
5252
}
5353
else
5454
{
55-
include_once BASE_PATH."/modules/".$module."/models/" . $databaseType."/".$model.'Model.php';
56-
$name = ucfirst($module).'_'.$model . 'Model';
55+
include_once BASE_PATH.'/modules/'.$module.'/models/'.$databaseType.'/'.$model.'Model.php';
56+
$name = ucfirst($module).'_'.$model.'Model';
5757
}
5858
if(class_exists($name))
5959
{
@@ -62,7 +62,7 @@ public function loadModel($model, $module = '')
6262
}
6363
else
6464
{
65-
throw new Zend_Exception('Unable to load class ' . $name);
65+
throw new Zend_Exception('Unable to load class '.$name);
6666
}
6767
}
6868
return $models[$module.$model];

0 commit comments

Comments
 (0)