ProjectTask.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. * @return mixed
  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', '<info>CakePHP is on your `include_path`. CAKE_CORE_INCLUDE_PATH will be set, but commented out.</info>'));
  91. } else {
  92. $this->out(__d('cake_console', '<warning>CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.</warning>'));
  93. $this->out(__d('cake_console', 'You can fix this by adding CakePHP to your `include_path`.'));
  94. $hardCode = true;
  95. }
  96. $success = $this->corePath($path, $hardCode) === true;
  97. if ($success) {
  98. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
  99. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
  100. } else {
  101. $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' .DS .'index.php'));
  102. $success = false;
  103. }
  104. if ($success && $hardCode) {
  105. $this->out(__d('cake_console', ' * <warning>Remember to check these values after moving to production server</warning>'));
  106. }
  107. $Folder = new Folder($path);
  108. if (!$Folder->chmod($path . 'tmp', 0777)) {
  109. $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS .'tmp'));
  110. $this->out(__d('cake_console', 'chmod -R 0777 %s', $path . DS .'tmp'));
  111. $success = false;
  112. }
  113. if ($success) {
  114. $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
  115. } else {
  116. $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
  117. }
  118. return $path;
  119. }
  120. }
  121. /**
  122. * Checks PHP's include_path for CakePHP.
  123. *
  124. * @return boolean Indicates whether or not CakePHP exists on include_path
  125. */
  126. public function cakeOnIncludePath() {
  127. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  128. foreach ($paths as $path) {
  129. if (file_exists($path . DS . 'Cake' . DS . 'bootstrap.php')) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. /**
  136. * Looks for a skeleton template of a Cake application,
  137. * and if not found asks the user for a path. When there is a path
  138. * this method will make a deep copy of the skeleton to the project directory.
  139. * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
  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. if (!class_exists('Security')) {
  264. require CAKE . 'Utility' . DS . 'security.php';
  265. }
  266. $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
  267. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \''.$string.'\');', $contents);
  268. if ($File->write($result)) {
  269. return true;
  270. }
  271. return false;
  272. }
  273. return false;
  274. }
  275. /**
  276. * Generates and writes CAKE_CORE_INCLUDE_PATH
  277. *
  278. * @param string $path Project path
  279. * @param boolean $hardCode Wether or not define calls should be hardcoded.
  280. * @return boolean Success
  281. */
  282. public function corePath($path, $hardCode = true) {
  283. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  284. $filename = $path . 'webroot' . DS . 'index.php';
  285. if (!$this->_replaceCorePath($filename, $hardCode)) {
  286. return false;
  287. }
  288. $filename = $path . 'webroot' . DS . 'test.php';
  289. if (!$this->_replaceCorePath($filename, $hardCode)) {
  290. return false;
  291. }
  292. return true;
  293. }
  294. }
  295. /**
  296. * Replaces the __CAKE_PATH__ placeholder in the template files.
  297. *
  298. * @param string $filename The filename to operate on.
  299. * @param boolean $hardCode Whether or not the define should be uncommented.
  300. * @return boolean Success
  301. */
  302. protected function _replaceCorePath($filename, $hardCode) {
  303. $contents = file_get_contents($filename);
  304. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  305. $corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
  306. $result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
  307. if ($hardCode) {
  308. $result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
  309. }
  310. if (!file_put_contents($filename, $result)) {
  311. return false;
  312. }
  313. if ($count == 0) {
  314. return false;
  315. }
  316. return true;
  317. }
  318. /**
  319. * Enables Configure::read('Routing.prefixes') in /app/Config/core.php
  320. *
  321. * @param string $name Name to use as admin routing
  322. * @return boolean Success
  323. */
  324. public function cakeAdmin($name) {
  325. $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
  326. $File = new File($path . 'core.php');
  327. $contents = $File->read();
  328. if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
  329. $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents);
  330. if ($File->write($result)) {
  331. Configure::write('Routing.prefixes', array($name));
  332. return true;
  333. } else {
  334. return false;
  335. }
  336. } else {
  337. return false;
  338. }
  339. }
  340. /**
  341. * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
  342. *
  343. * @return string Admin route to use
  344. */
  345. public function getPrefix() {
  346. $admin = '';
  347. $prefixes = Configure::read('Routing.prefixes');
  348. if (!empty($prefixes)) {
  349. if (count($prefixes) == 1) {
  350. return $prefixes[0] . '_';
  351. }
  352. if ($this->interactive) {
  353. $this->out();
  354. $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
  355. }
  356. $options = array();
  357. foreach ($prefixes as $i => $prefix) {
  358. $options[] = $i + 1;
  359. if ($this->interactive) {
  360. $this->out($i + 1 . '. ' . $prefix);
  361. }
  362. }
  363. $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
  364. return $prefixes[$selection - 1] . '_';
  365. }
  366. if ($this->interactive) {
  367. $this->hr();
  368. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  369. $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
  370. $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
  371. while ($admin == '') {
  372. $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
  373. }
  374. if ($this->cakeAdmin($admin) !== true) {
  375. $this->out(__d('cake_console', '<error>Unable to write to</error> /app/Config/core.php.'));
  376. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  377. $this->_stop();
  378. }
  379. return $admin . '_';
  380. }
  381. return '';
  382. }
  383. /**
  384. * get the option parser.
  385. *
  386. * @return ConsoleOptionParser
  387. */
  388. public function getOptionParser() {
  389. $parser = parent::getOptionParser();
  390. return $parser->description(
  391. __d('cake_console', 'Generate a new CakePHP project skeleton.')
  392. )->addArgument('name', array(
  393. 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
  394. ))->addOption('empty', array(
  395. 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
  396. ))->addOption('skel', array(
  397. 'default' => current(App::core('Console')) . 'Templates' . DS . 'skel',
  398. '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.')
  399. ));
  400. }
  401. }