PHP code example of bluepsyduck / zend-autowire-factory

1. Go to this page and download the library: Download bluepsyduck/zend-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 / zend-autowire-factory example snippets


\BluePsyduck\ZendAutoWireFactory\AutoWireFactory::setCacheFile('data/cache/autowire-factory.cache.php');

[
    'fancy-service' => [
        'fancy-property' => 'Hello World!',
        'fancy-adapters' => [
            FancyAdapterAlpha::class,
            FancyAdapterOmega::class,
        ],
    ],
]

class FancyService {
    public function __construct(FancyComponent $component, string $fancyProperty, array $fancyAdapters) {
    }
}

class FancyComponent {}
class FancyAdapterAlpha {}
class FancyAdapterOmega {}

 

use BluePsyduck\ZendAutoWireFactory\AutoWireFactory;
use Zend\ServiceManager\Factory\InvokableFactory;
use function BluePsyduck\ZendAutoWireFactory\injectAliasArray;
use function BluePsyduck\ZendAutoWireFactory\readConfig;

return [
    'dependencies' => [
        'factories' => [
            // Enable auto-wiring for the service itself.
            FancyService::class => AutoWireFactory::class,
            
            // FancyComponent does not need any factory as it does 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' => 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' => injectAliasArray('fancy-service', 'fancy-adapters'),
        ],
    ],
];

 

use BluePsyduck\ZendAutoWireFactory\AutoWireFactory;
use function BluePsyduck\ZendAutoWireFactory\injectAliasArray;
use function BluePsyduck\ZendAutoWireFactory\readConfig;

return [
    'dependencies' => [
        'abstract_factories' => [
            // Will auto-wire everything possible to be auto-wired, in our case both FancyService and FancyComponent.
            AutoWireFactory::class,
        ],
        'factories' => [
            // Any aliases using property names cannot be handled by the AutoWireFactory and must still get listed.
            'string $fancyProperty' => readConfig('fancy-service', 'fancy-property'),
            'array $fancyAdapters' => injectAliasArray('fancy-service', 'fancy-adapters'),
        ],
    ],
];
__construct(FancyClass $fancy)
__construct(array $fancyConfig)
__construct($fancyParameter)