2017 © Pedro Peláez
 

library dot-annotated-services

DotKernel service creation component through zend-servicemanager and annotations

image

dotkernel/dot-annotated-services

DotKernel service creation component through zend-servicemanager and annotations

  • Thursday, May 3, 2018
  • by dotkernel
  • Repository
  • 2 Watchers
  • 1 Stars
  • 569 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 9 % Grown

The README.md

dot-annotated-services

Dotkernel component used to create services through Laminas Service Manager and inject them with dependencies just using method annotations. It can also create services without the need to write factories. Annotation parsing can be cached, to improve performance., (*1)

This package can clean up your code, by getting rid of all the factories you write, sometimes just to inject a dependency or two., (*2)

OSS Lifecycle PHP from Packagist (specify version), (*3)

GitHub issues GitHub forks GitHub stars GitHub license, (*4)

Build Static codecov, (*5)

Installation

Run the following command in your project directory, (*6)

composer require dotkernel/dot-annotated-services

After installing, add the ConfigProvider class to your configuration aggregate., (*7)

Usage

Using the AnnotatedServiceFactory

You can register services in the service manager using the AnnotatedServiceFactory as below., (*8)

return [
    'factories' => [
        ServiceClass::class => AnnotatedServiceFactory::class,
    ],
];

You can use only the fully qualified class name as the service key., (*9)

The next step is to annotate the service constructor or setters with the service names to inject., (*10)

use Dot\AnnotatedServices\Annotation\Inject;

/**
 * @Inject({
 *     Dependency1::class,
 *     Dependency2::class,
 *     "config"
 * })
 */
public function __construct(
    protected Dependency1 $dep1,
    protected Dependency2 $dep2,
    protected array $config
) {
}

The annotation @Inject is telling the factory to inject the services between curly braces. Valid service names should be provided, as registered in the service manager., (*11)

To inject an array value from the service manager, you can use dot notation as below,, (*12)

use Dot\AnnotatedServices\Annotation\Inject;

/**
 * @Inject({"config.debug"})
 */

which will inject $container->get('config')['debug'];, (*13)

Even if using dot annotation, the annotated factory will check first if a service name exists with that name., (*14)

You can use the inject annotation on setters too, they will be called at creation time and injected with the configured dependencies., (*15)

Using the AnnotatedRepositoryFactory

You can register doctrine repositories and inject them using the AnnotatedRepositoryFactory as below., (*16)

return [
    'factories' => [
        ExampleRepository::class => AnnotatedRepositoryFactory::class,
    ],
];

The next step is to add the @Entity annotation in the repository class., (*17)

The name field has to be the fully qualified class name., (*18)

Every repository should extend Doctrine\ORM\EntityRepository., (*19)

use Doctrine\ORM\EntityRepository;
use Dot\AnnotatedServices\Annotation\Entity;

/**
 * @Entity(name="App\Entity\Example")
 */
class ExampleRepository extends EntityRepository
{
}

Using the abstract factory

Using this approach, no service manager configuration is required. It uses the registered abstract factory to create annotated services., (*20)

In order to tell the abstract factory which services are to be created, you need to annotate the service class with the @Service annotation., (*21)

use Dot\AnnotatedServices\Annotation\Service;

/*
 * @Service
 */
class ServiceClass
{
    // configure injections as described in the previous section
}

And that's it, you don't need to configure the service manager with this class, creation will happen automatically., (*22)

Cache annotations

This package is built on top of doctrine/annotation and doctrine/cache. In order to cache annotations, you should register a service factory at key AbstractAnnotatedFactory::CACHE_SERVICE that should return a valid Doctrine\Common\Cache\Cache cache driver. See Cache Drivers for available implementations offered by doctrine., (*23)

Below, we give an example, as defined in our frontend and admin starter applications:, (*24)

return [
    'annotations_cache_dir' => __DIR__ . '/../../data/cache/annotations',
    'dependencies' => [
        'factories' => [
            // used by dot-annotated-services to cache annotations
            // needs to return a cache instance from Doctrine\Common\Cache
            AbstractAnnotatedFactory::CACHE_SERVICE => AnnotationsCacheFactory::class,
        ]
    ],
];
namespace Frontend\App\Factory;

use Doctrine\Common\Cache\FilesystemCache;
use Psr\Container\ContainerInterface;

class AnnotationsCacheFactory
{
    public function __invoke(ContainerInterface $container)
    {
        //change this to suite your caching needs
        return new FilesystemCache($container->get('config')['annotations_cache_dir']);
    }
}

The Versions

03/05 2018

dev-master

9999999-dev

DotKernel service creation component through zend-servicemanager and annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

by n3vrax
by DotKerenel Team

services container zf2 annotations zf3 factories service-manager

03/05 2018

dev-develop

dev-develop

DotKernel service creation component through zend-servicemanager and annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

by n3vrax
by DotKerenel Team

services container zf2 annotations zf3 factories service-manager

03/05 2018

v1.1.1

1.1.1.0

DotKernel service creation component through zend-servicemanager and annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

by DotKerenel Team

services container zf2 annotations zf3 factories service-manager

15/03 2017

1.1.0

1.1.0.0

DotKernel service creation component through zend-servicemanager and annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

by n3vrax

services container zf2 annotations zf3 factories service-manager

10/03 2017

1.0.1

1.0.1.0

DotKernel service creation component through zend-servicemanager and annotations

  Sources   Download

MIT

The Requires

 

The Development Requires

by Popa Tiberiu

services container zf2 annotations zf3 factories service-manager

07/03 2017

1.0.0

1.0.0.0

Library providing auto-DI on services via annotations, without needing to write factories

  Sources   Download

MIT

The Requires

 

The Development Requires

by Popa Tiberiu

services container zf2 annotations zf3 factories service-manager