48 lines
1.5 KiB
PHP
48 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace KupShop\GTMBundle\DependencyInjection\Compiler;
|
|
|
|
use KupShop\GTMBundle\Utils\GTMLoader;
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
/**
|
|
* Check out https://symfony.com/doc/current/service_container/tags.html.
|
|
*/
|
|
class GTMPass implements CompilerPassInterface
|
|
{
|
|
public function process(ContainerBuilder $container)
|
|
{
|
|
// always first check if the primary service is defined
|
|
if (!$container->has(GTMLoader::class)) {
|
|
return;
|
|
}
|
|
|
|
$definition = $container->findDefinition(GTMLoader::class);
|
|
|
|
// find all service IDs with the kupshop.gtm.pageType tag
|
|
$taggedServices = $container->findTaggedServiceIds('kupshop.gtm.pageType');
|
|
|
|
foreach ($taggedServices as $id => $tags) {
|
|
$class = $container->findDefinition($id)->getClass();
|
|
|
|
foreach ($class::getSupportedViews() as $view) {
|
|
$definition->addMethodCall('setPageType', [$view, $class]);
|
|
}
|
|
}
|
|
|
|
// find all service IDs with the kupshop.gtm.pageType tag
|
|
$taggedServices = $container->findTaggedServiceIds('kupshop.gtm.ecommerce');
|
|
|
|
foreach ($taggedServices as $id => $tags) {
|
|
$class = $container->findDefinition($id)->getClass();
|
|
|
|
foreach ($tags as $tag) {
|
|
if ($tag['ecType']) {
|
|
$definition->addMethodCall('addEcommerceType', [$tag['ecType'], $class]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|