PHP Classes
elePHPant
Icontem

Object Manager Factory: Factory to create objects by name using reflection

Recommend this page to a friend!
  Info   View files View files (42)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-04-04 (13 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 79 This week: 79All time: 8,708 This week: 7Up
Version License PHP version Categories
objectmanagerfactory 1.0MIT/X Consortium ...5.6PHP 5, Language, Design Patterns
Description Author

This class implements a factory to create objects by name using reflection.

It can take as arguments the name of a class and optional parameters.

The class uses reflection to inspect the class of the object to be created and creates the object calling the class constructor passing eventual arguments if necessary.

A separate class is provided to act as a singleton for static usage.

The package also provides a unit test helper for mocking Object Manager, as well Docker configuration.

  Performance   Level  
Name: Sergii Pryz <contact>
Classes: 9 packages by
Country: Ukraine Ukraine
Innovation award
Innovation award
Nominee: 4x

Details

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.

  Files folder image Files  
File Role Description
Files folder imagebin (1 directory)
Files folder imagedocs (2 directories)
Files folder imagesrc (2 files, 2 directories)
Files folder imagetests (1 file, 3 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file LICENSE.txt Doc. Documentation
Accessible without login Plain text file README.md Doc. Class source

  Files folder image Files  /  bin  
File Role Description
Files folder imagedocker (2 files, 1 directory)

  Files folder image Files  /  bin  /  docker  
File Role Description
Files folder imagephp (1 file, 1 directory)
  Accessible without login Plain text file docker-compose.yml Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  bin  /  docker  /  php  
File Role Description
Files folder imageconfig (2 files)
  Accessible without login Plain text file Dockerfile Data Auxiliary data

  Files folder image Files  /  bin  /  docker  /  php  /  config  
File Role Description
  Accessible without login Plain text file 20-xdebug.ini Data Auxiliary data
  Accessible without login Plain text file php.ini Data Auxiliary data

  Files folder image Files  /  docs  
File Role Description
Files folder imageexample (3 directories)
Files folder imageuml (2 files)

  Files folder image Files  /  docs  /  example  
File Role Description
Files folder imageFactory (3 files, 1 directory)
Files folder imageLegacy (3 files, 1 directory)
Files folder imageStatistics (2 files, 1 directory)

  Files folder image Files  /  docs  /  example  /  Factory  
File Role Description
Files folder imageApi (2 files)
  Plain text file Connection.php Class Class source
  Plain text file ConnectionFactory.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  docs  /  example  /  Factory  /  Api  
File Role Description
  Plain text file ConnectionFactoryInterface.php Class Class source
  Plain text file ConnectionInterface.php Class Class source

  Files folder image Files  /  docs  /  example  /  Legacy  
File Role Description
Files folder imageApi (2 files)
  Plain text file Connection.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation
  Plain text file UserRepository.php Class Class source

  Files folder image Files  /  docs  /  example  /  Legacy  /  Api  
File Role Description
  Plain text file ConnectionInterface.php Class Class source
  Plain text file UserRepositoryInterface.php Class Class source

  Files folder image Files  /  docs  /  example  /  Statistics  
File Role Description
Files folder imageApi (1 file)
  Plain text file ObjectManagerStatistics.php Class Class source
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  docs  /  example  /  Statistics  /  Api  
File Role Description
  Plain text file ObjectManagerStatisticsInterface.php Class Class source

  Files folder image Files  /  docs  /  uml  
File Role Description
  Accessible without login Image file class.diagram.png Data Auxiliary data
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageApi (2 files)
Files folder imageException (2 files)
  Plain text file ObjectManager.php Class Class source
  Plain text file ObjectManagerSingleton.php Class Class source

  Files folder image Files  /  src  /  Api  
File Role Description
  Plain text file ObjectManagerInterface.php Class Class source
  Plain text file ObjectManagerSingletonInterface.php Class Class source

  Files folder image Files  /  src  /  Exception  
File Role Description
  Plain text file ExceptionInterface.php Class Class source
  Plain text file RuntimeException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageexample (1 directory)
Files folder imagehelper (1 directory)
Files folder imageunit (1 directory)
  Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data

  Files folder image Files  /  tests  /  example  
File Role Description
Files folder imagesrc (1 file, 3 directories)

  Files folder image Files  /  tests  /  example  /  src  
File Role Description
Files folder imageFactory (1 file)
Files folder imageLegacy (1 file)
Files folder imageStatistics (1 file)
  Plain text file BaseTest.php Class Class source

  Files folder image Files  /  tests  /  example  /  src  /  Factory  
File Role Description
  Plain text file FactoryTest.php Class Class source

  Files folder image Files  /  tests  /  example  /  src  /  Legacy  
File Role Description
  Plain text file UserRepositoryTest.php Class Class source

  Files folder image Files  /  tests  /  example  /  src  /  Statistics  
File Role Description
  Plain text file ObjectManagerStatisticsTest.php Class Class source

  Files folder image Files  /  tests  /  helper  
File Role Description
Files folder imagesrc (1 file)

  Files folder image Files  /  tests  /  helper  /  src  
File Role Description
  Plain text file ObjectManagerHelper.php Class Class source

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imagesrc (3 files)

  Files folder image Files  /  tests  /  unit  /  src  
File Role Description
  Plain text file BaseTest.php Class Class source
  Plain text file ObjectManagerSingletonTest.php Class Class source
  Plain text file ObjectManagerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:79
This week:79
All time:8,708
This week:7Up
User Comments (1)
nice
15 hours ago (muabshir)
70%StarStarStarStar