docs: Improve documentation and translate comments to English
This commit is contained in:
@@ -12,11 +12,11 @@ use ReflectionParameter;
|
||||
/**
|
||||
* Synapsis - DuckBrain
|
||||
*
|
||||
* Clase encargada de la resolución e inyección de dependencias
|
||||
* Class responsible for dependency resolution and injection.
|
||||
*
|
||||
* @author KJ
|
||||
* @website https://kj2.me
|
||||
* @licence MIT
|
||||
* @license MIT
|
||||
*/
|
||||
class Synapsis
|
||||
{
|
||||
@@ -30,12 +30,13 @@ class Synapsis
|
||||
private static array $instances = [];
|
||||
|
||||
/**
|
||||
* Asocia una interface a una clase para instanciarla en su lugar.
|
||||
* Binds an interface to a class to instantiate it instead.
|
||||
*
|
||||
* @param class-string $interfaceName
|
||||
* @param class-string $className
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception If the interface or class does not exist.
|
||||
*/
|
||||
public static function bind(string $interfaceName, string $className): void
|
||||
{
|
||||
@@ -48,7 +49,7 @@ class Synapsis
|
||||
}
|
||||
|
||||
/**
|
||||
* Asocia en lote múltiples interfaces con múltiples clases.
|
||||
* Binds multiple interfaces to multiple classes in bulk.
|
||||
*
|
||||
* @param array<class-string, class-string> $bindings
|
||||
*
|
||||
@@ -62,22 +63,23 @@ class Synapsis
|
||||
}
|
||||
|
||||
/**
|
||||
* Resuelve e inyecta las dependencias de un callable y devuelve su resultado.
|
||||
* Resolves and injects dependencies for a callable and returns its result.
|
||||
*
|
||||
* @param callable $action
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception If an unhandled callable type is provided.
|
||||
*/
|
||||
public static function resolve(callable $action): mixed
|
||||
{
|
||||
if ($action instanceof Closure) { // si es función anónima
|
||||
if ($action instanceof Closure) { // If it's an anonymous function
|
||||
$reflectionCallback = new ReflectionFunction($action);
|
||||
} else { // Areglo o string con el método
|
||||
} else { // Array or string with the method
|
||||
if (is_string($action)) {
|
||||
$action = preg_split('/::/', $action);
|
||||
}
|
||||
|
||||
// Revisamos su es un método o solo una función
|
||||
// Check if it's a method or just a function
|
||||
if (count($action) == 2) {
|
||||
$reflectionCallback = new ReflectionMethod($action[0], $action[1]);
|
||||
} else {
|
||||
@@ -85,7 +87,7 @@ class Synapsis
|
||||
}
|
||||
}
|
||||
|
||||
// Obtenemos los parámetros
|
||||
// Get the parameters
|
||||
return call_user_func_array(
|
||||
$action,
|
||||
static::resolveParameterValues($reflectionCallback->getParameters())
|
||||
@@ -93,11 +95,12 @@ class Synapsis
|
||||
}
|
||||
|
||||
/**
|
||||
* Resuelve e inyecta las dependencias de una clase y devuelve su resultado.
|
||||
* Resolves and injects dependencies for a class and returns its result.
|
||||
*
|
||||
* @param class-string $className
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception If a binding is missing for an interface or the class does not exist.
|
||||
*/
|
||||
public static function &resolveInstance(string $className): mixed
|
||||
{
|
||||
@@ -114,7 +117,7 @@ class Synapsis
|
||||
}
|
||||
|
||||
if (!isset(static::$instances[$className])) {
|
||||
// Si la instancia no ha sido resuelta antes, la devolvemos
|
||||
// If the instance has not been resolved before, resolve it
|
||||
$reflectionClass = new ReflectionClass($className);
|
||||
$constructor = $reflectionClass->getConstructor();
|
||||
static::$instances[$className] = new $className(
|
||||
@@ -126,17 +129,18 @@ class Synapsis
|
||||
}
|
||||
|
||||
/**
|
||||
* resolveParameterValues
|
||||
* Resolves parameter values by injecting dependencies.
|
||||
*
|
||||
* @param array<ReflectionParameter> $parameters
|
||||
*
|
||||
* @return array
|
||||
* @return array<mixed>
|
||||
* @throws Exception If a primitive parameter does not have a default value.
|
||||
*/
|
||||
public static function resolveParameterValues(array $parameters): array
|
||||
{
|
||||
$values = [];
|
||||
foreach ($parameters as $parameter) {
|
||||
if ($parameter->isOptional()) { // Siempre usamos el valor por defecto primero
|
||||
if ($parameter->isOptional()) { // Always use the default value first
|
||||
$values[] = $parameter->getDefaultValue();
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user