PHP code example of bluepsyduck / laminas-autowire-factory
1. Go to this page and download the library: Download bluepsyduck/laminas-autowire-factory library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bluepsyduck / laminas-autowire-factory example snippets
use BluePsyduck\LaminasAutoWireFactory\Attribute\Alias;
class ClassWithAliasParameter {
public function __construct(
#[Alias('alias-for-fancy-class')]
private FancyClass $fancy,
) {}
}
use BluePsyduck\LaminasAutoWireFactory\Attribute\ReadConfig;
class ClassWithConfigValue {
public function __construct(
#[ReadConfig('foo', 'bar')]
private string $timeout,
) {}
}
use BluePsyduck\LaminasAutoWireFactory\Attribute\InjectAliasArray;
/*
Config: [
'resolvers' => [
FancyResolver::class,
NotSoFancyResolver::class,
],
]
*/
class ClassWithAliasArray {
public function __construct(
#[InjectAliasArray('resolvers')]
private array $resolvers,
) {}
}
// dependencies.php
use BluePsyduck\LaminasAutoWireFactory\Factory\ConfigReaderFactory;
use BluePsyduck\LaminasAutoWireFactory\AutoWireUtils;
'factories' => [
// Either instantiate the factory directly:
'int $timeout' => new ConfigReaderFactory('fancy-service', 'timeout'),
// Or use the Utils class instead:
'int $timeout' => AutoWireUtils::readConfig('fancy-service', 'timeout'),
]
// dependencies.php
use BluePsyduck\LaminasAutoWireFactory\Factory\AliasArrayInjectorFactory;
use BluePsyduck\LaminasAutoWireFactory\AutoWireUtils;
'factories' => [
// Either instantiate the factory directly:
'array $resolvers' => new AliasArrayInjectorFactory('resolvers'),
// Or use the Utils class instead:
'array $resolvers' => AutoWireUtils::injectAliasArray('resolvers'),
]
use BluePsyduck\LaminasAutoWireFactory\Attribute\ReadConfig;
use BluePsyduck\LaminasAutoWireFactory\Attribute\InjectAliasArray;
class FancyService {
public function __construct(
private FancyComponent $component,
#[ReadConfig('fancy-service', 'fancy-property')]
private string $fancyProperty,
#[InjectAliasArray('fancy-service', 'fancy-adapters')]
private array $fancyAdapters,
) {}
}
class FancyComponent {}
class FancyAdapterAlpha {}
class FancyAdapterOmega {}
use BluePsyduck\LaminasAutoWireFactory\AutoWireFactory;
use Laminas\ServiceManager\Factory\InvokableFactory;
use function BluePsyduck\LaminasAutoWireFactory\injectAliasArray;
use function BluePsyduck\LaminasAutoWireFactory\readConfig;
return [
'dependencies' => [
'factories' => [
// Enable auto-wiring for the service itself.
FancyService::class => AutoWireFactory::class,
// FancyComponent and the other classes do not need any factory as they do not have a constructor.
// Both InvokableFactory and AutoWireFactory are usable here.
FancyComponent::class => InvokableFactory::class,
FancyAdapterAlpha::class => InvokableFactory::class,
FancyAdapterOmega::class => InvokableFactory::class,
],
],
];
use BluePsyduck\LaminasAutoWireFactory\AutoWireFactory;
use function BluePsyduck\LaminasAutoWireFactory\injectAliasArray;
use function BluePsyduck\LaminasAutoWireFactory\readConfig;
return [
'dependencies' => [
'abstract_factories' => [
// Will auto-wire everything possible to be auto-wired, in our case the FancyService, FancyComponent,
// and the adapters.
AutoWireFactory::class,
],
],
];
class FancyService {
public function __construct(
private FancyComponent $component,
private string $fancyProperty,
private array $fancyAdapters
) {}
}
class FancyComponent {}
class FancyAdapterAlpha {}
class FancyAdapterOmega {}
use BluePsyduck\LaminasAutoWireFactory\AutoWireFactory;
use BluePsyduck\LaminasAutoWireFactory\AutoWireUtils;
use Laminas\ServiceManager\Factory\InvokableFactory;
return [
'dependencies' => [
'factories' => [
// Enable auto-wiring for the service itself.
FancyService::class => AutoWireFactory::class,
// FancyComponent and the other classes do not need any factory as they do not have a constructor.
// Both InvokableFactory and AutoWireFactory are usable here.
FancyComponent::class => InvokableFactory::class,
FancyAdapterAlpha::class => InvokableFactory::class,
FancyAdapterOmega::class => InvokableFactory::class,
// Enable the scalar property for auto-wiring into the service.
// In this example, the factory would fetch "Hello World!" from the config.
'string $fancyProperty' => AutoWireUtils::readConfig('fancy-service', 'fancy-property'),
// Inject an array of other services through their aliases into the service.
// In this example, instances of FancyAdapterAlpha and FancyAdapterOmega would be injected.
'array $fancyAdapters' => AutoWireUtils::injectAliasArray('fancy-service', 'fancy-adapters'),
],
],
];
use BluePsyduck\LaminasAutoWireFactory\AutoWireFactory;
use BluePsyduck\LaminasAutoWireFactory\AutoWireUtils;
return [
'dependencies' => [
'abstract_factories' => [
// Will auto-wire everything possible to be auto-wired, in our case the FancyService, FancyComponent,
// and the adapters.
AutoWireFactory::class,
],
'factories' => [
// Any additional factories must still be specified in the config to make the corresponding parameters
// resolvable by the AutoWireFactory.
// Any aliases using property names cannot be handled by the AutoWireFactory and must still get listed.
'string $fancyProperty' => AutoWireUtils::readConfig('fancy-service', 'fancy-property'),
'array $fancyAdapters' => AutoWireUtils::injectAliasArray('fancy-service', 'fancy-adapters'),
],
],
];
__construct(FancyClass $fancy)
__construct(array $fancyConfig)
__construct($fancyParameter)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.