symplify/autowire-array-parameter

Autowire array parameters for your Symfony applications

Fund package maintenance!
tomasvotruba

Installs: 716 444

Dependents: 31

Suggesters: 0

Security: 0

Stars: 21

Watchers: 1

Forks: 0

v8.1.7 2020-07-14 22:36 UTC

README

Downloads total

Install

composer require symplify/autowire-array-parameter

Usage

1. Register Compiler Pass in kernel

<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\AutowireArrayParameterCompilerPass;

class AppKernel extends Kernel
{
    protected function build(ContainerBuilder $containerBuilder): void
    {
        $containerBuilder->addCompilerPass(
            new AutowireArrayParameterCompilerPass([
                // place for excluding types to resolve edge cases
                'Sonata\CoreBundle\Model\Adapter\AdapterInterface'
            ]
        ));
    }
}

2. Autowire Array Parameters

This feature surpasses YAML-defined, tag-based or CompilerPass-based collectors in minimalistic way:

<?php

class Application
{
    /**
     * @var Command[]
     */
    private $commands = [];

    /**
     * @param Command[] $commands
     */
    public function __construct(array $commands)
    {
        $this->commands = $commands;
        var_dump($commands); // instnace of Command collected from all services
    }
}