ProjectTask.php 14 KB

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