mtymek/modular-expressive

该软件包已被废弃,不再维护。作者建议使用 mtymek/expressive-config-manager 软件包代替。

已弃用,推荐使用 mtymek/expressive-config-manager。提供 Zend Expressive 和 ZF2 模块的集成

0.2.3 2015-12-02 16:24 UTC

This package is auto-updated.

Last update: 2022-02-01 12:50:31 UTC


README

注意:该软件包现在已弃用。请使用 mtymek/expressive-config-manager 代替。

使用与 Zend 框架兼容的模块构建 zend-expressive 应用程序。

Build Status Coverage Status

Modular Expressive 可以在以下三种主要场景下非常有帮助

  • 您想将应用程序逻辑拆分为共享配置和服务的模块,就像在 ZF2 中做的那样。
  • 您想重用一些 ZF2 模块(见下文 "限制" 部分)
  • 您正在将 ZF2 应用程序迁移到 Expressive

安装

使用 composer 安装此库

$ composer require mtymek/modular-expressive

使用方法

Modular Expressive 的核心是一个工厂,可以加载 Zend 框架模块,为 ServiceManager 构建配置,并使用它来创建 Zend\Expressive\Application 对象。

// index.php
use ModularExpressive\ModularApplicationFactory;

$appFactory = new ModularApplicationFactory();
$app = $appFactory->create(
     // system configuration, similar to ZF2
    [
        'modules' => [
            'Application',
            'AnotherModule'
        ],
        'module_listener_options' => [
            'config_glob_paths' => [
                'config/autoload/{{,*.}global,{,*.}local}.php',
            ],
        ]
    ]
);
$app->run();

模块类是 ZF2 消费者版本的简化版

namespace Application;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }
}

模块配置可以合并以加快应用程序引导 - 它与 ZF 应用程序中的系统配置完全一样

return [
    'module_listener_options' => [
        'config_glob_paths' => [
            __DIR__ . '/autoload/{,*.}{global,local}.php'
        ],

        'config_cache_enabled' => true,
        'config_cache_key' => 'config',
        'cache_dir' => 'data/cache',
    ],
];

限制

显然,Expressive 与 Zend 框架是不同的产品,因此您不能期望每个模块在两个环境中都能无缝运行。ZF 架构是事件驱动的,因此许多第三方模块将附加监听器到执行的不同点(路由、调度、渲染...)。在 Expressive 中,如果没有至少一些配置,这不会工作。

然而,有许多有用的模块仅用于为 ServiceManager 提供配置和工厂,并且可以使用此库重用它们,而无需任何额外的代码。

高级使用

自定义 ModuleManager

Modular Expressive 设计为将 ModuleManager 的开销保持在最低,因此默认情况下,它支持的功能仅是配置合并。有一种方法可以通过向 ModularApplicationFactory 注入自定义 ModuleManager 来启用其他模块功能。

例如,以下是允许模块执行一些初始化逻辑的方法(使用 init() 方法)

use Zend\ModuleManager\Listener\ConfigListener;
use Zend\ModuleManager\Listener\InitTrigger;
use Zend\ModuleManager\Listener\ModuleResolverListener;
use Zend\ModuleManager\ModuleManager;

$moduleManager = new ModuleManager([]);
$moduleManager->getEventManager()->attach(
    ModuleEvent::EVENT_LOAD_MODULE_RESOLVE, new ModuleResolverListener()
);
$this->moduleManager->getEventManager()->attach(
    ModuleEvent::EVENT_LOAD_MODULE, new InitTrigger()
);

// inject custom manager into application factory
$appFactory = new ModularApplicationFactory($moduleManager);

在此设置之后,您可以添加 init() 方法到您的模块中,该模块将在启动时执行

namespace Application;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }

    public function init()
    {
        // initialization code!
    }
}