commit version 22.12.12447

This commit is contained in:
2023-01-01 22:36:12 -05:00
parent af1b03d79f
commit b948283a96
744 changed files with 620715 additions and 27381 deletions

View File

@ -0,0 +1,254 @@
<?php
declare(strict_types=1);
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
namespace flight\core;
use Exception;
use InvalidArgumentException;
/**
* The Dispatcher class is responsible for dispatching events. Events
* are simply aliases for class methods or functions. The Dispatcher
* allows you to hook other functions to an event that can modify the
* input parameters and/or the output.
*/
class Dispatcher
{
/**
* Mapped events.
*/
protected array $events = [];
/**
* Method filters.
*/
protected array $filters = [];
/**
* Dispatches an event.
*
* @param string $name Event name
* @param array $params Callback parameters
*
*@throws Exception
*
* @return mixed|null Output of callback
*/
final public function run(string $name, array $params = [])
{
$output = '';
// Run pre-filters
if (!empty($this->filters[$name]['before'])) {
$this->filter($this->filters[$name]['before'], $params, $output);
}
// Run requested method
$output = self::execute($this->get($name), $params);
// Run post-filters
if (!empty($this->filters[$name]['after'])) {
$this->filter($this->filters[$name]['after'], $params, $output);
}
return $output;
}
/**
* Assigns a callback to an event.
*
* @param string $name Event name
* @param callback $callback Callback function
*/
final public function set(string $name, callable $callback): void
{
$this->events[$name] = $callback;
}
/**
* Gets an assigned callback.
*
* @param string $name Event name
*
* @return callback $callback Callback function
*/
final public function get(string $name): ?callable
{
return $this->events[$name] ?? null;
}
/**
* Checks if an event has been set.
*
* @param string $name Event name
*
* @return bool Event status
*/
final public function has(string $name): bool
{
return isset($this->events[$name]);
}
/**
* Clears an event. If no name is given,
* all events are removed.
*
* @param string|null $name Event name
*/
final public function clear(?string $name = null): void
{
if (null !== $name) {
unset($this->events[$name]);
unset($this->filters[$name]);
} else {
$this->events = [];
$this->filters = [];
}
}
/**
* Hooks a callback to an event.
*
* @param string $name Event name
* @param string $type Filter type
* @param callback $callback Callback function
*/
final public function hook(string $name, string $type, callable $callback): void
{
$this->filters[$name][$type][] = $callback;
}
/**
* Executes a chain of method filters.
*
* @param array $filters Chain of filters
* @param array $params Method parameters
* @param mixed $output Method output
*
* @throws Exception
*/
final public function filter(array $filters, array &$params, &$output): void
{
$args = [&$params, &$output];
foreach ($filters as $callback) {
$continue = self::execute($callback, $args);
if (false === $continue) {
break;
}
}
}
/**
* Executes a callback function.
*
* @param array|callback $callback Callback function
* @param array $params Function parameters
*
*@throws Exception
*
* @return mixed Function results
*/
public static function execute($callback, array &$params = [])
{
if (\is_callable($callback)) {
return \is_array($callback) ?
self::invokeMethod($callback, $params) :
self::callFunction($callback, $params);
}
throw new InvalidArgumentException('Invalid callback specified.');
}
/**
* Calls a function.
*
* @param callable|string $func Name of function to call
* @param array $params Function parameters
*
* @return mixed Function results
*/
public static function callFunction($func, array &$params = [])
{
// Call static method
if (\is_string($func) && false !== strpos($func, '::')) {
return \call_user_func_array($func, $params);
}
switch (\count($params)) {
case 0:
return $func();
case 1:
return $func($params[0]);
case 2:
return $func($params[0], $params[1]);
case 3:
return $func($params[0], $params[1], $params[2]);
case 4:
return $func($params[0], $params[1], $params[2], $params[3]);
case 5:
return $func($params[0], $params[1], $params[2], $params[3], $params[4]);
default:
return \call_user_func_array($func, $params);
}
}
/**
* Invokes a method.
*
* @param mixed $func Class method
* @param array $params Class method parameters
*
* @return mixed Function results
*/
public static function invokeMethod($func, array &$params = [])
{
[$class, $method] = $func;
$instance = \is_object($class);
switch (\count($params)) {
case 0:
return ($instance) ?
$class->$method() :
$class::$method();
case 1:
return ($instance) ?
$class->$method($params[0]) :
$class::$method($params[0]);
case 2:
return ($instance) ?
$class->$method($params[0], $params[1]) :
$class::$method($params[0], $params[1]);
case 3:
return ($instance) ?
$class->$method($params[0], $params[1], $params[2]) :
$class::$method($params[0], $params[1], $params[2]);
case 4:
return ($instance) ?
$class->$method($params[0], $params[1], $params[2], $params[3]) :
$class::$method($params[0], $params[1], $params[2], $params[3]);
case 5:
return ($instance) ?
$class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) :
$class::$method($params[0], $params[1], $params[2], $params[3], $params[4]);
default:
return \call_user_func_array($func, $params);
}
}
/**
* Resets the object to the initial state.
*/
final public function reset(): void
{
$this->events = [];
$this->filters = [];
}
}

View File

@ -0,0 +1,233 @@
<?php
declare(strict_types=1);
/**
* Flight: An extensible micro-framework.
*
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
* @license MIT, http://flightphp.com/license
*/
namespace flight\core;
use Exception;
use ReflectionClass;
use ReflectionException;
/**
* The Loader class is responsible for loading objects. It maintains
* a list of reusable class instances and can generate a new class
* instances with custom initialization parameters. It also performs
* class autoloading.
*/
class Loader
{
/**
* Registered classes.
*/
protected array $classes = [];
/**
* Class instances.
*/
protected array $instances = [];
/**
* Autoload directories.
*/
protected static array $dirs = [];
/**
* Registers a class.
*
* @param string $name Registry name
* @param callable|string $class Class name or function to instantiate class
* @param array $params Class initialization parameters
* @param callable|null $callback $callback Function to call after object instantiation
*/
public function register(string $name, $class, array $params = [], ?callable $callback = null): void
{
unset($this->instances[$name]);
$this->classes[$name] = [$class, $params, $callback];
}
/**
* Unregisters a class.
*
* @param string $name Registry name
*/
public function unregister(string $name): void
{
unset($this->classes[$name]);
}
/**
* Loads a registered class.
*
* @param string $name Method name
* @param bool $shared Shared instance
*
* @throws Exception
*
* @return object Class instance
*/
public function load(string $name, bool $shared = true): ?object
{
$obj = null;
if (isset($this->classes[$name])) {
[$class, $params, $callback] = $this->classes[$name];
$exists = isset($this->instances[$name]);
if ($shared) {
$obj = ($exists) ?
$this->getInstance($name) :
$this->newInstance($class, $params);
if (!$exists) {
$this->instances[$name] = $obj;
}
} else {
$obj = $this->newInstance($class, $params);
}
if ($callback && (!$shared || !$exists)) {
$ref = [&$obj];
\call_user_func_array($callback, $ref);
}
}
return $obj;
}
/**
* Gets a single instance of a class.
*
* @param string $name Instance name
*
* @return object Class instance
*/
public function getInstance(string $name): ?object
{
return $this->instances[$name] ?? null;
}
/**
* Gets a new instance of a class.
*
* @param callable|string $class Class name or callback function to instantiate class
* @param array $params Class initialization parameters
*
* @throws Exception
*
* @return object Class instance
*/
public function newInstance($class, array $params = []): object
{
if (\is_callable($class)) {
return \call_user_func_array($class, $params);
}
switch (\count($params)) {
case 0:
return new $class();
case 1:
return new $class($params[0]);
case 2:
return new $class($params[0], $params[1]);
case 3:
return new $class($params[0], $params[1], $params[2]);
case 4:
return new $class($params[0], $params[1], $params[2], $params[3]);
case 5:
return new $class($params[0], $params[1], $params[2], $params[3], $params[4]);
default:
try {
$refClass = new ReflectionClass($class);
return $refClass->newInstanceArgs($params);
} catch (ReflectionException $e) {
throw new Exception("Cannot instantiate {$class}", 0, $e);
}
}
}
/**
* @param string $name Registry name
*
* @return mixed Class information or null if not registered
*/
public function get(string $name)
{
return $this->classes[$name] ?? null;
}
/**
* Resets the object to the initial state.
*/
public function reset(): void
{
$this->classes = [];
$this->instances = [];
}
// Autoloading Functions
/**
* Starts/stops autoloader.
*
* @param bool $enabled Enable/disable autoloading
* @param mixed $dirs Autoload directories
*/
public static function autoload(bool $enabled = true, $dirs = []): void
{
if ($enabled) {
spl_autoload_register([__CLASS__, 'loadClass']);
} else {
spl_autoload_unregister([__CLASS__, 'loadClass']);
}
if (!empty($dirs)) {
self::addDirectory($dirs);
}
}
/**
* Autoloads classes.
*
* @param string $class Class name
*/
public static function loadClass(string $class): void
{
$class_file = str_replace(['\\', '_'], '/', $class) . '.php';
foreach (self::$dirs as $dir) {
$file = $dir . '/' . $class_file;
if (file_exists($file)) {
require $file;
return;
}
}
}
/**
* Adds a directory for autoloading classes.
*
* @param mixed $dir Directory path
*/
public static function addDirectory($dir): void
{
if (\is_array($dir) || \is_object($dir)) {
foreach ($dir as $value) {
self::addDirectory($value);
}
} elseif (\is_string($dir)) {
if (!\in_array($dir, self::$dirs, true)) {
self::$dirs[] = $dir;
}
}
}
}