1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<?php
namespace oroboros\core\traits\utilities;
trait LoaderTrait {
private $_LoaderTrait_loader_initialized = FALSE;
private $_LoaderTrait_factory;
private $_LoaderTrait_allowed_types = array();
private $_LoaderTrait_loader_types = array(
"model",
"view",
"controller",
"library",
"module",
"template",
"theme",
"app"
);
protected function _initializeLoader() {
$this->_LoaderTrait_factory = new \oroboros\core\patterns\creational\FactoryFactory();
$this->_LoaderTrait_factory->initialize();
$this->_LoaderTrait_loader_initialized = TRUE;
}
protected function _setAllowedLoaderTypes(array $types = array()) {
if ($this->_LoaderTrait_loader_initialized) {
throw new \oroboros\core\utilities\exception\Exception(
"[" . get_class($this) . "] Allowed loader type declaration must occur before initialization.",
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_INITIALIZATION);
}
foreach ($types as $type) {
if (!in_array($type, $this->_LoaderTrait_loader_types)) {
throw new \oroboros\core\utilities\exception\Exception(
"Invalid loader type [" . (string) $type . "]",
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS);
}
}
$this->_LoaderTrait_allowed_types = $types;
}
protected function _load($type, $subtype, $resource = FALSE, array $params = array(), array $flags = array()) {
if (!in_array($type, $this->_LoaderTrait_allowed_types)) {
throw new \oroboros\core\utilities\exception\Exception(
"Loading of type [" . $type . "] is not allowed for class: [" . __CLASS__ . "]",
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_LOGIC_BAD_PARAMETERS);
}
try {
$factory = $this->_LoaderTrait_factory->load($type);
$class = $factory->fetch($subtype, $resource, $params, $flags);
return $class;
} catch (\Exception $e) {
throw new \oroboros\core\utilities\exception\Exception(
"Error during load sequence for " . (($resource) ? '[' .$subtype . ']::[' . $type . ']::[' . $resource . ']:' : "[" . $type . "]::[" . $subtype . "]:") . " " . $e->getMessage(),
\oroboros\core\interfaces\api\ExceptionCodeApi::ERROR_INITIALIZATION, $e);
}
}
}