Download .zip |
Info | View files (42) | Download .zip | Reputation | Support forum (2) | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2017-04-04 (13 hours ago) | Not enough user ratings | Total: 79 This week: 79 | All time: 8,708 This week: 7 |
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:
composer install
To start helping the project please review CONTRIBUTING.
ObjectManager is licensed under the MIT License. Please see the LICENSE file for details.
Files |
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 | / | bin | / | docker |
File | Role | Description | ||
---|---|---|---|---|
php (1 file, 1 directory) | ||||
docker-compose.yml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files | / | bin | / | docker | / | php | / | config |
File | Role | Description |
---|---|---|
20-xdebug.ini | Data | Auxiliary data |
php.ini | Data | Auxiliary data |
Files | / | docs | / | example |
File | Role | Description | ||
---|---|---|---|---|
Factory (3 files, 1 directory) | ||||
Legacy (3 files, 1 directory) | ||||
Statistics (2 files, 1 directory) |
Files | / | docs | / | example | / | Factory |
File | Role | Description | ||
---|---|---|---|---|
Api (2 files) | ||||
Connection.php | Class | Class source | ||
ConnectionFactory.php | Class | Class source | ||
README.md | Doc. | Documentation |
Files | / | docs | / | example | / | Factory | / | Api |
File | Role | Description |
---|---|---|
ConnectionFactoryInterface.php | Class | Class source |
ConnectionInterface.php | Class | Class source |
Files | / | docs | / | example | / | Legacy |
File | Role | Description | ||
---|---|---|---|---|
Api (2 files) | ||||
Connection.php | Class | Class source | ||
README.md | Doc. | Documentation | ||
UserRepository.php | Class | Class source |
Files | / | docs | / | example | / | Legacy | / | Api |
File | Role | Description |
---|---|---|
ConnectionInterface.php | Class | Class source |
UserRepositoryInterface.php | Class | Class source |
Files | / | docs | / | example | / | Statistics |
File | Role | Description | ||
---|---|---|---|---|
Api (1 file) | ||||
ObjectManagerStatistics.php | Class | Class source | ||
README.md | Doc. | Documentation |
Files | / | docs | / | example | / | Statistics | / | Api |
File | Role | Description |
---|---|---|
ObjectManagerStatisticsInterface.php | Class | Class source |
Files | / | docs | / | uml |
File | Role | Description |
---|---|---|
class.diagram.png | Data | Auxiliary data |
README.md | Doc. | Documentation |
Files | / | src |
File | Role | Description | ||
---|---|---|---|---|
Api (2 files) | ||||
Exception (2 files) | ||||
ObjectManager.php | Class | Class source | ||
ObjectManagerSingleton.php | Class | Class source |
Files | / | src | / | Api |
File | Role | Description |
---|---|---|
ObjectManagerInterface.php | Class | Class source |
ObjectManagerSingletonInterface.php | Class | Class source |
Files | / | src | / | Exception |
File | Role | Description |
---|---|---|
ExceptionInterface.php | Class | Class source |
RuntimeException.php | Class | Class source |
Files | / | tests |
File | Role | Description | ||
---|---|---|---|---|
example (1 directory) | ||||
helper (1 directory) | ||||
unit (1 directory) | ||||
phpunit.xml.dist | Data | Auxiliary data |
Files | / | tests | / | example | / | src |
File | Role | Description | ||
---|---|---|---|---|
Factory (1 file) | ||||
Legacy (1 file) | ||||
Statistics (1 file) | ||||
BaseTest.php | Class | Class source |
Files | / | tests | / | example | / | src | / | Legacy |
File | Role | Description |
---|---|---|
UserRepositoryTest.php | Class | Class source |
Files | / | tests | / | example | / | src | / | Statistics |
File | Role | Description |
---|---|---|
ObjectManagerStatisticsTest.php | Class | Class source |
Files | / | tests | / | unit | / | src |
File | Role | Description |
---|---|---|
BaseTest.php | Class | Class source |
ObjectManagerSingletonTest.php | Class | Class source |
ObjectManagerTest.php | Class | Class source |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
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.