PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

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