init web ems all

This commit is contained in:
agtuser
2024-09-27 17:13:36 +08:00
parent 81c97acbe9
commit 5cc56f8078
4263 changed files with 798779 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\AliasItem class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
/**
* Class AliasItem
*
* @package PhpMyAdmin\Di
*/
class AliasItem implements Item
{
/** @var Container */
protected $container;
/** @var string */
protected $target;
/**
* Constructor
*
* @param Container $container Container
* @param string $target Target
*/
public function __construct(Container $container, $target)
{
$this->container = $container;
$this->target = $target;
}
/**
* Get the target item
*
* @param array $params Parameters
* @return mixed
*/
public function get(array $params = array())
{
return $this->container->get($this->target, $params);
}
}

View File

@@ -0,0 +1,189 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\Container class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
use Psr\Container\ContainerInterface;
/**
* Class Container
*
* @package PhpMyAdmin\Di
*/
class Container implements ContainerInterface
{
/**
* @var Item[] $content
*/
protected $content = array();
/**
* @var Container
*/
protected static $defaultContainer;
/**
* Create a dependency injection container
*
* @param Container $base Container
*/
public function __construct(Container $base = null)
{
if (isset($base)) {
$this->content = $base->content;
} else {
$this->alias('container', 'Container');
}
$this->set('Container', $this);
}
/**
* Get an object with given name and parameters
*
* @param string $name Name
* @param array $params Parameters
*
* @throws NotFoundException No entry was found for **this** identifier.
* @throws ContainerException Error while retrieving the entry.
*
* @return mixed
*/
public function get($name, array $params = array())
{
if (!$this->has($name)) {
throw new NotFoundException("No entry was found for $name identifier.");
}
if (isset($this->content[$name])) {
return $this->content[$name]->get($params);
} elseif (isset($GLOBALS[$name])) {
return $GLOBALS[$name];
} else {
throw new ContainerException("Error while retrieving the entry.");
}
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($name)` returning true does not mean that `get($name)` will not throw an exception.
* It does however mean that `get($name)` will not throw a `NotFoundException`.
*
* @param string $name Identifier of the entry to look for.
*
* @return bool
*/
public function has($name)
{
return isset($this->content[$name]) || isset($GLOBALS[$name]);
}
/**
* Remove an object from container
*
* @param string $name Name
*
* @return void
*/
public function remove($name)
{
unset($this->content[$name]);
}
/**
* Rename an object in container
*
* @param string $name Name
* @param string $newName New name
*
* @return void
*/
public function rename($name, $newName)
{
$this->content[$newName] = $this->content[$name];
$this->remove($name);
}
/**
* Set values in the container
*
* @param string|array $name Name
* @param mixed $value Value
*
* @return void
*/
public function set($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->set($key, $val);
}
return;
}
$this->content[$name] = new ValueItem($value);
}
/**
* Register a service in the container
*
* @param string $name Name
* @param mixed $service Service
*
* @return void
*/
public function service($name, $service = null)
{
if (!isset($service)) {
$service = $name;
}
$this->content[$name] = new ServiceItem($this, $service);
}
/**
* Register a factory in the container
*
* @param string $name Name
* @param mixed $factory Factory
*
* @return void
*/
public function factory($name, $factory = null)
{
if (!isset($factory)) {
$factory = $name;
}
$this->content[$name] = new FactoryItem($this, $factory);
}
/**
* Register an alias in the container
*
* @param string $name Name
* @param string $target Target
*
* @return void
*/
public function alias($name, $target)
{
// The target may be not defined yet
$this->content[$name] = new AliasItem($this, $target);
}
/**
* Get the global default container
*
* @return Container
*/
public static function getDefaultContainer()
{
if (!isset(static::$defaultContainer)) {
static::$defaultContainer = new Container();
}
return static::$defaultContainer;
}
}

View File

@@ -0,0 +1,21 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\ContainerException class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
use Exception;
use Psr\Container\ContainerExceptionInterface;
/**
* Class ContainerException
*
* @package PhpMyAdmin\Di
*/
class ContainerException extends Exception implements ContainerExceptionInterface
{
}

View File

@@ -0,0 +1,29 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\FactoryItem class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
/**
* Factory manager
*
* @package PhpMyAdmin\Di
*/
class FactoryItem extends ReflectorItem
{
/**
* Construct an instance
*
* @param array $params Parameters
*
* @return mixed
*/
public function get(array $params = array())
{
return $this->invoke($params);
}
}

View File

@@ -0,0 +1,25 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\Item class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
/**
* Interface Item
*
* @package PhpMyAdmin\Di
*/
interface Item
{
/**
* Get a value from the item
*
* @param array $params Parameters
* @return mixed
*/
public function get(array $params = array());
}

View File

@@ -0,0 +1,20 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\NotFoundException class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class NotFoundException
*
* @package PhpMyAdmin\Di
*/
class NotFoundException extends ContainerException implements NotFoundExceptionInterface
{
}

View File

@@ -0,0 +1,128 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\ReflectorItem class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
/**
* Reflector manager
*
* @package PhpMyAdmin\Di
*/
abstract class ReflectorItem implements Item
{
/** @var Container */
private $_container;
/** @var \Reflector */
private $_reflector;
/**
* Constructor
*
* @param Container $container Container
* @param mixed $definition Definition
*/
public function __construct(Container $container, $definition)
{
$this->_container = $container;
$this->_reflector = self::_resolveReflector($definition);
}
/**
* Invoke the reflector with given parameters
*
* @param array $params Parameters
* @return mixed
*/
protected function invoke(array $params = array())
{
$args = array();
$reflector = $this->_reflector;
if ($reflector instanceof \ReflectionClass) {
$constructor = $reflector->getConstructor();
if (isset($constructor)) {
$args = $this->_resolveArgs(
$constructor->getParameters(),
$params
);
}
return $reflector->newInstanceArgs($args);
}
/** @var \ReflectionFunctionAbstract $reflector */
$args = $this->_resolveArgs(
$reflector->getParameters(),
$params
);
if ($reflector instanceof \ReflectionMethod) {
/** @var \ReflectionMethod $reflector */
return $reflector->invokeArgs(null, $args);
}
/** @var \ReflectionFunction $reflector */
return $reflector->invokeArgs($args);
}
/**
* Getting required arguments with given parameters
*
* @param \ReflectionParameter[] $required Arguments
* @param array $params Parameters
*
*@return array
*/
private function _resolveArgs($required, array $params = array())
{
$args = array();
foreach ($required as $param) {
$name = $param->getName();
$type = $param->getClass();
if (isset($type)) {
$type = $type->getName();
}
if (isset($params[$name])) {
$args[] = $params[$name];
} elseif (is_string($type) && isset($params[$type])) {
$args[] = $params[$type];
} else {
try {
$content = $this->_container->get($name);
if (isset($content)) {
$args[] = $content;
} elseif (is_string($type)) {
$args[] = $this->_container->get($type);
} else {
$args[] = null;
}
} catch (NotFoundException $e) {
$args[] = null;
}
}
}
return $args;
}
/**
* Resolve the reflection
*
* @param mixed $definition Definition
*
* @return \Reflector
*/
private static function _resolveReflector($definition)
{
if (function_exists($definition)) {
return new \ReflectionFunction($definition);
}
if (is_string($definition)) {
$definition = explode('::', $definition);
}
if (!isset($definition[1])) {
return new \ReflectionClass($definition[0]);
}
return new \ReflectionMethod($definition[0], $definition[1]);
}
}

View File

@@ -0,0 +1,34 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\ServiceItem class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
/**
* Service manager
*
* @package PhpMyAdmin\Di
*/
class ServiceItem extends ReflectorItem
{
/** @var mixed */
protected $instance;
/**
* Get the instance of the service
*
* @param array $params Parameters
* @return mixed
*/
public function get(array $params = array())
{
if (!isset($this->instance)) {
$this->instance = $this->invoke();
}
return $this->instance;
}
}

View File

@@ -0,0 +1,41 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PhpMyAdmin\Di\ValueItem class
*
* @package PhpMyAdmin\Di
*/
namespace PhpMyAdmin\Di;
/**
* Value manager
*
* @package PhpMyAdmin\Di
*/
class ValueItem implements Item
{
/** @var mixed */
protected $value;
/**
* Constructor
*
* @param mixed $value Value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* Get the value
*
* @param array $params Parameters
* @return mixed
*/
public function get(array $params = array())
{
return $this->value;
}
}