9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Reload
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Reload
23 * Facilitates reloading of individual plugins for development purposes.
24 * Note that, because existing class definitions cannot be removed from
25 * memory, increased memory usage is an expected result of using this plugin.
28 * @package Phergie_Plugin_Reload
29 * @author Phergie Development Team <team@phergie.org>
30 * @license http://phergie.org/license New BSD License
31 * @link http://pear.phergie.org/package/Phergie_Plugin_Reload
32 * @uses Phergie_Plugin_Command pear.phergie.org
34 class Phergie_Plugin_Reload extends Phergie_Plugin_Abstract
37 * Checks for dependencies.
41 public function onLoad()
43 $this->getPluginHandler()->getPlugin('Command');
47 * Reloads a specified plugin.
49 * @param string $plugin Short name of the plugin to reload
53 public function onCommandReload($plugin)
55 $plugin = ucfirst($plugin);
57 if (!$this->plugins->hasPlugin($plugin)) {
58 echo 'DEBUG(Reload): ' . ucfirst($plugin) . ' is not loaded yet, loading', PHP_EOL;
59 $this->plugins->getPlugin($plugin);
60 $this->plugins->command->populateMethodCache();
65 $info = $this->plugins->getPluginInfo($plugin);
66 } catch (Phergie_Plugin_Exception $e) {
67 $source = $this->event->getSource();
68 $nick = $this->event->getNick();
69 $this->doNotice($source, $nick . ': ' . $e->getMessage());
73 $class = $info['class'];
74 $contents = file_get_contents($info['file']);
75 $newClass = $class . '_' . sha1($contents);
77 if (class_exists($newClass, false)) {
78 echo 'DEBUG(Reload): Class ', $class, ' has not changed since last reload', PHP_EOL;
82 $contents = preg_replace(
83 array('/^<\?(?:php)?/', '/class\s+' . $class . '/i'),
84 array('', 'class ' . $newClass),
89 $instance = new $newClass;
90 $instance->setName($plugin);
91 $instance->setEvent($this->event);
93 ->removePlugin($plugin)
94 ->addPlugin($instance);
96 $this->plugins->command->populateMethodCache();
97 if ($this->plugins->hasPlugin('Help')) {
98 $this->plugins->help->populateRegistry();
101 echo 'DEBUG(Reload): Reloaded ', $class, ' to ', $newClass, PHP_EOL;