Hook: normalize names
This commit is contained in:
parent
b66e4dfdf2
commit
997c5a0a13
|
@ -68,9 +68,24 @@ class Hook
|
||||||
*/
|
*/
|
||||||
public static function has($name)
|
public static function has($name)
|
||||||
{
|
{
|
||||||
|
$name = self::normalizeHookName($name);
|
||||||
return array_key_exists($name, self::$hooks);
|
return array_key_exists($name, self::$hooks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function normalizeHookName($name)
|
||||||
|
{
|
||||||
|
if (strpos($name, '\\') === false) {
|
||||||
|
$parts = explode('/', $name);
|
||||||
|
foreach ($parts as & $part) {
|
||||||
|
$part = ucfirst($part);
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode('\\', $parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $name;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create or return an instance of a given hook
|
* Create or return an instance of a given hook
|
||||||
*
|
*
|
||||||
|
@ -83,6 +98,8 @@ class Hook
|
||||||
*/
|
*/
|
||||||
public static function createInstance($name, $key)
|
public static function createInstance($name, $key)
|
||||||
{
|
{
|
||||||
|
$name = self::normalizeHookName($name);
|
||||||
|
|
||||||
if (!self::has($name, $key)) {
|
if (!self::has($name, $key)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -134,6 +151,8 @@ class Hook
|
||||||
*/
|
*/
|
||||||
private static function assertValidHook($instance, $name)
|
private static function assertValidHook($instance, $name)
|
||||||
{
|
{
|
||||||
|
$name = self::normalizeHookName($name);
|
||||||
|
|
||||||
$suffix = self::$classSuffix; // 'Hook'
|
$suffix = self::$classSuffix; // 'Hook'
|
||||||
$base = self::$BASE_NS; // 'Icinga\\Web\\Hook\\'
|
$base = self::$BASE_NS; // 'Icinga\\Web\\Hook\\'
|
||||||
|
|
||||||
|
@ -208,6 +227,8 @@ class Hook
|
||||||
*/
|
*/
|
||||||
public static function first($name)
|
public static function first($name)
|
||||||
{
|
{
|
||||||
|
$name = self::normalizeHookName($name);
|
||||||
|
|
||||||
if (self::has($name)) {
|
if (self::has($name)) {
|
||||||
return self::createInstance($name, key(self::$hooks[$name]));
|
return self::createInstance($name, key(self::$hooks[$name]));
|
||||||
}
|
}
|
||||||
|
@ -223,6 +244,8 @@ class Hook
|
||||||
*/
|
*/
|
||||||
public static function register($name, $key, $class)
|
public static function register($name, $key, $class)
|
||||||
{
|
{
|
||||||
|
$name = self::normalizeHookName($name);
|
||||||
|
|
||||||
if (!isset(self::$hooks[$name])) {
|
if (!isset(self::$hooks[$name])) {
|
||||||
self::$hooks[$name] = array();
|
self::$hooks[$name] = array();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue