使用方法的返回值作为服务

时间:2013-11-09 23:32:58

标签: symfony dependency-injection

我想创建一个访问Rackspace Cloud Files的服务。我已将以下配置添加到services.yml

services:
    # Rackspace
    # Note:  Rackspace parameters (e.g., %rackspace_username%, etc.) are set in
    #   app/config/parameters.yml.
    rackspace:
        class:  OpenCloud\Rackspace
        arguments:
            - "%rackspace_identity_endpoint%"
            - {"username": "%rackspace_username%", "apiKey": "%rackspace_api_key%"}

    rackspace.cloudfiles:
        parent: rackspace
        calls:
            - [objectStoreService, ["cloudFiles", '%rackspace_cloudfiles_region%', '%rackspace_url_type%']]

rackspace.cloudfiles服务无法正常工作,因为我需要objectStoreService()OpenCloud\ObjectStore\Service的实例)的结果,而不是OpenCloud\Rackspace实例的结果。

是否可以根据调用computeService()的结果定义服务?

1 个答案:

答案 0 :(得分:0)

rackspace服务视为工厂:

services:
    # Rackspace
    # Note:  Rackspace parameters (e.g., %rackspace_username%, etc.) are set in
    #   app/config/parameters.yml.
    rackspace:
        class:  OpenCloud\Rackspace
        arguments:
            - "%rackspace_identity_endpoint%"
            - {"username": "%rackspace_username%", "apiKey": "%rackspace_api_key%"}

    rackspace.cloudfiles:
        class:              OpenCloud\ObjectStore\Service
        factory_service:    rackspace
        factory_method:     objectStoreService
        arguments:
            - "cloudFiles"
            - "%rackspace_cloudfiles_region%"
            - "%rackspace_url_type%"

有关详细信息,请参阅http://symfony.com/doc/current/components/dependency_injection/factories.html