Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Only instantiate plugin classes if needed

  • Loading branch information...
commit 6e454fa8b6af22ad6b82122a5be30c66434e56c6 1 parent a2cd67e
Elbert Alias authored
Showing with 19 additions and 8 deletions.
  1. +19 −8 lib/Swiftlet/App.php
27 lib/Swiftlet/App.php
View
@@ -67,11 +67,21 @@ public static function run()
if ( is_file('lib/Swiftlet/Plugins/' . $file) && preg_match('/^(.+)\.php$/', $file, $match) ) {
$pluginName = 'Swiftlet\Plugins\\' . $match[1];
- self::$_plugins[] = new $pluginName();
+ self::$_plugins[$pluginName] = array(
+ 'hooks' => array()
+ );
+
+ foreach ( get_class_methods($pluginName) as $methodName ) {
+ $method = new \ReflectionMethod($pluginName, $methodName);
+
+ if ( $method->isPublic() && !$method->isFinal() ) {
+ self::$_plugins[$pluginName]['hooks'][] = $methodName;
+ }
+ }
}
}
- sort(self::$_plugins);
+ ksort(self::$_plugins);
closedir($handle);
}
@@ -224,13 +234,14 @@ public static function getRootPath()
public static function registerHook($hookName, array $params = array()) {
self::$_hooks[] = $hookName;
- foreach ( self::$_plugins as $plugin ) {
- if ( method_exists($plugin, $hookName) ) {
- $method = new \ReflectionMethod(get_class($plugin), $hookName);
-
- if ( $method->isPublic() && !$method->isFinal() ) {
- $plugin->{$hookName}($params);
+ foreach ( self::$_plugins as $pluginName => $plugin ) {
+ if ( in_array($hookName, $plugin['hooks']) ) {
+ if ( !isset($plugin['instance']) ) {
+ // Instantiate the plugin
+ self::$_plugins[$pluginName]['instance'] = $plugin['instance'] = new $pluginName;
}
+
+ $plugin['instance']->{$hookName}($params);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.