PHP code example of ulrack / services

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

    

ulrack / services example snippets




use Ulrack\Services\Component\Registry\ServiceRegistry;

$serviceRegistry = new ServiceRegistry();



use GrizzIt\Validator\Component\Logical\AlwaysValidator;

$serviceRegistry->add(
    'services',
    'foo',
    [
        'class' => AlwaysValidator::class,
        'parameters' => [
            'alwaysBool' => true
        ]
    ]
);



use GrizzIt\ObjectFactory\Component\Analyser\ClassAnalyser;
use GrizzIt\ObjectFactory\Factory\ObjectFactory;
use Ulrack\Services\Component\Compiler\ServiceCompiler;
use GrizzIt\Storage\Component\ObjectStorage;
use GrizzIt\ObjectFactory\Component\Reflector\MethodReflector;

// The services storage in this example only exists during the execution.
// Use an alternative implementation of the StorageInterface to keep the compiled services.
$serviceStorage = new ObjectStorage();

$analysisStorage = new ObjectStorage();

$methodReflector = new MethodReflector();

$classAnalyser = new ClassAnalyser(
    $analysisStorage,
    $methodReflector
);

$objectFactory = new ObjectFactory($classAnalyser);

$serviceCompiler = new ServiceCompiler(
    $serviceRegistry,
    $serviceStorage,
    $objectFactory
);



use Ulrack\Services\Component\Compiler\ServicesCompiler;

$serviceCompiler->addExtension(
    // This will be the key which is used in the service definitions for services.
    'services',
    ServicesCompiler::class,
    // This is the sort order of the extension, it is used to determine the order of execution.
    0,
    // Optionaly a validator implementing the ValidatorInterface can be provided here
    null,
    // Parameters can be provided to the extension here.
    []
);



$serviceCompiler->addHook(
    // Note that the key must the same as the the compiler which needs to be hooked into.
    'services',
    MyHook::class,
    // The sort order to determine the order of execution.
    0,
    // Optional parameters for the hook.
    []
);



use Ulrack\Services\Factory\ServiceFactory;

$serviceFactory = new ServiceFactory(
    // The previously configured service compiler.
    $serviceCompiler,
    $objectFactory,
    $classAnalyser,
    $methodReflector
);




use Ulrack\Services\Factory\Extension\ServicesFactory;

$serviceFactory->addExtension(
    // The key on which services are registered.
    'services',
    ServicesFactory::class,
    // Optional parameters.
    []
);



$serviceFactory->addHook(
    // The key on which services are registered.
    'services',
    MyHook::class,
    // The sort order, used for the order of execution.
    0,
    // A set of optional parameters for the hook.
    []
);



// The key is a combination of the scope of the 
bash
composer example.php