Zend Framework 2 Service Manager依赖注入

时间:2014-03-15 02:05:19

标签: dependency-injection zend-framework2 servicemanager

我的应用程序是POPO的集合,我试图使用Zend Framework 2 Service Manager连接这些POPO。

为了说明我的问题,请采用以下示例:

$config = array(
   'services' => array(
      'my.app.serviceA' => new \My\App\Services\ServiceA(),
      'my.app.serviceB' => new \My\App\Services\ServiceB(),

      'my.app.manager.task' => new \My\App\Manager\TaskManager(),
   ),

);

我的TaskManager类看起来像这样:

class TaskManager {

   protected $serviceA;
   protected $serviceB;

   public function setServiceA( \My\App\Service\ServiceA $serviceA )
   {
      $this->serviceA = $serviceA;
   }

   public function setServiceB( \My\App\Service\ServiceB $serviceB )
   {
      $this->serviceB = $serviceB;
   }

}

如您所见,TaskManager类对ServiceAServiceB都有依赖关系。如何使用为my.app.manager.taskServiceA定义的服务名称使用Service Manager配置将这些服务注入ServiceB

更新

我开始相信我不应该将ServiceManager组件用于我的目的,而是我应该使用Zend DI组件。

我得到的印象是ServiceManager是一个ZF2"框架"组件而Zend \ DI似乎更像是通用的全能DiC。因此,这可能是ServiceManager与MVC和ModuleManager组件(也似乎是"框架"组件)绑定关系的原因。

也许有人可以澄清一下?

3 个答案:

答案 0 :(得分:2)

module.config.php中的

可以通过7种不同的方式配置Service Manager:

return array(

    // instantiate the class for you when needed
    'invokables' => array(
        'commentController' => '\Comment\Controller\CommentController';
    ),

    // Aliasing a name to a known service name
    'aliases' => array(
        'Comment\Service' => 'commentService';
    ),

    // configure the instance of the object
    'factories' => array(
        'commentController' => function ($sm) {
            $locator = $sm->getServiceLocator();
            $controller = $locator->get('commentController');
            $controller->setCommentService($locator->get('Comment\Service'));
            return $controller;
        }
    ),

    // register already instantiated objects
    'services' => array(
        'commentController' => new \Comment\Controller\CommentController(),
    ),

    //factory instance that can create multiple services based on the name supplied to the factory.
    'abstract_factories' => array(
        'SomeModule\Service\FallbackFactory',
    ),

    // initialize the service whenever service created
    'initializers' => array(
        function ($instance, $sm) {
            if ($instance instanceof \Comment\Controller\CommentController){
                $instance->setCommentService($sm->get('Comment\Service'));
            }
        }
    ),

    // indicating whether or not a service should be shared
    'shared' => array(
        'commentController' => false,
    ),
);

并在Module.php中

public function getControllerConfig() {
    return array(
        'factories' => array(
            'commentController' => function ($sm) {
                $controller = new \Comment\Controller\CommentController();
                $locator = $sm->getServiceLocator();
                $controller->setCommentForm($locator->get('commentForm'));
                $controller->setCommentService($locator->get('commentService'));
                return $controller;
            }
        )
    );
}

在控制器中使用简单:

 $commentService = $this->serviceLocator->get('Comment\Service');

你把它放在getter或init()方法中 ZF2's New Controller::init() :: phly, boy, phly

答案 1 :(得分:0)

控制器中的

;

$yourService = $this->getServiceLocator()->get('your_service_alias');
View Helper中的

: 你应该通过viewHelper

的构造函数从Module.php发送
    public function getViewHelperConfig() {
        return array(
            'factories' => array(
                'loginHelper' => function($sm) {
                    return new LoginHelper($sm);
                }
        )
   }

在calss中

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
    public class UseCaseBO implements ServiceLocatorAwareInterface {
      protected $serviceLocator;

      public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
        $this->serviceLocator = $serviceLocator;
      }

      public function getServiceLocator() {
        return $this->serviceLocator;
      }

      // now instance of Service Locator is ready to use
      public function someMethod() {
        $table = $this->serviceLocator->get('your_service_alias');
        //...
      }
    }

答案 2 :(得分:0)

对我来说,最好的方法是创建一个类工厂并使用factoryInterface,如下所示:

 return array(

    'service_manager' => array(
        'factories' => [
            'Task' => 'Application\TaskFactory',
        ],
        'invokables' => array(
            'Task'=> 'Application\Task',
            'ServiceA'=> 'Application\ServiceA',
            'ServiceB' => 'Application\ServiceB'
        )
    ),
);

还有一个工厂类:

class TaskFactory implements FactoryInterface {

    /** @var  ServiceLocatorInterface $serviceLocator */
    var $serviceLocator;

    public function createService(ServiceLocatorInterface $serviceLocator) {
        $sl = $this->serviceLocator = $serviceLocator;

        // you can get your registered services
        $serviceA = $sl->get('ServiceA');
        $serviceB = $sl->get('ServiceB');


        // You can build your class using the class loader
        $task = new Application\Task();

        // Or the Service Locator Again
        $task = $sl->get('Task');

        return $task;
    }
}

您可以在Task类上实现工厂界面。我宁愿控制自己的建设。