lane4core / factory
The Factory class serves as a flexible instantiation and access factory for classes, supporting optional versioning and dependency injection (DI).
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/lane4core/factory
Requires
- php: >=8.1
- lane4core/classversion: ^1.0
- psr/container: ^2.0
Requires (Dev)
- phpstan/phpstan: ^2.0.4
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.11.2
README
Purpose
The Factory
serves as a flexible instantiation and access factory for classes, supporting optional versioning and dependency injection (DI).
Description for Developers
The class provides methods to dynamically create instances of classes or retrieve them from a provided container. It supports:
- Class versioning through a
ClassVersionInterface
. - Dependency Injection via a
Psr\Container\ContainerInterface
. - Dynamic construction of classes with parameter support.
Notes
- If a container is provided, the instance is retrieved from the container if available.
- If no container is available or the class is not registered in the container, the instance is dynamically created using reflection.
- Setting a container or ClassVersion is only possible once.
Usage
$factory = new Factory($container, $classVersion); $instance = $factory->get(MyClass::class, '1.0', ['param1', 'param2']);
The class provides a central point for instantiation and management of objects with optional versioning and dependency injection.
Example code without classVersioning and without DI container
use lane4core\Factory\Factory; $factory = new Factory(); $myClassInstance = $factory->get(MyClass::class); $myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);
Example code with classVersioning and without DI container
use lane4core\Factory\Factory; use lane4core\Version\config\ClassVersionConfig; $classVersions = ['subDirectory' => ['version1', 'version2']]; $classVersionConfig = new ClassVersionConfig($classVersions); $classVersion = new ClassVersion($classVersionConfig); $factory = new Factory(null, $classVersion); $myClassInstance = $factory->get(MyClass::class); $myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);
Example code with classVersioning and DI container
use lane4core\Factory\Factory; use lane4core\Version\config\ClassVersionConfig; $container = new Container(); $classVersions = ['subDirectory' => ['version1', 'version2']]; $classVersionConfig = new ClassVersionConfig($classVersions); $classVersion = new ClassVersion($classVersionConfig); $factory = new Factory($container, $classVersion); $myClassInstance = $factory->get(MyClass::class); $myClassInstance = $factory->get(MyClassWithTwoParameters::class, null, $var1, $var2);
Quickstart Composer
composer require lane4core/factory make install
Quickstart GitHub
git clone https://github.com/lane4core/factory.git
cd factory
Contents in the GitHub Repository
- Source Files:
- src
- tests
- Support:
- Docker Compose
- .env
- pre-commit-hook.sh
Makefile
Simply runmake
in the console
- Documentation:
- README.md