ProjectTask.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. /**
  3. * The Project Task handles creating the base application
  4. *
  5. *
  6. * PHP 5
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake.console.shells.tasks
  17. * @since CakePHP(tm) v 1.2
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::uses('File', 'Utility');
  21. App::uses('Folder', 'Utility');
  22. App::uses('String', 'Utility');
  23. App::uses('Security', 'Utility');
  24. /**
  25. * Task class for creating new project apps and plugins
  26. *
  27. * @package cake.console.shells.tasks
  28. */
  29. class ProjectTask extends Shell {
  30. /**
  31. * configs path (used in testing).
  32. *
  33. * @var string
  34. */
  35. public $configPath = null;
  36. /**
  37. * Checks that given project path does not already exist, and
  38. * finds the app directory in it. Then it calls bake() with that information.
  39. *
  40. * @param string $project Project path
  41. */
  42. public function execute() {
  43. $project = null;
  44. if (isset($this->args[0])) {
  45. $project = $this->args[0];
  46. }
  47. while (!$project) {
  48. $prompt = __d('cake_console', "What is the path to the project you want to bake?");
  49. $project = $this->in($prompt, null, APP_PATH . 'myapp');
  50. }
  51. if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
  52. $project = $_SERVER['PWD'] . DS . $project;
  53. }
  54. $response = false;
  55. while ($response == false && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
  56. $prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
  57. $response = $this->in($prompt, array('y','n'), 'n');
  58. if (strtolower($response) === 'n') {
  59. $response = $project = false;
  60. }
  61. }
  62. $success = true;
  63. if ($this->bake($project)) {
  64. $path = Folder::slashTerm($project);
  65. if ($this->createHome($path)) {
  66. $this->out(__d('cake_console', ' * Welcome page created'));
  67. } else {
  68. $this->err(__d('cake_console', 'The Welcome page was <error>NOT</error> created'));
  69. $success = false;
  70. }
  71. if ($this->securitySalt($path) === true) {
  72. $this->out(__d('cake_console', ' * Random hash key created for \'Security.salt\''));
  73. } else {
  74. $this->err(__d('cake_console', 'Unable to generate random hash for \'Security.salt\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
  75. $success = false;
  76. }
  77. if ($this->securityCipherSeed($path) === true) {
  78. $this->out(__d('cake_console', ' * Random seed created for \'Security.cipherSeed\''));
  79. } else {
  80. $this->err(__d('cake_console', 'Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
  81. $success = false;
  82. }
  83. if ($this->corePath($path) === true) {
  84. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
  85. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
  86. $this->out(__d('cake_console', ' * <warning>Remember to check these value after moving to production server</warning>'));
  87. } else {
  88. $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' .DS .'index.php'));
  89. $success = false;
  90. }
  91. if ($this->consolePath($path) === true) {
  92. $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
  93. } else {
  94. $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
  95. $success = false;
  96. }
  97. $Folder = new Folder($path);
  98. if (!$Folder->chmod($path . 'tmp', 0777)) {
  99. $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS .'tmp'));
  100. $this->out(__d('cake_console', 'chmod -R 0777 %s', $path . DS .'tmp'));
  101. $success = false;
  102. }
  103. if ($success) {
  104. $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
  105. } else {
  106. $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
  107. }
  108. return $path;
  109. }
  110. }
  111. /**
  112. * Looks for a skeleton template of a Cake application,
  113. * and if not found asks the user for a path. When there is a path
  114. * this method will make a deep copy of the skeleton to the project directory.
  115. * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
  116. *
  117. * @param string $path Project path
  118. * @param string $skel Path to copy from
  119. * @param string $skip array of directories to skip when copying
  120. * @access private
  121. */
  122. function bake($path, $skel = null, $skip = array('empty')) {
  123. if (!$skel && !empty($this->params['skel'])) {
  124. $skel = $this->params['skel'];
  125. }
  126. while (!$skel) {
  127. $skel = $this->in(
  128. __d('cake_console', "What is the path to the directory layout you wish to copy?"),
  129. null,
  130. CAKE . 'Console' . DS . 'templates' . DS . 'skel'
  131. );
  132. if (!$skel) {
  133. $this->err(__d('cake_console', 'The directory path you supplied was empty. Please try again.'));
  134. } else {
  135. while (is_dir($skel) === false) {
  136. $skel = $this->in(
  137. __d('cake_console', 'Directory path does not exist please choose another:'),
  138. null,
  139. CAKE . 'Console' . DS . 'templates' . DS . 'skel'
  140. );
  141. }
  142. }
  143. }
  144. $app = basename($path);
  145. $this->out(__d('cake_console', '<info>Skel Directory</info>: ') . $skel);
  146. $this->out(__d('cake_console', '<info>Will be copied to</info>: ') . $path);
  147. $this->hr();
  148. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  149. switch (strtolower($looksGood)) {
  150. case 'y':
  151. $Folder = new Folder($skel);
  152. if (!empty($this->params['empty'])) {
  153. $skip = array();
  154. }
  155. if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
  156. $this->hr();
  157. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $app, $path));
  158. $this->hr();
  159. } else {
  160. $this->err(__d('cake_console', "<error>Could not create</error> '%s' properly.", $app));
  161. return false;
  162. }
  163. foreach ($Folder->messages() as $message) {
  164. $this->out(String::wrap(' * ' . $message), 1, Shell::VERBOSE);
  165. }
  166. return true;
  167. case 'n':
  168. unset($this->args[0]);
  169. $this->execute();
  170. return false;
  171. case 'q':
  172. $this->out(__d('cake_console', '<error>Bake Aborted.</error>'));
  173. return false;
  174. }
  175. }
  176. /**
  177. * Writes a file with a default home page to the project.
  178. *
  179. * @param string $dir Path to project
  180. * @return boolean Success
  181. */
  182. public function createHome($dir) {
  183. $app = basename($dir);
  184. $path = $dir . 'View' . DS . 'Pages' . DS;
  185. $source = CAKE . 'Console' . DS . 'templates' . DS .'default' . DS . 'views' . DS . 'home.ctp';
  186. include($source);
  187. return $this->createFile($path.'home.ctp', $output);
  188. }
  189. /**
  190. * Generates the correct path to the CakePHP libs that are generating the project
  191. * and points app/console/cake.php to the right place
  192. *
  193. * @param string $path Project path.
  194. * @return boolean success
  195. */
  196. public function consolePath($path) {
  197. $File = new File($path . 'Console' . DS . 'cake.php');
  198. $contents = $File->read();
  199. if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
  200. $path = CAKE . 'Console' . DS;
  201. $replacement = "'" . str_replace(DS, "' . DIRECTORY_SEPARATOR . '", $path) . "'";
  202. $result = str_replace($match[0], $replacement, $contents);
  203. if ($File->write($result)) {
  204. return true;
  205. }
  206. return false;
  207. }
  208. return false;
  209. }
  210. /**
  211. * Generates and writes 'Security.salt'
  212. *
  213. * @param string $path Project path
  214. * @return boolean Success
  215. */
  216. public function securitySalt($path) {
  217. $File = new File($path . 'Config' . DS . 'core.php');
  218. $contents = $File->read();
  219. if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  220. $string = Security::generateAuthKey();
  221. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
  222. if ($File->write($result)) {
  223. return true;
  224. }
  225. return false;
  226. }
  227. return false;
  228. }
  229. /**
  230. * Generates and writes 'Security.cipherSeed'
  231. *
  232. * @param string $path Project path
  233. * @return boolean Success
  234. */
  235. public function securityCipherSeed($path) {
  236. $File = new File($path . 'Config' . DS . 'core.php');
  237. $contents = $File->read();
  238. if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  239. if (!class_exists('Security')) {
  240. require CAKE . 'Utility' . DS . 'security.php';
  241. }
  242. $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
  243. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents);
  244. if ($File->write($result)) {
  245. return true;
  246. }
  247. return false;
  248. }
  249. return false;
  250. }
  251. /**
  252. * Generates and writes CAKE_CORE_INCLUDE_PATH
  253. *
  254. * @param string $path Project path
  255. * @return boolean Success
  256. */
  257. public function corePath($path) {
  258. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  259. $File = new File($path . 'webroot' . DS . 'index.php');
  260. $contents = $File->read();
  261. if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9\.]*\);)/', $contents, $match)) {
  262. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  263. $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  264. if (!$File->write($result)) {
  265. return false;
  266. }
  267. } else {
  268. return false;
  269. }
  270. $File = new File($path . 'webroot' . DS . 'test.php');
  271. $contents = $File->read();
  272. if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  273. $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  274. if (!$File->write($result)) {
  275. return false;
  276. }
  277. } else {
  278. return false;
  279. }
  280. return true;
  281. }
  282. }
  283. /**
  284. * Enables Configure::read('Routing.prefixes') in /app/config/core.php
  285. *
  286. * @param string $name Name to use as admin routing
  287. * @return boolean Success
  288. */
  289. public function cakeAdmin($name) {
  290. $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
  291. $File = new File($path . 'core.php');
  292. $contents = $File->read();
  293. if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
  294. $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents);
  295. if ($File->write($result)) {
  296. Configure::write('Routing.prefixes', array($name));
  297. return true;
  298. } else {
  299. return false;
  300. }
  301. } else {
  302. return false;
  303. }
  304. }
  305. /**
  306. * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
  307. *
  308. * @return string Admin route to use
  309. */
  310. public function getPrefix() {
  311. $admin = '';
  312. $prefixes = Configure::read('Routing.prefixes');
  313. if (!empty($prefixes)) {
  314. if (count($prefixes) == 1) {
  315. return $prefixes[0] . '_';
  316. }
  317. if ($this->interactive) {
  318. $this->out();
  319. $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
  320. }
  321. $options = array();
  322. foreach ($prefixes as $i => $prefix) {
  323. $options[] = $i + 1;
  324. if ($this->interactive) {
  325. $this->out($i + 1 . '. ' . $prefix);
  326. }
  327. }
  328. $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
  329. return $prefixes[$selection - 1] . '_';
  330. }
  331. if ($this->interactive) {
  332. $this->hr();
  333. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.'));
  334. $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
  335. $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
  336. while ($admin == '') {
  337. $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
  338. }
  339. if ($this->cakeAdmin($admin) !== true) {
  340. $this->out(__d('cake_console', '<error>Unable to write to</error> /app/config/core.php.'));
  341. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.'));
  342. $this->_stop();
  343. }
  344. return $admin . '_';
  345. }
  346. return '';
  347. }
  348. /**
  349. * get the option parser.
  350. *
  351. * @return ConsoleOptionParser
  352. */
  353. public function getOptionParser() {
  354. $parser = parent::getOptionParser();
  355. return $parser->description(
  356. __d('cake_console', 'Generate a new CakePHP project skeleton.')
  357. )->addArgument('name', array(
  358. 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
  359. ))->addOption('empty', array(
  360. 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
  361. ))->addOption('skel', array(
  362. 'default' => current(App::core('Console')) . DS . 'templates' . DS . 'skel',
  363. 'help' => __d('cake_console', 'The directory layout to use for the new application skeleton. Defaults to cake/console/templates/skel of CakePHP used to create the project.')
  364. ));
  365. }
  366. }