Skip to content

Commit

Permalink
Merge branch 'hotfix/fix_deprecations' into 'release/2.3.0'
Browse files Browse the repository at this point in the history
Fix deprecations and session

See merge request metamodels/core!314
  • Loading branch information
zonky2 committed Aug 7, 2024
2 parents 1dd316f + aba91a4 commit 2e7c3dd
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 38 deletions.
10 changes: 6 additions & 4 deletions src/CoreBundle/ContaoManager/Plugin.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 @@ -13,7 +13,8 @@
* @package MetaModels/core
* @author Christian Schiffler <[email protected]>
* @author Sven Baumann <[email protected]>
* @copyright 2012-2019 The MetaModels team.
* @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
*/
Expand All @@ -30,6 +31,7 @@
use MetaModels\CoreBundle\MetaModelsCoreBundle;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouteCollection;

/**
* Plugin for the Contao Manager.
Expand All @@ -39,7 +41,7 @@ class Plugin implements BundlePluginInterface, RoutingPluginInterface
/**
* {@inheritdoc}
*/
public function getBundles(ParserInterface $parser)
public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(MetaModelsCoreBundle::class)
Expand All @@ -57,7 +59,7 @@ public function getBundles(ParserInterface $parser)
/**
* {@inheritdoc}
*/
public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel)
public function getRouteCollection(LoaderResolverInterface $resolver, KernelInterface $kernel): ?RouteCollection
{
if (false === ($resolved = $resolver->resolve(__DIR__ . '/../Resources/config/routing.yml'))) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use MetaModels\CoreBundle\Migration\TableCollationMigration;
use MetaModels\Filter\FilterUrlBuilder;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand Down Expand Up @@ -116,10 +117,11 @@ public function load(array $configs, ContainerBuilder $container): void
/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface
{
$projectDir = $container->getParameter('kernel.project_dir');
assert(\is_string($projectDir));

return new Configuration((bool) $container->getParameter('kernel.debug'), $projectDir);
}

Expand All @@ -139,6 +141,7 @@ private function buildCacheService(ContainerBuilder $container, array $config):
$cache->setClass(ArrayAdapter::class);
$cache->setArguments([]);
$container->setParameter('metamodels.cache_dir', null);

return;
}

Expand Down
12 changes: 1 addition & 11 deletions src/CoreBundle/EventListener/BackendNavigationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ class BackendNavigationListener
*/
private RouterInterface $router;

/**
* The session.
*
* @var Session
*/
private Session $session;

/**
* Create a new instance.
*
Expand All @@ -96,22 +89,19 @@ class BackendNavigationListener
* @param ViewCombination $viewCombination The view combination.
* @param TokenStorageInterface $tokenStorage The token storage.
* @param RouterInterface $router The router.
* @param Session $session The session.
*/
public function __construct(
TranslatorInterface $translator,
RequestStack $requestStack,
ViewCombination $viewCombination,
TokenStorageInterface $tokenStorage,
RouterInterface $router,
Session $session,
) {
$this->requestStack = $requestStack;
$this->translator = $translator;
$this->viewCombination = $viewCombination;
$this->tokenStorage = $tokenStorage;
$this->router = $router;
$this->session = $session;
}

/**
Expand Down Expand Up @@ -287,7 +277,7 @@ private function buildConfigNode(FactoryInterface $factory, Request $request): I
private function updateCollapsedState(ItemInterface $metaModelsNode): void
{
$nodeName = $metaModelsNode->getName();
$sessionBag = $this->session->getBag('contao_backend');
$sessionBag = $this->requestStack->getSession()->getBag('contao_backend');
$status = ($sessionBag instanceof AttributeBagInterface) ? $sessionBag->get('backend_modules') : [];
$isCollapsed = ($status[$nodeName] ?? 1) < 1;
$path = $this->router->generate('contao_backend');
Expand Down
1 change: 0 additions & 1 deletion src/CoreBundle/Resources/config/listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ services:
- '@metamodels.view_combination'
- '@security.token_storage'
- '@router'
- '@session'
tags:
- { name: kernel.event_listener }

Expand Down
15 changes: 11 additions & 4 deletions src/CoreBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services:
class: MetaModels\CoreBundle\Assets\IconBuilder
arguments:
- "@=service('contao.framework').getAdapter('Contao\\\\FilesModel')"
- "@contao.image.image_factory"
- "@contao.image.factory"
- "%kernel.project_dir%"
- "%metamodels.assets_dir%"
- "%metamodels.assets_web%"
Expand Down Expand Up @@ -87,6 +87,10 @@ services:
- 'Contao\Input'

metamodels.contao_session:
deprecated:
package: metamodels/core
version: 2.3
message: 'The service "%service_id%" is deprecated and to be removed in MetaModels 3 - Use the symfony session instead.'
class: Contao\Session
factory: ['@MetaModels\CoreBundle\Contao\Compat\ContaoFactory', 'createInstance']
arguments:
Expand Down Expand Up @@ -186,7 +190,10 @@ services:
- "@metamodels.view_combination.input_screen_information_builder"

MetaModels\MetaModelsServiceContainer:
deprecated: 'The service "%service_id%" is deprecated and to be removed in MetaModels 3 - inject needed services directly.'
deprecated:
package: metamodels/core
version: 2.3
message: 'The service "%service_id%" is deprecated and to be removed in MetaModels 3 - inject needed services directly.'
configurator: ['@MetaModels\CoreBundle\LegacyCompat\ServiceContainerInitializer', 'configure']
public: true

Expand Down Expand Up @@ -293,11 +300,11 @@ services:
MetaModels\Helper\ToolboxFile:
public: false
arguments:
- "@contao.image.image_factory"
- "@contao.image.factory"
- "%kernel.project_dir%"
- "@contao.assets.files_context"
- "@contao.image.picture_factory"
- "@session"
- "@request_stack"

MetaModels\CoreBundle\Translator\MetaModelTranslatorConfigurator:
arguments:
Expand Down
4 changes: 2 additions & 2 deletions src/CoreBundle/Translator/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ interface LoaderInterface
/**
* Loads a locale.
*
* @param mixed $resource A resource
* @param string $locale A locale
* @param IMetaModel $metaModel A MetaModel
* @param string $locale A locale
*
* @return MessageCatalogue
*
Expand Down
3 changes: 3 additions & 0 deletions src/CoreBundle/Translator/MetaModelTranslationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ private function setTranslationLabelAndDescription(
array $parameters,
): void {
$headlineKey = 'backend-module.' . $inputScreen['meta']['id'] . '.headline';
if (!$catalog->has($prefix . '.description', $domain)) {
$catalog->set($prefix . '.description', '', $domain);
}
if ('' !== $value = $this->extractLangString($inputScreen['description'], $locale, $mainLanguage) ?? '') {
$value = strtr($value, $parameters);
$catalog->set($prefix . '.description', $value, $domain);
Expand Down
5 changes: 5 additions & 0 deletions src/CoreBundle/Translator/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public function getCatalogue($locale = null): MessageCatalogueInterface
{
return $this->translator->getCatalogue($locale);
}

public function getCatalogues(): array
{
return $this->translator->getCatalogues();
}
}
6 changes: 3 additions & 3 deletions src/Events/DatabaseBackedListener.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-2023 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 @@ -16,7 +16,7 @@
* @author David Molineus <[email protected]>
* @author Cliff Parnitzky <[email protected]>
* @author Ingolf Steinhardt <[email protected]>
* @copyright 2012-2023 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 Down Expand Up @@ -250,7 +250,7 @@ public function createMetaModel(CreateMetaModelEvent $event)
{
if ($event->getMetaModel() !== null) {
/** @psalm-suppress DeprecatedMethod */
if (($metaModel = $event->getMetaModel()) instanceof ITranslatedMetaModel && $metaModel->isTranslated()) {
if (!($metaModel = $event->getMetaModel()) instanceof ITranslatedMetaModel && $metaModel->isTranslated()) {
// @codingStandardsIgnoreStart
@\trigger_error(
'Translated "\MetaModel\IMetamodel" instances are deprecated since MetaModels 2.2 ' .
Expand Down
26 changes: 14 additions & 12 deletions src/Helper/ToolboxFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use InvalidArgumentException;
use Symfony\Component\Asset\Context\ContextInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;

Expand Down Expand Up @@ -99,11 +100,11 @@ class ToolboxFile
private PictureFactoryInterface $pictureFactory;

/**
* Symfony session object
* Symfony requestStack object
*
* @var Session
* @var RequestStack
*/
private Session $session;
private RequestStack $requestStack;

/**
* Allowed file extensions.
Expand Down Expand Up @@ -210,7 +211,7 @@ class ToolboxFile
* @param string|null $rootDir The root path of the installation.
* @param ContextInterface|null $filesContext The assets file context.
* @param PictureFactoryInterface|null $pictureFactory The picture factory.
* @param Session|null $session The session.
* @param RequestStack|null $requestStack The requestStack.
*
* @SuppressWarnings(PHPMD.CamelCaseVariableName)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
Expand All @@ -221,7 +222,7 @@ public function __construct(
string $rootDir = null,
ContextInterface $filesContext = null,
PictureFactoryInterface $pictureFactory = null,
Session $session = null
RequestStack $requestStack = null
) {
switch (true) {
case ($imageFactory instanceof ImageFactoryInterface) && (null !== $rootDir):
Expand Down Expand Up @@ -290,17 +291,17 @@ public function __construct(
}
$this->pictureFactory = $pictureFactory;

if (null === $session) {
if (null === $requestStack) {
// @codingStandardsIgnoreStart
@trigger_error(
'Not passing a "Session" is deprecated.',
E_USER_DEPRECATED
);
// @codingStandardsIgnoreEnd
$session = System::getContainer()->get('session');
assert($session instanceof Session);
$requestStack = System::getContainer()->get('request_stack');
assert($requestStack instanceof RequestStack);
}
$this->session = $session;
$this->requestStack = $requestStack;
}

/**
Expand Down Expand Up @@ -573,8 +574,8 @@ protected function getDownloadLink($strFile)
->setQueryParameter('file', \urlencode($strFile))
->getUrl();
}

$bag = $this->session->getBag('attributes');
// Throws exception when running in CLI mode due to missing session.
$bag = $this->requestStack->getSession()->getBag('attributes');
assert($bag instanceof AttributeBagInterface);

$links = $bag->has('metaModels_downloads') ? $bag->get('metaModels_downloads') : [];
Expand Down Expand Up @@ -850,7 +851,8 @@ private function checkDownloads()

if (($file = Input::get('file'))) {
if ($this->withDownloadKeys) {
$bag = $this->session->getBag('attributes');
// Throws exception when running in CLI mode due to missing session.
$bag = $this->requestStack->getSession()->getBag('attributes');
assert($bag instanceof AttributeBagInterface);
$links = $bag->has('metaModels_downloads') ? $bag->get('metaModels_downloads') : [];

Expand Down

0 comments on commit 2e7c3dd

Please sign in to comment.