Skip to content

Commit

Permalink
Register globals in fragment pass
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Oct 8, 2020
1 parent 80a4465 commit ddec13d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 96 deletions.
21 changes: 17 additions & 4 deletions core-bundle/src/ContaoCoreBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Contao\CoreBundle\DependencyInjection\Compiler\CrawlerPass;
use Contao\CoreBundle\DependencyInjection\Compiler\DataContainerCallbackPass;
use Contao\CoreBundle\DependencyInjection\Compiler\MakeServicesPublicPass;
use Contao\CoreBundle\DependencyInjection\Compiler\MapFragmentsToGlobalsPass;
use Contao\CoreBundle\DependencyInjection\Compiler\PickerProviderPass;
use Contao\CoreBundle\DependencyInjection\Compiler\RegisterFragmentsPass;
use Contao\CoreBundle\DependencyInjection\Compiler\RegisterHookListenersPass;
Expand Down Expand Up @@ -82,11 +81,25 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new AddResourcesPathsPass());
$container->addCompilerPass(new TaggedMigrationsPass());
$container->addCompilerPass(new PickerProviderPass());
$container->addCompilerPass(new RegisterFragmentsPass(FrontendModuleReference::TAG_NAME));
$container->addCompilerPass(new RegisterFragmentsPass(ContentElementReference::TAG_NAME));

$container->addCompilerPass(
new RegisterFragmentsPass(
FrontendModuleReference::TAG_NAME,
FrontendModuleReference::GLOBALS_KEY,
FrontendModuleReference::PROXY_CLASS
)
);

$container->addCompilerPass(
new RegisterFragmentsPass(
ContentElementReference::TAG_NAME,
ContentElementReference::GLOBALS_KEY,
ContentElementReference::PROXY_CLASS
)
);

$container->addCompilerPass(new FragmentRendererPass('contao.fragment.handler'));
$container->addCompilerPass(new RemembermeServicesPass('contao_frontend'));
$container->addCompilerPass(new MapFragmentsToGlobalsPass());
$container->addCompilerPass(new DataContainerCallbackPass());
$container->addCompilerPass(new TranslationDataCollectorPass());
$container->addCompilerPass(new RegisterHookListenersPass(), PassConfig::TYPE_OPTIMIZE);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Contao\CoreBundle\DependencyInjection\Compiler;

use Contao\CoreBundle\EventListener\GlobalsMapListener;
use Contao\CoreBundle\Fragment\FragmentConfig;
use Contao\CoreBundle\Fragment\FragmentOptionsAwareInterface;
use Contao\CoreBundle\Fragment\FragmentPreHandlerInterface;
Expand All @@ -38,13 +39,25 @@ class RegisterFragmentsPass implements CompilerPassInterface
*/
private $tag;

public function __construct(string $tag = null)
/**
* @var string|null
*/
private $globalsKey;

/**
* @var string|null
*/
private $proxyClass;

public function __construct(string $tag = null, string $globalsKey = null, string $proxyClass = null)
{
if (null === $tag) {
@trigger_error('Using "new RegisterFragmentsPass()" without passing the tag name has been deprecated and will no longer work in Contao 5.0.', E_USER_DEPRECATED);
}

$this->tag = $tag;
$this->globalsKey = $globalsKey;
$this->proxyClass = $proxyClass;
}

/**
Expand All @@ -64,6 +77,7 @@ public function process(ContainerBuilder $container): void
*/
protected function registerFragments(ContainerBuilder $container, string $tag): void
{
$globals = [];
$preHandlers = [];
$registry = $container->findDefinition('contao.fragment.registry');
$command = $container->hasDefinition('contao.command.debug_fragments') ? $container->findDefinition('contao.command.debug_fragments') : null;
Expand Down Expand Up @@ -102,10 +116,19 @@ protected function registerFragments(ContainerBuilder $container, string $tag):

$childDefinition->addTag($tag, $attributes);
$container->setDefinition($serviceId, $childDefinition);

if ($this->globalsKey && $this->proxyClass) {
if (!isset($attributes['category'])) {
throw new InvalidConfigurationException(sprintf('Missing category for "%s" fragment on service ID "%s"', $tag, (string) $reference));
}

$globals[$this->globalsKey][$attributes['category']][$attributes['type']] = $this->proxyClass;
}
}
}

$this->addPreHandlers($container, $preHandlers);
$this->addGlobalsMapListener($globals, $container);
}

protected function getFragmentConfig(ContainerBuilder $container, Reference $reference, array $attributes): Reference
Expand Down Expand Up @@ -168,4 +191,17 @@ protected function getFragmentType(Definition $definition, array $attributes): s

return Container::underscore($className);
}

private function addGlobalsMapListener(array $globals, ContainerBuilder $container): void
{
if (empty($globals)) {
return;
}

$listener = new Definition(GlobalsMapListener::class, [$globals]);
$listener->setPublic(true);
$listener->addTag('contao.hook', ['hook' => 'initializeSystem', 'priority' => 255]);

$container->setDefinition('contao.listener.'.ContainerBuilder::hash($listener), $listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
namespace Contao\CoreBundle\Fragment\Reference;

use Contao\ContentModel;
use Contao\ContentProxy;

class ContentElementReference extends FragmentReference
{
public const TAG_NAME = 'contao.content_element';
public const GLOBALS_KEY = 'TL_CTE';
public const PROXY_CLASS = ContentProxy::class;

public function __construct(ContentModel $model, string $section = 'main')
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
namespace Contao\CoreBundle\Fragment\Reference;

use Contao\ModuleModel;
use Contao\ModuleProxy;

class FrontendModuleReference extends FragmentReference
{
public const TAG_NAME = 'contao.frontend_module';
public const GLOBALS_KEY = 'FE_MOD';
public const PROXY_CLASS = ModuleProxy::class;

public function __construct(ModuleModel $model, string $section = 'main')
{
Expand Down
2 changes: 0 additions & 2 deletions core-bundle/tests/ContaoCoreBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Contao\CoreBundle\DependencyInjection\Compiler\CrawlerPass;
use Contao\CoreBundle\DependencyInjection\Compiler\DataContainerCallbackPass;
use Contao\CoreBundle\DependencyInjection\Compiler\MakeServicesPublicPass;
use Contao\CoreBundle\DependencyInjection\Compiler\MapFragmentsToGlobalsPass;
use Contao\CoreBundle\DependencyInjection\Compiler\PickerProviderPass;
use Contao\CoreBundle\DependencyInjection\Compiler\RegisterFragmentsPass;
use Contao\CoreBundle\DependencyInjection\Compiler\RegisterHookListenersPass;
Expand Down Expand Up @@ -60,7 +59,6 @@ public function testAddsTheCompilerPaths(): void
RegisterFragmentsPass::class,
FragmentRendererPass::class,
RemembermeServicesPass::class,
MapFragmentsToGlobalsPass::class,
DataContainerCallbackPass::class,
TranslationDataCollectorPass::class,
RegisterHookListenersPass::class,
Expand Down

0 comments on commit ddec13d

Please sign in to comment.