picamator/object-manager

Utility to create objects

1.2.0 2017-04-04 19:24 UTC

This package is not auto-updated.

Last update: 2024-11-09 21:10:45 UTC


README

PHP 7 ready Latest Stable Version License SensioLabsInsight

Master

Build Status Coverage Status

Dev

Build Status Coverage Status

Object Manager is one class tool to build objects supplied with a Singleton wrapper and unit test stub helper.

The main usage are:

  • refactoring legacy code with unit-testable style without break backwards compatibility
  • having one place for creating new instances

Installation

Update to your composer.json with:

{
    "require": {
        "picamator/object-manager": "~1.0"
    }
}

Requirements

Examples

Legacy

Let's application has an UserRepository:

<?php
class UserRepository 
{
    private $connection;
    
    public function __construct() 
    {
        $this->connection = new Connection();    
    }
}

The Connection instance here was created inside constructor make it hard to mock for unit testing.

One of the solution to keep backward compatibility is using ObjectManagerSingleton:

<?php
class UserRepository 
{
    private $connection;
    
    public function __construct() 
    {
        $this->connection = ObjectManagerSingleton::getInstance()->create('Connection');    
    }
}

Inside unit test before running the test needs to stub ObjectManagerSingleton with mock ObjectManagerSingleton::setInstance($mockObjectManager). Having such is open the door for mocking Connection class.

Please follow link to find example source and unit test for them.

Factory

Let's application has a ConnectionFactory:

<?php
class ConnectionFactory 
{
    public function create() 
    {
        return new Connection();
    }
}

With ObjectManager it can be rewritten to:

<?php
class ConnectionFactory 
{
    private $objectManager;
    
    private $className;
    
    public function __construct(ObjectManager $objectManager, $className = 'Connection') 
    {
        $this->objectManager = $objectManager;
        $this->className = $className;
    } 
    
    public function create() 
    {
        return $this->objectManager->create($this->className);
    }
}

As a result it's possible to use Dependency Injection to override $className with new implementation. With PHP 7 features ConnectionFactory would look like:

<?php
declare(strict_types=1);

class ConnectionFactory 
{
    private $objectManager;
    
    private $className;
    
    public function __construct(ObjectManager $objectManager, string $className = 'Connection') 
    {
        $this->objectManager = $objectManager;
        $this->className = $className;
    } 
    
    public function create() : ConnectionInterface
    {
        return $this->objectManager->create($this->className);
    }
}

Please follow link to find example source and unit test for them.

Statistics

Suppose application needs to collect objects statistics without using any additional tools. The solution might be ObjectManager overriding with injection in development environment.

Please follow link to find example source and unit test for them.

Documentation

Developing

To configure developing environment please:

  1. Follow Docker installation steps
  2. Run inside Docker container composer install

Contribution

To start helping the project please review CONTRIBUTING.

License

ObjectManager is licensed under the MIT License. Please see the LICENSE file for details.