Permalink
Browse files

Moved from file-based to class-based plugin loading to ease composition

  • Loading branch information...
1 parent 90a5753 commit a144b99e3d7b96f10906aed6f32fd1433ed11920 Doug Hurst committed Oct 12, 2012
Showing with 56 additions and 16 deletions.
  1. +2 −0 .gitignore
  2. +41 −0 src/Philip/AbstractPlugin.php
  3. +13 −16 src/Philip/Philip.php
View
@@ -2,9 +2,11 @@
.DS_Store
*.swp
.idea
+*.iml
# Ignore Test Runners and Logs
test-bot.*
+TestPlugin.*
# Ignore composer files
composer.phar
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Philip
+ *
+ * PHP Version 5.3
+ *
+ * @package philip
+ * @copyright 2012 iostudio. LLC
+ */
+
+namespace Philip;
+
+/**
+ * Philip Plugin Abstract
+ *
+ * @package philip
+ * @author Doug Hurst <dalan.hurst@gmail.com>
+ * @since 2012-10-12
+ */
+abstract class AbstractPlugin
+{
+ /**
+ * @var Philip
+ */
+ protected $_bot;
+
+ /**
+ * Constructor
+ *
+ * @param Philip $bot
+ */
+ public function __construct(Philip $bot)
+ {
+ $this->_bot = $bot;
+ }
+
+ /**
+ * Init the plugin and start listening to messages
+ */
+ abstract public function init();
+}
View
@@ -160,36 +160,33 @@ public function getLogger()
/**
* Loads a plugin. See the README for plugin documentation.
*
- * @param string $name The name of the plugin to load
+ * @param string $name The fully-qualified classname of the plugin to load
*/
- public function loadPlugin($name)
+ public function loadPlugin($classname)
{
- $plugin_class = $name . 'Plugin';
- $path = 'plugins/' . $plugin_class . '.php';
- if (file_exists($path)) {
- require($path);
+ if (class_exists($classname)
+ && $plugin = new $classname($this)
+ and $plugin instanceof \Philip\AbstractPlugin
+ ) {
+ $plugin->init();
}
-
- $n = "\\" . $plugin_class;
- $plugin = new $n($this);
- $plugin->init();
}
/**
* Loads multiple plugins in a single call.
*
- * @param array $names The names of the plugins to load.
+ * @param array $names The fully-qualified classnames of the plugins to load.
*/
- public function loadPlugins($names)
+ public function loadPlugins($classnames)
{
- foreach ($names as $name) {
- $this->loadPlugin($name);
+ foreach ($classnames as $classname) {
+ $this->loadPlugin($classname);
}
}
/**
* Determines if the given user is an admin.
- *
+ *
* @param string $user The username to test
* @return boolean True if the user is an admin, false otherwise
*/
@@ -280,7 +277,7 @@ private function listen()
}
}
} while (!feof($this->socket));
- }
+ }
/**
* Convert the raw incoming IRC message into a Request object

0 comments on commit a144b99

Please sign in to comment.