chore: Inject dependencies into modules.

Avoid to inject de IoC container and inject required dependencies. A few helper modules have been created to provide common dependencies.

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2022-05-16 18:02:35 +02:00
parent 0b3d6e6bf1
commit fe349cb6f9
24 changed files with 897 additions and 489 deletions

View File

@@ -24,22 +24,10 @@
namespace SP\Core;
use Klein\Klein;
use Psr\Container\ContainerInterface;
use SP\Bootstrap;
use SP\Config\Config;
use SP\Config\ConfigDataInterface;
use SP\Core\Context\ContextInterface;
use SP\Core\Events\EventDispatcher;
use SP\Http\Request;
use SP\Providers\Acl\AclHandler;
use SP\Providers\Log\DatabaseLogHandler;
use SP\Providers\Log\FileLogHandler;
use SP\Providers\Log\RemoteSyslogHandler;
use SP\Providers\Log\SyslogHandler;
use SP\Providers\Mail\MailHandler;
use SP\Providers\Notification\NotificationHandler;
use SP\Util\Util;
/**
* Class ModuleBase
@@ -48,50 +36,27 @@ use SP\Util\Util;
*/
abstract class ModuleBase
{
protected Config $config;
protected ConfigDataInterface $configData;
protected Config $config;
protected Klein $router;
protected ContainerInterface $container;
protected Request $request;
protected ContextInterface $context;
private EventDispatcher $eventDispatcher;
private ProvidersHelper $providersHelper;
/**
* Module constructor.
*
* @param ContainerInterface $container
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @param \SP\Core\Application $application
* @param \SP\Core\ProvidersHelper $providersHelper
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->config = $container->get(Config::class);
public function __construct(
Application $application,
ProvidersHelper $providersHelper
) {
$this->config = $application->getConfig();
$this->configData = $this->config->getConfigData();
$this->router = $container->get(Klein::class);
$this->request = $container->get(Request::class);
}
abstract public function initialize(string $controller);
/**
* Comprobar si el modo mantenimiento está activado
* Esta función comprueba si el modo mantenimiento está activado.
*
* @throws \JsonException
*/
public function checkMaintenanceMode(ContextInterface $context): bool
{
if ($this->configData->isMaintenance()) {
Bootstrap::$LOCK = Util::getAppLock();
return !$this->request->isAjax()
|| !(Bootstrap::$LOCK !== false
&& Bootstrap::$LOCK->userId > 0
&& $context->isLoggedIn()
&& Bootstrap::$LOCK->userId === $context->getUserData()->getId());
}
return false;
$this->context = $application->getContext();
$this->eventDispatcher = $application->getEventDispatcher();
$this->providersHelper = $providersHelper;
}
/**
@@ -99,29 +64,51 @@ abstract class ModuleBase
*/
protected function initEventHandlers(): void
{
$eventDispatcher = $this->container->get(EventDispatcher::class);
if (DEBUG || $this->configData->isDebug()) {
$eventDispatcher->attach($this->container->get(FileLogHandler::class));
$handler = $this->providersHelper->getFileLogHandler();
$handler->initialize();
$this->eventDispatcher->attach($handler);
}
if ($this->configData->isLogEnabled()) {
$eventDispatcher->attach($this->container->get(DatabaseLogHandler::class));
$handler = $this->providersHelper->getDatabaseLogHandler();
$handler->initialize();
$this->eventDispatcher->attach($handler);
}
if ($this->configData->isMailEnabled()) {
$eventDispatcher->attach($this->container->get(MailHandler::class));
$handler = $this->providersHelper->getMailHandler();
$handler->initialize();
$this->eventDispatcher->attach($handler);
}
if ($this->configData->isSyslogEnabled()) {
$eventDispatcher->attach($this->container->get(SyslogHandler::class));
$handler = $this->providersHelper->getSyslogHandler();
$handler->initialize();
$this->eventDispatcher->attach($handler);
}
if ($this->configData->isSyslogRemoteEnabled()) {
$eventDispatcher->attach($this->container->get(RemoteSyslogHandler::class));
$handler = $this->providersHelper->getRemoteSyslogHandler();
$handler->initialize();
$this->eventDispatcher->attach($handler);
}
$eventDispatcher->attach($this->container->get(AclHandler::class));
$eventDispatcher->attach($this->container->get(NotificationHandler::class));
$aclHandler = $this->providersHelper->getAclHandler();
$aclHandler->initialize();
$this->eventDispatcher->attach($aclHandler);
$notificationHandler = $this->providersHelper->getNotificationHandler();
$notificationHandler->initialize();
$this->eventDispatcher->attach($notificationHandler);
}
abstract public function initialize(string $controller);
}