Reference example

 else {
            $code = '';
        }

        if (isset($this->referenceVariables[$targetId]) || (2 > $callCount && (!$hasSelfRef || !$forConstructor))) {
            return $code;
        }

        $name = $this->getNextVariableName();
        $this->referenceVariables[$targetId] = new Variable($name);

        $reference = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $behavior ? new Reference($targetId$behavior) : null;
        $code .= sprintf(" \$%s = %s;\n", $name$this->getServiceCall($targetId$reference));

        if (!$hasSelfRef || !$forConstructor) {
            return $code;
        }

        $code .= sprintf(<<<'EOTXT' if (isset($this->%s[%s])) { return $this->%1$s[%2$s]; }
$container->setDefinition($this->getServiceIdLoggerFormatter($servicePrefix)$this->createLoggerFormatter());
        $container->setDefinition($this->getServiceIdLogger($servicePrefix)$this->createLogger($servicePrefix));
    }

    protected function createLoggerHandler(string $servicePrefix): Definition
    {
        return (new Definition(RotatingFileHandler::class[
                sprintf('%%kernel.logs_dir%%/%s_%%kernel.environment%%.log', $servicePrefix),
                sprintf('%%%s%%', $this->getParameterNameLoggerMaxFiles($servicePrefix)),
                sprintf('%%%s%%', $this->getParameterNameLogLevel($servicePrefix)),
            ]))
            ->addMethodCall('pushProcessor', [new Reference('monolog.processor.uid')])
            ->setPublic(false)
        ;
    }

    protected function createLoggerFormatter(): Definition
    {
        return (new Definition(PsrLogMessageProcessor::class))
            ->setPublic(false)
        ;
    }

    
class RegisterDynamicController implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        /** @var array<string, array> $types */
        $types = $container->getParameter('shopware.bundle.content_type.types');

        foreach ($types as $name => $type) {
            $backendController = new Definition(ContentType::class);
            $backendController->setArguments([
                new Reference(\Shopware\Bundle\ContentTypeBundle\Services\ExtjsBuilderInterface::class),
                new Expression('service("Shopware\\\\Bundle\\\\ContentTypeBundle\\\\Services\\\\TypeProvider").getType("' . $name . '")'),
                new Reference('shopware.bundle.content_type.' . $name),
            ]);

            $backendController->addTag(
                'shopware.controller',
                [
                    'controller' => 'Custom' . ucfirst($name),
                    'module' => 'backend',
                ]
            );
            
class RegisterTypeRepositories implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        /** @var array<string, array> $types */
        $types = $container->getParameter('shopware.bundle.content_type.types');

        foreach (array_keys($types) as $name) {
            $def = new Definition(Repository::class);

            $def->setArguments([
                new Reference('dbal_connection'),
                new Expression('service("Shopware\\\\Bundle\\\\ContentTypeBundle\\\\Services\\\\TypeProvider").getType("' . $name . '")'),
                new Reference('translation'),
                new Reference('shopware_storefront.context_service'),
                new Reference(TypeFieldResolver::class),
            ]);

            $def->setPublic(true);

            $container->setDefinition('shopware.bundle.content_type.' . $name$def);
        }
    }
}
$container
            ->register(ReverseProxyCacheClearer::class)
            ->setPublic(true);

        $container
            ->register(FastlyReverseProxyGateway::class, FastlyService::class)
            ->setPublic(true);

        $container
            ->register(DummyService::class)
            ->setPublic(true)
            ->addArgument(new Reference(AbstractReverseProxyGateway::class, ContainerInterface::NULL_ON_INVALID_REFERENCE));

        $container->addCompilerPass(new ReverseProxyCompilerPass());

        return $container;
    }
}

/** * @internal */
class OriginalService
{
/** @var array<string, array<string, string>> $rateLimiterConfig */
        $rateLimiterConfig = $container->getParameter('shopware.api.rate_limiter');

        foreach ($rateLimiterConfig as $name => $config) {
            $this->setConfigDefaults($config);

            $def = new Definition(RateLimiterFactory::class);
            $def->addArgument($config + ['id' => $name]); // config
            $cacheDef = new Definition(CacheStorage::class);
            $cacheDef->addArgument(new Reference($config['cache_pool']));

            $def->addArgument($cacheDef);
            $def->addArgument(new Reference(SystemConfigService::class));
            $def->addArgument(new Reference($config['lock_factory']));

            $rateLimiter->addMethodCall('registerLimiterFactory', [$name$def]);
        }

        $container->setDefinition('shopware.rate_limiter', $rateLimiter);
    }

    

        $isDebugEnabled = $container->getParameterBag()->resolveValue($container->getParameter('kernel.debug'));

        if (!$isDebugEnabled) {
            $container->removeDefinition(DataCollector::class);

            return;
        }

        $clientDecorator = new Definition(ClientProfiler::class);
        $clientDecorator->setArguments([
            new Reference('shopware.es.profiled.client.inner'),
        ]);
        $clientDecorator->setDecoratedService(Client::class);

        $container->setDefinition('shopware.es.profiled.client', $clientDecorator);

        $adminClientDecorator = new Definition(ClientProfiler::class);
        $adminClientDecorator->setArguments([
            new Reference('shopware.es.profiled.adminClient.inner'),
        ]);
        $adminClientDecorator->setDecoratedService('admin.openSearch.client');

        
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ConfigureApiResourcesPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        foreach ($container->findTaggedServiceIds('shopware.api_resource') as $id => $options) {
            $definition = $container->getDefinition($id);
            $definition->addMethodCall('setContainer', [new Reference('service_container')]);
            $definition->addMethodCall('setManager', [new Reference('models')]);
        }
    }
}

    public function testEntityRepositoryAutowiring(): void
    {
        $container = new ContainerBuilder();

        $container->register(CustomerAddressDefinition::class, CustomerAddressDefinition::class)
            ->addTag('shopware.entity.definition');
        $container->register(CustomerDefinition::class, CustomerDefinition::class)
            ->addTag('shopware.entity.definition');

        $container->register(DefinitionInstanceRegistry::class, DefinitionInstanceRegistry::class)
            ->addArgument(new Reference('service_container'))
            ->addArgument([
                CustomerDefinition::ENTITY_NAME => CustomerDefinition::class,
                CustomerAddressDefinition::ENTITY_NAME => CustomerAddressDefinition::class,
            ])
            ->addArgument([
                CustomerDefinition::ENTITY_NAME => 'customer.repository',
                CustomerAddressDefinition::ENTITY_NAME => 'customer_address.repository',
            ]);

        $entityCompilerPass = new EntityCompilerPass();
        $entityCompilerPass->process($container);

        
$service = $container->getDefinition($serviceId);

            $repositoryId = 'sales_channel.' . $entityNames['entityName'] . '.repository';

            try {
                $repository = $container->getDefinition($repositoryId);
                $repository->setPublic(true);
            } catch (ServiceNotFoundException) {
                $repository = new Definition(
                    SalesChannelRepository::class,
                    [
                        new Reference($service->getClass()),
                        new Reference(EntityReaderInterface::class),
                        new Reference(EntitySearcherInterface::class),
                        new Reference(EntityAggregatorInterface::class),
                        new Reference('event_dispatcher'),
                        new Reference(EntityLoadedEventFactory::class),
                    ]
                );
                $repository->setPublic(true);

                $container->setDefinition($repositoryId$repository);

                
$definition = $container->getDefinition($id);
            } catch (ServiceNotFoundException $exception) {
                // Might be an alias, we don't want to register those                 continue;
            }

            $definitionClass = $definition->getClass();
            if (!\is_string($definitionClass)) {
                continue;
            }
            if (is_a($definitionClass, ContainerAwareInterface::class, true)) {
                $definition->addMethodCall('setContainer', [new Reference('service_container')]);
            }
        }
    }
}

    private function findAndSortTaggedServices($tagName, ContainerBuilder $container)
    {
        $services = $container->findTaggedServiceIds($tagName);

        $queue = new SplPriorityQueue();

        foreach ($services as $serviceId => $tags) {
            foreach ($tags as $attributes) {
                $priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
                $queue->insert(new Reference($serviceId)$priority);
            }
        }

        return iterator_to_array($queue, false);
    }
}

class ActionEventCompilerPassTest extends TestCase
{
    public function testProcess(): void
    {
        $container = new ContainerBuilder();

        $container->register(CustomerDefinition::class, CustomerDefinition::class);
        $container->register(OrderDefinition::class, OrderDefinition::class);

        $container->register(DefinitionInstanceRegistry::class, DefinitionInstanceRegistry::class)
            ->addArgument(new Reference('service_container'))
            ->addArgument([])
            ->addArgument([]);

        $container->register(BusinessEventRegistry::class, BusinessEventRegistry::class)
            ->addArgument(new Reference(DefinitionInstanceRegistry::class));

        $pass = new TestActionEventCompilerPass();
        $pass->process($container);

        $registry = $container->get(BusinessEventRegistry::class);

        
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

#[Package('core')] class AssetRegistrationCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container): void
    {
        $assets = [];
        foreach ($container->findTaggedServiceIds('shopware.asset') as $id => $config) {
            $container->getDefinition($id)->addTag('assets.package', ['package' => $config[0]['asset']]);
            $assets[$config[0]['asset']] = new Reference($id);
        }

        $assetService = $container->getDefinition('assets.packages');
        $assetService->addMethodCall('setDefaultPackage', [$assets['asset']]);

        if ($container->hasDefinition(ThemeCompiler::class)) {
            $container->getDefinition(ThemeCompiler::class)->replaceArgument(6, $assets);
        }
    }
}

        $entityNameMap = [];
        $repositoryNameMap = [];
        $services = $container->findTaggedServiceIds('shopware.entity.definition');

        $ids = array_keys($services);

        foreach ($ids as $serviceId) {
            $service = $container->getDefinition($serviceId);

            $service->addMethodCall('compile', [
                new Reference(DefinitionInstanceRegistry::class),
            ]);
            $service->setPublic(true);

            /** @var string $class */
            $class = $service->getClass();
            /** @var EntityDefinition $instance */
            $instance = new $class();

            $entityNameMap[$instance->getEntityName()] = $serviceId;
            $entity = $instance->getEntityName();

            
Home | Imprint | This part of the site doesn't use cookies.