PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/console/libs/tasks/project.php

https://github.com/msadouni/cakephp2x
PHP | 342 lines | 229 code | 25 blank | 88 comment | 55 complexity | eed9b40a97eb0892dc6ecfe2340508b4 MD5 | raw file
  1. <?php
  2. /**
  3. * The Project Task handles creating the base application
  4. *
  5. *
  6. * PHP Version 5.x
  7. *
  8. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2009, 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-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package cake
  17. * @subpackage cake.cake.console.bake
  18. * @since CakePHP(tm) v 1.2
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. /**
  22. * Task class for creating new project apps and plugins
  23. *
  24. * @package cake
  25. * @subpackage cake.cake.console.libs.tasks
  26. */
  27. class ProjectTask extends Shell {
  28. /**
  29. * configs path (used in testing).
  30. *
  31. * @var string
  32. */
  33. var $configPath = null;
  34. /**
  35. * Checks that given project path does not already exist, and
  36. * finds the app directory in it. Then it calls bake() with that information.
  37. *
  38. * @param string $project Project path
  39. * @access public
  40. */
  41. function execute($project = null) {
  42. if ($project === null) {
  43. if (isset($this->args[0])) {
  44. $project = $this->args[0];
  45. }
  46. }
  47. if ($project) {
  48. $this->Dispatch->parseParams(array('-app', $project));
  49. $project = $this->params['working'];
  50. }
  51. if (empty($this->params['skel'])) {
  52. $this->params['skel'] = '';
  53. if (is_dir(CAKE . 'console' . DS . 'templates' . DS . 'skel') === true) {
  54. $this->params['skel'] = CAKE . 'console' . DS . 'templates' . DS . 'skel';
  55. }
  56. }
  57. while (!$project) {
  58. $prompt = __("What is the full path for this app including the app directory name?\n Example:", true);
  59. $default = $this->params['working'] . DS . 'myapp';
  60. $project = $this->in($prompt . $default, null, $default);
  61. }
  62. if ($project) {
  63. $response = false;
  64. while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
  65. $prompt = sprintf(__('A project already exists in this location: %s Overwrite?', true), $project);
  66. $response = $this->in($prompt, array('y','n'), 'n');
  67. if (strtolower($response) === 'n') {
  68. $response = $project = false;
  69. }
  70. }
  71. }
  72. if ($this->bake($project)) {
  73. $path = Folder::slashTerm($project);
  74. if ($this->createHome($path)) {
  75. $this->out(__('Welcome page created', true));
  76. } else {
  77. $this->out(__('The Welcome page was NOT created', true));
  78. }
  79. if ($this->securitySalt($path) === true ) {
  80. $this->out(__('Random hash key created for \'Security.salt\'', true));
  81. } else {
  82. $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
  83. }
  84. $corePath = $this->corePath($path);
  85. if ($corePath === true ) {
  86. $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
  87. $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', true), CAKE_CORE_INCLUDE_PATH));
  88. $this->out(__('Remember to check these value after moving to production server', true));
  89. } elseif ($corePath === false) {
  90. $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' .DS .'index.php'));
  91. }
  92. $Folder = new Folder($path);
  93. if (!$Folder->chmod($path . 'tmp', 0777)) {
  94. $this->err(sprintf(__('Could not set permissions on %s', true), $path . DS .'tmp'));
  95. $this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS .'tmp'));
  96. }
  97. $this->params['working'] = $path;
  98. $this->params['app'] = basename($path);
  99. return true;
  100. }
  101. }
  102. /**
  103. * Looks for a skeleton template of a Cake application,
  104. * and if not found asks the user for a path. When there is a path
  105. * this method will make a deep copy of the skeleton to the project directory.
  106. * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
  107. *
  108. * @param string $path Project path
  109. * @param string $skel Path to copy from
  110. * @param string $skip array of directories to skip when copying
  111. * @access private
  112. */
  113. function bake($path, $skel = null, $skip = array('empty')) {
  114. if (!$skel) {
  115. $skel = $this->params['skel'];
  116. }
  117. while (!$skel) {
  118. $skel = $this->in(sprintf(__("What is the path to the directory layout you wish to copy?\nExample: %s"), APP, null, ROOT . DS . 'myapp' . DS));
  119. if ($skel == '') {
  120. $this->out(__('The directory path you supplied was empty. Please try again.', true));
  121. } else {
  122. while (is_dir($skel) === false) {
  123. $skel = $this->in(__('Directory path does not exist please choose another:', true));
  124. }
  125. }
  126. }
  127. $app = basename($path);
  128. $this->out(__('Bake Project', true));
  129. $this->out(__("Skel Directory: ", true) . $skel);
  130. $this->out(__("Will be copied to: ", true) . $path);
  131. $this->hr();
  132. $looksGood = $this->in(__('Look okay?', true), array('y', 'n', 'q'), 'y');
  133. if (strtolower($looksGood) == 'y') {
  134. $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
  135. $Folder = new Folder($skel);
  136. if (!empty($this->params['empty'])) {
  137. $skip = array();
  138. }
  139. if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
  140. $this->hr();
  141. $this->out(sprintf(__("Created: %s in %s", true), $app, $path));
  142. $this->hr();
  143. } else {
  144. $this->err(sprintf(__(" '%s' could not be created properly", true), $app));
  145. return false;
  146. }
  147. if (strtolower($verbose) == 'y') {
  148. foreach ($Folder->messages() as $message) {
  149. $this->out($message);
  150. }
  151. }
  152. return true;
  153. } elseif (strtolower($looksGood) == 'q') {
  154. $this->out(__('Bake Aborted.', true));
  155. } else {
  156. $this->execute(false);
  157. return false;
  158. }
  159. }
  160. /**
  161. * Writes a file with a default home page to the project.
  162. *
  163. * @param string $dir Path to project
  164. * @return boolean Success
  165. * @access public
  166. */
  167. function createHome($dir) {
  168. $app = basename($dir);
  169. $path = $dir . 'views' . DS . 'pages' . DS;
  170. $source = CAKE . 'console' . DS . 'templates' . DS .'default' . DS . 'views' . DS . 'home.ctp';
  171. include($source);
  172. return $this->createFile($path.'home.ctp', $output);
  173. }
  174. /**
  175. * Generates and writes 'Security.salt'
  176. *
  177. * @param string $path Project path
  178. * @return boolean Success
  179. * @access public
  180. */
  181. function securitySalt($path) {
  182. $File = new File($path . 'config' . DS . 'core.php');
  183. $contents = $File->read();
  184. if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
  185. if (!class_exists('Security')) {
  186. require LIBS . 'security.php';
  187. }
  188. $string = Security::generateAuthKey();
  189. $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
  190. if ($File->write($result)) {
  191. return true;
  192. }
  193. return false;
  194. }
  195. return false;
  196. }
  197. /**
  198. * Generates and writes CAKE_CORE_INCLUDE_PATH
  199. *
  200. * @param string $path Project path
  201. * @return boolean Success
  202. * @access public
  203. */
  204. function corePath($path) {
  205. if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
  206. $File = new File($path . 'webroot' . DS . 'index.php');
  207. $contents = $File->read();
  208. if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
  209. $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
  210. $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  211. if (!$File->write($result)) {
  212. return false;
  213. }
  214. } else {
  215. return false;
  216. }
  217. $File = new File($path . 'webroot' . DS . 'test.php');
  218. $contents = $File->read();
  219. if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
  220. $result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', " . $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "');", $contents);
  221. if (!$File->write($result)) {
  222. return false;
  223. }
  224. } else {
  225. return false;
  226. }
  227. return true;
  228. }
  229. }
  230. /**
  231. * Enables Configure::read('Routing.admin') in /app/config/core.php
  232. *
  233. * @param string $name Name to use as admin routing
  234. * @return boolean Success
  235. * @access public
  236. */
  237. function cakeAdmin($name) {
  238. $path = (empty($this->configPath)) ? CONFIGS : $this->configPath;
  239. $File = new File($path . 'core.php');
  240. $contents = $File->read();
  241. if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.prefixes\',[\\t\\x20\'a-z,\)\(]*\\);)%', $contents, $match)) {
  242. $result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.prefixes\', array(\''.$name.'\'));', $contents);
  243. if ($File->write($result)) {
  244. Configure::write('Routing.prefixes', array($name));
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. } else {
  250. return false;
  251. }
  252. }
  253. /**
  254. * Checks for Configure::read('Routing.admin') and forces user to input it if not enabled
  255. *
  256. * @return string Admin route to use
  257. * @access public
  258. */
  259. function getPrefix() {
  260. $admin = '';
  261. $prefixes = Configure::read('Routing.prefixes');
  262. if (!empty($prefixes)) {
  263. if ($this->interactive) {
  264. $this->out();
  265. $this->out(__('You have more than one routing prefix configured', true));
  266. }
  267. if (count($prefixes) == 1) {
  268. return $prefixes[0] . '_';
  269. }
  270. $options = array();
  271. foreach ($prefixes as $i => $prefix) {
  272. $options[] = $i + 1;
  273. if ($this->interactive) {
  274. $this->out($i + 1 . '. ' . $prefix);
  275. }
  276. }
  277. $selection = $this->in(__('Please choose a prefix to bake with.', true), $options, 1);
  278. return $prefixes[$selection - 1] . '_';
  279. }
  280. if ($this->interactive) {
  281. $this->hr();
  282. $this->out('You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.');
  283. $this->out(__('What would you like the prefix route to be?', true));
  284. $this->out(__('Example: www.example.com/admin/controller', true));
  285. while ($admin == '') {
  286. $admin = $this->in(__("Enter a routing prefix:", true), null, 'admin');
  287. }
  288. if ($this->cakeAdmin($admin) !== true) {
  289. $this->out(__('Unable to write to /app/config/core.php.', true));
  290. $this->out('You need to enable Configure::write(\'Routing.prefixes\',array(\'admin\')) in /app/config/core.php to use prefix routing.');
  291. $this->_stop();
  292. }
  293. return $admin . '_';
  294. }
  295. return '';
  296. }
  297. /**
  298. * Help
  299. *
  300. * @return void
  301. * @access public
  302. */
  303. function help() {
  304. $this->hr();
  305. $this->out("Usage: cake bake project <arg1>");
  306. $this->hr();
  307. $this->out('Commands:');
  308. $this->out();
  309. $this->out("project <name>");
  310. $this->out("\tbakes app directory structure.");
  311. $this->out("\tif <name> begins with '/' path is absolute.");
  312. $this->out();
  313. $this->_stop();
  314. }
  315. }
  316. ?>