PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Console/Command/Task/ProjectTask.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 435 lines | 327 code | 30 blank | 78 comment | 57 complexity | d21f8e1b34b83a53f42098d3098a6c35 MD5 | raw file
  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('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. * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
  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. * Writes a file with a default home page to the project.
  203. *
  204. * @param string $dir Path to project
  205. * @return boolean Success
  206. */
  207. public function createHome($dir) {
  208. $app = basename($dir);
  209. $path = $dir . 'View' . DS . 'Pages' . DS;
  210. $source = CAKE . 'Console' . DS . 'Templates' . DS .'default' . DS . 'views' . DS . 'home.ctp';
  211. include($source);
  212. return $this->createFile($path.'home.ctp', $output);
  213. }
  214. /**
  215. * Generates the correct path to the CakePHP libs that are generating the project
  216. * and points app/console/cake.php to the right place
  217. *
  218. * @param string $path Project path.
  219. * @return boolean success
  220. */
  221. public function consolePath($path) {
  222. $File = new File($path . 'Console' . DS . 'cake.php');
  223. $contents = $File->read();
  224. if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
  225. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " \$ds . '" : "'";
  226. $replacement = $root . str_replace(DS, "' . \$ds . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
  227. $result = str_replace($match[0], $replacement, $contents);
  228. if ($File->write($result)) {
  229. return true;
  230. }
  231. return false;
  232. }
  233. return false;
  234. }
  235. /**
  236. * Generates and writes 'Security.salt'
  237. *
  238. * @param string $path Project path
  239. * @return boolean Success
  240. */
  241. public function securitySalt($path) {
  242. $File = new File($path . 'Config' . DS . 'core.php');
  243. $contents = $File->read();
  244. if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  245. $string = Security::generateAuthKey();
  246. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \'' . $string . '\');', $contents);
  247. if ($File->write($result)) {
  248. return true;
  249. }
  250. return false;
  251. }
  252. return false;
  253. }
  254. /**
  255. * Generates and writes 'Security.cipherSeed'
  256. *
  257. * @param string $path Project path
  258. * @return boolean Success
  259. */
  260. public function securityCipherSeed($path) {
  261. $File = new File($path . 'Config' . DS . 'core.php');
  262. $contents = $File->read();
  263. if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
  264. App::uses('Security', 'Utility');
  265. $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
  266. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \'' . $string . '\');', $contents);
  267. if ($File->write($result)) {
  268. return true;
  269. }
  270. return false;
  271. }
  272. return false;
  273. }
  274. /**
  275. * Generates and writes CAKE_CORE_INCLUDE_PATH
  276. *
  277. * @param string $path Project path
  278. * @param boolean $hardCode Wether or not define calls should be hardcoded.
  279. * @return boolean Success
  280. */
  281. public function corePath($path, $hardCode = true) {
  282. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  283. $filename = $path . 'webroot' . DS . 'index.php';
  284. if (!$this->_replaceCorePath($filename, $hardCode)) {
  285. return false;
  286. }
  287. $filename = $path . 'webroot' . DS . 'test.php';
  288. if (!$this->_replaceCorePath($filename, $hardCode)) {
  289. return false;
  290. }
  291. return true;
  292. }
  293. }
  294. /**
  295. * Replaces the __CAKE_PATH__ placeholder in the template files.
  296. *
  297. * @param string $filename The filename to operate on.
  298. * @param boolean $hardCode Whether or not the define should be uncommented.
  299. * @return boolean Success
  300. */
  301. protected function _replaceCorePath($filename, $hardCode) {
  302. $contents = file_get_contents($filename);
  303. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  304. $corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
  305. $result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
  306. if ($hardCode) {
  307. $result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
  308. }
  309. if (!file_put_contents($filename, $result)) {
  310. return false;
  311. }
  312. if ($count == 0) {
  313. return false;
  314. }
  315. return true;
  316. }
  317. /**
  318. * Enables Configure::read('Routing.prefixes') in /app/Config/core.php
  319. *
  320. * @param string $name Name to use as admin routing
  321. * @return boolean Success
  322. */
  323. public function cakeAdmin($name) {
  324. $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
  325. $File = new File($path . 'core.php');
  326. $contents = $File->read();
  327. if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
  328. $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\'' . $name . '\'));', $contents);
  329. if ($File->write($result)) {
  330. Configure::write('Routing.prefixes', array($name));
  331. return true;
  332. } else {
  333. return false;
  334. }
  335. } else {
  336. return false;
  337. }
  338. }
  339. /**
  340. * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
  341. *
  342. * @return string Admin route to use
  343. */
  344. public function getPrefix() {
  345. $admin = '';
  346. $prefixes = Configure::read('Routing.prefixes');
  347. if (!empty($prefixes)) {
  348. if (count($prefixes) == 1) {
  349. return $prefixes[0] . '_';
  350. }
  351. if ($this->interactive) {
  352. $this->out();
  353. $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
  354. }
  355. $options = array();
  356. foreach ($prefixes as $i => $prefix) {
  357. $options[] = $i + 1;
  358. if ($this->interactive) {
  359. $this->out($i + 1 . '. ' . $prefix);
  360. }
  361. }
  362. $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
  363. return $prefixes[$selection - 1] . '_';
  364. }
  365. if ($this->interactive) {
  366. $this->hr();
  367. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  368. $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
  369. $this->out(__d('cake_console', 'Example: www.example.com/admin/controller'));
  370. while ($admin == '') {
  371. $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
  372. }
  373. if ($this->cakeAdmin($admin) !== true) {
  374. $this->out(__d('cake_console', '<error>Unable to write to</error> /app/Config/core.php.'));
  375. $this->out(__d('cake_console', 'You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/Config/core.php to use prefix routing.'));
  376. $this->_stop();
  377. }
  378. return $admin . '_';
  379. }
  380. return '';
  381. }
  382. /**
  383. * get the option parser.
  384. *
  385. * @return ConsoleOptionParser
  386. */
  387. public function getOptionParser() {
  388. $parser = parent::getOptionParser();
  389. return $parser->description(
  390. __d('cake_console', 'Generate a new CakePHP project skeleton.')
  391. )->addArgument('name', array(
  392. 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
  393. ))->addOption('empty', array(
  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. }