PHPackages                             mtymek/blast-reflection-factory - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mtymek/blast-reflection-factory

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mtymek/blast-reflection-factory
===============================

Universal auto-wiring factory for Laminas ServiceManager.

2.1(3y ago)1967.3k↓49.5%1[1 PRs](https://github.com/mtymek/blast-reflection-factory/pulls)BSD-2-ClausePHPPHP ^7.4 || ~8.0 || ~8.1CI passing

Since Jun 26Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/mtymek/blast-reflection-factory)[ Packagist](https://packagist.org/packages/mtymek/blast-reflection-factory)[ RSS](/packages/mtymek-blast-reflection-factory/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (5)Versions (11)Used By (0)

Blast\\ReflectionFactory
========================

[](#blastreflectionfactory)

[![Build Status](https://github.com/mtymek/blast-reflection-factory/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/mtymek/blast-reflection-factory/actions/workflows/continuous-integration.yml)

Universal auto-wiring factory for Laminas ServiceManager.

Introduction
------------

[](#introduction)

Writing factories for Laminas ServiceManager can be boring, repeatable task. Typical service will consume one or more dependencies using constructor injection:

```
class Mailer
{
    public function __construct(MailTransportInterface $transport, MailRenderer $renderer)
    {
      // ...
    }
}
```

This is how factory is going to look like:

```
class MailerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        return new Mailer(
            $container->get(MailTransportInterface::class),
            $container->get(MailRenderer::class)
        );
    }
}
```

In typical application, you will end up with multiple factories that simply pull some services and create new object. `ReflectionFactory` can take care of this use case for you - it uses `Reflection` to scan parameter types in constructor and instantiates new object based on this information.

Installation
------------

[](#installation)

Install this package using Composer:

```
$ composer require mtymek/blast-reflection-factory

```

Usage
-----

[](#usage)

After installing this package, all you have to do is to tell ServiceManager to use `ReflectionFactory` to create your services.

For Laminas Mezzio application, configuration can look like this:

```
use Blast\ReflectionFactory\ReflectionFactory;

return [
    'dependencies' => [
        'factories' => [
            // use normal factory for classes that require complex instantiation
            SmtpMailTransport::class => SmtpMailTransportFactory::class,

            // use ReflectionFactory for auto-wiring
            MailRenderer::class => ReflectionFactory::class,
            Mailer::class => ReflectionFactory::class,
        ],
        'aliases' => [
            MailTransportInterface::class => SmtpMailTransport::class,
        ],
    ]
];
```

### Caching

[](#caching)

Auto-wiring is expensive operation, so `ReflectionFactory` allows to store the result on disk to be reused later:

```
\Blast\ReflectionFactory\ReflectionFactory::enableCache('data/cache/reflection-factory.cache.php');
```

If you are using Zend Expressive Skeleton Application, then `config/container.php` would be a good place to enable this cache.

#### Warming-up cache

[](#warming-up-cache)

Cache file is automatically updated when a service is pulled from the container for the first time. This can lead to race conditions when your application is under heavy load. In order to avoid it, cache should be warmed up during deployment phase. The easiest way to do it is to go through all configured factories, pulling every service from the container.

Example script for applications based on Mezzio Skeleton:

```
