lehadnk/pluginator

该包已被 废弃 并不再维护。未建议替代包。
该包的最新版本(dev-master)没有可用的许可证信息。

在您的项目中提供插件支持的简单方法。

dev-master 2017-01-26 15:44 UTC

This package is not auto-updated.

Last update: 2024-03-22 14:31:49 UTC


README

在您的项目中提供插件支持的简单方法。

用法

// Application Bootstrap
\Pluginator\Core::setConfig([
    'pluginsDir' => __DIR__.'/plugins/',
]);
\Pluginator\Core::init();
// Pluginable app code
class Cart {
    public function addToCart(Product $product) {
        doSomething();    
        \Pluginator\EvenHandler::trigger('addToCart', [$cart, $product]);
    }
}
/**
* Black Friday 25% off plugin code
* in /plugins/blackfriday/plugin.php
*/
\Pluginator\EventHandler::bind('addToCart', function (Cart $cart, Product $product) {
    if (date('m-d') == date('m-d', strtotime('fourth friday of november'))) {
        $cart->total = $cart->calculate() * 0.75;
    }
});

事件系统

在开发系统时,您可以创建尽可能多的触发器,为插件开发者创建自定义系统行为的入口点。

class BlogController extends ControllerBase {
    public function savePost() {
        \Pluginator\EventHandler::trigger('beforeSavingPost');
        $this->post->save();
        \Pluginator\EventHandler::trigger('afterSavingPost');
    }
}

您还可以为插件开发者提供访问对象和变量的权限。请注意,您应该始终将它们作为数组传递给函数

$post = $db->getEntity('post', ['id' => 16]);
\Pluginator\EventHandler::trigger('loadPost', [$post]);

通过链接传递变量也将允许插件开发者更改它们

$price = $cart->calculatePrice();
\Pluginator\EventHandler::trigger('priceCalculate', [&$price]);
// Black friday plugin mentioned above recalculates 25% off
$user->card->charge($price);

插件结构

pluginName
└── plugin.php

plugin.php 是唯一的插件入口点。它在插件加载时被调用一次,应包含系统事件插件的绑定列表

<?php

\Pluginator\EventHandler::bind('addToCart', 'addToCart');
\Pluginator\EventHandler::bind('makeOrder', 'Plugin::makeOrder');
\Pluginator\EventHandler::bind('emailEntered', function() {});

绑定器

您可以使用所有类型的可调用实体来绑定您的处理器

\Pluginator\EventHandler::bind('testFunction', function (&$b) {
    $b += 10;
});

$a = 15;
\Pluginator\EventHandler::bind('testClosure', function (&$b) use ($a) {
    $b += $a;
});

class TestPlugin {
    public static function testStatic(&$a) {
        $a += 20;
    }

    public function testObject(&$a) {
        $a += 40;
    }
}

\Pluginator\EventHandler::bind('testStatic', 'TestPlugin::testStatic');

$plugin = new TestPlugin();
\Pluginator\EventHandler::bind('testObject', [$plugin, 'testObject']);

与应用程序的集成

您的处理器将由应用程序执行,因此只要存在某些自动加载功能,您就可以访问所有框架/应用程序功能

// Plugin for yii2-powered project
\Pluginator\EventHandler::bind('pageView', function (Page $page) {
    $connection = Yii::$app->getDb();
    $command = $connection->createCommand("
        UPDATE pages SET views = views + 1 WHERE id = :id
    ", [':id' => $page->id])->queryAll();
});

通过引用传递

您可以将变量通过引用传递给函数,以便函数可以修改该变量。语法如下

\Pluginator\EventHandler::bind('testFunction', function (&$b) {
    $b += 10;
});

请注意,您应该遵循应用程序插件开发者文档,或在使用此功能之前联系应用程序开发者,因为您只能获取应用程序开发者愿意共享的变量。否则,将抛出异常

Parameter 1 to TestPlugin::testStatic() expected to be a reference, value given

这意味着该开发者不允许您使用引用,而是传递值,因此无法修改它们。

插件安装

将插件放置在指定的 "pluginsDir" 目录下。通常,项目文档应说明哪一个。不需要额外的步骤。

plugins
├── blackfriday
├── googlefonts
└── seotags