ProjectTask.php 14 KB

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