ProjectTask.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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-2011, 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-2011, 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->consolePath($path) === true) {
  84. $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
  85. } else {
  86. $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
  87. $success = false;
  88. }
  89. $this->out(__d('cake_console', 'The value for CAKE_CORE_INCLUDE_PATH can be hardcoded set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
  90. $this->out(__d('cake_console', '<warning>If you hard code it, the project will possibly run only in your computer.</warning>'));
  91. $setConstants = $this->in(__d('cake_console', 'Do you want to set CAKE_CORE_INCLUDE_PATH in webroot/index.php?'), array('y', 'n'), 'n');
  92. if (strtolower($setConstants) === 'y') {
  93. if ($this->corePath($path) === true) {
  94. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
  95. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
  96. $this->out(__d('cake_console', ' * <warning>Remember to check these values after moving to production server</warning>'));
  97. } else {
  98. $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' .DS .'index.php'));
  99. $success = false;
  100. }
  101. } else {
  102. $this->out(__d('cake_console', '<warning>Please make sure your cake core is accessible, if you have problems edit CAKE_CORE_INCLUDE_PATH in webroot/index.php</warning>'));
  103. }
  104. $Folder = new Folder($path);
  105. if (!$Folder->chmod($path . 'tmp', 0777)) {
  106. $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS .'tmp'));
  107. $this->out(__d('cake_console', 'chmod -R 0777 %s', $path . DS .'tmp'));
  108. $success = false;
  109. }
  110. if ($success) {
  111. $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
  112. } else {
  113. $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
  114. }
  115. return $path;
  116. }
  117. }
  118. /**
  119. * Looks for a skeleton template of a Cake application,
  120. * and if not found asks the user for a path. When there is a path
  121. * this method will make a deep copy of the skeleton to the project directory.
  122. * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
  123. *
  124. * @param string $path Project path
  125. * @param string $skel Path to copy from
  126. * @param string $skip array of directories to skip when copying
  127. * @access private
  128. */
  129. public function bake($path, $skel = null, $skip = array('empty')) {
  130. if (!$skel && !empty($this->params['skel'])) {
  131. $skel = $this->params['skel'];
  132. }
  133. while (!$skel) {
  134. $skel = $this->in(
  135. __d('cake_console', "What is the path to the directory layout you wish to copy?"),
  136. null,
  137. CAKE . 'Console' . DS . 'Templates' . DS . 'skel'
  138. );
  139. if (!$skel) {
  140. $this->err(__d('cake_console', 'The directory path you supplied was empty. Please try again.'));
  141. } else {
  142. while (is_dir($skel) === false) {
  143. $skel = $this->in(
  144. __d('cake_console', 'Directory path does not exist please choose another:'),
  145. null,
  146. CAKE . 'Console' . DS . 'Templates' . DS . 'skel'
  147. );
  148. }
  149. }
  150. }
  151. $app = basename($path);
  152. $this->out(__d('cake_console', '<info>Skel Directory</info>: ') . $skel);
  153. $this->out(__d('cake_console', '<info>Will be copied to</info>: ') . $path);
  154. $this->hr();
  155. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  156. switch (strtolower($looksGood)) {
  157. case 'y':
  158. $Folder = new Folder($skel);
  159. if (!empty($this->params['empty'])) {
  160. $skip = array();
  161. }
  162. if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
  163. $this->hr();
  164. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $app, $path));
  165. $this->hr();
  166. } else {
  167. $this->err(__d('cake_console', "<error>Could not create</error> '%s' properly.", $app));
  168. return false;
  169. }
  170. foreach ($Folder->messages() as $message) {
  171. $this->out(String::wrap(' * ' . $message), 1, Shell::VERBOSE);
  172. }
  173. return true;
  174. case 'n':
  175. unset($this->args[0]);
  176. $this->execute();
  177. return false;
  178. case 'q':
  179. $this->out(__d('cake_console', '<error>Bake Aborted.</error>'));
  180. return false;
  181. }
  182. }
  183. /**
  184. * Writes a file with a default home page to the project.
  185. *
  186. * @param string $dir Path to project
  187. * @return boolean Success
  188. */
  189. public function createHome($dir) {
  190. $app = basename($dir);
  191. $path = $dir . 'View' . DS . 'Pages' . DS;
  192. $source = CAKE . 'Console' . DS . 'Templates' . DS .'default' . DS . 'views' . DS . 'home.ctp';
  193. include($source);
  194. return $this->createFile($path.'home.ctp', $output);
  195. }
  196. /**
  197. * Generates the correct path to the CakePHP libs that are generating the project
  198. * and points app/console/cake.php to the right place
  199. *
  200. * @param string $path Project path.
  201. * @return boolean success
  202. */
  203. public function consolePath($path) {
  204. $File = new File($path . 'Console' . DS . 'cake.php');
  205. $contents = $File->read();
  206. if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
  207. $path = CAKE . 'Console' . DS;
  208. $replacement = "'" . str_replace(DS, "' . DIRECTORY_SEPARATOR . '", $path) . "'";
  209. $result = str_replace($match[0], $replacement, $contents);
  210. if ($File->write($result)) {
  211. return true;
  212. }
  213. return false;
  214. }
  215. return false;
  216. }
  217. /**
  218. * Generates and writes 'Security.salt'
  219. *
  220. * @param string $path Project path
  221. * @return boolean Success
  222. */
  223. public function securitySalt($path) {
  224. $File = new File($path . 'Config' . DS . 'core.php');
  225. $contents = $File->read();
  226. if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  227. $string = Security::generateAuthKey();
  228. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
  229. if ($File->write($result)) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. return false;
  235. }
  236. /**
  237. * Generates and writes 'Security.cipherSeed'
  238. *
  239. * @param string $path Project path
  240. * @return boolean Success
  241. */
  242. public function securityCipherSeed($path) {
  243. $File = new File($path . 'Config' . DS . 'core.php');
  244. $contents = $File->read();
  245. if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  246. if (!class_exists('Security')) {
  247. require CAKE . 'Utility' . DS . 'security.php';
  248. }
  249. $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
  250. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents);
  251. if ($File->write($result)) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. return false;
  257. }
  258. /**
  259. * Generates and writes CAKE_CORE_INCLUDE_PATH
  260. *
  261. * @param string $path Project path
  262. * @return boolean Success
  263. */
  264. public function corePath($path) {
  265. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  266. $File = new File($path . 'webroot' . DS . 'index.php');
  267. $contents = $File->read();
  268. if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9\.]*\);)/', $contents, $match)) {
  269. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  270. $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  271. if (!$File->write($result)) {
  272. return false;
  273. }
  274. } else {
  275. return false;
  276. }
  277. $File = new File($path . 'webroot' . DS . 'test.php');
  278. $contents = $File->read();
  279. if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9\.]*\);)/', $contents, $match)) {
  280. $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  281. if (!$File->write($result)) {
  282. return false;
  283. }
  284. } else {
  285. return false;
  286. }
  287. return true;
  288. }
  289. }
  290. /**
  291. * Enables Configure::read('Routing.prefixes') in /app/Config/core.php
  292. *
  293. * @param string $name Name to use as admin routing
  294. * @return boolean Success
  295. */
  296. public function cakeAdmin($name) {
  297. $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
  298. $File = new File($path . 'core.php');
  299. $contents = $File->read();
  300. if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
  301. $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents);
  302. if ($File->write($result)) {
  303. Configure::write('Routing.prefixes', array($name));
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. } else {
  309. return false;
  310. }
  311. }
  312. /**
  313. * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
  314. *
  315. * @return string Admin route to use
  316. */
  317. public function getPrefix() {
  318. $admin = '';
  319. $prefixes = Configure::read('Routing.prefixes');
  320. if (!empty($prefixes)) {
  321. if (count($prefixes) == 1) {
  322. return $prefixes[0] . '_';
  323. }
  324. if ($this->interactive) {
  325. $this->out();
  326. $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
  327. }
  328. $options = array();
  329. foreach ($prefixes as $i => $prefix) {
  330. $options[] = $i + 1;
  331. if ($this->interactive) {
  332. $this->out($i + 1 . '. ' . $prefix);
  333. }
  334. }
  335. $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
  336. return $prefixes[$selection - 1] . '_';
  337. }
  338. if ($this->interactive) {
  339. $this->hr();
  340. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  341. $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
  342. $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
  343. while ($admin == '') {
  344. $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
  345. }
  346. if ($this->cakeAdmin($admin) !== true) {
  347. $this->out(__d('cake_console', '<error>Unable to write to</error> /app/Config/core.php.'));
  348. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  349. $this->_stop();
  350. }
  351. return $admin . '_';
  352. }
  353. return '';
  354. }
  355. /**
  356. * get the option parser.
  357. *
  358. * @return ConsoleOptionParser
  359. */
  360. public function getOptionParser() {
  361. $parser = parent::getOptionParser();
  362. return $parser->description(
  363. __d('cake_console', 'Generate a new CakePHP project skeleton.')
  364. )->addArgument('name', array(
  365. 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
  366. ))->addOption('empty', array(
  367. 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
  368. ))->addOption('skel', array(
  369. 'default' => current(App::core('Console')) . 'Templates' . DS . 'skel',
  370. '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.')
  371. ));
  372. }
  373. }