2017 © Pedro Peláez
 

library zsm-annotated-services

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

image

acelaya/zsm-annotated-services

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  • Sunday, July 23, 2017
  • by acelaya
  • Repository
  • 3 Watchers
  • 10 Stars
  • 859 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

Zend\ServiceManager Annotated Services

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License, (*1)

If you are tired of defining lots of factories in your projects just to fetch some dependencies from the ServiceManager and the create a new service instance that gets those dependencies injected, try this., (*2)

It is a component that allows to define how dependency injection has to be performed with Zend\ServiceManager via annotations., (*3)

Important! While I will keep maintaining this project and providing bugfixes, I recommend you use the ConfigAbstractFactory instead, which is included in the ServiceManager package since v3.2., (*4)

Installation

Install this component with composer., (*5)

composer require acelaya/zsm-annotated-services

Basic usage

The traditional process is that you need to create a factory for each new service. Maybe sometimes you can reuse certain factories or abstract factories, but it is not the usual case., (*6)

namespace Acelaya;

use Interop\Container\ContainerInterface;

class MyFactory
{
    public funciton __invoke(ContainerInterface $container, $requestedName)
    {
        $foo = $container->get(Foo::class);
        $bar = $container->get('bar');

        return new MyService($foo, $bar);
    }
}

With this component you just need to add a simple annotation to your service constructor with the services that need to be fetched from the ServiceManager and injected., (*7)

namespace Acelaya;

use Acelaya\ZsmAnnotatedServices\Annotation\Inject;

class MyService
{
    /**
     * @Inject({Foo::class, "bar"})
     */
    public function __construct($foo, $bar)
    {
        // [...]
    }

    // [...]
}

And then, register the service with one of the provided factories (There is one factory that's used with Zend\ServiceManager 2 and another that's used with Zend\ServiceManager 3), (*8)

use Acelaya\MyService;
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
use Zend\ServiceManager\ServiceManager;

$sm = new ServiceManager([
    'factories' => [
        MyService::class => AnnotatedFactory::class,
    ],
]);

You just need to replace Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory by Acelaya\ZsmAnnotatedServices\Factory\V2\AnnotatedFactory if you are using the v2 ServiceManager., (*9)

Cache

That looks cool, but processing annotations takes time. If you use this approach with several services, you will see your application's performance reduced., (*10)

That's why this library allows to use Doctrine\Cache adapters in order to cache the result of processing annotations., (*11)

First install the cache component., (*12)

composer require doctrine/cache

Then register another service which returns a Doctrine\Common\Cache\Cache instance with the key Acelaya\ZsmAnnotatedServices\Factory\AbstractAnnotatedFactory::CACHE_SERVICE (or just "Acelaya\ZsmAnnotatedServices\Cache", which is the value of the constant)., (*13)

By doing this, your annotations will be processed and cached, improving performance for subsequent requests., (*14)

Dot notation for array or ArrayAccess services

When you need to inject just one part of a service which contains an array or ArrayAccess object, you can use the dot notation, where the first part is the service name and the rest of the parts are the keys to fetch from the array., (*15)

For example, imagine this services specification:, (*16)

use Acelaya\MyService;
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
use Zend\ServiceManager\ServiceManager;

$sm = new ServiceManager([
    'services' => [
        'config' => [
            'mail' => [
                'smtp' => [
                    // [...]
                ],
                'from' => 'foo@bar.com',
                'subject' => 'Welcome!',
            ],
            'logger' => [
                'file' => '/var/log/my_log.log'
            ],
        ],
    ],
    'factories' => [
        MyService::class => AnnotatedFactory::class,
    ],
]);

And this service with the @Inject annotation:, (*17)

namespace Acelaya;

use Acelaya\ZsmAnnotatedServices\Annotation\Inject;

class MyService
{
    /**
     * @Inject({"config.mail.from"})
     */
    public function __construct($from)
    {
        // The value of $from will be 'foo@bar.com'
    }
}

The injectable service is defined by config.mail.from. In this case, the AnnotatedFactory will assume that the service name is config, and that it contains an associative array or ArrayAccess object. Then, it will use the rest of the dotted parts as nested keys in that array, and finally get the last value and inject it in the service., (*18)

The Versions

23/07 2017

dev-master

9999999-dev

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

23/07 2017

dev-develop

dev-develop

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

23/12 2016

v1.0.2

1.0.2.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

31/08 2016

v1.0.1

1.0.1.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

23/08 2016

v1.0.0

1.0.0.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

10/04 2016

v0.2.0

0.2.0.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

06/04 2016

v0.1.3

0.1.3.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

02/04 2016

v0.1.2

0.1.2.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

02/04 2016

v0.1.1

0.1.1.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager

01/04 2016

v0.1.0

0.1.0.0

A component to define how dependency injection has to be performed with Zend\ServiceManager via annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

services container zf2 annotations zf3 factories service-manager