Skip to content
Permalink
Browse files

Merge pull request tormjens#9 from tormjens/analysis-8bPRx5

Apply fixes from StyleCI
  • Loading branch information...
tormjens committed Mar 14, 2018
2 parents 1b91a9c + 5497869 commit 428a36213a3d958088d6b67eedc366cad992cec9
@@ -4,19 +4,20 @@
class Action extends Event
{
/**
* Filters a value
* @param string $action Name of action
* @param array $args Arguments passed to the filter
* @return string Always returns the value
* Filters a value.
*
* @param string $action Name of action
* @param array $args Arguments passed to the filter
*
* @return string Always returns the value
*/
public function fire($action, $args)
{
if ($this->getListeners()) {
$this->getListeners()->where('hook', $action)->each(function ($listener) use ($action, $args) {
$parameters = [];
for ($i=0; $i < $listener['arguments']; $i++) {
for ($i = 0; $i < $listener['arguments']; $i++) {
if (isset($args[$i])) {
$parameters[] = $args[$i];
}
@@ -4,9 +4,9 @@
abstract class Event
{
/**
* Holds the event listeners
* Holds the event listeners.
*
* @var array
*/
protected $listeners = null;
@@ -17,26 +17,28 @@ public function __construct()
}
/**
* Adds a listener
* @param string $hook Hook name
* @param mixed $callback Function to execute
* @param integer $priority Priority of the action
* @param integer $arguments Number of arguments to accept
* Adds a listener.
*
* @param string $hook Hook name
* @param mixed $callback Function to execute
* @param int $priority Priority of the action
* @param int $arguments Number of arguments to accept
*/
public function listen($hook, $callback, $priority = 20, $arguments = 1)
{
$this->listeners->push([
'hook' => $hook,
'callback' => $callback,
'priority' => $priority,
'arguments' => $arguments
'hook' => $hook,
'callback' => $callback,
'priority' => $priority,
'arguments' => $arguments,
]);
return $this;
}
/**
* Gets a sorted list of all listeners
* Gets a sorted list of all listeners.
*
* @return array
*/
public function getListeners()
@@ -51,15 +53,18 @@ public function getListeners()
}
/**
* Gets the function
* @param mixed $callback Callback
* @return mixed A closure, an array if "class@method" or a string if "function_name"
* Gets the function.
*
* @param mixed $callback Callback
*
* @return mixed A closure, an array if "class@method" or a string if "function_name"
*/
protected function getFunction($callback)
{
if (is_string($callback) && strpos($callback, '@')) {
$callback = explode('@', $callback);
return array(app('\\' . $callback[0]), $callback[1]);
return [app('\\'.$callback[0]), $callback[1]];
} elseif (is_callable($callback)) {
return $callback;
} else {
@@ -68,9 +73,10 @@ protected function getFunction($callback)
}
/**
* Fires a new action
* @param string $action Name of action
* @param array $args Arguments passed to the action
* Fires a new action.
*
* @param string $action Name of action
* @param array $args Arguments passed to the action
*/
abstract public function fire($action, $args);
}
@@ -2,8 +2,8 @@
namespace TorMorten\Eventy;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
class EventBladeServiceProvider extends ServiceProvider
{
@@ -14,14 +14,14 @@ class EventBladeServiceProvider extends ServiceProvider
*/
public function boot()
{
/**
/*
* Adds a directive in Blade for actions
*/
Blade::directive('action', function ($expression) {
return "<?php Eventy::action({$expression}); ?>";
});
/**
/*
* Adds a directive in Blade for filters
*/
Blade::directive('filter', function ($expression) {
@@ -7,7 +7,7 @@
class EventServiceProvider extends ServiceProvider
{
/**
* Registers the eventy singleton
* Registers the eventy singleton.
*
* @return void
*/
@@ -2,112 +2,120 @@
namespace TorMorten\Eventy;
class Events {
class Events
{
/**
* Holds all registered actions.
*
* @var TorMorten\Events\Action
*/
protected $action;
/**
* Holds all registered actions
* @var TorMorten\Events\Action
*/
protected $action;
/**
* Holds all registered filters.
*
* @var TorMorten\Events\Filter
*/
protected $filter;
/**
* Holds all registered filters
* @var TorMorten\Events\Filter
*/
protected $filter;
/**
* Construct the class.
*/
public function __construct()
{
$this->action = new Action();
$this->filter = new Filter();
}
/**
* Construct the class
*/
public function __construct()
{
$this->action = new Action();
$this->filter = new Filter();
}
/**
* Get the action instance.
*
* @return TorMorten\Events\Action
*/
public function getAction()
{
return $this->action;
}
/**
* Get the action instance
* @return TorMorten\Events\Action
*/
public function getAction() {
return $this->action;
}
/**
* Get the action instance.
*
* @return TorMorten\Events\Filter
*/
public function getFilter()
{
return $this->filter;
}
/**
* Add an action.
*
* @param string $hook Hook name
* @param mixed $callback Function to execute
* @param int $priority Priority of the action
* @param int $arguments Number of arguments to accept
*/
public function addAction($hook, $callback, $priority = 20, $arguments = 1)
{
$this->action->listen($hook, $callback, $priority, $arguments);
}
/**
* Get the action instance
* @return TorMorten\Events\Filter
*/
public function getFilter() {
return $this->filter;
}
/**
* Adds a filter.
*
* @param string $hook Hook name
* @param mixed $callback Function to execute
* @param int $priority Priority of the action
* @param int $arguments Number of arguments to accept
*/
public function addFilter($hook, $callback, $priority = 20, $arguments = 1)
{
$this->filter->listen($hook, $callback, $priority, $arguments);
}
/**
* Add an action
* @param string $hook Hook name
* @param mixed $callback Function to execute
* @param integer $priority Priority of the action
* @param integer $arguments Number of arguments to accept
*/
public function addAction($hook, $callback, $priority = 20, $arguments = 1)
{
$this->action->listen($hook, $callback, $priority, $arguments);
}
/**
* Set a new action.
*
* Actions never return anything. It is merely a way of executing code at a specific time in your code.
*
* You can add as many parameters as you'd like.
*
* @param string $action Name of hook
* @param mixed $parameter1 A parameter
* @param mixed $parameter2 Another parameter
*
* @return void
*/
public function action()
{
$args = func_get_args();
$hook = $args[0];
unset($args[0]);
$args = array_values($args);
$this->action->fire($hook, $args);
}
/**
* Adds a filter
* @param string $hook Hook name
* @param mixed $callback Function to execute
* @param integer $priority Priority of the action
* @param integer $arguments Number of arguments to accept
*/
public function addFilter($hook, $callback, $priority = 20, $arguments = 1)
{
$this->filter->listen($hook, $callback, $priority, $arguments);
}
/**
* Set a new filter.
*
* Filters should always return something. The first parameter will always be the default value.
*
* You can add as many parameters as you'd like.
*
* @param string $action Name of hook
* @param mixed $value The original filter value
* @param mixed $parameter1 A parameter
* @param mixed $parameter2 Another parameter
*
* @return void
*/
public function filter()
{
$args = func_get_args();
$hook = $args[0];
unset($args[0]);
$args = array_values($args);
/**
* Set a new action
*
* Actions never return anything. It is merely a way of executing code at a specific time in your code.
*
* You can add as many parameters as you'd like.
*
* @param string $action Name of hook
* @param mixed $parameter1 A parameter
* @param mixed $parameter2 Another parameter
*
* @return void
*/
public function action()
{
$args = func_get_args();
$hook = $args[0];
unset($args[0]);
$args = array_values($args);
$this->action->fire($hook, $args);
}
/**
* Set a new filter
*
* Filters should always return something. The first parameter will always be the default value.
*
* You can add as many parameters as you'd like.
*
* @param string $action Name of hook
* @param mixed $value The original filter value
* @param mixed $parameter1 A parameter
* @param mixed $parameter2 Another parameter
*
* @return void
*/
public function filter() {
$args = func_get_args();
$hook = $args[0];
unset($args[0]);
$args = array_values($args);
return $this->filter->fire($hook, $args);
}
}
return $this->filter->fire($hook, $args);
}
}
@@ -13,6 +13,6 @@ class Events extends Facade
*/
protected static function getFacadeAccessor()
{
return 'eventy';
return 'eventy';
}
}
}

0 comments on commit 428a362

Please sign in to comment.
You can’t perform that action at this time.