scrolla/micro

微框架应用程序

v0.1.2 2019-06-22 07:23 UTC

This package is auto-updated.

Last update: 2024-09-05 22:34:36 UTC


README

需要 PHP 7

用法

依赖注入容器

步骤 1: 声明依赖。

declare(strict_types=1);

use Satori\Micro\Application;

$app = new Application();

/**
 * Declares an action that has dependencies.
 */
$app->pageIndexAction = function (Application $app) {
    return new \Page\IndexAction(
        $app->pageIndexTemplate,
        $app->pageIndexService
    );
};

/**
 * Declares a template that depends on a configuration parameter.
 */
$app->pageIndexTemplate = function (Application $app) {
    return new \Page\IndexTemplate($app['path.template']);
};

/**
 * Declares an service.
 */
$app->pageIndexService = function (Application $app) {
    return new \Page\IndexService($app['fake.data']);
};

/**
 * Configuration parameters.
 */
$app['path.root'] = __DIR__ . '/../..';
$app['path.template'] = $app['path.root'] . '/template/site';
$app['fake.data'] = ['foo' => 'Foo', 'bar' => 'Bar'];

步骤 2: 获取具有解决依赖的动作。

$action = $app->pageIndexAction;

事件调度器

步骤 1: 订阅监听器。

declare(strict_types=1);

use Satori\Micro\Application;
use App\Logger;

$app = new Application();

/**
 * Declares a logger.
 */
$app->logger = function (Application $app) {
    return new \App\Logger();
};

/**
 * Subscribes a listener to an event.
 *
 * @param string   $event    The unique name of the event.
 * @param string   $listener The unique name of the listener.
 * @param callable $callback The closure or invokable object.
 */
$app->subscribe(
    'system_error', // $event
    'logger',       // $listener
    function (Application $app, array $arguments = null) {
        $logger = $app->logger;
        $logger->log('error', 'System error', $arguments);
    }
);

步骤 2: 通知所有监听器关于一个事件。

$app->notify('system_error', ['file' => '/app/functions.php', 'line' => 20]);

许可证

MIT 许可证