PHP code example of alsciende / make-registry-bundle

1. Go to this page and download the library: Download alsciende/make-registry-bundle 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/ */

    

alsciende / make-registry-bundle example snippets


// config/bundles.php

return [
    // ...
    Alsciende\Bundle\MakeRegistryBundle\AlsciendeMakeRegistryBundle::class => ['all' => true],
];

// src/OutputFormatter/OutputFormatterInterface.php


namespace App\OutputFormatter;

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

#[AutoconfigureTag('app.output_formatter')]
interface OutputFormatterInterface
{
    public function getName(): string;
}

// src/OutputFormatter/OutputFormatterRegistry.php


namespace App\OutputFormatter;

use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

#[AsAlias('app.output_formatter_registry')]
class OutputFormatterRegistry
{
    /**
     * @var array<OutputFormatterInterface>
     */
    private array $services = [];

    public function __construct(
        #[TaggedIterator('app.output_formatter')]
        iterable $services
    ) {
        foreach ($services as $service) {
            if ($service instanceof OutputFormatterInterface) {
                $name = $service->getName();

                if (array_key_exists($name, $this->services)) {
                    $duplicate = $this->services[$name];

                    throw new \LogicException(sprintf('Service name "%s" duplicate between %s and %s.', $name, $duplicate::class, $service::class));
                }
                $this->services[$name] = $service;
            }
        }
    }

    /**
     * @return array<string>
     */
    public function getNames(): array
    {
        return array_keys($this->services);
    }

    /**
     * @return array<OutputFormatterInterface>
     */
    public function getOutputFormatters(): array
    {
        return $this->services;
    }

    public function getOutputFormatter(string $name): OutputFormatterInterface
    {
        if (array_key_exists($name, $this->services)) {
            return $this->services[$name];
        }

        throw new \UnexpectedValueException(sprintf('Cannot find service "%s". Available services are %s.', $name, implode(',', $this->getNames())));
    }
}