dedupe getSubclassesOf

This commit is contained in:
Shish 2020-02-23 16:39:55 +00:00
parent 9b6eb0e5e2
commit 4ade3452ee
4 changed files with 46 additions and 39 deletions

View file

@ -304,19 +304,16 @@ abstract class ExtensionInfo
public static function load_all_extension_info() public static function load_all_extension_info()
{ {
foreach (get_declared_classes() as $class) { foreach (getSubclassesOf("ExtensionInfo") as $class) {
$rclass = new ReflectionClass($class); $extension_info = new $class();
if (!$rclass->isAbstract() && is_subclass_of($class, "ExtensionInfo")) { if (array_key_exists($extension_info->key, self::$all_info_by_key)) {
$extension_info = new $class(); throw new ScoreException("Extension Info $class with key $extension_info->key has already been loaded");
if (array_key_exists($extension_info->key, self::$all_info_by_key)) { }
throw new ScoreException("Extension Info $class with key $extension_info->key has already been loaded");
}
self::$all_info_by_key[$extension_info->key] = $extension_info; self::$all_info_by_key[$extension_info->key] = $extension_info;
self::$all_info_by_class[$class] = $extension_info; self::$all_info_by_class[$class] = $extension_info;
if ($extension_info->core===true) { if ($extension_info->core===true) {
self::$core_extensions[] = $extension_info->key; self::$core_extensions[] = $extension_info->key;
}
} }
} }
} }
@ -462,4 +459,12 @@ abstract class DataHandlerExtension extends Extension
abstract protected function check_contents(string $tmpname): bool; abstract protected function check_contents(string $tmpname): bool;
abstract protected function create_image_from_data(string $filename, array $metadata); abstract protected function create_image_from_data(string $filename, array $metadata);
abstract protected function create_thumb(string $hash, string $type): bool; abstract protected function create_thumb(string $hash, string $type): bool;
public static function get_all_supported_exts(): array {
$arr = [];
foreach(getSubclassesOf("DataHandlerExtension") as $handler) {
$arr = array_merge($arr, $handler->SUPPORTED_EXT);
}
return $arr;
}
} }

View file

@ -341,6 +341,17 @@ function get_extension(?string $mime_type): ?string
return ($ext ? $ext : null); return ($ext ? $ext : null);
} }
function getSubclassesOf(string $parent) {
$result = [];
foreach (get_declared_classes() as $class) {
$rclass = new ReflectionClass($class);
if (!$rclass->isAbstract() && is_subclass_of($class, $parent)) {
$result[] = $class;
}
}
return $result;
}
/** /**
* Like glob, with support for matching very long patterns with braces. * Like glob, with support for matching very long patterns with braces.
*/ */

View file

@ -35,26 +35,23 @@ function _set_event_listeners(): void
global $_shm_event_listeners; global $_shm_event_listeners;
$_shm_event_listeners = []; $_shm_event_listeners = [];
foreach (get_declared_classes() as $class) { foreach (getSubclassesOf("Extension") as $class) {
$rclass = new ReflectionClass($class); /** @var Extension $extension */
if (!$rclass->isAbstract() && is_subclass_of($class, "Extension")) { $extension = new $class();
/** @var Extension $extension */
$extension = new $class();
// skip extensions which don't support our current database // skip extensions which don't support our current database
if (!$extension->info->is_supported()) { if (!$extension->info->is_supported()) {
continue; continue;
} }
foreach (get_class_methods($extension) as $method) { foreach (get_class_methods($extension) as $method) {
if (substr($method, 0, 2) == "on") { if (substr($method, 0, 2) == "on") {
$event = substr($method, 2) . "Event"; $event = substr($method, 2) . "Event";
$pos = $extension->get_priority() * 100; $pos = $extension->get_priority() * 100;
while (isset($_shm_event_listeners[$event][$pos])) { while (isset($_shm_event_listeners[$event][$pos])) {
$pos += 1; $pos += 1;
}
$_shm_event_listeners[$event][$pos] = $extension;
} }
$_shm_event_listeners[$event][$pos] = $extension;
} }
} }
} }
@ -64,11 +61,8 @@ function _dump_event_listeners(array $event_listeners, string $path): void
{ {
$p = "<"."?php\n"; $p = "<"."?php\n";
foreach (get_declared_classes() as $class) { foreach (getSubclassesOf("Extension") as $class) {
$rclass = new ReflectionClass($class); $p .= "\$$class = new $class(); ";
if (!$rclass->isAbstract() && is_subclass_of($class, "Extension")) {
$p .= "\$$class = new $class(); ";
}
} }
$p .= "\$_shm_event_listeners = array(\n"; $p .= "\$_shm_event_listeners = array(\n";

View file

@ -72,11 +72,8 @@ class ET extends Extension
$info['stat_image_tags'] = $database->get_one("SELECT COUNT(*) FROM image_tags"); $info['stat_image_tags'] = $database->get_one("SELECT COUNT(*) FROM image_tags");
$els = []; $els = [];
foreach (get_declared_classes() as $class) { foreach (getSubclassesOf("Extension") as $class) {
$rclass = new ReflectionClass($class); $els[] = $class;
if (!$rclass->isAbstract() && is_subclass_of($class, "Extension")) {
$els[] = $class;
}
} }
$info['sys_extensions'] = join(', ', $els); $info['sys_extensions'] = join(', ', $els);