PHP code example of aatis / dependency-injection

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

    

aatis / dependency-injection example snippets


(new ContainerBuilder($ctx))->build();

// Directly in PHP code when building the container :

(new ContainerBuilder($ctx))
    ->excludePaths('/Folder')
    ->excludePaths('/OtherFolder/file.txt')
    ->build();

// Directly in PHP code when building the container :

(new ContainerBuilder($ctx))
    ->register(Service::class, [
        'environment' => ['env_name1', 'env_name2'],
        'arguments' => [
            'variable_name' => 'value',
        ],
        'tags' => [
            'tag_name_1',
            ['tag' => 'tag_name_2', 'priority' => 10],
        ],
    ])
    ->build();

public function __construct(string $_env_var)
{
    // ...
}

// Env Variable
$container->get('@_ENV_VAR_NAME');
$container->set('@_ENV_VAR_NAME', 'value');

// Service
$container->get(Service::class);

$service = $container->get(ServiceFactory::class)->create(Service::class);
$container->set(Service::class, $service);

$tagBuilder = $container->get(ServiceTagBuilder::class);

// returns the instance of the services tagged
$taggedServiceInstances = $container->get($tagBuilder->buildFromName('tag_name_1'));

// returns the Service component instances of the services tagged
$taggedServices = $container->get($tagBuilder->buildFromName('tag_name_1', [ServiceTagOption::SERVICE_TARGETED]));

$tagBuilder = $container->get(ServiceTagBuilder::class);
$taggedServiceInstances = $container->get($tagBuilder->buildFromInterface(Interface::class));

$tagBuilder = $container->get(ServiceTagBuilder::class);

// returns the Service component instance of the service targeted
$service = $container->get($tagBuilder->buildFromName(Service::class, [ServiceTagOption::FROM_CLASS, ServiceTagOption::SERVICE_TARGETED]));

// returns the instance of the service targeted (same as $container->get(Service::class))
$serviceInstance = $container->get($tagBuilder->buildFromName(Service::class, [ServiceTagOption::FROM_CLASS]));


#[AsDefaultTaggedService]
class ServiceWithTags
{
}

#[AsDefaultTaggedService(['tag_name_1', SomeInterface::class])]
class AnotherServiceWithTags implements SomeInterface
{
}


use Aatis\DependencyInjection\Interface\ServiceSubscriberInterface;
use Aatis\Tag\Interface\TagBuilderInterface;
use Psr\Container\ContainerInterface;

class MyService implements ServiceSubscriberInterface
{
    public function __construct(
        private ContainerInterface $container,
    ) {
    }

    public static function getSubscribedServices(TagBuilderInterface $tagBuilder): iterable
    {
        yield $tagBuilder->buildFromInterface(SomeInterface::class);
        yield $tagBuilder->buildFromName('specific_tag');
    }
}


use Aatis\DependencyInjection\Trait\ServiceSubscriberTrait;

/**
 * @template InputService of Service<SomeInterface>
 * @template OutputService of Service<SomeInterface> 
 * @template Context of array{key: string}
 */
class MyService implements ServiceSubscriberInterface
{
    /**
     * @use ServiceSubscriberTrait<InputService, OutputService, Context>
     */
    use ServiceSubscriberTrait {
        __construct as initServiceSubscriber;
    }
    
    public function __construct(ContainerInterface $container) {
        // Initialize the trait with the service stack
        $this->initServiceSubscriber($container);
    }
}

protected function pick(mixed $service, array $ctx): bool
{
    // Custom selection logic
}

protected function transformOut(mixed $service, array $ctx): mixed
{
    // Custom transformation logic
}

$service = $container->get(ServiceFactory::class)->create(Service::class);

$service = $container->get(ServiceFactory::class)->create(Service::class);

// Method 1
$instance = $container->get(ServiceInstanciator::class)->instanciate($service)

// Method 2
$instance = new Service($arg1, $arg2, ...);

$service->setInstance($instance);
$container->set(Service::class, $service);
yaml
# In config/services.yaml file :

exclude_paths:
  - "/Folder"
  - "/OtherFolder/file.txt"
  - <...>