This commit is contained in:
Dennis Eichhorn 2019-04-14 18:57:11 +02:00
parent 34c2673504
commit 26a13b1276
3 changed files with 112 additions and 178 deletions

View File

@ -1,132 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package phpOMS\Module
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\Module;
use phpOMS\ApplicationAbstract;
use phpOMS\Autoloader;
/**
* ModuleFactory class.
*
* Responsible for initializing modules as singletons
*
* @package phpOMS\Module
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
final class ModuleFactory
{
/**
* Module instances.
*
* Reference to module.class
*
* @var ModuleAbstract[]
* @since 1.0.0
*/
public static $loaded = [];
/**
* Unassigned providing.
*
* @var string[][]
* @since 1.0.0
*/
public static $providing = [];
/**
* Constructor.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Gets and initializes modules.
*
* @param string $module Module ID
* @param ApplicationAbstract $app Application
*
* @return ModuleAbstract
*
* @since 1.0.0
*/
public static function getInstance(string $module, ApplicationAbstract $app) : ModuleAbstract
{
$class = '\\Modules\\' . $module . '\\Controller\\' . $app->appName . 'Controller';
if (!isset(self::$loaded[$module])) {
if (Autoloader::exists($class) !== false) {
try {
$obj = new $class($app);
self::$loaded[$module] = $obj;
self::registerRequesting($obj);
self::registerProvided($obj);
} catch (\Throwable $e) {
self::$loaded[$module] = new NullModule();
}
} else {
self::$loaded[$module] = new NullModule();
}
}
return self::$loaded[$module];
}
/**
* Load modules this module is requesting from
*
* @param ModuleAbstract $obj Current module
*
* @return void
*
* @since 1.0.0
*/
private static function registerRequesting(ModuleAbstract $obj) : void
{
$providings = $obj->getProviding();
foreach ($providings as $providing) {
if (isset(self::$loaded[$providing])) {
self::$loaded[$providing]->addReceiving($obj->getName());
} else {
self::$providing[$providing][] = $obj->getName();
}
}
}
/**
* Register modules this module is receiving from
*
* @param ModuleAbstract $obj Current module
*
* @return void
*
* @since 1.0.0
*/
private static function registerProvided(ModuleAbstract $obj) : void
{
$name = $obj->getName();
if (isset(self::$providing[$name])) {
foreach (self::$providing[$name] as $providing) {
$obj->addReceiving($providing);
}
}
}
}

View File

@ -43,6 +43,16 @@ final class ModuleManager
*/
private $running = [];
/**
* All modules another module is providing for.
*
* This is important to inform other moduels what kind of information they can receive from other modules.
*
* @var \phpOMS\Module\ModuleAbstract[]
* @since 1.0.0
*/
private $providing = [];
/**
* Application instance.
*
@ -186,9 +196,11 @@ final class ModuleManager
// throw new PathException($path);
}
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$this->active[$json['name']['internal']] = $json === false ? [] : $json;
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$name = $this->generateModuleName($json['name']['internal']);
$this->active[$name] = $json === false ? [] : $json;
}
}
@ -207,7 +219,7 @@ final class ModuleManager
*/
public function isActive(string $module) : bool
{
return isset($this->getActiveModules(false)[$module]);
return isset($this->getActiveModules(false)[$this->generateModuleName($module)]);
}
/**
@ -232,9 +244,11 @@ final class ModuleManager
// throw new PathException($path);
}
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$this->all[$json['name']['internal']] = $json === false ? [] : $json;
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$name = $this->generateModuleName($json['name']['internal']);
$this->all[$name] = $json === false ? [] : $json;
}
}
@ -281,9 +295,11 @@ final class ModuleManager
// throw new PathException($path);
}
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$this->installed[$json['name']['internal']] = $json === false ? [] : $json;
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$name = $this->generateModuleName($json['name']['internal']);
$this->installed[$name] = $json === false ? [] : $json;
}
}
@ -325,8 +341,9 @@ final class ModuleManager
public function deactivate(string $module) : bool
{
$installed = $this->getInstalledModules(false);
$name = $this->generateModuleName($module);
if (!isset($installed[$module])) {
if (!isset($installed[$name])) {
return false;
}
@ -376,8 +393,9 @@ final class ModuleManager
public function activate(string $module) : bool
{
$installed = $this->getInstalledModules(false);
$name = $this->generateModuleName($module);
if (!isset($installed[$module])) {
if (!isset($installed[$name])) {
return false;
}
@ -641,10 +659,91 @@ final class ModuleManager
*/
private function initModuleController(string $module) : void
{
$this->running[$module] = ModuleFactory::getInstance($module, $this->app);
$this->running[$module] = $this->getModuleInstance($module);
$this->app->dispatcher->set($this->running[$module], '\Modules\\Controller\\' . $module . '\\' . $this->app->appName . 'Controller');
}
private function generateModuleName(string $module) : string
{
return '\\Modules\\' . $module . '\\Controller\\' . $this->app->appName . 'Controller';
}
/**
* Gets and initializes modules.
*
* @param string $module Module ID
*
* @return ModuleAbstract
*
* @since 1.0.0
*/
public function getModuleInstance(string $module) : ModuleAbstract
{
$class = '\\Modules\\' . $module . '\\Controller\\' . $this->app->appName . 'Controller';
$name = $this->generateModuleName($module);
if (!isset($this->running[$class])) {
if (Autoloader::exists($class) !== false) {
try {
$obj = new $class($this->app);
$this->running[$name] = $obj;
self::registerRequesting($obj);
self::registerProvided($obj);
} catch (\Throwable $e) {
$this->running[$name] = new NullModule();
}
} else {
$this->running[$name] = new NullModule();
}
}
return $this->running[$name];
}
/**
* Load modules this module is requesting from
*
* @param ModuleAbstract $obj Current module
*
* @return void
*
* @since 1.0.0
*/
private function registerRequesting(ModuleAbstract $obj) : void
{
$providings = $obj->getProviding();
$name = '';
foreach ($providings as $providing) {
$name = $this->generateModuleName($providing);
if (isset($this->running[$name])) {
$this->running[$name]->addReceiving($obj->getName());
} else {
$this->providing[$name][] = $obj->getName();
}
}
}
/**
* Register modules this module is receiving from
*
* @param ModuleAbstract $obj Current module
*
* @return void
*
* @since 1.0.0
*/
private function registerProvided(ModuleAbstract $obj) : void
{
$name = $this->generateModuleName($obj->getName());
if (isset($this->providing[$name])) {
foreach ($this->providing[$name] as $providing) {
$obj->addReceiving($providing);
}
}
}
/**
* Initialize all modules for a request.
*

View File

@ -1,33 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\Module;
require_once __DIR__ . '/../Autoloader.php';
use phpOMS\ApplicationAbstract;
use phpOMS\Module\ModuleFactory;
use phpOMS\Module\NullModule;
class ModuleFactoryTest extends \PHPUnit\Framework\TestCase
{
public function testFactory() : void
{
$instance = NullModule::class;
if (\file_exists(__DIR__ . '/../../../Modules')) {
$instance = \Modules\Admin\Controller\ApiController::class;
}
self::assertInstanceOf($instance, ModuleFactory::getInstance('Admin', new class extends ApplicationAbstract { protected $appName = 'Api'; }));
}
}