|
| 1 | +<?php |
| 2 | +namespace Module\Module\Modules; |
| 3 | + |
| 4 | +use DateTime; |
| 5 | +use Skinny\Core\Configure; |
| 6 | +use Skinny\Module\ModuleInterface; |
| 7 | +use Skinny\Network\Wrapper; |
| 8 | +use Skinny\Utility\Command; |
| 9 | +use Skinny\Utility\Inflector; |
| 10 | + |
| 11 | +class Module implements ModuleInterface |
| 12 | +{ |
| 13 | + /** |
| 14 | + * {@inheritDoc} |
| 15 | + * |
| 16 | + * @param \Skinny\Network\Wrapper $wrapper The Wrapper instance. |
| 17 | + * @param array $message The message array. |
| 18 | + * |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function onChannelMessage(Wrapper $wrapper, $message) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * {@inheritDoc} |
| 27 | + * |
| 28 | + * @param \Skinny\Network\Wrapper $wrapper The Wrapper instance. |
| 29 | + * @param array $message The message array. |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function onPrivateMessage(Wrapper $wrapper, $message) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritDoc} |
| 39 | + * |
| 40 | + * @param \Skinny\Network\Wrapper $wrapper The Wrapper instance. |
| 41 | + * @param array $message The message array. |
| 42 | + * |
| 43 | + * @return void |
| 44 | + */ |
| 45 | + public function onCommandMessage(Wrapper $wrapper, $message) |
| 46 | + { |
| 47 | + if ($message['command'] !== 'module') { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (!isset($message['arguments'][1]) && $message['arguments'][0] !== 'loaded') { |
| 52 | + $wrapper->Message->reply(Command::syntax($message)); |
| 53 | + |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + switch ($message['arguments'][0]) { |
| 58 | + case 'load': |
| 59 | + //Load the Module. |
| 60 | + $module = $wrapper->ModuleManager->load($message['arguments'][1]); |
| 61 | + |
| 62 | + switch ($module) { |
| 63 | + //AlreadyLoaded |
| 64 | + case 'AL': |
| 65 | + $wrapper->Channel->sendMessage('The Module `' . $message['arguments'][1] . |
| 66 | + '` is already loaded.'); |
| 67 | + break; |
| 68 | + |
| 69 | + //Loaded |
| 70 | + case 'L': |
| 71 | + $wrapper->Channel->sendMessage('Module `' . $message['arguments'][1] . |
| 72 | + '` loaded successfully.'); |
| 73 | + break; |
| 74 | + |
| 75 | + //NotFound |
| 76 | + case 'NF': |
| 77 | + $wrapper->Channel->sendMessage('The Module `' . $message['arguments'][1] . '` was not found.'); |
| 78 | + break; |
| 79 | + } |
| 80 | + break; |
| 81 | + |
| 82 | + case 'unload': |
| 83 | + //Prevent for loading a file in the memory for nothing. |
| 84 | + if (Configure::read('debug') === false) { |
| 85 | + $wrapper->Channel->sendMessage('You can\'t unload a Module when the debug is `false`.'); |
| 86 | + break; |
| 87 | + } |
| 88 | + |
| 89 | + //Unload the Module. |
| 90 | + $module = $wrapper->ModuleManager->unload($message['arguments'][1]); |
| 91 | + |
| 92 | + //AlreadyUnloaded |
| 93 | + if ($module === 'AU') { |
| 94 | + $wrapper->Channel->sendMessage('The Module `' . $message['arguments'][1] . |
| 95 | + '` is already unloaded or doesn\'t exist.'); |
| 96 | + } else { |
| 97 | + $wrapper->Channel->sendMessage('Module `' . $message['arguments'][1] . '` unloaded successfully.'); |
| 98 | + } |
| 99 | + break; |
| 100 | + |
| 101 | + case 'reload': |
| 102 | + //Prevent for loading a file in the memory for nothing. |
| 103 | + if (Configure::read('debug') === false) { |
| 104 | + $wrapper->Channel->sendMessage('You can\'t reload a Module when the debug is `false`.'); |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + //Check if we must reload all Modules. |
| 109 | + if ($message['arguments'][1] == "all") { |
| 110 | + //Get the list of the loaded Modules. |
| 111 | + $loadedModules = $wrapper->ModuleManager->getLoadedModules(); |
| 112 | + |
| 113 | + //For each Modules, we reload it. |
| 114 | + foreach ($loadedModules as $module) { |
| 115 | + $this->reloadModule($wrapper, $module); |
| 116 | + |
| 117 | + //To avoid spam. |
| 118 | + usleep(500000); |
| 119 | + } |
| 120 | + |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + //Else there is just one Module to reload. |
| 125 | + $this->reloadModule($wrapper, $message['arguments'][1]); |
| 126 | + break; |
| 127 | + |
| 128 | + case 'time': |
| 129 | + //Get the UNIX time. |
| 130 | + $time = $wrapper->ModuleManager->timeLoaded($message['arguments'][1]); |
| 131 | + |
| 132 | + //If $time is false, that mean the Module is not loaded and/or doesn't exist. |
| 133 | + if ($time === false) { |
| 134 | + $wrapper->Channel->sendMessage('This Module is not loaded.'); |
| 135 | + break; |
| 136 | + } |
| 137 | + |
| 138 | + $seconds = floor(microtime(true) - $time); |
| 139 | + |
| 140 | + $start = new DateTime("@0"); |
| 141 | + $end = new DateTime("@$seconds"); |
| 142 | + |
| 143 | + $wrapper->Channel->sendMessage('The Module `' . Inflector::camelize($message['arguments'][1]) . |
| 144 | + '` is loaded since ' . $start->diff($end)->format('%a days, %h hours, %i minutes and %s seconds.')); |
| 145 | + break; |
| 146 | + |
| 147 | + case 'loaded': |
| 148 | + //Get the loaded Modules and implode the array as a string. |
| 149 | + $modules = $wrapper->ModuleManager->getLoadedModules(); |
| 150 | + $modules = implode("`, `", $modules); |
| 151 | + |
| 152 | + $wrapper->Channel->sendMessage('Modules loaded : `' . Inflector::camelize($modules) . '`.'); |
| 153 | + break; |
| 154 | + |
| 155 | + default: |
| 156 | + $wrapper->Channel->sendMessage(Command::unknown($message)); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Function to reload a Module and send the response. |
| 162 | + * |
| 163 | + * @param \Skinny\Network\Wrapper $wrapper The Wrapper instance. |
| 164 | + * @param string $module The module to reload. |
| 165 | + * |
| 166 | + * @return void |
| 167 | + */ |
| 168 | + protected function reloadModule(Wrapper $wrapper, $module) |
| 169 | + { |
| 170 | + $moduleStatus = $wrapper->ModuleManager->reload($module); |
| 171 | + |
| 172 | + $module = Inflector::camelize($module); |
| 173 | + |
| 174 | + switch ($moduleStatus) { |
| 175 | + //AlreadyUnloaded |
| 176 | + case 'AU': |
| 177 | + $wrapper->Channel->sendMessage('The Module `' . $module . '` doesn\'t exist and cannot be reloaded.'); |
| 178 | + break; |
| 179 | + |
| 180 | + //AlreadyLoaded |
| 181 | + case 'AL': |
| 182 | + $wrapper->Channel->sendMessage('The Module `' . $module . '` is already loaded.'); |
| 183 | + break; |
| 184 | + |
| 185 | + //Loaded |
| 186 | + case 'L': |
| 187 | + $wrapper->Channel->sendMessage('Module `' . $module . '` reloaded successfully.'); |
| 188 | + break; |
| 189 | + |
| 190 | + //NotFound |
| 191 | + case 'NF': |
| 192 | + $wrapper->Channel->sendMessage('Failed to reload the Module `' . $module . '`.'); |
| 193 | + break; |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments