ProjectTask.php 13 KB

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