PHP Classes

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

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 65%Total: 117 All time: 9,545 This week: 78Up
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.

Picture of Sergii Pryz
  Performance   Level  
Name: Sergii Pryz <contact>
Classes: 10 packages by
Country: Ukraine Ukraine
Age: 40
All time rank: 133921 in Ukraine Ukraine
Week rank: 206 Up2 in Ukraine Ukraine Up
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 (42)  
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 (42)  /  bin  
File Role Description
Files folder imagedocker (2 files, 1 directory)

  Files folder image Files (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  docs  
File Role Description
Files folder imageexample (3 directories)
Files folder imageuml (2 files)

  Files folder image Files (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  docs  /  example  /  Statistics  /  Api  
File Role Description
  Plain text file ObjectManagerStatisticsInterface.php Class Class source

  Files folder image Files (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  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 (42)  /  tests  /  example  
File Role Description
Files folder imagesrc (1 file, 3 directories)

  Files folder image Files (42)  /  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 (42)  /  tests  /  example  /  src  /  Factory  
File Role Description
  Plain text file FactoryTest.php Class Class source

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

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

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

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

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

  Files folder image Files (42)  /  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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:117
This week:0
All time:9,545
This week:78Up
User Ratings User Comments (1)
 All time
Utility:93%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:609
 
nice
7 years ago (muabshir)
70%StarStarStarStar