PHP code example of kostislav / php-class-symfony-service-config

1. Go to this page and download the library: Download kostislav/php-class-symfony-service-config 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/ */

    

kostislav / php-class-symfony-service-config example snippets


return function(ContainerConfigurator $configurator) {
    $services = $configurator->services();

    $services->set('innerService1', SimpleService::class)
        ->arg(['Hello']);

    $services->set('innerService2', SimpleService::class)
        ->args(['world!']);

    $services->set('combinedService', CombinedService::class)
        ->args(service('innerService1'), service('innerService2'))
        ->public();
}

class ExampleConfig {
    #[ServiceDefinition]
    public function innerService1(): SimpleService {
        return new SimpleService('Hello');
    }

    #[ServiceDefinition]
    public function innerService2(): SimpleService {
        return new SimpleService('world!');
    }

    #[ServiceDefinition(isPublic: true)]
    public function combinedService(SimpleService $innerService1, SimpleService $innerService2): CombinedService {
        return new CombinedService($innerService1, $innerService2);
    }
}

protected function configureContainer(ContainerBuilder $container, Loader $loader): void {
    $loader->getResolver()->addLoader(new Kostislav\ClassConfig\ConfigClassServiceConfigLoader($container));
    // other stuff
}

protected function configureContainer(ContainerBuilder $container, Loader $loader): void {
    $loader->getResolver()->addLoader(new Kostislav\ClassConfig\ConfigClassServiceConfigLoader($container));
    // now you can load all your config classes with $loader
    $loader->load(MyConfig::class);
}

#[ServiceDefinition('alternative.name')]
public function whatever(): SimpleService {
    return new SimpleService('serv1');
}

#[ServiceDefinition(isPublic: true)]
public function publicService(): SimpleService {
    return new SimpleService('serv1');
}

#[ServiceDefinition]
public function myService(#[Service('annotations.cached_reader')] Reader annotationReader): SimpleService {
    // do whatever here
}

#[ServiceDefinition]
public function myService(#[Parameter('kernel.debug')] string $debug): SimpleService {
    // do whatever here
}

#[Import(AnotherConfig::class)]
class OneConfig {
    // some service definitions here
}

#[Tag('kernel.event_listener')]
#[ServiceDefinition]
public function myService(): SimpleService {
    // do whatever here
}