PHP code example of highcore / registry-bundle

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

    

highcore / registry-bundle example snippets


return [
    // ...
    Highcore\Bundle\RegistryBundle\RegistryBundle::class => ['all' => true],
];


declare(strict_types=1)

namespace App\YourBundle;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Highcore\Bundle\RegistryBundle\Compiler\Pass\ServiceAttributeRegistryPass;
use Highcore\Component\Criteria\Doctrine\Handler\CriteriaRepository;
use Highcore\Bundle\RegistryBundle\Registry\IdentityServiceRegistry;
use Highcore\Bundle\RegistryBundle\Registry\ServiceRegistry;

class YourBundle extends Bundle
{
    public function build(ContainerBuilder $container): void
    {
        parent::build($container);

        $container->addCompilerPass(new ServiceAttributeRegistryPass(
            definitionId: 'some.your.project.namespace.first.resource.registry',
            definitionClass: IdentityServiceRegistry::class,
            targetClassAttribute: \App\YourBundle\Attribute\AsYourResourceAttribute::class, // your attribute class
            interface: \App\YourBundle\YourServiceInterface::class, // your interface class (interface is optional, if passed, CompilerPass will check your service for an implementation of that interface)
        ));

        $container->addCompilerPass(new ServiceAttributeRegistryPass(
            definitionId: 'some.your.project.namespace.second.resource.registry',
            definitionClass: ServiceRegistry::class,
            targetClassAttribute: \App\YourBundle\Attribute\AsYourSecondResourceAttribute::class,
            // register registry without interface
        ));
    }
}


declare(strict_types=1)

namespace App\YourBundle;

interface YourServiceInterface
{
    public function yourMethod(): void;
}


declare(strict_types=1)

namespace App\YourBundle\Attribute;

use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;

#[\Attribute(\Attribute::TARGET_CLASS)]
#[NamedArgumentConstructor]
class AsYourResourceAttribute implements IdentityServiceAttributeInterface
{
    public function __construct(private readonly ?string $identifier = null)
    {
    }

    // If self::hasIdentifier() returns false, this method will not be called,
    // instead we will take the name of the class to which this attribute will be assigned as the identifier
    public function getIdentifier(): string
    {
        return $this->identifier;
    }

    public function hasIdentifier(): bool
    {
        return null !== $this->identifier;
    }
}


declare(strict_types=1)

namespace App;

use Highcore\Component\Registry\Attribute\ServiceAttributeInterface;

#[\Attribute(\Attribute::TARGET_CLASS)]
class AsYourSecondResourceAttribute implements ServiceAttributeInterface
{
}


declare(strict_types=1);

namespace App\YourBundle\Service;

use Highcore\JsonApi\Configurator\JsonApiResourceConfigurator;
use App\YourBundle\Attribute\AsYourResourceAttribute;
use App\YourBundle\YourServiceInterface;

#[AsYourResourceAttribute('some_identifier')]
class MyService implements YourServiceInterface
{
    public function yourMethod(): void
    {
        // Implementation of the configurator
    }
}

# src/YourBundle/Resources/config/services.php


declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator): void {
    $services = $configurator->services();
    $defaults = $services->defaults();
    $defaults->autowire();
    $defaults->autoconfigure();

    // that's all you need to register your service in the registry
    $services->set(\App\YourBundle\Service\MyService::class);
};

# src/YourBundle/Resources/config/services.php


declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $configurator): void {
    // ...
    $services->set(\App\YourBundle\Service\SomeYourServiceUsingRegistry::class)
        ->args([service('some.your.project.namespace.first.resource.registry')]);
};


declare(strict_types=1);

namespace App\YourBundle;

use Highcore\Component\Registry\IdentityServiceRegistryInterface;

final class SomeYourServiceUsingRegistry
{
    public function __construct(private readonly Highcore\Component\Registry\IdentityServiceRegistryInterface $registry)
    {
    }

    public function someMethod()
    {
        // Retrieve all registered services
        $yourServices = $this->registry->all();

        // Use the services
    }