diff --git a/src/BackendIntegration/PurgeTranslator.php b/src/BackendIntegration/PurgeTranslator.php new file mode 100644 index 000000000..6f1128ff2 --- /dev/null +++ b/src/BackendIntegration/PurgeTranslator.php @@ -0,0 +1,54 @@ + + * @copyright 2012-2024 The MetaModels team. + * @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later + * @filesource + */ + +namespace MetaModels\BackendIntegration; + +use Contao\CoreBundle\Monolog\ContaoContext; +use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents; +use ContaoCommunityAlliance\Contao\Bindings\Events\System\LogEvent; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\Filesystem\Filesystem; + +class PurgeTranslator +{ + public function __construct( + private readonly string $cacheDir, + private readonly EventDispatcherInterface $dispatcher, + ) { + } + + + /** + * Purge the symfony translator. + * + * @return void + * + * @SuppressWarnings(PHPMD.Superglobals) + * @SuppressWarnings(PHPMD.CamelCaseVariableName) + */ + public function purge() + { + $fileSystem = new Filesystem(); + $fileSystem->remove($this->cacheDir); + $this->dispatcher->dispatch( + new LogEvent('Purged the Symfony translator', __METHOD__, ContaoContext::CRON), + ContaoEvents::SYSTEM_LOG + ); + } +} diff --git a/src/CoreBundle/DependencyInjection/CompilerPass/PrepareTranslatorPass.php b/src/CoreBundle/DependencyInjection/CompilerPass/PrepareTranslatorPass.php index 09abf4a6d..fe5f5886b 100644 --- a/src/CoreBundle/DependencyInjection/CompilerPass/PrepareTranslatorPass.php +++ b/src/CoreBundle/DependencyInjection/CompilerPass/PrepareTranslatorPass.php @@ -21,6 +21,7 @@ namespace MetaModels\CoreBundle\DependencyInjection\CompilerPass; +use MetaModels\BackendIntegration\PurgeTranslator; use MetaModels\CoreBundle\Translator\MetaModelTranslationLoader; use MetaModels\CoreBundle\Translator\MetaModelTranslatorConfigurator; use Symfony\Bundle\FrameworkBundle\Translation\Translator; @@ -62,16 +63,23 @@ public function process(ContainerBuilder $container): void ); // We need to keep us "first" to allow others to override the values from our loader. - if ($container->hasDefinition('translator.default')) { - $translator = $container->getDefinition('translator.default'); - /** @var array> $loaders */ - $loaders = $translator->getArgument(3); - $keys = array_keys($loaders); - $last = array_pop($keys); - if ($last === MetaModelTranslationLoader::class) { - $value = array_pop($loaders); - $loaders = [MetaModelTranslationLoader::class => $value] + $loaders; - $translator->replaceArgument(3, $loaders); + /** @var array> $loaders */ + $loaders = $definition->getArgument(3); + $keys = array_keys($loaders); + $last = array_pop($keys); + + if ($last === MetaModelTranslationLoader::class) { + $value = array_pop($loaders); + $loaders = [MetaModelTranslationLoader::class => $value] + $loaders; + $definition->replaceArgument(3, $loaders); + } + + if ($container->hasDefinition(PurgeTranslator::class)) { + $options = $definition->getArgument(4); + $cacheDir = $options['cache_dir'] ?? null; + if (null !== $cacheDir) { + $purger = $container->getDefinition(PurgeTranslator::class); + $purger->replaceArgument('$cacheDir', $cacheDir); } } } diff --git a/src/CoreBundle/EventListener/SubSystemBootListener.php b/src/CoreBundle/EventListener/SubSystemBootListener.php index 5b8487dd2..e7f18a108 100644 --- a/src/CoreBundle/EventListener/SubSystemBootListener.php +++ b/src/CoreBundle/EventListener/SubSystemBootListener.php @@ -34,6 +34,8 @@ /** * Base event listener to boot up a MetaModelServiceContainer. + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ class SubSystemBootListener { @@ -102,9 +104,19 @@ public function __construct( */ public function boot(): void { + // Saves some DB queries. + /** @psalm-suppress DeprecatedConstant */ + if ( + !$this->dispatcher->hasListeners(MetaModelsEvents::SUBSYSTEM_BOOT) + && !$this->dispatcher->hasListeners(MetaModelsEvents::SUBSYSTEM_BOOT_FRONTEND) + && !$this->dispatcher->hasListeners(MetaModelsEvents::SUBSYSTEM_BOOT_BACKEND) + ) { + return; + } + /** @psalm-suppress InternalMethod - the ContaoFramework class is internal, not the method usage. */ $environment = $this->contaoFramework->getAdapter(Environment::class); - $script = explode('?', $environment->get('relativeRequest'), 2)[0]; + $script = \explode('?', $environment->get('relativeRequest'), 2)[0]; // There is no need to boot in login or install screen. if (('contao/login' === $script) || ('contao/install' === $script)) { @@ -138,9 +150,12 @@ public function boot(): void return; } + /** @psalm-suppress DeprecatedClass */ $event = new MetaModelsBootEvent(); + /** @psalm-suppress DeprecatedConstant */ $this->tryDispatch(MetaModelsEvents::SUBSYSTEM_BOOT, $event); + /** @psalm-suppress DeprecatedConstant */ switch (true) { case $this->scopeMatcher->currentScopeIsFrontend(): $this->tryDispatch(MetaModelsEvents::SUBSYSTEM_BOOT_FRONTEND, $event); diff --git a/src/CoreBundle/Resources/config/services.yml b/src/CoreBundle/Resources/config/services.yml index 37162dcb5..5b1464a99 100644 --- a/src/CoreBundle/Resources/config/services.yml +++ b/src/CoreBundle/Resources/config/services.yml @@ -325,3 +325,9 @@ services: $builder: '@metamodels.view_combination.input_screen_information_builder' $loaders: [] tags: [ { name: translation.loader, alias: 'metamodels' } ] + + MetaModels\BackendIntegration\PurgeTranslator: + arguments: + $cacheDir: ~ + $dispatcher: '@event_dispatcher' + public: true diff --git a/src/CoreBundle/Resources/contao/config/config.php b/src/CoreBundle/Resources/contao/config/config.php index 53f711b94..d8b815e6c 100644 --- a/src/CoreBundle/Resources/contao/config/config.php +++ b/src/CoreBundle/Resources/contao/config/config.php @@ -3,7 +3,7 @@ /** * This file is part of MetaModels/core. * - * (c) 2012-2019 The MetaModels team. + * (c) 2012-2024 The MetaModels team. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -21,48 +21,61 @@ * @author Ingolf Steinhardt * @author Sven Baumann * @author Richard Henkenjohann - * @copyright 2012-2019 The MetaModels team. + * @copyright 2012-2024 The MetaModels team. * @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later * @filesource */ -$container = \Contao\System::getContainer(); +use Contao\ArrayUtil; +use Contao\System; +use MetaModels\BackendIntegration\PurgeAssets; +use MetaModels\BackendIntegration\PurgeTranslator; +use MetaModels\CoreBundle\Contao\Hooks\LoadDataContainer; +use MetaModels\FrontendIntegration\Content\Filter; +use MetaModels\FrontendIntegration\Content\FilterClearAll; +use MetaModels\FrontendIntegration\FrontendFilter; +use MetaModels\Widgets\MultiTextWidget; +use MetaModels\Widgets\SubDcaWidget; +use MetaModels\Widgets\TagsWidget; + +$container = System::getContainer(); // @deprecated Use the config parameter metamodels.system_columns instead. $GLOBALS['METAMODELS_SYSTEM_COLUMNS'] = $container->getParameter('metamodels.system_columns'); // Front-end modules. -$GLOBALS['FE_MOD']['metamodels']['metamodels_frontendfilter'] = 'MetaModels\FrontendIntegration\Module\Filter'; -$GLOBALS['FE_MOD']['metamodels']['metamodels_frontendclearall'] = - 'MetaModels\FrontendIntegration\Module\FilterClearAll'; +$GLOBALS['FE_MOD']['metamodels']['metamodels_frontendfilter'] = Filter::class; +$GLOBALS['FE_MOD']['metamodels']['metamodels_frontendclearall'] = FilterClearAll::class; // Content elements. -$GLOBALS['TL_CTE']['metamodels']['metamodels_frontendfilter'] = 'MetaModels\FrontendIntegration\Content\Filter'; -$GLOBALS['TL_CTE']['metamodels']['metamodels_frontendclearall'] = - 'MetaModels\FrontendIntegration\Content\FilterClearAll'; +$GLOBALS['TL_CTE']['metamodels']['metamodels_frontendfilter'] = Filter::class; +$GLOBALS['TL_CTE']['metamodels']['metamodels_frontendclearall'] = FilterClearAll::class; // Frontend widgets. -$GLOBALS['TL_FFL']['multitext'] = 'MetaModels\Widgets\MultiTextWidget'; -$GLOBALS['TL_FFL']['tags'] = 'MetaModels\Widgets\TagsWidget'; +$GLOBALS['TL_FFL']['multitext'] = MultiTextWidget::class; +$GLOBALS['TL_FFL']['tags'] = TagsWidget::class; // HOOKS. -$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = - array('MetaModels\FrontendIntegration\FrontendFilter', 'generateClearAll'); +$GLOBALS['TL_HOOKS']['outputFrontendTemplate'][] = [FrontendFilter::class, 'generateClearAll']; // Add cache only if dir defined in container (and therefore we are using the cache). if ($cacheDir = $container->getParameter('metamodels.cache_dir')) { // We need to translate the cache dir - otherwise the backend view is distorted. See \Contao\PurgeData::run(). - $GLOBALS['TL_PURGE']['folders']['metamodels']['affected'] = [str_replace( - $container->getParameter('kernel.cache_dir') . '/', - '%s/', - $cacheDir - )]; + $GLOBALS['TL_PURGE']['folders']['metamodels']['affected'] = [ + str_replace( + $container->getParameter('kernel.cache_dir') . '/', + '%s/', + $cacheDir + ) + ]; $GLOBALS['TL_PURGE']['folders']['metamodels']['callback'] = ['metamodels.cache.purger', 'purge']; } $GLOBALS['TL_PURGE']['folders']['metamodels_assets']['affected'][] = 'assets/metamodels'; -$GLOBALS['TL_PURGE']['folders']['metamodels_assets']['callback'] = - array('MetaModels\BackendIntegration\PurgeAssets', 'purge'); +$GLOBALS['TL_PURGE']['folders']['metamodels_assets']['callback'] = [PurgeAssets::class, 'purge']; + +$GLOBALS['TL_PURGE']['folders']['translator']['affected'] = []; +$GLOBALS['TL_PURGE']['folders']['translator']['callback'] = [PurgeTranslator::class, 'purge']; // Meta Information. $GLOBALS['METAMODELS']['metainformation']['allowedTitle'][] = 'text'; @@ -70,25 +83,24 @@ $GLOBALS['METAMODELS']['metainformation']['allowedTitle'][] = 'translatedtext'; $GLOBALS['METAMODELS']['metainformation']['allowedTitle'][] = 'translatedselect'; $GLOBALS['METAMODELS']['metainformation']['allowedTitle'][] = 'combinedvalues'; +$GLOBALS['METAMODELS']['metainformation']['allowedTitle'][] = 'translatedcombinedvalues'; $GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'text'; $GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'select'; $GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'translatedtext'; $GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'translatedselect'; $GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'longtext'; $GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'translatedlongtext'; -$GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'combinedvalues'; +$GLOBALS['METAMODELS']['metainformation']['allowedDescription'][] = 'translatedcombinedvalues'; -\Contao\ArrayUtil::arrayInsert($GLOBALS['BE_FFL'], 15, array -( - 'mm_subdca' => 'MetaModels\Widgets\SubDcaWidget' -)); +ArrayUtil::arrayInsert($GLOBALS['BE_FFL'], 15, [ + 'mm_subdca' => SubDcaWidget::class +]); // Initialize the filter parameters to an empty array if not initialized yet. if (!isset($GLOBALS['MM_FILTER_PARAMS'])) { - $GLOBALS['MM_FILTER_PARAMS'] = array(); + $GLOBALS['MM_FILTER_PARAMS'] = []; } $GLOBALS['TL_HOOKS']['initializeSystem'][] = ['metamodels.sub_system_boot', 'boot']; -$GLOBALS['TL_HOOKS']['loadDataContainer'][] = - [\MetaModels\CoreBundle\Contao\Hooks\LoadDataContainer::class, 'onLoadDataContainer']; +$GLOBALS['TL_HOOKS']['loadDataContainer'][] = [LoadDataContainer::class, 'onLoadDataContainer']; diff --git a/src/CoreBundle/Resources/contao/languages/en/tl_maintenance.php b/src/CoreBundle/Resources/contao/languages/en/tl_maintenance.php index 0df3671e3..ab37b76f2 100755 --- a/src/CoreBundle/Resources/contao/languages/en/tl_maintenance.php +++ b/src/CoreBundle/Resources/contao/languages/en/tl_maintenance.php @@ -3,7 +3,7 @@ /** * This file is part of MetaModels/core. * - * (c) 2012-2018 The MetaModels team. + * (c) 2012-2024 The MetaModels team. * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,7 +15,7 @@ * @author Christian Schiffler * @author Ingolf Steinhardt * @author Sven Baumann - * @copyright 2012-2018 The MetaModels team. + * @copyright 2012-2024 The MetaModels team. * @license https://github.com/MetaModels/core/blob/master/LICENSE LGPL-3.0-or-later * @filesource */ @@ -25,3 +25,6 @@ $GLOBALS['TL_LANG']['tl_maintenance_jobs']['metamodels_assets'][0] = 'MetaModels assets'; $GLOBALS['TL_LANG']['tl_maintenance_jobs']['metamodels_assets'][1] = 'Clear the MetaModels assets files (backend icons etc.).'; + +$GLOBALS['TL_LANG']['tl_maintenance_jobs']['translator'][0] = 'Symfony translator'; +$GLOBALS['TL_LANG']['tl_maintenance_jobs']['translator'][1] = 'Clear the Symfony translator cache for active environment.';