shift/shift

一个事件驱动的MOVE框架

dev-master 2014-01-03 22:24 UTC

This package is auto-updated.

Last update: 2024-08-25 00:29:34 UTC


README

欢迎使用ShiftPHP,这是一个非常简单的PHP事件驱动、以MOVE为导向的框架。

安装

使用Composer安装ShiftPHP框架

$ php composer.phar require shift/shift:0.*

MOVE

ShiftPHP以MOVE为导向:模型、操作、视图和事件。要了解更多信息,请参阅"MVC已死,是时候转向MOVE了"。

使用方法

核心框架不提供您预期的功能。它只提供事件调度器和一些用于它的门面。这意味着您可以做任何您想做的事情,并使用任何您想用的东西。标准的Shift版提供对一些Symfony组件的集成,但您也可以使用其他工具。

主要的门面是Event,其他三个(OperationModelApplication)为这个门面提供了快捷方式。

应用

确保在使用之前始终启动您的应用,启动应用确保事件调度器被添加到Event门面。要启动它,请使用Application::boot()

use Wj\Shift\Facade\Application;

Application::boot();

事件

使用Event::on($event_name)将监听器附加到事件。这将返回一个AttachingEvent,您必须在其中设置目标(使用->asA / ->asAn)和监听器(使用->call)。目标是事件的上下文,例如类名或类类型。监听器可以是任何类型的可调用对象。

use Wj\Shift\Facade\Event;

Event::on('some_event')->forA('the_target')->call(function ($event) {
    // ... do something nice
});

使用Event::trigger($event_name)触发事件,这将调用所有监听器。此方法返回一个TriggeringEvent,您必须在其中设置目标(->forA / ->forAn)和事件类(可以是null)。

use Wj\Shift\Facade\Event;

$event = new SomeEvent();
// ... set up event class

Event::trigger('some_event')->forA('the_target')->with($event);

操作

操作在调用特定事件时执行,使用Operation::on()将操作附加到事件。这将返回一个已将目标设置为operationAttachingEvent。您可以更改此设置。

use Wj\Shift\Facade\Operation;

Operation::on('some_event')->call(function ($event) {
    // ... perform an operation
});

模型

模型也监听事件,可以使用Model::on()附加。这将返回一个已将目标设置为modelAttachingEvent

use Wj\Shift\Facade\Model;

Model::on('some_event')->call(function ($event) {
    // ... do some modelish things
});

视图

视图也监听事件,可以使用View::on()附加。这将返回一个已将目标设置为viewAttachingEvent

View::on('some_event')->call(function ($event) {
    // ... render view
});

操作员

操作员是包含多个操作的类。操作员类实现Wj\Shift\Operator\OperatorInterface。这需要一个名为getOperations的静态方法,它返回一个方法数组和要监听的事件。

namespace Acme\Operator;

use Wj\Shift\Operator\OperatorInterface;

class UserOperator implements OperatorInterface
{
    public static function getOperations()
    {
        return array(
            // key is the event and value is the method
            'save_user' => 'onSaveUser',
            // you can also attach multiple methods to one event
            'find_user' => array('onFindUser', 'onSecondFindUser'),
        );
    }
}

之后,您可以使用Operation::add()注册操作员。

Operation::add('Acme\Operator\UserOperator');

操作员也可以分组/捆绑成包。这意味着您可以轻松激活多个包。包必须实现Wj\Shift\Bundle\BundleInterface。这需要一个名为getOperators的方法,它必须返回一个类名列表。

ShiftPHP提供了一个基本的Bundle类,它在包的根目录中查找*Operator.php文件,并检查其中的类是否实现了OperatorInterface

一个常见的包看起来像

namespace Amce\Bundle\DemoBundle;

use Wj\Shift\Bundle\Bundle;

class AcmeDemoBundle implements Bundle
{ }

要注册包,请使用Application:registerBundle()

Application::registerBundle(new \Acme\Bundle\DemoBundle\AcmeDemoBundle());

贡献

一个框架只有依靠其他人的贡献才能变得更好。问题和讨论以及拉取请求非常受欢迎!没有太多规则,只要您遵守Symfony 标准即可。

许可证

Shift 框架采用 MIT 许可证授权。