PHPackages                             everon/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. everon/factory

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

everon/factory
==============

Everon Factory Component

3.0.0(4y ago)03.3M—9%[1 PRs](https://github.com/oliwierptak/everon-factory/pulls)3MITPHPPHP ^8CI passing

Since Dec 14Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/oliwierptak/everon-factory)[ Packagist](https://packagist.org/packages/everon/factory)[ RSS](/packages/everon-factory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (18)Used By (3)

Everon Factory Component
========================

[](#everon-factory-component)

Library to handle dependency injection and instantiation. Allows to produce code that is easy to test.

### Versions

[](#versions)

- Use v1.x and v2.x with PHP 7.2+
- Use v3.x with PHP 8+

Features
--------

[](#features)

- One line, lazy loaded dependency injection (via setters or constructors)
- Trait based, simple to use and implement, no hassle with config files, pure PHP
- Factory/FactoryWorker gives full control and overview, on how each and every object is created
- FactoryWorker allows for custom implementation of Factory methods
- Full control when a dependency should be reused (via Dependency Container) or created from scratch (eg. Logger vs Value Object)
- Minimized file/memory access/usage due to callbacks and lazy load
- Intuitive Interface: clear, small and simple API
- Convention over configuration
- Clean code

How it works
------------

[](#how-it-works)

Every instantiation should happen only inside of the `FactoryWorker` class. Its role is similar of `AbstractFactory`. It sits between *Everon Factory Component* and the client code that uses it, so there is no direct coupling. It's easy to implement and manage, because injection is cheap and dependency setup happens in one place.

This makes testing much easier, as everything can be easily mocked/stubbed/faked. It's ok to use `new` operator outside of Factory methods for simple value object like classes. For example `new Collection()`.

For a particular module or application, the `registerBeforeWork` method of `FactoryWorker` is one of a places where you could setup your dependency tree. Or you could use [Root Composition pattern](http://blog.ploeh.dk/2011/07/28/CompositionRoot/) and handle your whole dependency graph outside of your application.

### Easy Dependency Injection

[](#easy-dependency-injection)

To use dependency injection, register it with `Dependency Container` and use one line trait to inject it.

For example, this will auto inject a predefined `Logger` instance via setter injection into `Foo` class.

```
class Foo
{
    use Dependency\Setter\Logger;
}
```

### Register with Dependency Container

[](#register-with-dependency-container)

Use `register` method to register the dependency under `Logger` name.

```
$Container->register('Logger', function () use ($FactoryWorker) {
    return $FactoryWorker->buildLogger();
});
```

### Define the traits and interface

[](#define-the-traits-and-interface)

Example of `Logger` dependency trait, which is reused between all of the classes that use `Dependency\Setter\Logger` trait. The only thing to remember is that, the name of the trait should be the same, as the name under which the dependency was registered with the `Dependency Container`.

```
trait Logger
{
    /**
     * @var LoggerInterface
     */
    protected $Logger;

    /**
     * @inheritdoc
     */
    public function getLogger()
    {
        return $this->Logger;
    }

    /**
     * @inheritdoc
     */
    public function setLogger(LoggerInterface $Logger)
    {
        $this->Logger = $Logger;
    }
}
```

Bonus: You can also define and assign the `LoggerAwareInterface` too all classes that are being injected with `Logger` instance.

```
interface LoggerAwareInterface
{
    /**
     * @return LoggerInterface
     */
    public function getLogger();

    /**
     * @param Logger LoggerInterface
     */
    public function setLogger(LoggerInterface $Logger);
}
```

Define the setter injection trait. The only requirement is that the name ends with `Dependency\Setter\`. You can reuse already defined `Dependency\Logger` trait, in every class that implements LoggerAwareInterface.

```
namespace Application\Modules\Logger\Dependency\Setter;

use Application\Modules\Logger\Dependency;

trait Logger
{
    use Dependency\Logger;
}
```

### Register with Factory

[](#register-with-factory)

Use `registerWorkerCallback` to register callback which will return instance of required FactoryWorker.

```
$Factory->registerWorkerCallback('ApplicationFactoryWorker', function() use ($Factory) {
    return $Factory->buildWorker(Application::class);
});
```

### Build with FactoryWorker

[](#build-with-factoryworker)

To build your dependencies use the `FactoryWorker` classes.

```
class ApplicationFactoryWorker extends AbstractWorker implements FactoryWorkerInterface
{
    /**
     * @inheritdoc
     */
    protected function registerBeforeWork()
    {
        $this->getFactory()->registerWorkerCallback('ApplicationFactoryWorker', function () {
            return $this->getFactory()->buildWorker(self::class);
        });
    }

    /**
     * @return Logger
     */
    public function buildLogger()
    {
        $Logger = new Logger();
        $this->getFactory()->injectDependencies(Logger::class, $Logger);
        return $Logger;
    }

    /**
     * @param LoggerInterface $Logger
     * @param string $anotherArgument
     * @param array $data
     *
     * @return ApplicationInterface
     */
    public function buildApplication(LoggerInterface $Logger)
    {
        $Application = new Application($Logger);
        $this->getFactory()->injectDependencies(Application::class, $Application);
        return $Application;
    }

    /**
     * @param LoggerInterface $Logger
     *
     * @return UserManagerInterface
     */
    public function buildUserManager(LoggerInterface $Logger)
    {
        $UserManager = new UserManager($Logger);
        $this->getFactory()->injectDependencies(UserManager::class, $UserManager);
        return $UserManager;
    }
}
```

### Resolve with Dependency Container

[](#resolve-with-dependency-container)

Use `resolve` to receive dependency defined earlier with `register` or `propose`. So you can pass the same instance to another class via constructor injection.

```
$Container->register('Logger', function () use ($FactoryWorker) {
    return $FactoryWorker->buildLogger();
});

$Container->register('UserManager', function () use ($FactoryWorker, $Container) {
    $Logger = $Container->resolve('Logger');
    return $FactoryWorker->buildUserManager($Logger);
});

$Container->register('Application', function () use ($FactoryWorker, $Container) {
    $Logger = $Container->resolve('Logger');
    return $FactoryWorker->buildApplication($UserManager, $Logger);
});
```

Now `Application` and `UserManager` will share the same instance of `Logger` class.

```
$Application->getLogger()->log('It works');
$UserManager->getLogger()->log('It works, too');
```

If you don't do any work in constructors, and you shouldn't, and only require the `Logger` functionality later, it would be easier to just use the `Logger` as the infrastructure type dependency and just inject it via setter injection with one line. The end result is the same.

**Every required class will be injected with the same `Logger` instance, that was registered with the `Dependency Container` and assembled by `FactoryWorker` in `Factory`.**

### Ensures Tests Ready Code (TM)

[](#ensures-tests-ready-code-tm)

Writing tests of classes that use `Everon Factory` for the dependency injection and instantiation removes the hassle of dealing with dependency problems since everything is so easy to mock.

### Dependency Container, Factory and FactoryWorker

[](#dependency-container-factory-and-factoryworker)

Instantiate new `Dependency Container` and assign it to `Factory`. Use `Factory` to get instance of your specific `FactoryWorker`.

The best thing is, that the classes which are being instantiated with the `FactoryWorker` are not aware about the `Dependency Container` at all.

It could be in separate files, obviously, split by the application type and the dependencies it needs.

An example, of using the same instance of `Logger`, in every class, through out whole application, which required `Logger` dependency.

```
$Container = new Dependency\Container();
$Factory = new Factory($Container);
$Factory->registerWorkerCallback('ApplicationFactoryWorker', function() use ($Factory) {
    return $Factory->buildWorker(Application::class);
});

$FactoryWorker = $Factory->getWorkerByName('ApplicationFactoryWorker');

$Container->register('Application', function () use ($FactoryWorker, $Container) {
    $UserManager = $Container->resolve('UserManager');
    $Logger = $Container->resolve('Logger');
    return $FactoryWorker->buildApplication($UserManager, $Logger);
});

$Container->register('UserManager', function () use ($FactoryWorker) {
    $Logger = $FactoryWorker->getFactory()->getDependencyContainer()->resolve('Logger');
    return $FactoryWorker->buildUserManager($UserRepository, $Logger);
});

$Container->register('Logger', function () use ($FactoryWorker) {
    return $FactoryWorker->buildLogger();
});

//..
//.. Instantiate your application, and proceed as usual
//..
$Application = $Container->resolve('Application');
$Application
    ->bootstrap()
    ->run();
```

### What's the best way to inject dependencies?

[](#whats-the-best-way-to-inject-dependencies)

Use constructor for dependencies that are part of what the class is doing, and use setters/getters for infrastructure type dependencies. In general, a `Logger` or `FactoryWorker` could be good examples of infrastructure type dependencies.

Test Driven
-----------

[](#test-driven)

See [tests](https://github.com/oliwierptak/everon-factory/blob/master/tests/unit/)for [more examples with trait dependencies](https://github.com/oliwierptak/everon-factory/tree/master/tests/unit/doubles/).

Example
-------

[](#example)

Check [Everon Criteria Builder](https://github.com/oliwierptak/everon-criteria-builder) to see how to use Everon Factory by example.

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance53

Moderate activity, may be stable

Popularity40

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 99% 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 ~154 days

Recently: every ~58 days

Total

14

Last Release

1809d ago

Major Versions

v1.1.4 → v2.x-dev2020-10-15

2.0.3 → 3.0.02021-06-04

PHP version history (3 changes)1.0.0PHP &gt;=5.6

v2.x-devPHP ^7.2

3.0.0PHP ^8

### Community

Maintainers

![](https://www.gravatar.com/avatar/73a4cc056d2b09189dda46d01b76e71161dd9b75a226230bed6bec8f414d1cf8?d=identicon)[oliwierptak](/maintainers/oliwierptak)

---

Top Contributors

[![oliwierptak](https://avatars.githubusercontent.com/u/495101?v=4)](https://github.com/oliwierptak "oliwierptak (102 commits)")[![oliwier-spryker](https://avatars.githubusercontent.com/u/13624598?v=4)](https://github.com/oliwier-spryker "oliwier-spryker (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/everon-factory/health.svg)

```
[![Health](https://phpackages.com/badges/everon-factory/health.svg)](https://phpackages.com/packages/everon-factory)
```

###  Alternatives

[gallib/laravel-short-url

A Laravel package to shorten urls

16516.4k](/packages/gallib-laravel-short-url)[funstaff/tika

libs for tika wrapper

1124.4k](/packages/funstaff-tika)

PHPackages © 2026

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