大卫开发/pimple-aware-event-dispatcher

该包最新版本(3.0)没有提供许可证信息。

一个兼容Symfony的事件分发器,允许从Pimple DI容器的实例中延迟加载服务

3.0 2018-02-23 08:26 UTC

This package is not auto-updated.

Last update: 2024-09-22 08:02:06 UTC


README

安装

composer.phar require "davedevelopment/pimple-aware-event-dispatcher:*@dev"

使用方法

Silex应用程序中使用

<?php

use PimpleAwareEventDispatcher\PimpleAwareEventDispatcher;
use Silex\Application;
use Symfony\Component\EventDispatcher\EventDispatcher;

$app = new Application;

// define the dispatcher
$app['event_dispatcher'] = function () use ($app) {
    $dispatcher = new EventDispatcher();
    return new PimpleAwareEventDispatcher($dispatcher, $app);
};

// define our application services
$app['some.service'] = function() use ($app) {
    // let's assume this takes a bit of doing and/or is dependant on several other
    // services
    sleep(1);
    return new SomeService;
};

// add a listener, that will lazily fetch the service when needed
$app['event_dispatcher']->addListenerService(
    "some.event",
    array("some.service", "serviceMethod")
);