Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Core/Template/ExtensionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ trait ExtensionsTrait
*/
protected static $extensionCache = [];

/**
* Cache to avoid repeated warnings for duplicated extensions.
*
* @var array
*/
protected static array $avisoExtensionCache = [];

/**
* Executes the first matched extension.
*
Expand All @@ -59,6 +66,7 @@ public function __call($name, $arguments = [])

// Execute first extension found (respecting priority)
if (!empty(static::$extensionCache[$name])) {
$this->avisarExtensionDuplicada($name);
return call_user_func_array(static::$extensionCache[$name][0]->bindTo($this, static::class), $arguments);
}

Expand Down Expand Up @@ -95,6 +103,7 @@ public static function addExtension($extension, int $priority = 100): void

// Clear cache when adding new extensions
static::$extensionCache = [];
static::$avisoExtensionCache = [];
}

/**
Expand All @@ -104,6 +113,7 @@ public static function clearExtensions(): void
{
static::$extensions = [];
static::$extensionCache = [];
static::$avisoExtensionCache = [];
}

/**
Expand Down Expand Up @@ -194,6 +204,7 @@ public static function removeExtension(string $name): bool
// Clear cache when removing extensions
if ($removed) {
unset(static::$extensionCache[$name]);
unset(static::$avisoExtensionCache[static::class . '::' . $name]);
}

return $removed;
Expand All @@ -219,4 +230,26 @@ private function buildExtensionCache(string $name): void
// Store sorted functions in cache
static::$extensionCache[$name] = array_column($extensions, 'function');
}

/**
* Comprobamos que no existe la misma funcion en otra extensión de otro plugin
*
* @param string $name
*
* @return void
*/
private function avisarExtensionDuplicada(string $name): void
{
if (count(static::$extensionCache[$name]) < 2) {
return;
}

$clave = static::class . '::' . $name;
if (isset(static::$avisoExtensionCache[$clave])) {
return;
}

static::$avisoExtensionCache[$clave] = true;
throw new \RuntimeException('Método de extensión duplicado ' . $clave . '. Este método existe en otro plugin y solo ejecutará el primero.');
}
}
Loading