File: README.md

Recommend this page to a friend!
  Classes of Sergii Pryz  >  Object Manager Factory  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Class source
Class: Object Manager Factory
Factory to create objects by name using reflection
Author: By
Last change: Added class_exists check. Fixed bug in readme example.
Date: 4 years ago
Size: 5,005 bytes
 

Contents

Class file image Download

ObjectManager

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 `ObjectManagerSingletonwith mockObjectManagerSingleton::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.

For more information send a message to info at phpclasses dot org.