PHP Classes
elePHPant
Icontem

PHP IoC Container Service Manager: Dependency injection containers that can be nested

Recommend this page to a friend!
  Info   View files Documentation   View files View files (24)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-05-25 (19 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 57 This week: 5All time: 9,463 This week: 76Up
Version License PHP version Categories
ioc-service-manager 1.0MIT/X Consortium ...5Design Patterns
Description Author

This package can be used to dependency injection containers that can be nested.

It can register one or more service handler classes that will be called when a service is requested.

It also supports nested containers that depend on each other.

  Performance   Level  
Name: Payam Naderi <contact>
Classes: 11 packages by
Country: Iran Iran
Innovation award
Innovation award
Nominee: 2x

Details

Poirot\Container

Modern. Fast. Minimalism. Service Manager Container.

Overview usage sample

class defaultService extends AbstractService 
{
    /
     * Create Service
     *
     * @return mixed
     */
    function createService()
    {
        return new Directory();
    }
}


$container = new Container('main');
$container->set(new FactoryService(['name' => 'sysdir',
    'delegate' => function() {
        // Delegates will bind to service object as closure method
        / @var FactoryService $this */
        $sc = $this->getServiceContainer();
        return $sc->from('files')->get('folder');
    },
    'allow_override' => false]
));

$nest = new Container('main');
$nest->set(new defaultService(['name' => 'directory', 'allow_override' => true]));
$nest->setAlias('dir', 'directory');
$nest->setAlias('folder', 'dir');
$nest->setAlias('boom', 'boomService');

$container->nest($nest, 'files');

$dir = $container->get('sysdir')
    ->scanDir();

Or From Builder

$container = new Container(new ContainerBuilder([
    'namespace' => 'main',
    'services'  => [
        'FactoryService' => [ // Prefixed with Container namespace
            'name' => 'sysdir',
            'delegate' => function() {
                // Delegates will bind to service object as closure method
                / @var FactoryService $this */
                $sc = $this->getServiceContainer();
                return $sc->from('files')->get('folder');
            },
            'allow_override' => false
        ],
    ],
    'nested' => [
        [
            'namespace' => 'files',
            'services'  => [
                new defaultService(['name' => 'directory'
                    , 'allow_override' => true
                ])
            ],
            'aliases' => [
                'dir'    => 'directory',
                'folder' => 'dir',
            ],
        ],
    ],
]));

$dir = $container->get('sysdir')
    ->scanDir();

Nested Hierarchy

$container = new Container(new ContainerBuilder([
    'namespace' => 'main',
    'services'  => [
        'FactoryService' => [ // Prefixed with Container namespace
            'name' => 'sysdir',
            'delegate' => function() {
                // Delegates will bind to service object as closure method
                / @var FactoryService $this */
                $sc = $this->getServiceContainer();
                return $sc->from('/filesystem/system')->get('folder'); // <<<<<=====----
            },
            'allow_override' => false
        ],
    ],
    'nested' => [
        [
            'namespace' => 'filesystem',
            'nested' => [                                             // <<<<<=====----
                'system' => [
                    'services'  => [
                        new defaultService(['name' => 'directory'
                            , 'allow_override' => true
                        ])
                    ],
                    'aliases' => [
                        'dir'    => 'directory',
                        'folder' => 'dir',
                    ],
                ],
            ],
        ],
    ],
]));

Shared Service as Alias

$container = new Container(new ContainerBuilder([
    'namespace' => 'main',
    'aliases' => [
        'sysdir' => ['/filesystem/system', 'folder'],  // <<<====---- Shared Alias
    ],
    'nested' => [
        [
            'namespace' => 'filesystem',
            'nested' => [
                'system' => [
                    'services'  => [
                        new defaultService(['name' => 'directory' // <<<===--- share this
                            , 'allow_override' => true
                        ])
                    ],
                    'aliases' => [
                        'dir'    => 'directory',
                        'folder' => 'dir', // <<<===--- consumed here
                    ],
                ],
            ],
        ],
    ],
]));

/ @var Directory $dir */
$dir = $container->get('sysdir')
    ->scanDir();

Invoke Services With Service Options

we can build any container service with some options these options must implemented in iCService class interface an example of usage is on FunctorService.

$container->set(new FunctorService([
   'name'     => 'service_name',
   'callable' => function($arg1, $arg2) {
       # callable function will bind to service object as closure method
       # so you can access methods from FunctorService
       $sc = $this->getServiceContainer();

       # here we return service result
       return $arg1.' '.$arg2;
   },
   'allow_override'   => false
]));

$container->fresh('service_name', [$arg1Val, $arg2Val]);

Understand Refresh Service Retrieve

$services->set(new FunctorService('dynamicUri', function($arg = null) {
    return sprintf(
        '%s Service Requested. <br/>'
        , date('H:i:s'), $arg
    );
}));

echo $services->get('dynamicUri');
sleep(2);
echo $services->get('dynamicUri');
sleep(2);
echo $services->fresh('dynamicUri');
sleep(2);
echo $services->get('dynamicUri');
sleep(2);
echo $services->get('dynamicUri', ['arg' => 'this is new request because options changed.']);

result:

12:19:49 Service Requested. 
12:19:49 Service Requested. 
12:19:53 Service Requested. // fresh request
12:19:49 Service Requested. 
12:19:57 Service Requested. // with new options consume as fresh
  Files folder image Files  
File Role Description
Files folder imageContainer (3 files, 3 directories)
Files folder imageException (2 files)
Files folder imageInterfaces (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Plain text file Container.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Plain text file _functions.php Class Class source

  Files folder image Files  /  Container  
File Role Description
Files folder imageException (1 file)
Files folder imageInterfaces (4 files)
Files folder imageService (5 files)
  Plain text file aContainerCapped.php Class Class source
  Plain text file BuildContainer.php Class Class source
  Plain text file InitializerAggregate.php Class Class source

  Files folder image Files  /  Container  /  Exception  
File Role Description
  Plain text file exContainerInvalidServiceType.php Class Class source

  Files folder image Files  /  Container  /  Interfaces  
File Role Description
  Plain text file iContainerInitializer.php Class Class source
  Plain text file iContainerService.php Class Class source
  Plain text file iServiceFeatureAggregate.php Class Class source
  Plain text file iServiceFeatureDelegate.php Class Class source

  Files folder image Files  /  Container  /  Service  
File Role Description
  Plain text file aServiceAggregate.php Class Class source
  Plain text file aServiceContainer.php Class Class source
  Plain text file ServiceFactory.php Class Class source
  Plain text file ServiceInstance.php Class Class source
  Plain text file ServicePluginLoader.php Class Class source

  Files folder image Files  /  Exception  
File Role Description
  Plain text file exContainerCreateService.php Class Class source
  Plain text file exContainerNoService.php Class Class source

  Files folder image Files  /  Interfaces  
File Role Description
Files folder imageRespec (3 files)
  Plain text file iContainer.php Class Class source

  Files folder image Files  /  Interfaces  /  Respec  
File Role Description
  Plain text file iServicesAware.php Class Class source
  Plain text file iServicesComplex.php Class Class source
  Plain text file iServicesProvider.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:57
This week:5
All time:9,463
This week:76Up