2017 © Pedro Peláez
 

library object-manager

Utility to create objects

image

picamator/object-manager

Utility to create objects

  • Tuesday, April 4, 2017
  • by picamator
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

ObjectManager

PHP 7 ready Latest Stable Version License SensioLabsInsight, (*1)

Master

Build Status Coverage Status, (*2)

Dev

Build Status Coverage Status, (*3)

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

The main usage are:, (*5)

  • 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:, (*6)

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

Requirements

Examples

Legacy

Let's application has an UserRepository:, (*7)

<?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., (*8)

One of the solution to keep backward compatibility is using ObjectManagerSingleton:, (*9)

<?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., (*10)

Please follow link to find example source and unit test for them., (*11)

Factory

Let's application has a ConnectionFactory:, (*12)

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

With ObjectManager it can be rewritten to:, (*13)

<?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:, (*14)

<?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., (*15)

Statistics

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

Please follow link to find example source and unit test for them., (*17)

Documentation

Developing

To configure developing environment please:, (*18)

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

Contribution

To start helping the project please review CONTRIBUTING., (*19)

License

ObjectManager is licensed under the MIT License. Please see the LICENSE file for details., (*20)

The Versions

04/04 2017

dev-master

9999999-dev

Utility to create objects

  Sources   Download

MIT

The Requires

  • php ~5.6|~7.0

 

The Development Requires

by Sergii Pryz

object manager factory

04/04 2017

1.2.0

1.2.0.0

Utility to create objects

  Sources   Download

MIT

The Requires

  • php ~5.6|~7.0

 

The Development Requires

by Sergii Pryz

object manager factory

04/04 2017

dev-dev

dev-dev

Utility to create objects

  Sources   Download

MIT

The Requires

  • php ~5.6|~7.0

 

The Development Requires

by Sergii Pryz

object manager factory

01/04 2017

1.1.0

1.1.0.0

Utility to create objects

  Sources   Download

MIT

The Requires

  • php ~5.6|~7.0

 

The Development Requires

by Sergii Pryz

object manager factory

09/03 2017

1.0.1

1.0.1.0

Utility to create objects

  Sources   Download

MIT

The Requires

  • php ~5.6|~7.0

 

The Development Requires

by Sergii Pryz

object manager factory

09/03 2017

1.0.0

1.0.0.0

It's one class tool to build objects supplied with a Singleton wrapper and unit test stub helper

  Sources   Download

MIT

The Requires

  • php ~5.6|~7.0

 

The Development Requires

by Sergii Pryz

utility object factory