]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Reload.php
MINOR: Please don't set a+x on files which are not being executed as shell script...
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Reload.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
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.
26  *
27  * @category Phergie
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
33  */
34 class Phergie_Plugin_Reload extends Phergie_Plugin_Abstract
35 {
36     /**
37      * Checks for dependencies.
38      *
39      * @return void
40      */
41     public function onLoad()
42     {
43         $this->getPluginHandler()->getPlugin('Command');
44     }
45
46     /**
47      * Reloads a specified plugin.
48      *
49      * @param string $plugin Short name of the plugin to reload
50      *
51      * @return void
52      */
53     public function onCommandReload($plugin)
54     {
55         $plugin = ucfirst($plugin);
56
57         $evalClass = true;
58         if (strpos($plugin, ' ') !== false) {
59             $args = explode(' ', $plugin);
60             $plugin = $args[0];
61             if (strtolower($args[1]) == 'force') {
62                 $evalClass = false;
63             }
64         }
65
66         if (!$this->plugins->hasPlugin($plugin)) {
67             echo 'DEBUG(Reload): ' . ucfirst($plugin) . ' is not loaded yet, loading', PHP_EOL;
68             try {
69                 $this->plugins->getPlugin($plugin);
70                 $this->plugins->command->populateMethodCache();
71             } catch (Phergie_Plugin_Exception $e) {
72                 if ($e->getCode() == Phergie_Plugin_Exception::ERR_CLASS_NOT_FOUND) {
73                     echo 'DEBUG(Reload): ', $e->getMessage(), PHP_EOL;
74                 } else {
75                     throw $e;
76                 }
77             }
78             return;
79         }
80
81         try {
82             $info = $this->plugins->getPluginInfo($plugin);
83         } catch (Phergie_Plugin_Exception $e) {
84             $source = $this->event->getSource();
85             $nick = $this->event->getNick();
86             $this->doNotice($source, $nick . ': ' . $e->getMessage());
87             return;
88         }
89
90         $class = $info['class'];
91         $contents = file_get_contents($info['file']);
92         $newClass = $class . '_' . sha1($contents);
93
94         if (class_exists($newClass, false)) {
95             if ($evalClass == true) {
96                 echo 'DEBUG(Reload): Class ', $class, ' has not changed since last reload', PHP_EOL;
97                 return;
98             }
99         } else {
100             $contents = preg_replace(
101                 array('/^<\?(?:php)?/', '/class\s+' . $class . '/i'),
102                 array('', 'class ' . $newClass),
103                 $contents
104             );
105             eval($contents);
106         }
107
108         $instance = new $newClass;
109         $instance->setName($plugin);
110         $instance->setEvent($this->event);
111         $this->plugins
112             ->removePlugin($plugin)
113             ->addPlugin($instance);
114
115         $this->plugins->command->populateMethodCache();
116         if ($this->plugins->hasPlugin('Help')) {
117             $this->plugins->help->populateRegistry();
118         }
119
120         echo 'DEBUG(Reload): Reloaded ', $class, ' to ', $newClass, PHP_EOL;
121     }
122 }