2017 © Pedro Peláez
 

library hook

Simple hook engine for Laravel 5.*

image

esemve/hook

Simple hook engine for Laravel 5.*

  • Sunday, February 25, 2018
  • by esemve
  • Repository
  • 2 Watchers
  • 15 Stars
  • 82 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

Hook engine for Laravel

What is this?, (*1)

The purpose of this project is that your packages could modify each other without overriding the source code., (*2)

What is a Hook?, (*3)

It is similar to an event. A code bounded by a hook runs unless a hook listener catches it and orders that instead of the function in the hook, something else should run. They could be set in an established order, so you are able to make several modifications in the code., (*4)

What is it good for?, (*5)

Example 1: You have a module which displays an editor. This remains the same editor in every case. If you bound the display of the editor in a hook, then you can write a module which can redefine/override this hook, and for example changs the textarea to a ckeditor., (*6)

Example 2: You list the users. You can include every line's print in a hook. This way you can write a separate module which could extend this line with an e-mail address print., (*7)

Example 3: You save the users' data in a database. If you do it in a hook, you can write a module which could add extra fields to the user model like "first name" or "last name". To do that, you didn't need to modify the code that handles the users, the extension module doesn't need to know the functioning of the main module., (*8)

... and so many other things. If you are building a CMS-like system, it will make your life a lot easier., (*9)

How do I install it?

composer require esemve/hook

then to the app.php :, (*10)

...
'providers' => [
    ...
    Esemve\Hook\HookServiceProvider::class,
    ...
 ],
 'aliases' =>[
    ...
    'Hook' => Esemve\Hook\Facades\Hook::class
    ...
 ]

How does it work?

Example:, (*11)

$user = new User();
$user = Hook::get('fillUser',[$user],function($user){
    return $user;
});

In this case a fillUser hook is thrown, which receive the $user object as a parameter. If nothing catches it, the internal function, the return $user will run, so nothing happens. But it can be caught by a listener from a provider:, (*12)

Hook::listen('fillUser', function ($callback, $output, $user) {
    if (empty($output))
    {
      $output = $user;
    }
    $output->profilImage = ProfilImage::getForUser($user->id);
    return $output;
}, 10);

The $callback contains the hook's original internal function, so it can be called here., (*13)

Multiple listeners could be registered to a hook, so in the $output the listener receives the response of the previously registered listeners of the hook., (*14)

THen come the parameters delivered by the hook, in this case the user., (*15)

The hook listener above caught the call of the fillUser, extended the received object, and returned it to its original place. After the run of the hook the $user object contains a profilImage variable as well., (*16)

Number 10 in the example is the priority. They are executed in an order, so if a number 5 is registered to the fillUser as well, it will run before number 10., (*17)

Initial output

You can pass initial output to the listeners too., (*18)

$initialOutput='test string';

\Hook::get('testing',['other string'],function($otherString){
    return $otherString;
},$initialOutput)

// and later ...

Hook::listen('testing', function ($callback, $output, $otherString) {
    if ($output==='test string') {
        $output="{$output} yeeeaaaayyy!";
    }
    if ($otherString==='other_string') {
        // other string is good too
    }
    return $output; // 'test string yeeeaaaayyy!'
});

If there is no listeners, 'other string' will be returned., (*19)

Usage in blade templates

@hook('hookName')

In this case the hook listener can catch it like this:, (*20)

 Hook::listen('template.hookName', function ($callback, $output, $variables) {
   return view('test.button');
 });

In the $variables variable it receives all of the variables that are available for the blade template., (*21)

:exclamation: To listen blade templates you need to listen template.hookName instead of just hookName!, (*22)

Wrap HTML

@hook('hookName', true)
    this content can be modified with dom parsers
    you can inject some html here
@endhook

Now the $output parameter contains html wrapped by hook component., (*23)

Hook::listen('template.hookName', function ($callback, $output, $variables) {
  return "<div class=\"alert alert-success\">$output</div>";
});

Stop

Hook::stop();

Put in a hook listener it stops the running of the other listeners that are registered to this hook., (*24)

For testing

Hook::mock('hookName','returnValue');

After that the hookName hook will return returnValue as a response., (*25)

Artisan

php artisan hook::list

Lists all the active hook listeners., (*26)


License: MIT, (*27)

The Versions

25/02 2018

dev-master

9999999-dev

Simple hook engine for Laravel 5.*

  Sources   Download

MIT

The Requires

 

by Bence Kádár

laravel hook

25/02 2018

0.0.4

0.0.4.0

Simple hook engine for Laravel 5.*

  Sources   Download

MIT

The Requires

 

by Bence Kádár

laravel hook

25/02 2018

dev-analysis-8K4GO3

dev-analysis-8K4GO3

Simple hook engine for Laravel 5.*

  Sources   Download

MIT

The Requires

 

by Bence Kádár

laravel hook

01/09 2016

0.0.3

0.0.3.0

Simple hook engine for Laravel 5.*

  Sources   Download

MIT

The Requires

 

by Bence Kádár

laravel hook

25/05 2016

0.0.2

0.0.2.0

Simple hook engine for Laravel 5.*

  Sources   Download

MIT

The Requires

 

by Bence Kádár

laravel hook

18/05 2016

0.0.1

0.0.1.0

Simple hook engine for Laravel 5.*

  Sources   Download

MIT

The Requires

 

by Bence Kádár

laravel hook