Zend_DI-Examples icon indicating copy to clipboard operation
Zend_DI-Examples copied to clipboard

Static factory method use case

Open theUniC opened this issue 13 years ago • 2 comments

Hi Ralph,

Could be possible to add a static factory method use case as an example? I'm in the case I have a dependency of a Zend\Cache\Storage\Adapter\AbstractAdapter that gets generated via Zend\Cache\StorageFactory, but I'm not able to find the way to get an instance of the AbstractAdapter and fullfil the dependency. Googling a bit I could get this snippet from some example seen in a gist

<?php
// file MyModule/configs/module.config.php
return array(
    'di' => array(

        'definition' => array(
            'class' => array(
                'Zend\Cache\StorageFactory' => array(
                    'methods' => array(
                        'factory' => array(
                            'cfg' => array(
                                'type' => false,
                                'required' => true
                            )
                        )
                    )
                ),

                'Zend\Cache\Storage\Adapter\AbstractAdapter' => array(
                    'instantiator' => array(
                        'Zend\Cache\StorageFactory',
                        'factory'
                    )
                )
            )
        ),

        'instance' => array(

            'alias' => array(
                'class_with_dependency'  => 'Class\With\Dependency',
                'zend_cache_storage_adapter_apc'  => 'Zend\Cache\Storage\Adapter\AbstractAdapter'
            ),

            'class_with_dependency' => array(
                'injections' => array(
                    'cache'     => 'zend_cache_storage_adapter_apc'
                )
            ),
            'zend_cache_storage_adapter_apc' => array(
                'parameters' => array(
                    'cfg' => array(
                        'adapter' => array(
                            'name' => 'apc',
                            'options' => array(
                                'ttl' => 123,
                            )
                        ),
                        'plugins' => array('Serializer')
                    )
                )
            )
        ),
    ),
);

Thanks in advance! Christian.

theUniC avatar Dec 29 '11 08:12 theUniC

I need to update the examples to use beta2, ill add an example for this as well. Catch me on IRC today, -ralph

ralphschindler avatar Jan 03 '12 15:01 ralphschindler

Hi Ralph!

Thanks a lot! I'll do it! See you on IRC :D

Christian.

theUniC avatar Jan 03 '12 15:01 theUniC