PHP code example of setono / composite-compiler-pass

1. Go to this page and download the library: Download setono/composite-compiler-pass library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

setono / composite-compiler-pass example snippets



final class YourCompositeService
{
    /**
     * @var list<object>
     */
    private array $taggedServices = [];

    public function add(object $taggedService): void
    {
        $this->taggedServices[] = $taggedService;
    }
}



use Setono\CompositeCompilerPass\CompositeCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class YourBundle extends Bundle
{
    public function build(ContainerBuilder $container): void
    {
        $container->addCompilerPass(new CompositeCompilerPass('your_bundle.your_service', 'tag'));
    }
}



use Setono\CompositeCompilerPass\CompositeService;

/**
 * @property list<TaggedServiceInterface> $services
 *
 * @extends CompositeService<TaggedServiceInterface>
 */
final class ConcreteCompositeService extends CompositeService implements TaggedServiceInterface
{
    public function process(): void
    {
        foreach ($this->services as $service) {
            // Both your IDE, Psalm, and PHPStan knows that $service is an instance of TaggedServiceInterface
        }
    }
}

interface TaggedServiceInterface
{

}