pine3ree / pine3ree-generic-factories
Abstract factories based on reflection and configuration
0.7.2
2025-05-04 21:43 UTC
Requires
- php: ^7.4 || ^8.0
- pine3ree/pine3ree-params-resolver: ^0.8 || ^0.9
- psr/container: ^1.1.2 || ^2.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^1.1 || ^2.0
- phpstan/phpstan: ^1.12 || ^2.0
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5.7
- webimpress/coding-standard: ^1.3
README
This package provides a reflection-based factory, which is based on the
pine3ree-params-resolver
package and a configuration-based factory with
dependencies specified inside configuration
Example for reflection-based factory:
<?php use My\App\Model\PostMapper; use Psr\Container\ContainerInterface; use pine3ree\Container\Factory\ReflectionBasedFactory; $container = include("config/container.php"); $factory = new ReflectionBasedFactory(); // Need just one instance // All dependencies of PostMapper are resolved by the factory if found in the // container $postMapper = $factory($container, PostMapper::class);
Example for configuration-based factory:
<?php use My\App\Model\Database\Db; use My\App\Model\Database\PdoFactory; use My\App\Model\PostMapper; use PDO; use Psr\Container\ContainerInterface; use pine3ree\Container\Factory\ConfigurationBasedFactory; $container = include("config/container.php"); $factory = new ReflectionBasedFactory(); // Need just one instance $config = $container->get('config'); // The 'config' service in the container should return an array like the // one below: $config = [ //... 'dependencies' => [ 'factories' => [ //... PDO::class => PdoFactory::class, // Ad-hoc factory //... ], // Configuration key for dependencies built by the config-based factory ConfigurationBasedFactory:class => [ // Just list the dependencies class names or container service-ids // for each managed class Db::class => [ PDO::class, // Resolved by a custom factory ], PostMapper::class => [ Db::class, // Resolved by the config-based factory as well 'config', // THe 'config' array will be injected as 2nd argument ], ], ], //... ]; $postMapper = $factory($container, PostMapper::class);