PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Settings/Console/Command/SettingsShell.php

https://github.com/kareypowell/croogo
PHP | 229 lines | 175 code | 11 blank | 43 comment | 20 complexity | 4fbec9ed4a075693073a5e915822a21c MD5 | raw file
  1. <?php
  2. /**
  3. * Settings Shell
  4. *
  5. * Manipulates Settings via CLI
  6. * ./Console/croogo settings.settings read -a
  7. * ./Console/croogo settings.settings delete Some.key
  8. * ./Console/croogo settings.settings write Some.key newvalue
  9. * ./Console/croogo settings.settings write Some.key newvalue -create
  10. *
  11. * @category Shell
  12. * @package Croogo.Settings.Console.Command
  13. * @author Rachman Chavik <rchavik@xintesa.com>
  14. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  15. * @link http://www.croogo.org
  16. */
  17. class SettingsShell extends AppShell {
  18. /**
  19. * models
  20. */
  21. public $uses = array('Settings.Setting');
  22. /**
  23. * getOptionParser
  24. */
  25. public function getOptionParser() {
  26. return parent::getOptionParser()
  27. ->description('Croogo Settings utility')
  28. ->addSubCommand('read', array(
  29. 'help' => __d('croogo', 'Displays setting values'),
  30. 'parser' => array(
  31. 'arguments' => array(
  32. 'key' => array(
  33. 'help' => __d('croogo', 'Setting key'),
  34. 'required' => false,
  35. ),
  36. ),
  37. 'options' => array(
  38. 'all' => array(
  39. 'help' => __d('croogo', 'List all settings'),
  40. 'short' => 'a',
  41. 'boolean' => true,
  42. )
  43. ),
  44. ),
  45. ))
  46. ->addSubcommand('write', array(
  47. 'help' => __d('croogo', 'Write setting value for a given key'),
  48. 'parser' => array(
  49. 'arguments' => array(
  50. 'key' => array(
  51. 'help' => __d('croogo', 'Setting key'),
  52. 'required' => true,
  53. ),
  54. 'value' => array(
  55. 'help' => __d('croogo', 'Setting value'),
  56. 'required' => true,
  57. ),
  58. ),
  59. 'options' => array(
  60. 'create' => array(
  61. 'boolean' => true,
  62. 'short' => 'c',
  63. ),
  64. 'title' => array(
  65. 'short' => 't',
  66. ),
  67. 'description' => array(
  68. 'short' => 'd',
  69. ),
  70. 'input_type' => array(
  71. 'choices' => array('text', 'textarea', 'checkbox', 'multiple', 'radio'),
  72. 'short' => 'i',
  73. ),
  74. 'editable' => array(
  75. 'short' => 'e',
  76. 'boolean' => true,
  77. ),
  78. 'params' => array(
  79. 'short' => 'p',
  80. ),
  81. ),
  82. )
  83. ))
  84. ->addSubcommand('delete', array(
  85. 'help' => __d('croogo', 'Delete setting based on key'),
  86. 'parser' => array(
  87. 'arguments' => array(
  88. 'key' => array(
  89. 'help' => __d('croogo', 'Setting key'),
  90. 'required' => true,
  91. ),
  92. ),
  93. )
  94. ))
  95. ->addSubcommand('update_version_info', array(
  96. 'help' => __d('croogo', 'Update version string from git tag information'),
  97. ));
  98. }
  99. /**
  100. * Read setting
  101. *
  102. * @param string $key
  103. * @return void
  104. */
  105. public function read() {
  106. if (empty($this->args)) {
  107. if ($this->params['all'] === true) {
  108. $key = null;
  109. } else {
  110. $this->out($this->OptionParser->help('get'));
  111. return;
  112. }
  113. } else {
  114. $key = $this->args[0];
  115. }
  116. $settings = $this->Setting->find('all', array(
  117. 'conditions' => array(
  118. 'Setting.key like' => '%' . $key . '%',
  119. ),
  120. 'order' => 'Setting.weight asc',
  121. ));
  122. $this->out("Settings: ", 2);
  123. foreach ($settings as $data) {
  124. $this->out(__d('croogo', " %-30s: %s", $data['Setting']['key'], $data['Setting']['value']));
  125. }
  126. $this->out();
  127. }
  128. /**
  129. * Write setting
  130. *
  131. * @param string $key
  132. * @param string $val
  133. * @return void
  134. */
  135. public function write() {
  136. $key = $this->args[0];
  137. $val = $this->args[1];
  138. $setting = $this->Setting->find('first', array(
  139. 'fields' => array('id', 'key', 'value'),
  140. 'conditions' => array(
  141. 'Setting.key' => $key,
  142. ),
  143. ));
  144. $this->out(__d('croogo', 'Updating %s', $key), 2);
  145. $ask = __d('croogo', "Confirm update");
  146. if ($setting || $this->params['create']) {
  147. $text = '-';
  148. if ($setting) {
  149. $text = __d('croogo', '- %s', $setting['Setting']['value']);
  150. }
  151. $this->warn($text);
  152. $this->success(__d('croogo', '+ %s', $val));
  153. if ('y' == $this->in($ask, array('y', 'n'), 'n')) {
  154. $keys = array(
  155. 'title' => null, 'description' => null,
  156. 'input_type' => null, 'editable' => null, 'params' => null,
  157. );
  158. $options = array_intersect_key($this->params, $keys);
  159. $this->Setting->write($key, $val, $options);
  160. $this->success(__d('croogo', 'Setting updated'));
  161. } else {
  162. $this->warn(__d('croogo', 'Cancelled'));
  163. }
  164. } else {
  165. $this->warn(__d('croogo', 'Key: %s not found', $key));
  166. }
  167. }
  168. /**
  169. * Delete setting
  170. *
  171. * @param string $key
  172. * @return void
  173. */
  174. public function delete() {
  175. $key = $this->args[0];
  176. $setting = $this->Setting->find('first', array(
  177. 'fields' => array('id', 'key', 'value'),
  178. 'conditions' => array(
  179. 'Setting.key' => $key,
  180. ),
  181. ));
  182. $this->out(__d('croogo', 'Deleting %s', $key), 2);
  183. $ask = __d('croogo', 'Delete?');
  184. if ($setting) {
  185. if ('y' == $this->in($ask, array('y', 'n'), 'n')) {
  186. $this->Setting->deleteKey($setting['Setting']['key']);
  187. $this->success(__d('croogo', 'Setting deleted'));
  188. } else {
  189. $this->warn(__d('croogo', 'Cancelled'));
  190. }
  191. } else {
  192. $this->warn(__d('croogo', 'Key: %s not found', $key));
  193. }
  194. }
  195. /**
  196. * Update Croogo.version in settings.json
  197. */
  198. public function update_version_info() {
  199. $gitDir = realpath(CakePlugin::path('Croogo') . '..') . DS . '.git';
  200. if (!file_exists($gitDir)) {
  201. $this->err('Git repository not found');
  202. return false;
  203. }
  204. if (!is_dir($gitDir)) {
  205. $gitDir = dirname($gitDir);
  206. }
  207. $git = trim(shell_exec('which git'));
  208. if (empty($git)) {
  209. $this->err('Git executable not found');
  210. return false;
  211. }
  212. chdir($gitDir);
  213. $version = trim(shell_exec('git describe --tags'));
  214. if ($version) {
  215. $this->runCommand('write', array('write', 'Croogo.version', $version));
  216. }
  217. }
  218. }