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: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166:
<?php
namespace Nethgui\Module;
class ModuleLoader implements \Nethgui\Module\ModuleSetInterface, \Nethgui\Utility\PhpConsumerInterface, \Nethgui\Log\LogConsumerInterface
{
private $namespaceMap;
private $instanceCache;
private $phpWrapper;
private $objectInjector;
public function __construct($objectInjector)
{
$this->objectInjector = $objectInjector;
$this->namespaceMap = new \ArrayObject();
$this->instanceCache = new \ArrayObject();
$this->phpWrapper = new \Nethgui\Utility\PhpWrapper(__CLASS__);
$this->cacheIsFilled = FALSE;
}
public function setNamespace($nsPrefix, $nsRootPath)
{
$this->namespaceMap[$nsPrefix] = $nsRootPath;
return $this;
}
public function getIterator()
{
if ($this->cacheIsFilled !== TRUE) {
$this->fillCache();
}
return $this->instanceCache->getIterator();
}
private function fillCache()
{
foreach ($this->namespaceMap as $nsPrefix => $nsRootPath) {
if ($nsRootPath === FALSE) {
continue;
}
$path = $nsRootPath . '/' . str_replace('\\', '/', $nsPrefix);
$files = $this->phpWrapper->scandir($path);
if ($files === FALSE) {
throw new \UnexpectedValueException(sprintf("%s: `%s` is not a valid module directory!", get_class($this), $path), 1322649822);
}
foreach ($files as $fileName) {
if (substr($fileName, -4) !== '.php') {
continue;
}
$moduleIdentifier = substr($fileName, 0, -4);
if ( ! isset($this->instanceCache[$moduleIdentifier])) {
$className = $nsPrefix . '\\' . $moduleIdentifier;
$moduleInstance = new $className();
NETHGUI_DEBUG && $this->getLog()->notice(sprintf('%s::fillCache(): Created "%s" instance', get_class($this), $className));
call_user_func($this->objectInjector, $moduleInstance);
$this->instanceCache[$moduleIdentifier] = $moduleInstance;
}
}
}
$this->cacheIsFilled = TRUE;
}
public function getModule($moduleIdentifier)
{
if (isset($this->instanceCache[$moduleIdentifier])) {
return $this->instanceCache[$moduleIdentifier];
}
$nsPrefixList = array_keys(iterator_to_array($this->namespaceMap));
$moduleInstance = NULL;
while ($nsPrefix = array_pop($nsPrefixList)) {
$className = $nsPrefix . '\\' . $moduleIdentifier;
if ($this->phpWrapper->class_exists($className)) {
$moduleInstance = new $className();
NETHGUI_DEBUG && $this->getLog()->notice(sprintf('%s::getModule(): Created "%s" instance', get_class($this), $className));
call_user_func($this->objectInjector, $moduleInstance);
$this->instanceCache[$moduleIdentifier] = $moduleInstance;
break;
}
}
if ($moduleInstance === NULL) {
throw new \RuntimeException(sprintf("%s: `%s` is an unknown module identifier", __CLASS__, $moduleIdentifier), 1322231262);
}
return $moduleInstance;
}
public function setPhpWrapper(\Nethgui\Utility\PhpWrapper $object)
{
$this->phpWrapper = $object;
return $this;
}
public function getLog()
{
return $this->phpWrapper->getLog();
}
public function setLog(\Nethgui\Log\LogInterface $log)
{
$this->phpWrapper->setLog($log);
return $this;
}
}