ProjectTask.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. if ($project && isset($_SERVER['PWD'])) {
  48. $project = $_SERVER['PWD'] . DS . $project;
  49. }
  50. while (!$project) {
  51. $prompt = __d('cake_console', "What is the path to the project you want to bake?");
  52. $project = $this->in($prompt, null, APP_PATH . 'myapp');
  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 && !empty($this->params['skel'])) {
  126. $skel = $this->params['skel'];
  127. }
  128. while (!$skel) {
  129. $skel = $this->in(
  130. __d('cake_console', "What is the path to the directory layout you wish to copy?"),
  131. null,
  132. CAKE . 'Console' . DS . 'templates' . DS . 'skel'
  133. );
  134. if (!$skel) {
  135. $this->err(__d('cake_console', 'The directory path you supplied was empty. Please try again.'));
  136. } else {
  137. while (is_dir($skel) === false) {
  138. $skel = $this->in(
  139. __d('cake_console', 'Directory path does not exist please choose another:'),
  140. null,
  141. CAKE . 'Console' . DS . 'templates' . DS . 'skel'
  142. );
  143. }
  144. }
  145. }
  146. $app = basename($path);
  147. $this->out(__d('cake_console', '<info>Skel Directory</info>: ') . $skel);
  148. $this->out(__d('cake_console', '<info>Will be copied to</info>: ') . $path);
  149. $this->hr();
  150. $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
  151. if (strtolower($looksGood) == 'y') {
  152. $Folder = new Folder($skel);
  153. if (!empty($this->params['empty'])) {
  154. $skip = array();
  155. }
  156. if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
  157. $this->hr();
  158. $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $app, $path));
  159. $this->hr();
  160. } else {
  161. $this->err(__d('cake_console', "<error>Could not create</error> '%s' properly.", $app));
  162. return false;
  163. }
  164. foreach ($Folder->messages() as $message) {
  165. $this->out(String::wrap(' * ' . $message), 1, Shell::VERBOSE);
  166. }
  167. return true;
  168. } elseif (strtolower($looksGood) == 'q') {
  169. $this->out(__d('cake_console', 'Bake Aborted.'));
  170. } else {
  171. $this->execute(false);
  172. return false;
  173. }
  174. }
  175. /**
  176. * Writes a file with a default home page to the project.
  177. *
  178. * @param string $dir Path to project
  179. * @return boolean Success
  180. */
  181. public function createHome($dir) {
  182. $app = basename($dir);
  183. $path = $dir . 'View' . DS . 'pages' . DS;
  184. $source = LIBS . 'Console' . DS . 'templates' . DS .'default' . DS . 'views' . DS . 'home.ctp';
  185. include($source);
  186. return $this->createFile($path.'home.ctp', $output);
  187. }
  188. /**
  189. * Generates the correct path to the CakePHP libs that are generating the project
  190. * and points app/console/cake.php to the right place
  191. *
  192. * @param string $path Project path.
  193. * @return boolean success
  194. */
  195. public function consolePath($path) {
  196. $File = new File($path . 'Console' . DS . 'cake.php');
  197. $contents = $File->read();
  198. if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
  199. $path = LIBS . 'Console' . DS;
  200. $replacement = "'" . str_replace(DS, "' . DIRECTORY_SEPARATOR . '", $path) . "'";
  201. $result = str_replace($match[0], $replacement, $contents);
  202. if ($File->write($result)) {
  203. return true;
  204. }
  205. return false;
  206. }
  207. return false;
  208. }
  209. /**
  210. * Generates and writes 'Security.salt'
  211. *
  212. * @param string $path Project path
  213. * @return boolean Success
  214. */
  215. public function securitySalt($path) {
  216. $File = new File($path . 'Config' . DS . 'core.php');
  217. $contents = $File->read();
  218. if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  219. $string = Security::generateAuthKey();
  220. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
  221. if ($File->write($result)) {
  222. return true;
  223. }
  224. return false;
  225. }
  226. return false;
  227. }
  228. /**
  229. * Generates and writes 'Security.cipherSeed'
  230. *
  231. * @param string $path Project path
  232. * @return boolean Success
  233. */
  234. public function securityCipherSeed($path) {
  235. $File = new File($path . 'Config' . DS . 'core.php');
  236. $contents = $File->read();
  237. if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  238. if (!class_exists('Security')) {
  239. require LIBS . 'Utility' . DS . 'security.php';
  240. }
  241. $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
  242. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents);
  243. if ($File->write($result)) {
  244. return true;
  245. }
  246. return false;
  247. }
  248. return false;
  249. }
  250. /**
  251. * Generates and writes CAKE_CORE_INCLUDE_PATH
  252. *
  253. * @param string $path Project path
  254. * @return boolean Success
  255. */
  256. public function corePath($path) {
  257. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  258. $File = new File($path . 'webroot' . DS . 'index.php');
  259. $contents = $File->read();
  260. if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  261. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  262. $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  263. if (!$File->write($result)) {
  264. return false;
  265. }
  266. } else {
  267. return false;
  268. }
  269. $File = new File($path . 'webroot' . DS . 'test.php');
  270. $contents = $File->read();
  271. if (preg_match('/([\s]*define\(\'CAKE_CORE_INCLUDE_PATH\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  272. $result = str_replace($match[0], "\n\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  273. if (!$File->write($result)) {
  274. return false;
  275. }
  276. } else {
  277. return false;
  278. }
  279. return true;
  280. }
  281. }
  282. /**
  283. * Enables Configure::read('Routing.prefixes') in /app/config/core.php
  284. *
  285. * @param string $name Name to use as admin routing
  286. * @return boolean Success
  287. */
  288. public function cakeAdmin($name) {
  289. $path = (empty($this->configPath)) ? CONFIGS : $this->configPath;
  290. $File = new File($path . 'core.php');
  291. $contents = $File->read();
  292. if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
  293. $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents);
  294. if ($File->write($result)) {
  295. Configure::write('Routing.prefixes', array($name));
  296. return true;
  297. } else {
  298. return false;
  299. }
  300. } else {
  301. return false;
  302. }
  303. }
  304. /**
  305. * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
  306. *
  307. * @return string Admin route to use
  308. */
  309. public function getPrefix() {
  310. $admin = '';
  311. $prefixes = Configure::read('Routing.prefixes');
  312. if (!empty($prefixes)) {
  313. if (count($prefixes) == 1) {
  314. return $prefixes[0] . '_';
  315. }
  316. if ($this->interactive) {
  317. $this->out();
  318. $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
  319. }
  320. $options = array();
  321. foreach ($prefixes as $i => $prefix) {
  322. $options[] = $i + 1;
  323. if ($this->interactive) {
  324. $this->out($i + 1 . '. ' . $prefix);
  325. }
  326. }
  327. $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
  328. return $prefixes[$selection - 1] . '_';
  329. }
  330. if ($this->interactive) {
  331. $this->hr();
  332. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.'));
  333. $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
  334. $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
  335. while ($admin == '') {
  336. $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
  337. }
  338. if ($this->cakeAdmin($admin) !== true) {
  339. $this->out(__d('cake_console', '<error>Unable to write to</error> /app/config/core.php.'));
  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->_stop();
  342. }
  343. return $admin . '_';
  344. }
  345. return '';
  346. }
  347. /**
  348. * get the option parser.
  349. *
  350. * @return ConsoleOptionParser
  351. */
  352. public function getOptionParser() {
  353. $parser = parent::getOptionParser();
  354. return $parser->description(
  355. __d('cake_console', 'Generate a new CakePHP project skeleton.')
  356. )->addArgument('name', array(
  357. 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
  358. ))->addOption('empty', array(
  359. 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
  360. ))->addOption('skel', array(
  361. 'default' => current(App::core('Console')) . DS . 'templates' . DS . 'skel',
  362. '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.')
  363. ));
  364. }
  365. }