sonrisa / factory-component
Factory component for sonrisa framework. Build for PHP5.3 and above.
Installs: 6 347
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.3.0
- sonrisa/patterns-component: *
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2018-01-07 08:46:44 UTC
README
1. Description
Factory component allows the creation of classes by using PHP's ReflectionClass.
The purpose of using the Factory component is to give the developer the power to decide whether to create a single instance of a class through-out the execution thread, or have multiple instances of the class.
The factory component can instantiate normal and Singleton classes, allowing the user to pass to the class' __construct or getInstance methods arguments (as an array) if desired.
2. Methods:
- getInstance()
- create($classPath,$args=array(),$unique=false)
- get($classPath)
- remove($classPath)
- removeAll()
3. Usage
Usage of the factory component is real simple:
3.1 - Create a single instance per class in the whole execution thread
$factory = \NilPortugues\Component\Factory\ClassFactory::getInstance(); //One instance of these classes will be created. //This is determined by the "true" value at the end of the create method. //We'll be passing an argument to the DateTime class. //Arguments should be passed in the same order defined in the class. $dateTime = $factory->create("\\DateTime",array('2013-08-08'),true); //We'll be passing no values to the StdClass constructor $stdClass = $factory->create("\\StdClass",array(),true);
3.2 - Create many class instances in the whole execution thread
$factory = \NilPortugues\Component\Factory\ClassFactory::getInstance(); //Create a class instance using the Factory Class. $dateTime = $factory->create("\\DateTime",array('2013-08-08')); //Which is actually the same as doing... $instance = new \DateTime('2013-08-08');
3.3 - Get an existing class instance
$factory = \NilPortugues\Component\Factory\ClassFactory::getInstance(); $dateTime = $factory->create("\\DateTime",array('2013-08-08'),true); //Call the code below in another function or class $dateTime = $factory->get('\\DateTime'); var_dump($dateTime); // Outputs: object(DateTime) ... echo $dateTime->format('Y-m-d'); // Outputs 2013-08-08
3.4 - Remove one or all classes from the Factory
You should not try to delete a non-registered class in the Factory or a FactoryException
will be thrown at you.
$factory = \NilPortugues\Component\Factory\ClassFactory::getInstance(); //throws FactoryException $factory->remove('\\DateTime'); //this will throw a FactoryException too as the DateTime object hasn't been stored to make it unique. $dateTime = $factory->create("\\DateTime",array('2013-08-08')); $factory->remove('\\DateTime'); //remove DateTime $dateTime = $factory->create("\\DateTime",array('2013-08-08'),true); $factory->remove('\\DateTime'); //removeAll will not throw a FactoryException if empty. $factory->removeAll();
4. Fully tested.
Testing has been done using PHPUnit and Travis-CI. All filters have been tested to be compatible from PHP 5.3 up to PHP 5.5.