new pisilinux web sites
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?php namespace ZN\EventHandler;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\EventHandler\Exception\InvalidCallbackMethodException;
|
||||
|
||||
class Event implements EventInterface
|
||||
{
|
||||
/**
|
||||
* Event Callable
|
||||
*/
|
||||
use EventCallable;
|
||||
|
||||
/**
|
||||
* Run event by event name
|
||||
*
|
||||
* @param string $event
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function run(string $event, array $parameters = []) : bool
|
||||
{
|
||||
$events = self::get($event);
|
||||
|
||||
$return = NULL;
|
||||
|
||||
foreach( $events as $e )
|
||||
{
|
||||
$return = $e(...$parameters);
|
||||
}
|
||||
|
||||
if( $return === false )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a listener.
|
||||
*
|
||||
* @param callable|int $callback
|
||||
* @param int|null $priority
|
||||
*/
|
||||
public static function callback($callback = NULL, $priority = NULL) : Event
|
||||
{
|
||||
if( ! is_callable($callback) )
|
||||
{
|
||||
throw new InvalidCallbackMethodException;
|
||||
}
|
||||
|
||||
if( $priority )
|
||||
{
|
||||
Properties::$listener[$priority] = $callback;
|
||||
}
|
||||
else
|
||||
{
|
||||
Properties::$listener[] = $callback;
|
||||
}
|
||||
|
||||
return new self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a listener.
|
||||
*
|
||||
* @param string $event
|
||||
*/
|
||||
public static function create($event = NULL)
|
||||
{
|
||||
$listener = Properties::$listener;
|
||||
|
||||
Properties::$listener = [];
|
||||
|
||||
Properties::$listeners[$event] = $listener;
|
||||
|
||||
return new self;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a listener.
|
||||
*
|
||||
* @param string $event
|
||||
* @param int $priority = NULL
|
||||
*
|
||||
* @return array|callback
|
||||
*/
|
||||
public static function get(string $event, ?int $priority = NULL)
|
||||
{
|
||||
if ( ! isset(Properties::$listeners[$event]))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$listeners = Properties::$listeners[$event];
|
||||
|
||||
ksort($listeners);
|
||||
|
||||
if( $priority )
|
||||
{
|
||||
return $listeners[$priority] ?? [];
|
||||
}
|
||||
|
||||
return $listeners;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a listener.
|
||||
*
|
||||
* @param string $event
|
||||
* @param int $priority = NULL
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function remove(string $event, ?int $priority = NULL) : bool
|
||||
{
|
||||
if( isset(Properties::$listeners[$event][$priority]) )
|
||||
{
|
||||
unset(Properties::$listeners[$event][$priority]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if( $priority === NULL && isset(Properties::$listeners[$event]) )
|
||||
{
|
||||
unset(Properties::$listeners[$event]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php namespace ZN\EventHandler;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
trait EventCallable
|
||||
{
|
||||
/**
|
||||
* Magic call static
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function __callStatic($method, $parameters)
|
||||
{
|
||||
return self::run($method, ...$parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic call for facade Events::run()
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
return self::run($method, ...$parameters);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class Events
|
||||
{
|
||||
use ZN\Ability\Facade;
|
||||
|
||||
const target = 'ZN\EventHandler\Event';
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php namespace ZN\EventHandler;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface EventInterface
|
||||
{
|
||||
/**
|
||||
* Run event by event name
|
||||
*
|
||||
* @param string $event
|
||||
* @param array $parameters
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function run(string $event, array $parameters = []) : bool;
|
||||
|
||||
/**
|
||||
* Insert a listener.
|
||||
*
|
||||
* @param callable|int $callback
|
||||
* @param int|null $priority
|
||||
*/
|
||||
public static function callback($callback = NULL, $priority = NULL) : Event;
|
||||
|
||||
/**
|
||||
* Insert a listener.
|
||||
*
|
||||
* @param string $event
|
||||
*/
|
||||
public static function create($event = NULL);
|
||||
|
||||
/**
|
||||
* Select a listener.
|
||||
*
|
||||
* @param string $event
|
||||
* @param int $priority = NULL
|
||||
*
|
||||
* @return array|callback
|
||||
*/
|
||||
public static function get(string $event, ?int $priority = NULL);
|
||||
|
||||
/**
|
||||
* Delete a listener.
|
||||
*
|
||||
* @param string $event
|
||||
* @param int $priority = NULL
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function remove(string $event, ?int $priority = NULL) : bool;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace ZN\EventHandler\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class InvalidCallbackMethodException extends Exception
|
||||
{
|
||||
const lang =
|
||||
[
|
||||
'en' => 'A callable callback must be specified!',
|
||||
'tr' => 'Bir geri dönüş çağrısı belirtilmelidir!'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php namespace ZN\EventHandler;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class Properties
|
||||
{
|
||||
/**
|
||||
* Keeps listeners
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $listeners = [];
|
||||
|
||||
/**
|
||||
* Keeps listener
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $listener = [];
|
||||
}
|
||||
Reference in New Issue
Block a user