Download the PHP package piggly/php-hooks without Composer

On this page you can find all versions of the php package piggly/php-hooks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-hooks

Create hooks for run pieces of code in your application

Latest Version on Packagist

Para ler esse arquivo em português clique aqui

This library was inspired by the functions do_action and apply_filter available at Wordress core.

Hooks are a way to run certain piece of code at specific, pre-defined spots. The main avantage of this feature is that hook can accumulates a bunch of functions that will be performed only when requested.

In this library, in contrast to the Wordpress proposal, there are three types of hooks: filters, actions and dispatchers. All of them register a callback, including or not parameters, to a tag. And, later, this tag can be executed.

If you like this library and want to support this job, be free to donate any value to BTC wallet 3DNssbspq7dURaVQH6yBoYwW3PhsNs8dnK ❤.

Installation

This library can be installed by using Composer with composer require piggly/php-hooks;

Registering callbacks

As long as a hook tag is not fired, it is possible to add and/or remove callbacks registered to it. And, for each registered callback, it is possible to create a tag composed by: tag name, function name, required arguments and execution priority.

Tags are formed by two ways: using the Syntax::create() function or writing a string by using tag syntax below.

The syntax of a tag consists of:

A name gave to a callback always needs to be unique. If function name already exists in tag, it will throws NameAlreadyExistsException.

The tag syntax regex is /^(?:(?P<tag>[^\.\:\?]+))(?:\.(?P<name>[^\:\?]+))?(?:\?(?P<args>[\d]+))?(?:\:\:(?P<priority>[\d]+))?$. If tag syntax is wrong, it will throws InvalidSyntaxException.

For example:

Check below another examples:

Callbacks

Any registred function, in addition to requiring a tag, it also requires a callback. The callbacks that can be received by the Hook::filter(), Hook::action() and Hook::dispatch() methods are:

Additional parameters

After referencing the callback, the last parameters will always be the additional parameters attached to that function. For example, Hook::dispatch( $tagSyntax, 'sum', 1 ); will have an additional parameter as 1. Additional parameters are used only in the Hook::dispatch() method.

Understand more about Hook::filter(), Hook::action() and Hook::dispatch() below.

Filters x Actions x Dispatchers

The main difference between these three types of hooks are:

An example of a filter is the filter Hook::apply('comment', $comment) which filters the comment by removing illegal words, removing links, etc.

An example of an action is a notification when a new ticket is created Hook::run('new_ticket', $ticket). The action will receive $ticket and it can send a notification accordingly.

An example of a dispatcher is to include a CSS file in the HTML tag Hook::run('head'). Here, dispatcher will receive CSS $name file at moment it is registered Hook::dispatch('head', 'importCss', 'home.css').

Filters Hook::filter()

Filters give hooks the ability to manipulate data during execution. The filter functions will receive a variable, modify it and return it. The functions registered in a filter must work isolated and should never have side effects. Filters always expect something to be returned to them.

To register a new callback to a filter, the method Hook::filter() should be called with the following parameters:

To apply a filter, the method Hook::apply() should be called with the following parameters:

In addition, there are two variations of the Hook::apply() method, they are:

It is also possible to remove filters before execution with the method Hook::removeFilter().

Tip: Use the Hook::applyOnce() method when a filter tag is used only once during the life cycle of your application, it will free up memory space during run time.

Check out a practical example of using filters below:

Full examples are available here.

Actions Hook::action()

Actions allow you to modify the behavior of your application. The functions of an action can write some output, insert data into the database, send notifications and the so on. The functions registered in an action must always perform some type of task, for this reason no type of return will happen.

To register a new callback to an action, the method Hook::action() should be called with the following parameters:

To run an action, the method Hook::run() should be called with the following parameters:

In addition, there are two variations of the Hook::run() method, they are:

It is also possible to remove actions before execution with the method Hook::removeAction().

Tip: Use the Hook::runOnce() method when an action tag is used only once during the life cycle of your application, it will free up memory space during run time.

Check out a practical example of using actions below:

Full examples are available here.

Dispatchers Hook::dispatch()

Dispatchers have the same behavior as actions. They execute code snippets without any return and are registered by the Hook::dispatch() method. However, the main difference how they receive the additional parameters.

Consider that in your application there is a hook head that will run to add HTML tags into <head> tag of your application.

Let's assume, then, that you have the Controller which includes the methods home(), services() and about() (one equivalent for each page). And yet, there are the files home.css, services.css and about.css. To include them into the head that, it is required to create one callback to each action. See:

Too boring isn't it? This happens because the method Hook::run('head') does not send parameters for actions and actions do not receive parameters. That's because we need one callback by action. It is also not possible to send the required parameters of home(), services() e about() in the point of execution Hook::run('head').

The dispatchers were created to solve this problem. In this case, imagine that we have the function importCss($name). This function echo the <link> tag in the page by using $name as CSS name file. Now, all we need to do is register the dispatcher of this function with the parameter $name. See:

As you can see, we reduced our code and worked with even more efficient hooks. With triggers we can include functions in the code more intelligently without relying on functions equivalent to the action we are creating (since actions do not allow additional parameters outside the Hook::run() executation point).

It is very important to understand that a hook Hook::run('new_ticket', $ticket) is sending the $ticket parameter and this parameter will be received to any action as Hook::action('new_ticket.notification', 'notification');, however it is not received by a Hook::dispatch('new_ticket.notification', 'notification', $ticket); which needs to include $ticket while is being registered.

Actions must be used when they need to inherit Hook::run() execution parameters, while dispatchers must be used when we want to include a function with its own parameters inside the hook.

It is also possible to remove dispatchers before execution with the method Hook::removeDispatcher().

Check below a practical example of using dispatchers:

Full examples are available here.

Changelog

See the CHANGELOG file for information about all code changes.

Testing the code

This library uses the PHPUnit. We carry out tests of all the main classes of this application.

Contributions

See the file CONTRIBUTING for information before submitting your contribution.

Security

If you discover any issues related to security, please send an email to [email protected] instead of using Github's issue tracker.

Credits

Support the project

Piggly Studio is an agency located in Rio de Janeiro, Brazil. If you like this library and want to support this job, be free to donate any value to BTC wallet 3DNssbspq7dURaVQH6yBoYwW3PhsNs8dnK ❤.

License

MIT License (MIT). See LICENSE.


All versions of php-hooks with dependencies

PHP Build Version
Package Version
Requires php Version ^7.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package piggly/php-hooks contains the following files

Loading the files please wait ....