Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Only instantiate plugin classes if needed

  • Loading branch information...
commit 6e454fa8b6af22ad6b82122a5be30c66434e56c6 1 parent a2cd67e
Elbert Alias authored

Showing 1 changed file with 19 additions and 8 deletions. Show diff stats Hide diff stats

  1. +19 8 lib/Swiftlet/App.php
27 lib/Swiftlet/App.php
@@ -67,11 +67,21 @@ public static function run()
67 67 if ( is_file('lib/Swiftlet/Plugins/' . $file) && preg_match('/^(.+)\.php$/', $file, $match) ) {
68 68 $pluginName = 'Swiftlet\Plugins\\' . $match[1];
69 69
70   - self::$_plugins[] = new $pluginName();
  70 + self::$_plugins[$pluginName] = array(
  71 + 'hooks' => array()
  72 + );
  73 +
  74 + foreach ( get_class_methods($pluginName) as $methodName ) {
  75 + $method = new \ReflectionMethod($pluginName, $methodName);
  76 +
  77 + if ( $method->isPublic() && !$method->isFinal() ) {
  78 + self::$_plugins[$pluginName]['hooks'][] = $methodName;
  79 + }
  80 + }
71 81 }
72 82 }
73 83
74   - sort(self::$_plugins);
  84 + ksort(self::$_plugins);
75 85
76 86 closedir($handle);
77 87 }
@@ -224,13 +234,14 @@ public static function getRootPath()
224 234 public static function registerHook($hookName, array $params = array()) {
225 235 self::$_hooks[] = $hookName;
226 236
227   - foreach ( self::$_plugins as $plugin ) {
228   - if ( method_exists($plugin, $hookName) ) {
229   - $method = new \ReflectionMethod(get_class($plugin), $hookName);
230   -
231   - if ( $method->isPublic() && !$method->isFinal() ) {
232   - $plugin->{$hookName}($params);
  237 + foreach ( self::$_plugins as $pluginName => $plugin ) {
  238 + if ( in_array($hookName, $plugin['hooks']) ) {
  239 + if ( !isset($plugin['instance']) ) {
  240 + // Instantiate the plugin
  241 + self::$_plugins[$pluginName]['instance'] = $plugin['instance'] = new $pluginName;
233 242 }
  243 +
  244 + $plugin['instance']->{$hookName}($params);
234 245 }
235 246 }
236 247 }

0 comments on commit 6e454fa

Please sign in to comment.
Something went wrong with that request. Please try again.