PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/myth/CIModules/forge/controllers/Forge.php

https://gitlab.com/digitalpoetry/exceptionally-timed
PHP | 303 lines | 161 code | 65 blank | 77 comment | 29 complexity | 3615368edbbd3a1858578927e496d08c MD5 | raw file
  1. <?php
  2. /**
  3. * Sprint
  4. *
  5. * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * @package Sprint
  26. * @author Lonnie Ezell
  27. * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com)
  28. * @license http://opensource.org/licenses/MIT (MIT)
  29. * @link http://sprintphp.com
  30. * @since Version 1.0
  31. */
  32. use Myth\CLI as CLI;
  33. class Forge extends \Myth\Controllers\CLIController {
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. $this->load->config('forge');
  38. }
  39. //--------------------------------------------------------------------
  40. public function _remap($method, $params)
  41. {
  42. if (method_exists($this, $method))
  43. {
  44. call_user_func_array( [$this, $method], $params);
  45. }
  46. else
  47. {
  48. $params = array_merge([CLI::cli_string()], $params);
  49. call_user_func_array( [$this, 'run'], $params);
  50. }
  51. }
  52. //--------------------------------------------------------------------
  53. /**
  54. * Overrides to implement dynamic description building based on
  55. * scanning the collections and grabbing the information from
  56. * 'forge.php' files.
  57. */
  58. public function index()
  59. {
  60. $collections = config_item('forge.collections');
  61. if (! is_array($collections) || ! count($collections) )
  62. {
  63. return CLI::error('No generator collections found.');
  64. }
  65. // We loop through each collection scanning
  66. // for any generator folders that have a
  67. // 'forge.php' file. For each one found
  68. // we build out another section in our help commands
  69. foreach ($collections as $alias => $path)
  70. {
  71. $path = rtrim($path, '/ ') .'/';
  72. $folders = scandir($path);
  73. $_descriptions = [];
  74. foreach ($folders as $dir)
  75. {
  76. if ($dir == '.' || $dir == '..' || ! is_file($path . $dir .'/forge.php'))
  77. {
  78. continue;
  79. }
  80. include $path . $dir .'/forge.php';
  81. // Don't have valid arrays to work with? Move along...
  82. if (! isset($descriptions))
  83. {
  84. log_message('debug', '[Forge] Invalid forge.php file at: '. $path . $dir .'/forge.php');
  85. continue;
  86. }
  87. $_descriptions = array_merge($descriptions, $_descriptions);
  88. }
  89. ksort($_descriptions);
  90. CLI::new_line();
  91. CLI::write(ucwords( str_replace('_', ' ', $alias)) .' Collection');
  92. $this->sayDescriptions($_descriptions);
  93. }
  94. }
  95. //--------------------------------------------------------------------
  96. /**
  97. * The primary method that calls the correct generator and
  98. * makes it run.
  99. */
  100. public function run($command)
  101. {
  102. $quiet = false;
  103. $segments = explode(" ", $command);
  104. // Get rid of the 'forge' command
  105. if ($segments[0] == 'forge') {
  106. array_shift( $segments );
  107. }
  108. $command = trim(str_ireplace("forge", '', array_shift($segments)));
  109. $dir = $this->locateGenerator($command);
  110. $class_name = ucfirst($command) .'Generator';
  111. if (! file_exists($dir . $class_name .'.php'))
  112. {
  113. return CLI::error("Generator file not found for: {$class_name}");
  114. }
  115. require_once $dir . $class_name .'.php';
  116. if (! class_exists($class_name, false))
  117. {
  118. return CLI::error("No class `{$class_name}` found in generator file.");
  119. }
  120. // Should we run the process quietly?
  121. if ( (CLI::option('q') || CLI::option('quiet')))
  122. {
  123. $quiet = true;
  124. }
  125. CLI::write('Invoked '. CLI::color($class_name, 'yellow'));
  126. $class = new $class_name();
  127. $class->run( $segments, $quiet );
  128. }
  129. //--------------------------------------------------------------------
  130. /**
  131. * Displays the readme file for a generator if it exists.
  132. *
  133. * @param $command
  134. */
  135. public function readme($command)
  136. {
  137. $dir = $this->locateGenerator($command);
  138. if (! is_file($dir .'readme.txt'))
  139. {
  140. return CLI::error('Unable to locate the readme.txt file.');
  141. }
  142. $lines = file($dir .'readme.txt');
  143. if (! is_array($lines) || ! count($lines))
  144. {
  145. return CLI::error('The readme file does not have anything to display.');
  146. }
  147. $line_count = 0; // Total we're currently viewing.
  148. $max_rows = CLI::getHeight() -3;
  149. foreach ($lines as $line)
  150. {
  151. $line_count++;
  152. if ($line_count >= $max_rows)
  153. {
  154. CLI::prompt("\nContinue...");
  155. $line_count = 0;
  156. }
  157. echo CLI::wrap($line, 125);
  158. }
  159. CLI::new_line(2);
  160. }
  161. //--------------------------------------------------------------------
  162. /**
  163. * Overrides CLIController's version to support searching our
  164. * collections for the help description.
  165. *
  166. * @param null $method
  167. */
  168. public function longDescribeMethod($method=null)
  169. {
  170. $collections = config_item('forge.collections');
  171. if (! is_array($collections) || ! count($collections) )
  172. {
  173. return CLI::error('No generator collections found.');
  174. }
  175. // We loop through each collection scanning
  176. // for any generator folders that have a
  177. // 'forge.php' file. For each one found
  178. // we build out another section in our help commands
  179. foreach ($collections as $alias => $path)
  180. {
  181. $path = rtrim($path, '/ ') .'/';
  182. $folders = scandir($path);
  183. if (! $i = array_search(ucfirst($method), $folders))
  184. {
  185. continue;
  186. }
  187. $dir = $path . $folders[$i] .'/';
  188. if (! is_file($dir .'/forge.php'))
  189. {
  190. CLI::error("The {$method} command does not have any cli help available.");
  191. }
  192. include $dir .'/forge.php';
  193. // Don't have valid arrays to work with? Move along...
  194. if (! isset($long_description))
  195. {
  196. log_message('debug', '[Forge] Invalid forge.php file at: '. $dir .'/forge.php');
  197. continue;
  198. }
  199. if (empty($long_description))
  200. {
  201. return CLI::error("The {$method} command does not have an cli help available.");
  202. }
  203. CLI::new_line();
  204. CLI::write( CLI::color(ucfirst($method) .' Help', 'yellow') );
  205. return CLI::write( CLI::wrap($long_description, CLI::getWidth()) );
  206. }
  207. // Still here?
  208. CLI::error("No help found for command: {$method}");
  209. }
  210. //--------------------------------------------------------------------
  211. /**
  212. * Scans through the collections for the folder for this generator.
  213. *
  214. * @param $name
  215. *
  216. * @return null|string
  217. */
  218. protected function locateGenerator($name)
  219. {
  220. $collections = config_item('forge.collections');
  221. if (! is_array($collections) || ! count($collections) )
  222. {
  223. return CLI::error('No generator collections found.');
  224. }
  225. foreach ($collections as $alias => $path)
  226. {
  227. $path = rtrim($path, '/ ') .'/';
  228. $folders = scandir($path);
  229. if (! $i = array_search(ucfirst($name), $folders))
  230. {
  231. continue;
  232. }
  233. return $path . $folders[$i] .'/';
  234. }
  235. return null;
  236. }
  237. //--------------------------------------------------------------------
  238. }