diff --git a/composer.json b/composer.json index 0a9a97ca..24a60fee 100755 --- a/composer.json +++ b/composer.json @@ -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" : "*", diff --git a/rawilum/Events.php b/rawilum/Events.php new file mode 100644 index 00000000..8344f109 --- /dev/null +++ b/rawilum/Events.php @@ -0,0 +1,102 @@ + + * + * 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']); + } + } + } + } + } + } +} diff --git a/rawilum/Rawilum.php b/rawilum/Rawilum.php index 8d1acb3e..52471cf3 100755 --- a/rawilum/Rawilum.php +++ b/rawilum/Rawilum.php @@ -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) {