PHPackages                             lehadnk/pluginator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lehadnk/pluginator

AbandonedLibrary[Utility &amp; Helpers](/categories/utility)

lehadnk/pluginator
==================

Easy way to provide a plugin support in your projects.

06PHP

Since Jan 26Pushed 9y agoCompare

[ Source](https://github.com/lehadnk/pluginator)[ Packagist](https://packagist.org/packages/lehadnk/pluginator)[ RSS](/packages/lehadnk-pluginator/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Pluginator
==========

[](#pluginator)

An easy way to provide plugin support in your projects.

Usage
-----

[](#usage)

```
// 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;
    }
});
```

Event System
------------

[](#event-system)

While developing the system, you could create as many triggers as you want, creating entry points for plugin developers to customize the system's behaviour.

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

You could also provide plugin developers an access to the objects and variables. Notice that you should always pass them into the function as an array:

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

Passing variables by link will also allow a plugin developer to change them:

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

Plugin Structure
----------------

[](#plugin-structure)

```
pluginName
└── plugin.php

```

plugin.php is the only plugin entry point. It's being called once plugin is being loaded and should contain a list of all the binders for the system events plugin going to hook:

```
