Commit efb4bad8 authored by Tino Goratsch's avatar Tino Goratsch

refactored the Modules class

parent 76adcfd4
<?php
namespace ACP3\Core;
use ACP3\Core\Modules\ModuleInfoCache;
use ACP3\Core\Modules\Vendors;
use ACP3\Modules\ACP3\System;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -16,21 +17,9 @@ class Modules
*/
protected $container;
/**
* @var \ACP3\Core\Lang
* @var \ACP3\Core\Modules\ModuleInfoCache
*/
protected $lang;
/**
* @var \ACP3\Core\XML
*/
protected $xml;
/**
* @var \ACP3\Core\Cache
*/
protected $modulesCache;
/**
* @var \ACP3\Modules\ACP3\System\Model
*/
protected $systemModel;
protected $moduleInfoCache;
/**
* @var \ACP3\Core\Modules\Vendors
*/
......@@ -38,7 +27,7 @@ class Modules
/**
* @var array
*/
private $parseModules = [];
private $modulesInfo = [];
/**
* @var array
*/
......@@ -46,31 +35,22 @@ class Modules
/**
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\XML $xml
* @param \ACP3\Core\Cache $cache
* @param \ACP3\Core\Modules\ModuleInfoCache $moduleInfoCache
* @param \ACP3\Core\Modules\Vendors $vendors
* @param \ACP3\Modules\ACP3\System\Model $systemModel
*/
public function __construct(
ContainerInterface $container,
Lang $lang,
XML $xml,
Cache $cache,
Vendors $vendors,
System\Model $systemModel
ModuleInfoCache $moduleInfoCache,
Vendors $vendors
)
{
$this->container = $container;
$this->lang = $lang;
$this->xml = $xml;
$this->modulesCache = $cache;
$this->moduleInfoCache = $moduleInfoCache;
$this->vendors = $vendors;
$this->systemModel = $systemModel;
}
/**
* Überprüft, ob eine Modulaktion überhaupt existiert
* Returns, whether the given module controller action exists
*
* @param string $path
*
......@@ -78,9 +58,7 @@ class Modules
*/
public function controllerActionExists($path)
{
$pathArray = array_map(function ($value) {
return str_replace(' ', '', strtolower(str_replace('_', ' ', $value)));
}, explode('/', $path));
$pathArray = explode('/', strtolower(str_replace('_', '', $path)));
if (empty($pathArray[2]) === true) {
$pathArray[2] = 'index';
......@@ -99,7 +77,7 @@ class Modules
}
/**
* Gibt zurück, ob ein Modul aktiv ist oder nicht
* Returns, whether a module is active or not
*
* @param string $module
*
......@@ -112,8 +90,7 @@ class Modules
}
/**
* Durchläuft für das angeforderte Modul den <info> Abschnitt in der
* module.xml und gibt die gefundenen Informationen als Array zurück
* Returns the available information about the given module
*
* @param string $module
*
......@@ -122,14 +99,10 @@ class Modules
public function getModuleInfo($module)
{
$module = strtolower($module);
if (empty($this->parseModules)) {
$filename = $this->_getCacheKey();
if ($this->modulesCache->contains($filename) === false) {
$this->saveModulesCache();
}
$this->parseModules = $this->modulesCache->fetch($filename);
if (empty($this->modulesInfo)) {
$this->modulesInfo = $this->moduleInfoCache->getModulesInfoCache();
}
return !empty($this->parseModules[$module]) ? $this->parseModules[$module] : [];
return !empty($this->modulesInfo[$module]) ? $this->modulesInfo[$module] : [];
}
/**
......@@ -143,93 +116,6 @@ class Modules
return !empty($info) ? $info['id'] : 0;
}
/**
* @return string
*/
protected function _getCacheKey()
{
return 'infos_' . $this->lang->getLanguage();
}
/**
* Saves a modules info cache
*/
public function saveModulesCache()
{
$infos = [];
// 1. fetch all core modules
// 2. Fetch all 3rd party modules
// 3. Fetch all local module customizations
foreach ($this->vendors->getVendors() as $namespace) {
$infos += $this->_fetchModulesInVendors($namespace);
}
$this->modulesCache->save($this->_getCacheKey(), $infos);
}
/**
* @param string $vendor
*
* @return array
*/
protected function _fetchModulesInVendors($vendor)
{
$infos = [];
$modules = array_diff(scandir(MODULES_DIR . $vendor . '/'), ['.', '..', '.gitignore', '.svn', '.htaccess', '.htpasswd']);
if (!empty($modules)) {
foreach ($modules as $module) {
$moduleInfo = $this->_fetchModuleInfo($module);
if (!empty($moduleInfo)) {
$infos[strtolower($module)] = $moduleInfo;
}
}
}
return $infos;
}
/**
* @param string $moduleDirectory
*
* @return array
*/
protected function _fetchModuleInfo($moduleDirectory)
{
$namespaces = array_reverse($this->vendors->getVendors()); // Reverse the order of the array -> search module customizations first, then 3rd party modules, then core modules
foreach ($namespaces as $namespace) {
$path = MODULES_DIR . $namespace . '/' . $moduleDirectory . '/config/module.xml';
if (is_file($path) === true) {
$moduleInfo = $this->xml->parseXmlFile($path, 'info');
if (!empty($moduleInfo)) {
$moduleName = strtolower($moduleDirectory);
$moduleInfoDb = $this->systemModel->getInfoByModuleName($moduleName);
return [
'id' => !empty($moduleInfoDb) ? $moduleInfoDb['id'] : 0,
'dir' => $moduleDirectory,
'installed' => (!empty($moduleInfoDb)),
'active' => (!empty($moduleInfoDb) && $moduleInfoDb['active'] == 1),
'schema_version' => !empty($moduleInfoDb) ? (int)$moduleInfoDb['version'] : 0,
'description' => $this->getModuleDescription($moduleInfo, $moduleName),
'author' => $moduleInfo['author'],
'version' => $moduleInfo['version'],
'name' => $this->getModuleName($moduleInfo, $moduleName),
'categories' => isset($moduleInfo['categories']),
'protected' => isset($moduleInfo['protected']),
'dependencies' => array_values($this->xml->parseXmlFile($path, 'info/dependencies')),
];
}
}
}
return [];
}
/**
* Checks, whether a modules in currently installed or not
*
......@@ -302,34 +188,4 @@ class Modules
return $this->allModules;
}
/**
* @param array $moduleInfo
* @param string $moduleName
*
* @return string
*/
protected function getModuleDescription(array $moduleInfo, $moduleName)
{
if (isset($moduleInfo['description']['lang']) && $moduleInfo['description']['lang'] === 'true') {
return $this->lang->t($moduleName, 'mod_description');
}
return $moduleInfo['description']['lang'];
}
/**
* @param array $moduleInfo
* @param string $moduleName
*
* @return string
*/
protected function getModuleName(array $moduleInfo, $moduleName)
{
if (isset($moduleInfo['name']['lang']) && $moduleInfo['name']['lang'] == 'true') {
return $this->lang->t($moduleName, $moduleName);
}
return $moduleInfo['name'];
}
}
\ No newline at end of file
<?php
namespace ACP3\Core\Modules;
use ACP3\Core\Cache;
use ACP3\Core\Lang;
use ACP3\Core\XML;
use ACP3\Modules\ACP3\System\Model;
/**
* Class ModuleInfoCache
* @package ACP3\Core\Modules
*/
class ModuleInfoCache
{
/**
* @var \ACP3\Core\Cache
*/
protected $cache;
/**
* @var \ACP3\Core\Lang
*/
protected $lang;
/**
* @var \ACP3\Core\Modules\Vendors
*/
protected $vendors;
/**
* @var \ACP3\Core\XML
*/
protected $xml;
/**
* @var \ACP3\Modules\ACP3\System\Model
*/
protected $systemModel;
/**
* @param \ACP3\Core\Cache $cache
* @param \ACP3\Core\Lang $lang
* @param \ACP3\Core\Modules\Vendors $vendors
* @param \ACP3\Core\XML $xml
* @param \ACP3\Modules\ACP3\System\Model $systemModel
*/
public function __construct(
Cache $cache,
Lang $lang,
Vendors $vendors,
XML $xml,
Model $systemModel
)
{
$this->cache = $cache;
$this->lang = $lang;
$this->vendors = $vendors;
$this->xml = $xml;
$this->systemModel = $systemModel;
}
/**
* @return string
*/
public function getCacheKey()
{
return 'infos_' . $this->lang->getLanguage();
}
/**
* @return array
*/
public function getModulesInfoCache()
{
if ($this->cache->contains($this->getCacheKey()) === false) {
$this->saveModulesInfoCache();
}
return $this->cache->fetch($this->getCacheKey());
}
/**
* Saves the modules info cache
*/
public function saveModulesInfoCache()
{
$infos = [];
// 1. fetch all core modules
// 2. Fetch all 3rd party modules
// 3. Fetch all local module customizations
foreach ($this->vendors->getVendors() as $vendor) {
$infos += $this->_fetchVendorModules($vendor);
}
$this->cache->save($this->getCacheKey(), $infos);
}
/**
* @param string $vendor
*
* @return array
*/
protected function _fetchVendorModules($vendor)
{
$infos = [];
$modules = array_diff(scandir(MODULES_DIR . $vendor . '/'), ['.', '..', '.gitignore', '.svn', '.htaccess', '.htpasswd']);
if (!empty($modules)) {
foreach ($modules as $module) {
$moduleInfo = $this->_fetchModuleInfo($module);
if (!empty($moduleInfo)) {
$infos[strtolower($module)] = $moduleInfo;
}
}
}
return $infos;
}
/**
* @param string $moduleDirectory
*
* @return array
*/
protected function _fetchModuleInfo($moduleDirectory)
{
$vendors = array_reverse($this->vendors->getVendors()); // Reverse the order of the array -> search module customizations first, then 3rd party modules, then core modules
foreach ($vendors as $vendor) {
$path = MODULES_DIR . $vendor . '/' . $moduleDirectory . '/config/module.xml';
if (is_file($path) === true) {
$moduleInfo = $this->xml->parseXmlFile($path, 'info');
if (!empty($moduleInfo)) {
$moduleName = strtolower($moduleDirectory);
$moduleInfoDb = $this->systemModel->getInfoByModuleName($moduleName);
return [
'id' => !empty($moduleInfoDb) ? $moduleInfoDb['id'] : 0,
'dir' => $moduleDirectory,
'installed' => (!empty($moduleInfoDb)),
'active' => (!empty($moduleInfoDb) && $moduleInfoDb['active'] == 1),
'schema_version' => !empty($moduleInfoDb) ? (int)$moduleInfoDb['version'] : 0,
'description' => $this->getModuleDescription($moduleInfo, $moduleName),
'author' => $moduleInfo['author'],
'version' => $moduleInfo['version'],
'name' => $this->getModuleName($moduleInfo, $moduleName),
'categories' => isset($moduleInfo['categories']),
'protected' => isset($moduleInfo['protected']),
'dependencies' => array_values($this->xml->parseXmlFile($path, 'info/dependencies')),
];
}
}
}
return [];
}
/**
* @param array $moduleInfo
* @param string $moduleName
*
* @return string
*/
protected function getModuleDescription(array $moduleInfo, $moduleName)
{
if (isset($moduleInfo['description']['lang']) && $moduleInfo['description']['lang'] === 'true') {
return $this->lang->t($moduleName, 'mod_description');
}
return $moduleInfo['description']['lang'];
}
/**
* @param array $moduleInfo
* @param string $moduleName
*
* @return string
*/
protected function getModuleName(array $moduleInfo, $moduleName)
{
if (isset($moduleInfo['name']['lang']) && $moduleInfo['name']['lang'] == 'true') {
return $this->lang->t($moduleName, $moduleName);
}
return $moduleInfo['name'];
}
}
\ No newline at end of file
......@@ -16,10 +16,23 @@ services:
core.modules:
class: ACP3\Core\Modules
arguments: ['@service_container', '@core.lang', '@core.xml', '@core.cache.system', @core.modules.vendors, '@system.model']
arguments: ['@service_container', @core.modules.moduleInfoCache, @core.modules.vendors]
core.modules.vendors:
class: ACP3\Core\Modules\Vendors
core.modules.controller:
abstract: true
arguments: ['@core.context']
core.modules.controller.frontend:
abstract: true
arguments: ['@core.context.frontend']
core.modules.controller.admin:
abstract: true
arguments: ['@core.context.admin']
core.modules.moduleInfoCache:
class: ACP3\Core\Modules\ModuleInfoCache
arguments: [@core.cache.system, @core.lang, @core.modules.vendors, @core.xml, @system.model]
core.modules.schemaHelper:
class: ACP3\Core\Modules\SchemaHelper
......@@ -27,16 +40,6 @@ services:
calls:
- ['setContainer', ['@service_container']]
core.modules.schemaInstaller:
class: ACP3\Core\Modules\SchemaInstaller
arguments: ['@core.db', '@core.xml', '@core.cache.system', '@system.model', '@permissions.model']
calls:
- ['setContainer', ['@service_container']]
core.modules.schemaUpdater:
class: ACP3\Core\Modules\SchemaUpdater
parent: core.modules.schemaHelper
core.modules.installer.abstractMigration:
abstract: true
arguments: [@core.modules.schemaHelper]
......@@ -45,15 +48,15 @@ services:
abstract: true
arguments: [@core.modules.schemaHelper]
core.modules.controller:
abstract: true
arguments: ['@core.context']
core.modules.controller.frontend:
abstract: true
arguments: ['@core.context.frontend']
core.modules.schemaInstaller:
class: ACP3\Core\Modules\SchemaInstaller
arguments: ['@core.db', '@core.xml', '@core.cache.system', '@system.model', '@permissions.model']
calls:
- ['setContainer', ['@service_container']]
core.modules.controller.admin:
abstract: true
arguments: ['@core.context.admin']
core.modules.schemaUpdater:
class: ACP3\Core\Modules\SchemaUpdater
parent: core.modules.schemaHelper
core.modules.vendors:
class: ACP3\Core\Modules\Vendors
\ No newline at end of file
......@@ -12,6 +12,10 @@ use ACP3\Modules\ACP3\System;
*/
class Extensions extends Core\Modules\AdminController
{
/**
* @var \ACP3\Core\Modules\ModuleInfoCache
*/
protected $moduleInfoCache;
/**
* @var \ACP3\Core\XML
*/
......@@ -31,6 +35,7 @@ class Extensions extends Core\Modules\AdminController
/**
* @param \ACP3\Core\Modules\Controller\AdminContext $context
* @param \ACP3\Core\Modules\ModuleInfoCache $moduleInfoCache
* @param \ACP3\Core\XML $xml
* @param \ACP3\Modules\ACP3\System\Model $systemModel
* @param \ACP3\Modules\ACP3\System\Helpers $systemHelpers
......@@ -38,6 +43,7 @@ class Extensions extends Core\Modules\AdminController
*/
public function __construct(
Core\Modules\Controller\AdminContext $context,
Core\Modules\ModuleInfoCache $moduleInfoCache,
Core\XML $xml,
System\Model $systemModel,
System\Helpers $systemHelpers,
......@@ -45,6 +51,7 @@ class Extensions extends Core\Modules\AdminController
{
parent::__construct($context);
$this->moduleInfoCache = $moduleInfoCache;
$this->xml = $xml;
$this->systemModel = $systemModel;
$this->systemHelpers = $systemHelpers;
......@@ -188,7 +195,7 @@ class Extensions extends Core\Modules\AdminController
protected function _renewCaches()
{
$this->get('core.lang.cache')->saveLanguageCache($this->lang->getLanguage());
$this->modules->saveModulesCache();
$this->moduleInfoCache->saveModulesInfoCache();
$this->permissionsCache->saveResourcesCache();
}
......
......@@ -11,7 +11,7 @@ parameters:
services:
system.controller.admin.extensions:
class: %system.controller.admin.extensions.fqdn%
arguments: ['@core.context.admin', '@core.xml', '@system.model', '@system.helpers', '@permissions.cache']
arguments: ['@core.context.admin', @core.modules.moduleInfoCache, '@core.xml', '@system.model', '@system.helpers', '@permissions.cache']
system.controller.admin.index:
class: %system.controller.admin.index.fqdn%
......
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