DownloadObjectManager
 
 
 
  
Master
 
  
Dev
 
  
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: 
- 
Follow Docker installation steps
 
- 
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. 
 |