ProjectTask.php 14 KB

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