PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/Console/Command/ExtShell.php

https://github.com/kareypowell/croogo
PHP | 276 lines | 185 code | 15 blank | 76 comment | 41 complexity | 96c9125f710ab560674687520a75c9fa MD5 | raw file
  1. <?php
  2. App::uses('AppShell', 'Console/Command');
  3. App::uses('CakeRequest', 'Network');
  4. App::uses('CakeResponse', 'Network');
  5. App::uses('Controller', 'Controller');
  6. App::uses('AppController', 'Controller');
  7. App::uses('CroogoPlugin', 'Extensions.Lib');
  8. App::uses('CroogoTheme', 'Extensions.Lib');
  9. /**
  10. * Ext Shell
  11. *
  12. * Activate/Deactivate Plugins/Themes
  13. * ./Console/croogo ext activate plugin Example
  14. * ./Console/croogo ext activate theme minimal
  15. * ./Console/croogo ext deactivate plugin Example
  16. * ./Console/croogo ext deactivate theme
  17. *
  18. * @category Shell
  19. * @package Croogo.Croogo.Console.Command
  20. * @version 1.0
  21. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  22. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  23. * @link http://www.croogo.org
  24. */
  25. class ExtShell extends AppShell {
  26. /**
  27. * Models we use
  28. *
  29. * @var array
  30. */
  31. public $uses = array('Settings.Setting');
  32. /**
  33. * CroogoPlugin class
  34. *
  35. * @var CroogoPlugin
  36. */
  37. protected $_CroogoPlugin = null;
  38. /**
  39. * CroogoTheme class
  40. *
  41. * @var CroogoTheme
  42. */
  43. protected $_CroogoTheme = null;
  44. /**
  45. * Controller
  46. *
  47. * @var Controller
  48. * @todo Remove this when PluginActivation dont need controllers
  49. */
  50. protected $_Controller = null;
  51. /**
  52. * Initialize
  53. *
  54. * @param type $stdout
  55. * @param type $stderr
  56. * @param type $stdin
  57. */
  58. public function __construct($stdout = null, $stderr = null, $stdin = null) {
  59. parent::__construct($stdout, $stderr, $stdin);
  60. $this->_CroogoPlugin = new CroogoPlugin();
  61. $this->_CroogoTheme = new CroogoTheme();
  62. $CakeRequest = new CakeRequest();
  63. $CakeResponse = new CakeResponse();
  64. $this->_Controller = new AppController($CakeRequest, $CakeResponse);
  65. $this->_Controller->constructClasses();
  66. $this->_Controller->startupProcess();
  67. $this->_CroogoPlugin->setController($this->_Controller);
  68. $this->initialize();
  69. }
  70. /**
  71. * Call the appropriate command
  72. *
  73. * @return void
  74. */
  75. public function main() {
  76. $args = $this->args;
  77. $this->args = array_map('strtolower', $this->args);
  78. $method = $this->args[0];
  79. $type = $this->args[1];
  80. $ext = isset($args[2]) ? $args[2] : null;
  81. $force = isset($this->params['force']) ? $this->params['force'] : false;
  82. if ($type == 'theme') {
  83. $extensions = $this->_CroogoTheme->getThemes();
  84. $theme = Configure::read('Site.theme');
  85. $active = !empty($theme) ? $theme == 'default' : true;
  86. } elseif ($type == 'plugin') {
  87. $extensions = $this->_CroogoPlugin->getPlugins();
  88. if ($force) {
  89. $plugins = array_combine($p = App::objects('plugins'), $p);
  90. $extensions += $plugins;
  91. }
  92. $active = CakePlugin::loaded($ext);
  93. }
  94. if ($type == 'theme' && $method == 'deactivate') {
  95. $this->err(__d('croogo', 'Theme cannot be deactivated, instead activate another theme.'));
  96. return false;
  97. }
  98. if (!empty($ext) && !in_array($ext, $extensions) && !$active && !$force) {
  99. $this->err(__d('croogo', '%s "%s" not found.', ucfirst($type), $ext));
  100. return false;
  101. }
  102. switch ($method) {
  103. case 'list':
  104. $call = Inflector::pluralize($type);
  105. return $this->{$call}($ext);
  106. default:
  107. if (empty($ext)) {
  108. $this->err(__d('croogo', '%s name must be provided.', ucfirst($type)));
  109. return false;
  110. }
  111. return $this->{'_' . $method . ucfirst($type)}($ext);
  112. }
  113. }
  114. /**
  115. * Display help/options
  116. */
  117. public function getOptionParser() {
  118. return parent::getOptionParser()
  119. ->description(__d('croogo', 'Activate Plugins & Themes'))
  120. ->addArguments(array(
  121. 'method' => array(
  122. 'help' => __d('croogo', 'Method to perform'),
  123. 'required' => true,
  124. 'choices' => array('list', 'activate', 'deactivate'),
  125. ),
  126. 'type' => array(
  127. 'help' => __d('croogo', 'Extension type'),
  128. 'required' => true,
  129. 'choices' => array('plugin', 'theme'),
  130. ),
  131. 'extension' => array(
  132. 'help' => __d('croogo', 'Name of extension'),
  133. ),
  134. ))
  135. ->addOption('all', array(
  136. 'short' => 'a',
  137. 'boolean' => true,
  138. 'help' => 'List all extensions',
  139. ))
  140. ->addOption('force', array(
  141. 'short' => 'f',
  142. 'boolean' => true,
  143. 'help' => 'Force method operation even when plugin does not provide a `plugin.json` file.'
  144. ));
  145. }
  146. /**
  147. * Activate a plugin
  148. *
  149. * @param string $plugin
  150. * @return boolean
  151. */
  152. protected function _activatePlugin($plugin) {
  153. $result = $this->_CroogoPlugin->activate($plugin);
  154. if ($result === true) {
  155. $this->out(__d('croogo', 'Plugin "%s" activated successfully.', $plugin));
  156. return true;
  157. } elseif (is_string($result)) {
  158. $this->err($result);
  159. } else {
  160. $this->err(__d('croogo', 'Plugin "%s" could not be activated. Please, try again.', $plugin));
  161. }
  162. return false;
  163. }
  164. /**
  165. * Deactivate a plugin
  166. *
  167. * @param string $plugin
  168. * @return boolean
  169. */
  170. protected function _deactivatePlugin($plugin) {
  171. $usedBy = $this->_CroogoPlugin->usedBy($plugin);
  172. if ($usedBy === false) {
  173. $result = $this->_CroogoPlugin->deactivate($plugin);
  174. if ($result === false) {
  175. $this->err(__d('croogo', 'Plugin "%s" could not be deactivated. Please, try again.', $plugin));
  176. } elseif (is_string($result)) {
  177. $this->err($result);
  178. }
  179. } else {
  180. $result = false;
  181. if ($usedBy !== false) {
  182. if ($this->params['force'] === false) {
  183. $this->err(__d('croogo', 'Plugin "%s" could not be deactivated since "%s" depends on it.', $plugin, implode(', ', $usedBy)));
  184. } else {
  185. $result = true;
  186. }
  187. }
  188. }
  189. if ($this->params['force'] === true || $result === true) {
  190. $this->_CroogoPlugin->removeBootstrap($plugin);
  191. $result = true;
  192. }
  193. if ($result === true) {
  194. $this->out(__d('croogo', 'Plugin "%s" deactivated successfully.', $plugin));
  195. return true;
  196. }
  197. return false;
  198. }
  199. /**
  200. * Activate a theme
  201. *
  202. * @param string $theme Name of theme
  203. * @return boolean
  204. */
  205. protected function _activateTheme($theme) {
  206. if ($this->_CroogoTheme->activate($theme)) {
  207. $this->out(__d('croogo', 'Theme "%s" activated successfully.', $theme));
  208. } else {
  209. $this->err(__d('croogo', 'Theme "%s" activation failed.', $theme));
  210. }
  211. return true;
  212. }
  213. /**
  214. * List plugins
  215. */
  216. public function plugins($plugin = null) {
  217. App::uses('CroogoPlugin', 'Extensions.Lib');
  218. $all = $this->params['all'];
  219. $plugins = $plugin == null ? App::objects('plugins') : array($plugin);
  220. $loaded = CakePlugin::loaded();
  221. $CroogoPlugin = new CroogoPlugin();
  222. $this->out(__d('croogo', 'Plugins:'), 2);
  223. $this->out(__d('croogo', '%-20s%-50s%s', __d('croogo', 'Plugin'), __d('croogo', 'Author'), __d('croogo', 'Status')));
  224. $this->out(str_repeat('-', 80));
  225. foreach ($plugins as $plugin) {
  226. $status = '<info>inactive</info>';
  227. if ($active = in_array($plugin, $loaded)) {
  228. $status = '<success>active</success>';
  229. }
  230. if (!$active && !$all) {
  231. continue;
  232. }
  233. $data = $CroogoPlugin->getPluginData($plugin);
  234. $author = isset($data['author']) ? $data['author'] : '';
  235. $this->out(__d('croogo', '%-20s%-50s%s', $plugin, $author, $status));
  236. }
  237. }
  238. /**
  239. * List themes
  240. */
  241. public function themes($theme = null) {
  242. $CroogoTheme = new CroogoTheme();
  243. $all = $this->params['all'];
  244. $current = Configure::read('Site.theme');
  245. $themes = $theme == null ? $CroogoTheme->getThemes() : array($theme);
  246. $this->out("Themes:", 2);
  247. $default = empty($current) || $current == 'default';
  248. $this->out(__d('croogo', '%-20s%-50s%s', __d('croogo', 'Theme'), __d('croogo', 'Author'), __d('croogo', 'Status')));
  249. $this->out(str_repeat('-', 80));
  250. foreach ($themes as $theme) {
  251. $active = $theme == $current || $default && $theme == 'default';
  252. $status = $active ? '<success>active</success>' : '<info>inactive</info>';
  253. if (!$active && !$all) {
  254. continue;
  255. }
  256. $data = $CroogoTheme->getThemeData($theme);
  257. $author = isset($data['author']) ? $data['author'] : '';
  258. $this->out(__d('croogo', '%-20s%-50s%s', $theme, $author, $status));
  259. }
  260. }
  261. }