Skip to content

Commit

Permalink
Merge pull request #84 from minhkhoablieu/master
Browse files Browse the repository at this point in the history
  • Loading branch information
tormjens authored Apr 1, 2024
2 parents f4d502e + 913a135 commit 6e3e6ca
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ You are @filter('my.hook', 'awesome')
```

### Helper functions

Eventy also comes with a few helper functions to make it easier to work with actions and filters.

```php
// Add an action
add_action('my.hook', function($user) {
if ($user->is_awesome) {
$this->doSomethingAwesome($user);
}
}, 20, 1);
```

```php
// Add a filter
add_filter('my.hook', function($what) {
$what = 'not '. $what;
return $what;
}, 20, 1);
```

```php
// Fire an action
do_action('my.hook', $user);
```

```php
// Fire a filter
apply_filters('my.hook', 'awesome');
```

### Using it to enable extensibility

Here's an example of how Eventy could be used in a real application where you have the concept of plugins.
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"autoload": {
"psr-4": {
"TorMorten\\Eventy\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
9 changes: 9 additions & 0 deletions src/Facades/Eventy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

use Illuminate\Support\Facades\Facade;

/**
* @method static void addFilter(string $hook, callable $callback, int $priority = 20, int $arguments = 1)
* @method static mixed filter(string $action, $value, ...$parameters)
* @method static void addAction(string $hook, callable $callback, int $priority = 20, int $arguments = 1)
* @method static void action(string $action, ...$parameters)
*
* @see \TorMorten\Eventy\Events
*/

class Eventy extends Facade
{
/**
Expand Down
85 changes: 85 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

if (!function_exists('eventy')) {

/**
* Get the Eventy instance
*
* @return TorMorten\Eventy\Facades\Events
*/
function eventy()
{
return app('eventy');
}
}

if (!function_exists('add_filter')) {

/**
* Add a filter to the Eventy instance
*
* @param string $hook
* @param callable $function
* @param int $priority
* @param int $arguments
*
* @return void
*/
function add_filter($hook, $callback, $priority = 20, $arguments = 1)
{
eventy()->addFilter($hook, $callback, $priority, $arguments);
}
}


if (!function_exists('apply_filters')) {

/**
* Apply filters to a value
*
* @param string $action
* @param mixed $value
* @param mixed $parameters
*
* @return mixed
*/
function apply_filters(string $action, $value, ...$parameters)
{
return eventy()->filter($action, $value, ...$parameters);
}
}


if (!function_exists('add_action')) {

/**
* Add an action to the Eventy instance
*
* @param string $hook
* @param callable $function
* @param int $priority
* @param int $arguments
*
* @return void
*/
function add_action($hook, $callback, $priority = 20, $arguments = 1)
{
eventy()->addAction($hook, $callback, $priority, $arguments);
}
}

if (!function_exists('do_action')) {

/**
* Execute the actions for the hook
*
* @param string $hook
* @param mixed $parameters
*
* @return void
*/
function do_action(string $action, ...$parameters)
{
eventy()->action($action, ...$parameters);
}
}

0 comments on commit 6e3e6ca

Please sign in to comment.