implement Components
This commit is contained in:
parent
d4ff4b2c34
commit
3c3b96ef1f
5 changed files with 252 additions and 0 deletions
0
controllers/components/empty
Normal file
0
controllers/components/empty
Normal file
85
core/Component.inc
Normal file
85
core/Component.inc
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NRE
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
* @copyright 2013 coderkun (http://www.coderkun.de)
|
||||||
|
* @license http://www.gnu.org/licenses/gpl.html
|
||||||
|
* @link http://www.coderkun.de/projects/nre
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace nre\core;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract class to implement a (Controller) Component.
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
*/
|
||||||
|
abstract class Component
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the class of a Component.
|
||||||
|
*
|
||||||
|
* @throws ComponentNotFoundException
|
||||||
|
* @throws ComponentNotValidException
|
||||||
|
* @param string $componentName Name of the Component to load
|
||||||
|
*/
|
||||||
|
public static function load($componentName)
|
||||||
|
{
|
||||||
|
// Determine full classname
|
||||||
|
$className = self::getClassName($componentName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Load class
|
||||||
|
ClassLoader::load($className);
|
||||||
|
|
||||||
|
// Validate class
|
||||||
|
ClassLoader::check($className, get_class());
|
||||||
|
}
|
||||||
|
catch(\nre\exceptions\ClassNotValidException $e) {
|
||||||
|
throw new \nre\exceptions\ComponentNotValidException($e->getClassName());
|
||||||
|
}
|
||||||
|
catch(\nre\exceptions\ClassNotFoundException $e) {
|
||||||
|
throw new \nre\exceptions\ComponentNotFoundException($e->getClassName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate a Component (Factory Pattern).
|
||||||
|
*
|
||||||
|
* @param string $componentName Name of the Component to instantiate
|
||||||
|
*/
|
||||||
|
public static function factory($componentName)
|
||||||
|
{
|
||||||
|
// Determine full classname
|
||||||
|
$className = self::getClassName($componentName);
|
||||||
|
|
||||||
|
// Construct and return Controller
|
||||||
|
return new $className();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine the classname for the given Component name.
|
||||||
|
*
|
||||||
|
* @param string $componentName Component name to get classname of
|
||||||
|
* @return string Classname for the Component name
|
||||||
|
*/
|
||||||
|
private static function getClassName($componentName)
|
||||||
|
{
|
||||||
|
$className = \nre\core\ClassLoader::concatClassNames($componentName, \nre\core\ClassLoader::stripNamespace(get_class()));
|
||||||
|
|
||||||
|
|
||||||
|
return \nre\configs\AppConfig::$app['namespace']."controllers\\components\\$className";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -138,6 +138,9 @@
|
||||||
// Store values
|
// Store values
|
||||||
$this->agent = $agent;
|
$this->agent = $agent;
|
||||||
|
|
||||||
|
// Load Components
|
||||||
|
$this->loadComponents();
|
||||||
|
|
||||||
// Load Models
|
// Load Models
|
||||||
$this->loadModels();
|
$this->loadModels();
|
||||||
|
|
||||||
|
|
@ -306,6 +309,36 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load the Components of this Controller.
|
||||||
|
*
|
||||||
|
* @throws ComponentNotValidException
|
||||||
|
* @throws ComponentNotFoundException
|
||||||
|
*/
|
||||||
|
private function loadComponents()
|
||||||
|
{
|
||||||
|
// Determine components
|
||||||
|
$components = array();
|
||||||
|
if(property_exists($this, 'components')) {
|
||||||
|
$components = $this->components;
|
||||||
|
}
|
||||||
|
if(!is_array($components)) {
|
||||||
|
$components = array($components);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load components
|
||||||
|
foreach($components as &$component)
|
||||||
|
{
|
||||||
|
// Load class
|
||||||
|
Component::load($component);
|
||||||
|
|
||||||
|
// Construct component
|
||||||
|
$componentName = ucfirst(strtolower($component));
|
||||||
|
$this->$componentName = Component::factory($component);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the Models of this Controller.
|
* Load the Models of this Controller.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
67
exceptions/ComponentNotFoundException.inc
Normal file
67
exceptions/ComponentNotFoundException.inc
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NRE
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
* @copyright 2013 coderkun (http://www.coderkun.de)
|
||||||
|
* @license http://www.gnu.org/licenses/gpl.html
|
||||||
|
* @link http://www.coderkun.de/projects/nre
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace nre\exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception: Component not found.
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
*/
|
||||||
|
class ComponentNotFoundException extends \nre\exceptions\ClassNotFoundException
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Error code
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
const CODE = 67;
|
||||||
|
/**
|
||||||
|
* Error message
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const MESSAGE = 'component not found';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new exception.
|
||||||
|
*
|
||||||
|
* @param string $componentName Name of the Component that was not found
|
||||||
|
*/
|
||||||
|
function __construct($componentName)
|
||||||
|
{
|
||||||
|
parent::__construct(
|
||||||
|
$componentName,
|
||||||
|
self::MESSAGE,
|
||||||
|
self::CODE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the Component that was not found.
|
||||||
|
*
|
||||||
|
* @return string Name of the Component that was not found
|
||||||
|
*/
|
||||||
|
public function getComponentName()
|
||||||
|
{
|
||||||
|
return $this->getClassName();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
67
exceptions/ComponentNotValidException.inc
Normal file
67
exceptions/ComponentNotValidException.inc
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NRE
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
* @copyright 2013 coderkun (http://www.coderkun.de)
|
||||||
|
* @license http://www.gnu.org/licenses/gpl.html
|
||||||
|
* @link http://www.coderkun.de/projects/nre
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace nre\exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception: Component not valid.
|
||||||
|
*
|
||||||
|
* @author coderkun <olli@coderkun.de>
|
||||||
|
*/
|
||||||
|
class ComponentNotValidException extends \nre\exceptions\ClassNotValidException
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Error code
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
const CODE = 77;
|
||||||
|
/**
|
||||||
|
* Error message
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
const MESSAGE = 'component not valid';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new exception.
|
||||||
|
*
|
||||||
|
* @param string $componentName Name of the invalid Component
|
||||||
|
*/
|
||||||
|
function __construct($componentName)
|
||||||
|
{
|
||||||
|
parent::__construct(
|
||||||
|
$componentName,
|
||||||
|
self::MESSAGE,
|
||||||
|
self::CODE
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the invalid Component.
|
||||||
|
*
|
||||||
|
* @return string Name of the invalid Component
|
||||||
|
*/
|
||||||
|
public function getComponentName()
|
||||||
|
{
|
||||||
|
return $this->getClassName();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue