PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/pmjones/php-framework-benchmarks
PHP | 229 lines | 125 code | 20 blank | 84 comment | 12 complexity | 9a17d41b278344c4d36976b54e291d39 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 Vendor directory set with symlinks to the right
  5. * places.
  6. *
  7. * @category Solar
  8. *
  9. * @package Solar_Cli
  10. *
  11. * @author Paul M. Jones <pmjones@solarphp.com>
  12. *
  13. * @license http://opensource.org/licenses/bsd-license.php BSD
  14. *
  15. * @version $Id: MakeVendor.php 4490 2010-03-02 15:51:00Z pmjones $
  16. *
  17. * @todo Make Vendor_App_Hello, Vendor_Cli_Help. Also make Vendor_App_Base
  18. * and Vendor_Cli_Base?
  19. *
  20. */
  21. class Solar_Cli_MakeVendor extends Solar_Controller_Command
  22. {
  23. /**
  24. *
  25. * The "StudlyCaps" version of the vendor name.
  26. *
  27. * @var string
  28. *
  29. */
  30. protected $_studly = null;
  31. /**
  32. *
  33. * The "lowercase-dashes" version of the vendor name.
  34. *
  35. * @var string
  36. *
  37. */
  38. protected $_dashes = null;
  39. /**
  40. *
  41. * The various "source/" dirs to create.
  42. *
  43. * @var array
  44. *
  45. */
  46. protected $_dirs = array(
  47. '/{:dashes}/config',
  48. '/{:dashes}/docs',
  49. '/{:dashes}/script',
  50. '/{:dashes}/tests',
  51. '/{:dashes}/tests/Fixture',
  52. '/{:dashes}/tests/Fixture/{:studly}',
  53. '/{:dashes}/tests/Mock',
  54. '/{:dashes}/tests/Mock/{:studly}',
  55. '/{:dashes}/tests/Test',
  56. '/{:dashes}/tests/Test/{:studly}',
  57. '/{:dashes}/{:studly}/Cli/Help',
  58. '/{:dashes}/{:studly}/Cli/Help/Info',
  59. '/{:dashes}/{:studly}/Cli/Help/Locale',
  60. '/{:dashes}/{:studly}/Controller/Bread/Locale',
  61. '/{:dashes}/{:studly}/Controller/Bread/Public',
  62. '/{:dashes}/{:studly}/Controller/Bread/View',
  63. '/{:dashes}/{:studly}/Controller/Command/Info',
  64. '/{:dashes}/{:studly}/Controller/Command/Locale',
  65. '/{:dashes}/{:studly}/Controller/Page/Layout',
  66. '/{:dashes}/{:studly}/Controller/Page/Locale',
  67. '/{:dashes}/{:studly}/Controller/Page/Public',
  68. '/{:dashes}/{:studly}/Controller/Page/View',
  69. '/{:dashes}/{:studly}/Model',
  70. );
  71. /**
  72. *
  73. * The registered Solar_Inflect instance.
  74. *
  75. * @var Solar_Inflect
  76. *
  77. */
  78. protected $_inflect;
  79. /**
  80. *
  81. * Write out a series of dirs and symlinks for a new Vendor source.
  82. *
  83. * @param string $vendor The Vendor name.
  84. *
  85. * @return void
  86. *
  87. */
  88. protected function _exec($vendor = null)
  89. {
  90. // we need a vendor name, at least
  91. if (! $vendor) {
  92. throw $this->_exception('ERR_NO_VENDOR');
  93. }
  94. // build "foo-bar" and "FooBar" versions of the vendor name.
  95. $this->_inflect = Solar_Registry::get('inflect');
  96. $this->_dashes = $this->_inflect->camelToDashes($vendor);
  97. $this->_studly = $this->_inflect->dashesToStudly($this->_dashes);
  98. // create dirs, files, and symlinks
  99. $this->_createDirs();
  100. $this->_createFiles();
  101. $this->_createLinks();
  102. }
  103. /**
  104. *
  105. * Creates the "source/" directories for the vendor.
  106. *
  107. * @return void
  108. *
  109. */
  110. protected function _createDirs()
  111. {
  112. $this->_outln('Making vendor source directories.');
  113. $system = Solar::$system;
  114. foreach ($this->_dirs as $dir) {
  115. $dir = "$system/source" . str_replace(
  116. array('{:dashes}', '{:studly}'),
  117. array($this->_dashes, $this->_studly),
  118. $dir
  119. );
  120. if (is_dir($dir)) {
  121. $this->_outln("Directory $dir exists.");
  122. } else {
  123. $this->_outln("Creating $dir.");
  124. mkdir($dir, 0755, true);
  125. }
  126. }
  127. }
  128. /**
  129. *
  130. * Creates the various symlinks for the vendor directories.
  131. *
  132. * @return void
  133. *
  134. */
  135. protected function _createLinks()
  136. {
  137. $link_vendor = Solar::factory('Solar_Cli_LinkVendor');
  138. $link_vendor->exec($this->_studly);
  139. }
  140. /**
  141. *
  142. * Creates the baseline PHP files in the Vendor directories from the
  143. * skeleton files in `Data/*.txt`.
  144. *
  145. * @return void
  146. *
  147. */
  148. protected function _createFiles()
  149. {
  150. $system = Solar::$system;
  151. $data_dir = Solar_Class::dir($this, 'Data');
  152. $list = glob($data_dir . "*.txt");
  153. foreach ($list as $data_file) {
  154. $file = substr($data_file, strlen($data_dir));
  155. $file = str_replace('.txt', '.php', $file);
  156. $file = str_replace('_', '/', $file);
  157. $file = str_replace('-', '_', $file);
  158. $file = "$system/source/{$this->_dashes}/{$this->_studly}/$file";
  159. if (file_exists($file)) {
  160. $this->_outln("File $file exists.");
  161. continue;
  162. }
  163. $dirname = dirname($file);
  164. if (! is_dir($dirname)) {
  165. $this->_out("Making directory $dirname ... ");
  166. mkdir($dirname, 0755, true);
  167. $this->_outln("done.");
  168. }
  169. $text = file_get_contents($data_file);
  170. $text = str_replace('{:php}', '<?php', $text);
  171. $text = str_replace('{:vendor}', $this->_studly, $text);
  172. $this->_out("Writing $file ... ");
  173. file_put_contents($file, $text);
  174. $this->_outln("done.");
  175. }
  176. // write a "config/default.php" file (empty)
  177. $file = "$system/source/{$this->_dashes}/config/default.php";
  178. $text = "<?php // placeholder config file for {$this->_studly} classes\n";
  179. if (file_exists($file)) {
  180. $this->_outln("File $file exists.");
  181. } else {
  182. $this->_out("Writing $file ... ");
  183. file_put_contents($file, $text);
  184. $this->_outln("done.");
  185. }
  186. // write a "config/run-tests.php" file
  187. $file = "$system/source/{$this->_dashes}/config/run-tests.php";
  188. $text = "<?php\n\$config = array();\nreturn \$config;\n";
  189. if (file_exists($file)) {
  190. $this->_outln("File $file exists.");
  191. } else {
  192. $this->_out("Writing $file ... ");
  193. file_put_contents($file, $text);
  194. $this->_outln("done.");
  195. }
  196. // write a "vendor/Vendor/Cli/Help/Info/help.txt" file
  197. $file = "$system/source/{$this->_dashes}/{$this->_studly}/Cli/Help/Info/help.txt";
  198. $text = "{$this->_studly} command line tool." . PHP_EOL . PHP_EOL
  199. . "Usage: %*{$this->_dashes} <command> <options> <params>%n" . PHP_EOL . PHP_EOL
  200. . "Try '{$this->_dashes} help <command>' for help on a specific command." . PHP_EOL;
  201. if (file_exists($file)) {
  202. $this->_outln("File $file exists.");
  203. } else {
  204. $this->_out("Writing $file ... ");
  205. file_put_contents($file, $text);
  206. $this->_outln("done.");
  207. }
  208. }
  209. }