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

/htdocs/solar/1.1.1/source/solar/Solar/Cli/MakeCli.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 364 lines | 153 code | 40 blank | 171 comment | 19 complexity | 98b6aa6d1ab0c2d244c05a4d022c54f5 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * Solar command to make a command-controller CLI structure.
  5. *
  6. * @category Solar
  7. *
  8. * @package Solar_Cli
  9. *
  10. * @author Paul M. Jones <pmjones@solarphp.com>
  11. *
  12. * @license http://opensource.org/licenses/bsd-license.php BSD
  13. *
  14. * @version $Id: MakeCli.php 4436 2010-02-25 21:38:34Z pmjones $
  15. *
  16. */
  17. class Solar_Cli_MakeCli extends Solar_Controller_Command
  18. {
  19. /**
  20. *
  21. * Default configuration values.
  22. *
  23. * @config string extends The default command-controller class to extend.
  24. *
  25. * @var array
  26. *
  27. */
  28. protected $_Solar_Cli_MakeCli = array(
  29. 'extends' => null,
  30. );
  31. /**
  32. *
  33. * The base directory where we will write the class file to, typically
  34. * the local PEAR directory.
  35. *
  36. * @var string
  37. *
  38. */
  39. protected $_target = null;
  40. /**
  41. *
  42. * The name of the CLI class.
  43. *
  44. * @var string
  45. *
  46. */
  47. protected $_class;
  48. /**
  49. *
  50. * The directory for the CLI class.
  51. *
  52. * @var string
  53. *
  54. */
  55. protected $_class_dir;
  56. /**
  57. *
  58. * The filename for the CLI class.
  59. *
  60. * @var string
  61. *
  62. */
  63. protected $_class_file;
  64. /**
  65. *
  66. * What base command to extend?
  67. *
  68. * @var string
  69. *
  70. */
  71. protected $_extends = null;
  72. /**
  73. *
  74. * Array of class templates (skeletons).
  75. *
  76. * @var array
  77. *
  78. */
  79. protected $_tpl = array();
  80. /**
  81. *
  82. * Write out a series of files and dirs for a page-controller.
  83. *
  84. * @param string $class The target class name for the app.
  85. *
  86. * @return void
  87. *
  88. */
  89. protected function _exec($class = null)
  90. {
  91. // we need a class name, at least
  92. if (! $class) {
  93. throw $this->_exception('ERR_NO_CLASS');
  94. } else {
  95. $this->_class = $class;
  96. }
  97. $this->_outln('Making CLI command.');
  98. // we need a target directory
  99. $this->_setTarget();
  100. // extending which class?
  101. $this->_setExtends($class);
  102. // load the templates
  103. $this->_loadTemplates();
  104. // the class file locations
  105. $this->_class_file = $this->_target
  106. . str_replace('_', DIRECTORY_SEPARATOR, $this->_class)
  107. . '.php';
  108. // the class dir location
  109. $this->_class_dir = Solar_Dir::fix(
  110. $this->_target . str_replace('_', '/', $this->_class)
  111. );
  112. // create the Locale and Info dirs
  113. $this->_createDirs();
  114. // write the CLI class itself
  115. $this->_writeCliClass();
  116. // write Locale/en_US.php
  117. $this->_writeLocale();
  118. // write Info/help.txt
  119. $this->_writeInfoHelp();
  120. // write Info/options.php
  121. $this->_writeInfoOptions();
  122. // done!
  123. $this->_outln("Done.");
  124. }
  125. /**
  126. *
  127. * Writes the application class file itself.
  128. *
  129. * @return void
  130. *
  131. */
  132. protected function _writeCliClass()
  133. {
  134. // emit feedback
  135. $this->_outln("CLI class '{$this->_class}' extends '{$this->_extends}'.");
  136. $this->_outln("Preparing to write to '{$this->_target}'.");
  137. // get the cli class template
  138. $tpl_key = 'cli';
  139. $text = $this->_parseTemplate($tpl_key);
  140. // write the cli class
  141. if (file_exists($this->_class_file)) {
  142. $this->_outln('CLI class already exists.');
  143. } else {
  144. $this->_outln('Writing CLI class.');
  145. file_put_contents($this->_class_file, $text);
  146. }
  147. }
  148. /**
  149. *
  150. * Creates the CLI directories.
  151. *
  152. * @return void
  153. *
  154. */
  155. protected function _createDirs()
  156. {
  157. $dir = $this->_class_dir;
  158. if (! file_exists($dir)) {
  159. $this->_outln('Creating CLI directory.');
  160. mkdir($dir, 0755, true);
  161. } else {
  162. $this->_outln('CLI directory exists.');
  163. }
  164. $list = array('Info', 'Locale');
  165. foreach ($list as $sub) {
  166. if (! file_exists("$dir/$sub")) {
  167. $this->_outln("Creating CLI $sub directory.");
  168. mkdir("$dir/$sub", 0755, true);
  169. } else {
  170. $this->_outln("CLI $sub directory exists.");
  171. }
  172. }
  173. }
  174. /**
  175. *
  176. * Writes the `Locale/en_US.php` locale file.
  177. *
  178. * @return void
  179. *
  180. */
  181. protected function _writeLocale()
  182. {
  183. $text = $this->_tpl['locale'];
  184. $file = $this->_class_dir . DIRECTORY_SEPARATOR . "/Locale/en_US.php";
  185. if (file_exists($file)) {
  186. $this->_outln('Locale file exists.');
  187. } else {
  188. $this->_outln('Writing locale file.');
  189. file_put_contents($file, $text);
  190. }
  191. }
  192. /**
  193. *
  194. * Writes the `Info/help.txt` file.
  195. *
  196. * @return void
  197. *
  198. */
  199. protected function _writeInfoHelp()
  200. {
  201. $text = $this->_tpl['help'];
  202. $file = $this->_class_dir . DIRECTORY_SEPARATOR . "/Info/help.txt";
  203. if (file_exists($file)) {
  204. $this->_outln('Help file exists.');
  205. } else {
  206. $this->_outln('Writing help file.');
  207. file_put_contents($file, $text);
  208. }
  209. }
  210. /**
  211. *
  212. * Writes the `Info/options.php` file.
  213. *
  214. * @return void
  215. *
  216. */
  217. protected function _writeInfoOptions()
  218. {
  219. $text = $this->_tpl['options'];
  220. $file = $this->_class_dir . DIRECTORY_SEPARATOR . "/Info/options.php";
  221. if (file_exists($file)) {
  222. $this->_outln('Options file exists.');
  223. } else {
  224. $this->_outln('Writing options file.');
  225. file_put_contents($file, $text);
  226. }
  227. }
  228. /**
  229. *
  230. * Parses a template and sets placeholder values.
  231. *
  232. * @param string $key The template array key.
  233. *
  234. * @return string The template with placeholder values set.
  235. *
  236. */
  237. protected function _parseTemplate($key)
  238. {
  239. $data = array(
  240. '{:class}' => $this->_class,
  241. '{:extends}' => $this->_extends,
  242. );
  243. return str_replace(
  244. array_keys($data),
  245. array_values($data),
  246. $this->_tpl[$key]
  247. );
  248. }
  249. /**
  250. *
  251. * Loads the template array from skeleton files.
  252. *
  253. * @return void
  254. *
  255. */
  256. protected function _loadTemplates()
  257. {
  258. $this->_tpl = array();
  259. $dir = Solar_Class::dir($this, 'Data');
  260. $list = glob($dir . '*.php');
  261. foreach ($list as $file) {
  262. // strip .php off the end of the file name to get the key
  263. $key = substr(basename($file), 0, -4);
  264. // load the file template
  265. $this->_tpl[$key] = file_get_contents($file);
  266. // we need to add the php-open tag ourselves, instead of
  267. // having it in the template file, becuase the PEAR packager
  268. // complains about parsing the skeleton code.
  269. //
  270. // however, only do this on non-help files.
  271. if ($key != 'help') {
  272. $this->_tpl[$key] = "<?php\n" . $this->_tpl[$key];
  273. }
  274. }
  275. }
  276. /**
  277. *
  278. * Sets the base directory target.
  279. *
  280. * @return void
  281. *
  282. */
  283. protected function _setTarget()
  284. {
  285. // use the solar system "include" directory.
  286. // that should automatically point to the right vendor for us.
  287. $target = Solar::$system . "/include";
  288. $this->_target = Solar_Dir::fix($target);
  289. }
  290. /**
  291. *
  292. * Sets the class this app will extend from.
  293. *
  294. * @param string $class The app class name.
  295. *
  296. * @return void
  297. *
  298. */
  299. protected function _setExtends($class)
  300. {
  301. // explicit as cli option?
  302. $extends = $this->_options['extends'];
  303. if ($extends) {
  304. $this->_extends = $extends;
  305. return;
  306. }
  307. // explicit as a config value?
  308. $extends = $this->_config['extends'];
  309. if ($extends) {
  310. $this->_extends = $extends;
  311. return;
  312. }
  313. // look at the vendor name and find a controller class
  314. $vendor = Solar_Class::vendor($class);
  315. $name = "{$vendor}_Controller_Command";
  316. $file = $this->_target . "$vendor/Controller/Command.php";
  317. if (file_exists($file)) {
  318. $this->_extends = $name;
  319. return;
  320. }
  321. // final fallback: Solar_Controller_Command
  322. $this->_extends = 'Solar_Controller_Command';
  323. }
  324. }