Recommend this page to a friend! |
Download |
Info | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
65% | Total: 117 | All time: 9,545 This week: 78 |
Version | License | PHP version | Categories | |||
objectmanagerfactory 1.0 | MIT/X Consortium ... | 5.6 | PHP 5, Language, Design Patterns |
Description | Author | |
This class implements a factory to create objects by name using reflection. |
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
Update to your composer.json
with:
{
"require": {
"picamator/object-manager": "~1.0"
}
}
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 `ObjectManagerSingleton
with mock
ObjectManagerSingleton::setInstance($mockObjectManager)
`.
Having such is open the door for mocking `Connection
` class.
Please follow link to find example source and unit test for them.
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.
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.
To configure developing environment please:
To start helping the project please review CONTRIBUTING.
ObjectManager is licensed under the MIT License. Please see the LICENSE file for details.
Files (42) |
File | Role | Description | ||
---|---|---|---|---|
bin (1 directory) | ||||
docs (2 directories) | ||||
src (2 files, 2 directories) | ||||
tests (1 file, 3 directories) | ||||
.travis.yml | Data | Auxiliary data | ||
CHANGELOG.md | Data | Auxiliary data | ||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
CONTRIBUTING.md | Data | Auxiliary data | ||
LICENSE.txt | Doc. | Documentation | ||
README.md | Doc. | Class source |
Files (42) | / | bin | / | docker |
File | Role | Description | ||
---|---|---|---|---|
php (1 file, 1 directory) | ||||
docker-compose.yml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (42) | / | bin | / | docker | / | php |
File | Role | Description | ||
---|---|---|---|---|
config (2 files) | ||||
Dockerfile | Data | Auxiliary data |
Files (42) | / | bin | / | docker | / | php | / | config |
File | Role | Description |
---|---|---|
20-xdebug.ini | Data | Auxiliary data |
php.ini | Data | Auxiliary data |
Files (42) | / | docs | / | example |
File | Role | Description | ||
---|---|---|---|---|
Factory (3 files, 1 directory) | ||||
Legacy (3 files, 1 directory) | ||||
Statistics (2 files, 1 directory) |
Files (42) | / | docs | / | example | / | Factory |
File | Role | Description | ||
---|---|---|---|---|
Api (2 files) | ||||
Connection.php | Class | Class source | ||
ConnectionFactory.php | Class | Class source | ||
README.md | Doc. | Documentation |
Files (42) | / | docs | / | example | / | Factory | / | Api |
File | Role | Description |
---|---|---|
ConnectionFactoryInterface.php | Class | Class source |
ConnectionInterface.php | Class | Class source |
Files (42) | / | docs | / | example | / | Legacy |
File | Role | Description | ||
---|---|---|---|---|
Api (2 files) | ||||
Connection.php | Class | Class source | ||
README.md | Doc. | Documentation | ||
UserRepository.php | Class | Class source |
Files (42) | / | docs | / | example | / | Legacy | / | Api |
File | Role | Description |
---|---|---|
ConnectionInterface.php | Class | Class source |
UserRepositoryInterface.php | Class | Class source |
Files (42) | / | docs | / | example | / | Statistics |
File | Role | Description | ||
---|---|---|---|---|
Api (1 file) | ||||
ObjectManagerStatistics.php | Class | Class source | ||
README.md | Doc. | Documentation |
Files (42) | / | docs | / | example | / | Statistics | / | Api |
File | Role | Description |
---|---|---|
ObjectManagerStatisticsInterface.php | Class | Class source |
Files (42) | / | docs | / | uml |
File | Role | Description |
---|---|---|
class.diagram.png | Data | Auxiliary data |
README.md | Doc. | Documentation |
Files (42) | / | src |
File | Role | Description | ||
---|---|---|---|---|
Api (2 files) | ||||
Exception (2 files) | ||||
ObjectManager.php | Class | Class source | ||
ObjectManagerSingleton.php | Class | Class source |
Files (42) | / | src | / | Api |
File | Role | Description |
---|---|---|
ObjectManagerInterface.php | Class | Class source |
ObjectManagerSingletonInterface.php | Class | Class source |
Files (42) | / | src | / | Exception |
File | Role | Description |
---|---|---|
ExceptionInterface.php | Class | Class source |
RuntimeException.php | Class | Class source |
Files (42) | / | tests |
File | Role | Description | ||
---|---|---|---|---|
example (1 directory) | ||||
helper (1 directory) | ||||
unit (1 directory) | ||||
phpunit.xml.dist | Data | Auxiliary data |
Files (42) | / | tests | / | example | / | src |
File | Role | Description | ||
---|---|---|---|---|
Factory (1 file) | ||||
Legacy (1 file) | ||||
Statistics (1 file) | ||||
BaseTest.php | Class | Class source |
Files (42) | / | tests | / | example | / | src | / | Factory |
File | Role | Description |
---|---|---|
FactoryTest.php | Class | Class source |
Files (42) | / | tests | / | example | / | src | / | Legacy |
File | Role | Description |
---|---|---|
UserRepositoryTest.php | Class | Class source |
Files (42) | / | tests | / | example | / | src | / | Statistics |
File | Role | Description |
---|---|---|
ObjectManagerStatisticsTest.php | Class | Class source |
Files (42) | / | tests | / | unit | / | src |
File | Role | Description |
---|---|---|
BaseTest.php | Class | Class source |
ObjectManagerSingletonTest.php | Class | Class source |
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 |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Ratings | User Comments (1) | ||||||||||||||||||||||||||||||||||
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.