PHPackages                             acelaya/zsm-annotated-services - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. acelaya/zsm-annotated-services

AbandonedArchivedLibrary[PSR &amp; Standards](/categories/psr-standards)

acelaya/zsm-annotated-services
==============================

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

v1.0.2(9y ago)108797MITPHPPHP ^5.6 || ^7.0

Since Apr 1Pushed 8y ago4 watchersCompare

[ Source](https://github.com/acelaya/zsm-annotated-services)[ Packagist](https://packagist.org/packages/acelaya/zsm-annotated-services)[ RSS](/packages/acelaya-zsm-annotated-services/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (10)Used By (0)

Zend\\ServiceManager Annotated Services
=======================================

[](#zendservicemanager-annotated-services)

[![Build Status](https://camo.githubusercontent.com/1a3ee13b148431c8f507199d6adb7ec6e96baa269e0caf02ee2d7787d6594ec2/68747470733a2f2f7472617669732d63692e6f72672f6163656c6179612f7a736d2d616e6e6f74617465642d73657276696365732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/acelaya/zsm-annotated-services)[![Code Coverage](https://camo.githubusercontent.com/c14a9945273d907140c03d38f4f46727467f987d70fb85aee3efd4e21f92674c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6163656c6179612f7a736d2d616e6e6f74617465642d73657276696365732f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/acelaya/zsm-annotated-services/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9897e31b2d58babcf979f759a90e1cb7a45d26b0c97ad1b00d1c3113754299be/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6163656c6179612f7a736d2d616e6e6f74617465642d73657276696365732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/acelaya/zsm-annotated-services/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/0d99408b8553ed9f397858f1fad6e27f4105ca7b6c5bc7ace8fe6f4b85ca39a3/68747470733a2f2f706f7365722e707567782e6f72672f6163656c6179612f7a736d2d616e6e6f74617465642d73657276696365732f762f737461626c652e706e67)](https://packagist.org/packages/acelaya/zsm-annotated-services)[![Total Downloads](https://camo.githubusercontent.com/0cb359d6c843459df5c971294d124c29fb8d05f849fcd44787bc926c65c249f2/68747470733a2f2f706f7365722e707567782e6f72672f6163656c6179612f7a736d2d616e6e6f74617465642d73657276696365732f646f776e6c6f6164732e706e67)](https://packagist.org/packages/acelaya/zsm-annotated-services)[![License](https://camo.githubusercontent.com/efc44e056adf5be391250d85c8cd35a25c63ee8ccb7ba07e7b08bcb842d2ca59/68747470733a2f2f706f7365722e707567782e6f72672f6163656c6179612f7a736d2d616e6e6f74617465642d73657276696365732f6c6963656e73652e706e67)](https://packagist.org/packages/acelaya/zsm-annotated-services)

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.

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

> **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.

### Installation

[](#installation)

Install this component with composer.

```
composer require acelaya/zsm-annotated-services

```

### Basic usage

[](#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.

```
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.

```
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)

```
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.

### Cache

[](#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.

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

First install the cache component.

```
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).

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

### Dot notation for array or ArrayAccess services

[](#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.

For example, imagine this services specification:

```
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:

```
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.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 97.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~38 days

Recently: every ~65 days

Total

8

Last Release

3425d ago

Major Versions

v0.2.0 → v1.0.02016-08-23

PHP version history (2 changes)v0.1.3PHP ^5.5|^7.0

v1.0.0PHP ^5.6 || ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/73480af83259e096d154a8c4333e550c186b28ccb7a0d11f537b9aa57ad35392?d=identicon)[acelaya](/maintainers/acelaya)

---

Top Contributors

[![acelaya](https://avatars.githubusercontent.com/u/2719332?v=4)](https://github.com/acelaya "acelaya (46 commits)")[![samsonasik](https://avatars.githubusercontent.com/u/459648?v=4)](https://github.com/samsonasik "samsonasik (1 commits)")

---

Tags

annotationsdependency-injectionfactorieszend-servicemanagerzf2zf3containerannotationsservice-managerzf2serviceszf3factories

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/acelaya-zsm-annotated-services/health.svg)

```
[![Health](https://phpackages.com/badges/acelaya-zsm-annotated-services/health.svg)](https://phpackages.com/packages/acelaya-zsm-annotated-services)
```

###  Alternatives

[psr/container

Common Container Interface (PHP FIG PSR-11)

10.0k1.0B3.7k](/packages/psr-container)[php-di/php-di

The dependency injection container for humans

2.8k48.9M995](/packages/php-di-php-di)[league/container

A fast and intuitive dependency injection container.

86387.8M342](/packages/league-container)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
