mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 14:16:46 +02:00
Implement Events #8
This commit is contained in:
@@ -24,10 +24,8 @@
|
||||
"symfony/console": "4.0.4",
|
||||
"symfony/filesystem": "4.0.4",
|
||||
"symfony/finder": "4.0.4",
|
||||
"symfony/event-dispatcher": "4.0.5",
|
||||
"pimple/pimple": "3.2.3",
|
||||
"force/session" : "*",
|
||||
"force/shortcode" : "*",
|
||||
"force/arr" : "*",
|
||||
"force/http" : "*",
|
||||
"force/token" : "*",
|
||||
|
102
rawilum/Events.php
Normal file
102
rawilum/Events.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace Rawilum;
|
||||
|
||||
use Arr;
|
||||
|
||||
/**
|
||||
* This file is part of the Rawilum.
|
||||
*
|
||||
* (c) Romanenko Sergey / Awilum <awilum@msn.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
class Events
|
||||
{
|
||||
/**
|
||||
* @var Rawilum
|
||||
*/
|
||||
protected $rawilum;
|
||||
|
||||
/**
|
||||
* Events
|
||||
*
|
||||
* @var array
|
||||
* @access protected
|
||||
*/
|
||||
protected $events = [];
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*/
|
||||
public function __construct(Rawilum $c)
|
||||
{
|
||||
$this->rawilum = $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hooks a function on to a specific event.
|
||||
*
|
||||
* @access public
|
||||
* @param string $event_name Event name
|
||||
* @param mixed $added_function Added function
|
||||
* @param integer $priority Priority. Default is 10
|
||||
* @param array $args Arguments
|
||||
*/
|
||||
public function addListener(string $event_name, $added_function, int $priority = 10, array $args = null)
|
||||
{
|
||||
// Hooks a function on to a specific event.
|
||||
$this->events[] = array(
|
||||
'event_name' => $event_name,
|
||||
'function' => $added_function,
|
||||
'priority' => $priority,
|
||||
'args' => $args
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run functions hooked on a specific event.
|
||||
*
|
||||
* @access public
|
||||
* @param string $event_name Event name
|
||||
* @param array $args Arguments
|
||||
* @param boolean $return Return data or not. Default is false
|
||||
* @return mixed
|
||||
*/
|
||||
public function dispatch(string $event_name, array $args = [], bool $return = false)
|
||||
{
|
||||
// Redefine arguments
|
||||
$event_name = $event_name;
|
||||
$return = $return;
|
||||
|
||||
// Run event
|
||||
if (count($this->events) > 0) {
|
||||
|
||||
// Sort actions by priority
|
||||
$events = Arr::subvalSort($this->events, 'priority');
|
||||
|
||||
// Loop through $events array
|
||||
foreach ($events as $action) {
|
||||
|
||||
// Execute specific action
|
||||
if ($action['event_name'] == $event_name) {
|
||||
// isset arguments ?
|
||||
if (isset($args)) {
|
||||
// Return or Render specific action results ?
|
||||
if ($return) {
|
||||
return call_user_func_array($action['function'], $args);
|
||||
} else {
|
||||
call_user_func_array($action['function'], $args);
|
||||
}
|
||||
} else {
|
||||
if ($return) {
|
||||
return call_user_func_array($action['function'], $action['args']);
|
||||
} else {
|
||||
call_user_func_array($action['function'], $action['args']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,8 +4,6 @@ namespace Rawilum;
|
||||
use Pimple\Container as Container;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcher;
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
use ParsedownExtra;
|
||||
use Url;
|
||||
|
||||
@@ -64,11 +62,11 @@ class Rawilum extends Container
|
||||
};
|
||||
|
||||
$container['events'] = function ($c) {
|
||||
return new EventDispatcher();
|
||||
return new Events($c);
|
||||
};
|
||||
|
||||
$container['filters'] = function ($c) {
|
||||
return new Filter($c);
|
||||
return new Filters($c);
|
||||
};
|
||||
|
||||
$container['markdown'] = function ($c) {
|
||||
|
Reference in New Issue
Block a user