ProjectTask.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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->securitySalt($path) === true) {
  66. $this->out(__d('cake_console', ' * Random hash key created for \'Security.salt\''));
  67. } else {
  68. $this->err(__d('cake_console', 'Unable to generate random hash for \'Security.salt\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
  69. $success = false;
  70. }
  71. if ($this->securityCipherSeed($path) === true) {
  72. $this->out(__d('cake_console', ' * Random seed created for \'Security.cipherSeed\''));
  73. } else {
  74. $this->err(__d('cake_console', 'Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
  75. $success = false;
  76. }
  77. if ($this->consolePath($path) === true) {
  78. $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
  79. } else {
  80. $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
  81. $success = false;
  82. }
  83. $hardCode = false;
  84. if ($this->cakeOnIncludePath()) {
  85. $this->out(__d('cake_console', '<info>CakePHP is on your `include_path`. CAKE_CORE_INCLUDE_PATH will be set, but commented out.</info>'));
  86. } else {
  87. $this->out(__d('cake_console', '<warning>CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.</warning>'));
  88. $this->out(__d('cake_console', 'You can fix this by adding CakePHP to your `include_path`.'));
  89. $hardCode = true;
  90. }
  91. $success = $this->corePath($path, $hardCode) === true;
  92. if ($success) {
  93. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
  94. $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
  95. } else {
  96. $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' . DS . 'index.php'));
  97. $success = false;
  98. }
  99. if ($success && $hardCode) {
  100. $this->out(__d('cake_console', ' * <warning>Remember to check these values after moving to production server</warning>'));
  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 boolean 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. *
  135. * @param string $path Project path
  136. * @param string $skel Path to copy from
  137. * @param string $skip array of directories to skip when copying
  138. * @return mixed
  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. * Generates the correct path to the CakePHP libs that are generating the project
  196. * and points app/console/cake.php to the right place
  197. *
  198. * @param string $path Project path.
  199. * @return boolean success
  200. */
  201. public function consolePath($path) {
  202. $File = new File($path . 'Console' . DS . 'cake.php');
  203. $contents = $File->read();
  204. if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
  205. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " \$ds . '" : "'";
  206. $replacement = $root . str_replace(DS, "' . \$ds . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
  207. $result = str_replace($match[0], $replacement, $contents);
  208. if ($File->write($result)) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. return false;
  214. }
  215. /**
  216. * Generates and writes 'Security.salt'
  217. *
  218. * @param string $path Project path
  219. * @return boolean Success
  220. */
  221. public function securitySalt($path) {
  222. $File = new File($path . 'Config' . DS . 'core.php');
  223. $contents = $File->read();
  224. if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  225. $string = Security::generateAuthKey();
  226. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \'' . $string . '\');', $contents);
  227. if ($File->write($result)) {
  228. return true;
  229. }
  230. return false;
  231. }
  232. return false;
  233. }
  234. /**
  235. * Generates and writes 'Security.cipherSeed'
  236. *
  237. * @param string $path Project path
  238. * @return boolean Success
  239. */
  240. public function securityCipherSeed($path) {
  241. $File = new File($path . 'Config' . DS . 'core.php');
  242. $contents = $File->read();
  243. if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  244. App::uses('Security', 'Utility');
  245. $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
  246. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \'' . $string . '\');', $contents);
  247. if ($File->write($result)) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. return false;
  253. }
  254. /**
  255. * Generates and writes CAKE_CORE_INCLUDE_PATH
  256. *
  257. * @param string $path Project path
  258. * @param boolean $hardCode Wether or not define calls should be hardcoded.
  259. * @return boolean Success
  260. */
  261. public function corePath($path, $hardCode = true) {
  262. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  263. $filename = $path . 'webroot' . DS . 'index.php';
  264. if (!$this->_replaceCorePath($filename, $hardCode)) {
  265. return false;
  266. }
  267. $filename = $path . 'webroot' . DS . 'test.php';
  268. if (!$this->_replaceCorePath($filename, $hardCode)) {
  269. return false;
  270. }
  271. return true;
  272. }
  273. }
  274. /**
  275. * Replaces the __CAKE_PATH__ placeholder in the template files.
  276. *
  277. * @param string $filename The filename to operate on.
  278. * @param boolean $hardCode Whether or not the define should be uncommented.
  279. * @return boolean Success
  280. */
  281. protected function _replaceCorePath($filename, $hardCode) {
  282. $contents = file_get_contents($filename);
  283. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  284. $corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
  285. $result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
  286. if ($hardCode) {
  287. $result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
  288. }
  289. if (!file_put_contents($filename, $result)) {
  290. return false;
  291. }
  292. if ($count == 0) {
  293. return false;
  294. }
  295. return true;
  296. }
  297. /**
  298. * Enables Configure::read('Routing.prefixes') in /app/Config/core.php
  299. *
  300. * @param string $name Name to use as admin routing
  301. * @return boolean Success
  302. */
  303. public function cakeAdmin($name) {
  304. $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
  305. $File = new File($path . 'core.php');
  306. $contents = $File->read();
  307. if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
  308. $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\'' . $name . '\'));', $contents);
  309. if ($File->write($result)) {
  310. Configure::write('Routing.prefixes', array($name));
  311. return true;
  312. } else {
  313. return false;
  314. }
  315. } else {
  316. return false;
  317. }
  318. }
  319. /**
  320. * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
  321. *
  322. * @return string Admin route to use
  323. */
  324. public function getPrefix() {
  325. $admin = '';
  326. $prefixes = Configure::read('Routing.prefixes');
  327. if (!empty($prefixes)) {
  328. if (count($prefixes) == 1) {
  329. return $prefixes[0] . '_';
  330. }
  331. if ($this->interactive) {
  332. $this->out();
  333. $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
  334. }
  335. $options = array();
  336. foreach ($prefixes as $i => $prefix) {
  337. $options[] = $i + 1;
  338. if ($this->interactive) {
  339. $this->out($i + 1 . '. ' . $prefix);
  340. }
  341. }
  342. $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
  343. return $prefixes[$selection - 1] . '_';
  344. }
  345. if ($this->interactive) {
  346. $this->hr();
  347. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  348. $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
  349. $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
  350. while ($admin == '') {
  351. $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
  352. }
  353. if ($this->cakeAdmin($admin) !== true) {
  354. $this->out(__d('cake_console', '<error>Unable to write to</error> /app/Config/core.php.'));
  355. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  356. $this->_stop();
  357. }
  358. return $admin . '_';
  359. }
  360. return '';
  361. }
  362. /**
  363. * get the option parser.
  364. *
  365. * @return ConsoleOptionParser
  366. */
  367. public function getOptionParser() {
  368. $parser = parent::getOptionParser();
  369. return $parser->description(
  370. __d('cake_console', 'Generate a new CakePHP project skeleton.')
  371. )->addArgument('name', array(
  372. 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
  373. ))->addOption('empty', array(
  374. 'boolean' => true,
  375. 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
  376. ))->addOption('skel', array(
  377. 'default' => current(App::core('Console')) . 'Templates' . DS . 'skel',
  378. '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.')
  379. ));
  380. }
  381. }