Commit 3e350c00 authored by Jordan Breton's avatar Jordan Breton

WIP modules (removed load and templates necessity for models, commandHandlers...

WIP modules (removed load and templates necessity for models, commandHandlers and domainEventsListeners)
parent 02250247
<?php
use wfw\engine\core\conf\IModuleDescriptor;
use wfw\engine\core\conf\WFWModulesCollector;
WFWModulesCollector::collectModules();
return array_merge(
require dirname(__DIR__,3)."/engine/config/default.command.handlers.php",
require dirname(__DIR__)."/site.command.handlers.php",
...array_map(
function($module){
if(is_a((string) $module, IModuleDescriptor::class, true)){
/** @var IModuleDescriptor $module */
return $module::commandHandlers();
}else return [];
}, WFWModulesCollector::modules()
)
);
\ No newline at end of file
<?php
use wfw\engine\core\conf\IModuleDescriptor;
use wfw\engine\core\conf\WFWModulesCollector;
WFWModulesCollector::collectModules();
return array_merge(
require dirname(__DIR__,3)."/engine/config/default.domain_events.listeners.php",
require dirname(__DIR__)."/site.domain_events.listeners.php",
...array_map(
function($module){
if(is_a((string) $module, IModuleDescriptor::class, true)){
/** @var IModuleDescriptor $module */
return $module::domainEventListeners();
}else return [];
}, WFWModulesCollector::modules()
)
);
\ No newline at end of file
<?php
use wfw\engine\core\conf\IModuleDescriptor;
use wfw\engine\core\conf\WFWModulesCollector;
WFWModulesCollector::collectModules();
return array_merge(
require dirname(__DIR__,3)."/engine/config/default.models.php",
require dirname(__DIR__)."/site.models.php",
...array_map(
function($module){
if(is_a((string) $module, IModuleDescriptor::class, true)){
/** @var IModuleDescriptor $module */
return $module::models();
}else return [];
}, WFWModulesCollector::modules()
)
);
\ No newline at end of file
<?php
return [];
\ No newline at end of file
<?php
return [];
\ No newline at end of file
......@@ -12,6 +12,7 @@ use wfw\daemons\modelSupervisor\server\MSServerPool;
use wfw\daemons\modelSupervisor\server\requestHandler\MSServerRequestHandlerManager;
use wfw\daemons\modelSupervisor\socket\protocol\MSServerSocketProtocol;
use wfw\engine\core\conf\WFWModulesCollector;
use wfw\engine\core\data\DBAccess\NOSQLDB\kvs\KVSAccess;
use wfw\engine\core\data\model\loaders\KVStoreBasedModelLoader;
use wfw\engine\lib\cli\argv\ArgvOpt;
......@@ -65,6 +66,8 @@ try{
if(!is_null($pPath = $confs->getProjectPath($name)))
(new Autoloader([],$pPath))->register(false,true);
WFWModulesCollector::collectModules();
if($oldPID){
sleep(10);//ugly but let the time for childs to die;...
if(!isset($restarts[$name])) $restarts[$name]=[];
......@@ -102,7 +105,7 @@ try{
$confs->getKVSContainer($name),
$confs->getKVSDefaultStorage($name) ?? null
),
require $confs->getModelsToLoadPath($name)
WFWModulesCollector::models()
),
$confs->getUsers($name),
$confs->getGroups($name),
......
......@@ -29,7 +29,6 @@
"password": "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
}
},
"models_to_load_path": "{ROOT}\/engine\/config\/default.models.php",
"request_ttl": 900,
"session_ttl": 900,
"shutdown_on_error": false,
......
......@@ -587,6 +587,8 @@ class DefaultContext implements IWebAppContext {
//TODO : implements
}
/**
* @return IErrorHandler Gestionnaire d'erreurs.
*/
......
......@@ -21,4 +21,11 @@ interface IAppModulesCollector {
* @param string ...$modules List of class that implements IModuleDescriptor interface.
*/
public static function registerModules(string ...$modules):void;
/**
* @param string[] $modules List of class modules saved in cache. Must prevent the collectModules
* method to perform a slow task to retrieve all modules.
* [Class => dirname]
*/
public static function restoreModulesFromCache(array $modules):void;
}
\ No newline at end of file
......@@ -7,6 +7,8 @@ namespace wfw\engine\core\conf;
* if new methods are added to the interface.
*/
abstract class ModuleDescriptor implements IModuleDescriptor{
protected static $_confTemplate = "*conf.json";
protected static $_langTemplate = "*.lang.json";
/**
* @return array Dependency injection rules to add to the general DI
......@@ -15,12 +17,16 @@ abstract class ModuleDescriptor implements IModuleDescriptor{
return [];
}
/**
* @return string[] List of file confs needed by the module.
* Confs applyance order is engine -> modules -> project
*/
/**
* @return string[] List of file confs needed by the module.
* Confs applyance order is engine -> modules -> project
* @throws \ReflectionException
*/
public static function confs(): array {
return [];
$confs = [];
$root = static::root();
exec("find \"$root\" -name \"".static::$_confTemplate."\" -type f | sort");
return $confs;
}
/**
......@@ -37,11 +43,15 @@ abstract class ModuleDescriptor implements IModuleDescriptor{
return [];
}
/**
* @return string[] List of translation files.
*/
/**
* @return string[] List of translation files.
* @throws \ReflectionException
*/
public static function langs(): array {
return [];
$langs = [];
$root = static::root();
exec("find \"$root\" -name \"".static::$_langTemplate."\" -type f | sort");
return $langs;
}
/**
......
......@@ -11,33 +11,54 @@ final class WFWModulesCollector extends ModuleDescriptor implements IAppModulesC
private static $_modules = [];
private static $_collected = false;
/**
* @return array
*/
public static function confs(): array {
$projectFolder = dirname(__DIR__,3);
$engineFile = ["$projectFolder/engine/config/conf.json"];
$siteFile = ["$projectFolder/site/config/conf.json"];
return array_merge(
array_merge(
$engineFile,
...array_map(function($module){
/** @var IModuleDescriptor $module */
return $module::confs();
},
self::$_modules
)
),
$siteFile
);
}
/**
* @param array $langs
* @return array
*/
public static function langs(?array $langs=null): array {
$langs = is_array($langs) ? array_flip($langs) : null;
$engineFiles = array_diff(
scandir(dirname(__DIR__,2)."/config/lang"),
['.','..']
);
$siteFiles = [];
if(is_dir(dirname(__DIR__,3).'/site/config/lang')){
$siteFiles = array_diff(
scandir(dirname(__DIR__,3)."/site/config/lang"),
['.','..']
$files = [];
$projectFolder = dirname(__DIR__,3);
foreach(["engine","site"] as $folder){
$files[$folder] = [];
exec(
"find \"$projectFolder/$folder\" -name *.lang.json -type f | sort",
$files[$folder]
);
}
$indexedFiles = [];
$files = array_merge(
array_merge($files["engine"],...array_map(
function($module){
/** @var IModuleDescriptor $module */
return $module::langs();
},self::$_modules
)),
$files["site"]
);
array_filter(
array_merge(
array_map(function($path){
return dirname(__DIR__,2)."/config/lang/$path";
},$engineFiles),
array_map(function($path){
return dirname(__DIR__,3)."/site/config/lang/$path";
},$siteFiles)
),
$files,
function($path) use ($langs,&$indexedFiles):bool{
$index = explode(".",basename($path))[0];
if(is_null($langs) || isset($langs[$index])){
......@@ -66,13 +87,90 @@ final class WFWModulesCollector extends ModuleDescriptor implements IAppModulesC
);
}
/**
* @return array
*/
public static function di(): array {
return array_merge_recursive(...array_map(function($module){
/** @var IModuleDescriptor $module */
return $module::di();
},self::$_modules));
}
/**
* @return array
*/
public static function commandHandlers(): array {
$projectFolder = dirname(__DIR__,3);
$siteFile = "$projectFolder/site/config/site.command.handlers.php";
$engineFile = "$projectFolder/engine/config/default.command.handlers.php";
$site = [];
$engine = [];
if(file_exists($siteFile)) $site = require $siteFile;
if(file_exists($engineFile)) $engine = require $engineFile;
return array_merge_recursive(
array_merge_recursive(
$engine,
...array_map(function($module){
/** @var IModuleDescriptor $module */
return $module::domainEventListeners();
},self::$_modules)),
$site
);
}
/**
* @return array
*/
public static function domainEventListeners(): array {
$projectFolder = dirname(__DIR__,3);
$siteFile = "$projectFolder/site/config/site.domain_events.listeners.php";
$engineFile = "$projectFolder/engine/config/default.domain_events.listeners.php";
$site = [];
$engine = [];
if(file_exists($siteFile)) $site = require $siteFile;
if(file_exists($engineFile)) $engine = require $engineFile;
return array_merge_recursive(
array_merge_recursive(
$engine,
...array_map(function($module){
/** @var IModuleDescriptor $module */
return $module::domainEventListeners();
},self::$_modules)),
$site
);
}
/**
* @return array
*/
public static function models(): array {
$projectFolder = dirname(__DIR__,3);
$siteFile = "$projectFolder/site/config/site.models.php";
$engineFile = "$projectFolder/engine/config/default.models.php";
$site = [];
$engine = [];
if(file_exists($siteFile)) $site = require $siteFile;
if(file_exists($engineFile)) $engine = require $engineFile;
return array_merge(
array_merge(
$engine,
...array_map(function($module){
/** @var IModuleDescriptor $module */
return $module::models();
},self::$_modules)),
$site
);
}
/**
* @param string ...$modules List of class that implements IModuleDescriptor interface.
*/
public static function registerModules(string ...$modules): void {
foreach($modules as $module){
if(is_a($module,IModuleDescriptor::class)){
self::$_modules[] = $module;
/** @var IModuleDescriptor $module */
self::$_modules[(string)$module] = $module::root();
}else throw new \InvalidArgumentException(
"$module doesn't implements ".IModuleDescriptor::class
);
......@@ -87,7 +185,7 @@ final class WFWModulesCollector extends ModuleDescriptor implements IAppModulesC
if(!self::$_collected){
$dir = dirname(__DIR__,3);
$out = [];
exec("find \"$dir\" -f -name \"$fileName\"",$out);
exec("find \"$dir\" -type f -name \"*$fileName\" | sort",$out);
foreach($out as $file) require_once $file;
}
}
......@@ -98,4 +196,14 @@ final class WFWModulesCollector extends ModuleDescriptor implements IAppModulesC
public static function modules(): array {
return self::$_modules;
}
/**
* @param string[] $modules List of class modules saved in cache. Must prevent the collectModules
* method to perform a slow task to retrieve all modules.
* [Class => dirname]
*/
public static function restoreModulesFromCache(array $modules): void {
self::$_modules = $modules;
self::$_collected = true;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment