Skip to content

Commit

Permalink
Add purge translator
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed Oct 21, 2024
1 parent d0e62af commit b9ad94d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 41 deletions.
54 changes: 54 additions & 0 deletions src/BackendIntegration/PurgeTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is part of MetaModels/core.
*
* (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.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package MetaModels/core
* @author Ingolf Steinhardt <[email protected]>
* @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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, list<string>> $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<string, list<string>> $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);
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/CoreBundle/EventListener/SubSystemBootListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

/**
* Base event listener to boot up a MetaModelServiceContainer.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
class SubSystemBootListener
{
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 40 additions & 28 deletions src/CoreBundle/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,74 +21,86 @@
* @author Ingolf Steinhardt <[email protected]>
* @author Sven Baumann <[email protected]>
* @author Richard Henkenjohann <[email protected]>
* @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';
$GLOBALS['METAMODELS']['metainformation']['allowedTitle'][] = 'select';
$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'];
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,7 +15,7 @@
* @author Christian Schiffler <[email protected]>
* @author Ingolf Steinhardt <[email protected]>
* @author Sven Baumann <[email protected]>
* @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
*/
Expand All @@ -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.';

0 comments on commit b9ad94d

Please sign in to comment.