PHP Classes

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

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 61 All time: 10,486 This week: 560Up
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.

Picture of Payam Naderi
  Performance   Level  
Name: Payam Naderi <contact>
Classes: 15 packages by
Country: United States United States
Age: 41
All time rank: 2017286 in United States United States
Week rank: 136 Up14 in United States United States Up
Innovation award
Innovation award
Nominee: 2x

Documentation

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 (24)  
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 (24)  /  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 (24)  /  Container  /  Exception  
File Role Description
  Plain text file exContainerInvalidServiceType.php Class Class source

  Files folder image Files (24)  /  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 (24)  /  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 (24)  /  Exception  
File Role Description
  Plain text file exContainerCreateService.php Class Class source
  Plain text file exContainerNoService.php Class Class source

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

  Files folder image Files (24)  /  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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:61
This week:0
All time:10,486
This week:560Up