Commit fa09a993 authored by Jordan Breton's avatar Jordan Breton

Default context splited to keep the base context that doesn't depends of Web components.

parent 53ad72a1
......@@ -3,7 +3,7 @@
namespace wfw\cli\tester\contexts;
use Dice\Dice;
use wfw\engine\core\app\context\DefaultContext;
use wfw\engine\core\app\context\WebAppContext;
use wfw\engine\core\app\context\IWebAppContext;
use wfw\engine\core\app\WebApp;
use wfw\engine\core\security\rules\RequireAuthentification;
......@@ -20,7 +20,7 @@ use wfw\engine\package\users\domain\types\Client;
* propriété _dice dans laquelle une instance de Dice est présente avec une méthode create(string class,array $args)
*/
class DefaultTestsEnvironment implements ITestsEnvironment {
/** @var null|DefaultContext $_context */
/** @var null|WebAppContext $_context */
private $_context;
/** @var Dice $_dice */
private $_dice;
......@@ -49,11 +49,12 @@ class DefaultTestsEnvironment implements ITestsEnvironment {
/**
* Crée un IWebAppContext
*
* @param array $args
* @return DefaultContext
* @return WebAppContext
*/
private function createWebAppContext(array $args = []):IWebAppContext{
$class = $args["context"] ?? DefaultContext::class;
$class = $args["context"] ?? WebAppContext::class;
if(!is_a($class,IWebAppContext::class,true))
throw new \InvalidArgumentException("$class doesn't implements ".IWebAppContext::class);
return new $class(...$this->initContextArgs($args));
......
......@@ -4,6 +4,7 @@ namespace wfw\engine\core\app;
use wfw\engine\core\action\errors\ActionHandlerNotEnabled;
use wfw\engine\core\action\errors\ActionResolutionFailure;
use wfw\engine\core\notifier\Message;
use wfw\engine\core\response\errors\ResponseHandlerNotEnabled;
use wfw\engine\core\response\responses\ErrorResponse;
use wfw\engine\core\response\errors\ResponseResolutionFailure;
use wfw\engine\core\response\responses\StaticResponse;
......@@ -34,8 +35,7 @@ final class WebApp {
$session = $this->_context->getSession();
$session->start();
$cache = $this->_context->getCacheSystem();
$cacheKey = $this->_context::CACHE_KEYS[$this->_context::VIEWS]
."/".$action->getLang()."::".$action->getRequest()->getURI();
$cacheKey = $this->_context::VIEWS."/".$action->getLang()."::".$action->getRequest()->getURI();
if( !$session->isLogged()
&& $action->getRequest()->getMethod() === "GET"
......@@ -107,6 +107,12 @@ final class WebApp {
$responseRouter = $this->_context->getResponseRouter();
try{
$handler = $responseRouter->findResponseHandler($action,$response);
}catch(ResponseHandlerNotEnabled $e){
$response = new ErrorResponse(
HTTPStatus::FORBIDDEN,
$translator->get("server/engine/core/app/DISABLED_MODULE")
);
$handler = $responseRouter->findResponseHandler($action,$response);
}catch(ResponseResolutionFailure $e){
$response = new ErrorResponse(
HTTPStatus::NOT_FOUND,
......
This diff is collapsed.
......@@ -5,7 +5,7 @@ namespace wfw\engine\core\app\context;
use wfw\engine\core\cache\ICacheSystem;
use wfw\engine\core\conf\IConf;
use wfw\engine\core\lang\ITranslator;
use wfw\engine\core\security\IAccessControlCenter;
use wfw\engine\core\notifier\INotifier;
/**
* wfw general app context interface
......@@ -13,6 +13,13 @@ use wfw\engine\core\security\IAccessControlCenter;
interface IAppContext {
public const CONF="WFW/WebApp/Confs";
public const TRANSLATOR = "WFW/WebApp/Translator";
public const DOMAIN_EVENT_LISTENERS = "WFW/WebApp/DomainEventListeners";
public const LANGS = "WFW/WebApp/Langs";
public const COMMAND_HANDLERS = "WFW/WebApp/CommandHandlers";
public const MODULES = "WFW/WebApp/Modules";
public const COMMAND_RULES = "WFW/WebApp/CommandRules";
public const QUERY_RULES = "WFW/WebApp/QueryRules";
public const CONF_FILES = "WFW/WebApp/ConfFiles";
/**
* @return ICacheSystem Système de cache de l'application.
......@@ -25,14 +32,14 @@ interface IAppContext {
public function getConf():IConf;
/**
* @return IAccessControlCenter Retourne le centre de contrôle des accés.
* @return ITranslator Gestionnaire de langues.
*/
public function getAccessControlCenter():IAccessControlCenter;
public function getTranslator():ITranslator;
/**
* @return ITranslator Gestionnaire de langues.
* @return INotifier Notifier
*/
public function getTranslator():ITranslator;
public function getNotifier(): INotifier;
/**
* Called by the app just before closing
......
......@@ -19,41 +19,11 @@ use wfw\engine\core\view\ILayoutResolver;
/**
* Contexte de l'application web.
*/
interface IWebAppContext {
public const CONF="CONF";
public const CONF_FILES="CONF_FILES";
public const TRANSLATOR = "TRANSLATOR";
public const DOMAIN_EVENT_LISTENERS = "DOMAIN_EVENT_LISTENERS";
public const COMMAND_HANDLERS = "COMMAND_HANDLERS";
public const ROUTER="ROUTER";
public const VIEWS="VIEWS";
public const LANGS="LANGS";
public const MODULES="MODULES";
public const ACCESS_RULES="ACCESS_RULES";
public const COMMAND_RULES="COMMAND_RULES";
public const QUERY_RULES="QUERY_RULES";
public const HOOKS = "HOOKS";
public const CACHE_KEYS = [
self::CONF => "WFW/WebApp/Confs",
self::ROUTER => "WFW/WebApp/Router",
self::TRANSLATOR => "WFW/WebApp/Translator",
self::DOMAIN_EVENT_LISTENERS => "WFW/WebApp/DomainEventListeners",
self::VIEWS => "WFW/WebApp/Views",
self::LANGS => "WFW/WebApp/Langs",
self::COMMAND_HANDLERS => "WFW/WebApp/CommandHandlers",
self::MODULES => "WFW/WebApp/Modules",
self::ACCESS_RULES => "WFW/WebApp/AccessRules",
self::COMMAND_RULES => "WFW/WebApp/CommandRules",
self::QUERY_RULES => "WFW/WebApp/QueryRules",
self::HOOKS => "WFW/WebApp/Hooks",
self::CONF_FILES => "WFW/WebApp/ConfFiles"
];
/**
* @return ICacheSystem Système de cache de l'application.
*/
public function getCacheSystem():ICacheSystem;
interface IWebAppContext extends IAppContext {
public const HOOKS="WFW/WebApp/Hooks";
public const VIEWS="WFW/WebApp/Views";
public const ACCESS_RULES="WFW/WebApp/AccessRules";
public const ROUTER = "WFW/WebApp/Router";
/**
* @return ISession Session associée à l'utilisateur courant.
......@@ -76,11 +46,6 @@ interface IWebAppContext {
*/
public function getResponseRouter():IResponseRouter;
/**
* @return IConf Configuration de l'application.
*/
public function getConf():IConf;
/**
* @return IRequest Requête courante
*/
......@@ -101,16 +66,6 @@ interface IWebAppContext {
*/
public function getAccessControlCenter():IAccessControlCenter;
/**
* @return INotifier Notifier
*/
public function getNotifier():INotifier;
/**
* @return ITranslator Gestionnaire de langues.
*/
public function getTranslator():ITranslator;
/**
* @return IAction Action correspondant à la requête courante.
*/
......@@ -120,9 +75,4 @@ interface IWebAppContext {
* @return IActionHook Hook.
*/
public function getActionHook():IActionHook;
/**
* Called by the app just before closing
*/
public function close();
}
\ No newline at end of file
<?php
namespace wfw\engine\core\notifier\printer;
use wfw\engine\core\notifier\IMessage;
/**
* Printer JSON
*/
class JSONPrinter implements IPrinter {
/**
* @param IMessage $message Message à printer.
* @return string Représentation d'un message
*/
public function print(IMessage $message): string {
return json_encode([
"message" => (string) $message,
"type" => $message->getType()??'success'
]);
}
}
\ No newline at end of file
......@@ -2,7 +2,9 @@
namespace wfw\engine\core\response;
use wfw\engine\core\action\IAction;
use wfw\engine\core\conf\IConf;
use wfw\engine\core\response\errors\InvalidResponseHandler;
use wfw\engine\core\response\errors\ResponseHandlerNotEnabled;
use wfw\engine\core\response\errors\ResponseHandlerNotFound;
use wfw\engine\core\response\responses\ErrorResponse;
use wfw\engine\package\general\handlers\response\AjaxHandler;
......@@ -18,20 +20,36 @@ final class ResponseRouter implements IResponseRouter {
private $_factory;
/** @var int $_foldingLimit */
private $_foldingLimit;
/** @var array $_enabledPackage */
private $_enabledPackages;
/**
* ActionResponseRouter constructor.
*
* @param IResponseHandlerFactory $factory Factory pour la création des ResponseHandlers
* @param int $foldingLimit (optionnel defaut : 5) Limite du nombre de sous
* repertoirs de recherche. Si la limite est atteinte, et que le
* handler n'est pas trouvé, l'exception HandlerNotFound sera levée.
* @param IConf $conf
* @param IResponseHandlerFactory $factory Factory pour la création des ResponseHandlers
* @param int $foldingLimit (optionnel defaut : 5) Limite du nombre de sous
* repertoirs de recherche. Si la limite est atteinte, et que le
* handler n'est pas trouvé, l'exception HandlerNotFound sera levée.
*/
public function __construct(IResponseHandlerFactory $factory, int $foldingLimit = 5) {
public function __construct(IConf $conf,IResponseHandlerFactory $factory, int $foldingLimit = 5) {
$this->_enabledPackages = array_flip($conf->getArray("server/packages") ?? []);
$this->_factory = $factory;
$this->_foldingLimit = $foldingLimit;
}
/**
* @param string $package
* @param null|string $location
* @return bool
*/
private function enabledPackage(string $package, ?string $location = null):bool{
if(is_null($location)) return isset($this->_enabledPackages["site/$package"])
|| isset($this->_enabledPackages["modules/$package"])
|| isset($this->_enabledPackages["engine/$package"]);
else return isset($this->_enabledPackages["$location/$package"]);
}
/**
* @param IAction $action Action à l'origine de la réponse.
* @param IResponse $response Réponse à router.
......@@ -49,21 +67,29 @@ final class ResponseRouter implements IResponseRouter {
}else{
$path = explode('/',$action->getInternalPath());
if(!is_null($package = array_shift($path))){
$handlerClass = "package\\$package\\handlers\\response";
$handlerClass = "handlers\\response";
$modulePackage = $package;
$moduleHandlerClass = $handlerClass;
$handlerFound = false;
$handlerArgs = [];
$folding = 0;
while(!is_null($part = array_shift($path)) && $folding<$this->_foldingLimit){
$handlerClass.="\\";
$viewClass = str_replace("handlers\\response","views",$handlerClass)
$viewClass = str_replace("handlers\\response","views","package\\$package\\".$handlerClass)
.lcfirst($part)."\\".ucfirst($part);
$moduleViewClass = str_replace("handlers\\response","views","wfw\\modules\\$modulePackage\\".$moduleHandlerClass)
.lcfirst($part)."\\".ucfirst($part);
$tmpName = ucfirst($part)."Handler";
if(class_exists("wfw\\site\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\site\\".$handlerClass.$tmpName;
if(class_exists("wfw\\site\\package\\$package\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\site\\package\\$package\\".$handlerClass.$tmpName;
$handlerFound = true;
break;
}else if(class_exists("wfw\\modules\\$modulePackage\\".$moduleHandlerClass.$tmpName)){
$handlerClass = "wfw\\modules\\$modulePackage\\".$moduleHandlerClass.$tmpName;
$handlerFound = true;
break;
}else if(class_exists("wfw\\engine\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\engine\\".$handlerClass.$tmpName;
}else if(class_exists("wfw\\engine\\package\\$package\\".$handlerClass.$tmpName)){
$handlerClass = "wfw\\engine\\package\\$package\\".$handlerClass.$tmpName;
$handlerFound = true;
break;
}else if(class_exists("wfw\\site\\$viewClass")){
......@@ -71,6 +97,11 @@ final class ResponseRouter implements IResponseRouter {
$handlerArgs[] = "wfw\\site\\$viewClass";
$handlerFound = true;
break;
}else if(class_exists("wfw\\modules\\$moduleViewClass")){
$handlerClass = GenericResponseHandler::class;
$handlerArgs[] = "wfw\\modules\\$moduleViewClass";
$handlerFound = true;
break;
}else if(class_exists("wfw\\engine\\$viewClass")){
$handlerClass = GenericResponseHandler::class;
$handlerArgs[] = "wfw\\engine\\$viewClass";
......@@ -78,9 +109,18 @@ final class ResponseRouter implements IResponseRouter {
break;
}else{
$handlerClass.=$part;
if($this->enabledPackage($modulePackage,"modules"))
$moduleHandlerClass.="\\$part\\";
else $modulePackage.="\\$part";
}
}
if($handlerFound){
if(!$this->enabledPackage($package) && !$this->enabledPackage($modulePackage,"modules")) {
throw new ResponseHandlerNotEnabled(
"Response found : $handlerClass, but the '$package' package or the module '$modulePackage' haven't been enabled. "
."Please check your project configuration and add this package to the 'server/packages' list."
);
}
try{
return $this->_factory->create(
$handlerClass,
......
......@@ -242,7 +242,6 @@ wfw.define("plugins/miel/medias",function($params){
);
};
let $update = ($k,$arr,$window)=>{
console.log($arr);
let $loader = $displayLoader($lstr("WAIT_UPDATE"),$window.querySelector(".body"));
wfw.network.wfwAPI(wfw.webroot+"miel/update",{
type : "POST",
......
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