1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: App::uses('AppShell', 'Console/Command');
19: App::uses('File', 'Utility');
20: App::uses('Folder', 'Utility');
21: App::uses('String', 'Utility');
22: App::uses('Security', 'Utility');
23:
24: 25: 26: 27: 28:
29: class ProjectTask extends AppShell {
30:
31: 32: 33: 34: 35:
36: public $configPath = null;
37:
38: 39: 40: 41: 42: 43:
44: public function execute() {
45: $project = null;
46: if (isset($this->args[0])) {
47: $project = $this->args[0];
48: } else {
49: $appContents = array_diff(scandir(APP), array('.', '..'));
50: if (empty($appContents)) {
51: $suggestedPath = rtrim(APP, DS);
52: } else {
53: $suggestedPath = APP . 'myapp';
54: }
55: }
56:
57: while (!$project) {
58: $prompt = __d('cake_console', "What is the path to the project you want to bake?");
59: $project = $this->in($prompt, null, $suggestedPath);
60: }
61:
62: if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
63: $project = $_SERVER['PWD'] . DS . $project;
64: }
65:
66: $response = false;
67: while (!$response && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
68: $prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
69: $response = $this->in($prompt, array('y', 'n'), 'n');
70: if (strtolower($response) === 'n') {
71: $response = $project = false;
72: }
73: }
74:
75: $success = true;
76: if ($this->bake($project)) {
77: $path = Folder::slashTerm($project);
78:
79: if ($this->securitySalt($path) === true) {
80: $this->out(__d('cake_console', ' * Random hash key created for \'Security.salt\''));
81: } else {
82: $this->err(__d('cake_console', 'Unable to generate random hash for \'Security.salt\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
83: $success = false;
84: }
85:
86: if ($this->securityCipherSeed($path) === true) {
87: $this->out(__d('cake_console', ' * Random seed created for \'Security.cipherSeed\''));
88: } else {
89: $this->err(__d('cake_console', 'Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
90: $success = false;
91: }
92:
93: if ($this->cachePrefix($path)) {
94: $this->out(__d('cake_console', ' * Cache prefix set'));
95: } else {
96: $this->err(__d('cake_console', 'The cache prefix was <error>NOT</error> set'));
97: $success = false;
98: }
99:
100: if ($this->consolePath($path) === true) {
101: $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
102: } else {
103: $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
104: $success = false;
105: }
106:
107: $hardCode = false;
108: if ($this->cakeOnIncludePath()) {
109: $this->out(__d('cake_console', '<info>CakePHP is on your `include_path`. CAKE_CORE_INCLUDE_PATH will be set, but commented out.</info>'));
110: } else {
111: $this->out(__d('cake_console', '<warning>CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.</warning>'));
112: $this->out(__d('cake_console', 'You can fix this by adding CakePHP to your `include_path`.'));
113: $hardCode = true;
114: }
115: $success = $this->corePath($path, $hardCode) === true;
116: if ($success) {
117: $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in %s', CAKE_CORE_INCLUDE_PATH, 'webroot/index.php'));
118: $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in %s', CAKE_CORE_INCLUDE_PATH, 'webroot/test.php'));
119: } else {
120: $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' . DS . 'index.php'));
121: $success = false;
122: }
123: if ($success && $hardCode) {
124: $this->out(__d('cake_console', ' * <warning>Remember to check these values after moving to production server</warning>'));
125: }
126:
127: $Folder = new Folder($path);
128: if (!$Folder->chmod($path . 'tmp', 0777)) {
129: $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS . 'tmp'));
130: $this->out('chmod -R 0777 ' . $path . DS . 'tmp');
131: $success = false;
132: }
133: if ($success) {
134: $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
135: } else {
136: $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
137: }
138: return $path;
139: }
140: }
141:
142: 143: 144: 145: 146:
147: public function cakeOnIncludePath() {
148: $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
149: foreach ($paths as $path) {
150: if (file_exists($path . DS . 'Cake' . DS . 'bootstrap.php')) {
151: return true;
152: }
153: }
154: return false;
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165: 166:
167: public function bake($path, $skel = null, $skip = array('empty')) {
168: if (!$skel && !empty($this->params['skel'])) {
169: $skel = $this->params['skel'];
170: }
171: while (!$skel) {
172: $skel = $this->in(
173: __d('cake_console', "What is the path to the directory layout you wish to copy?"),
174: null,
175: CAKE . 'Console' . DS . 'Templates' . DS . 'skel'
176: );
177: if (!$skel) {
178: $this->err(__d('cake_console', 'The directory path you supplied was empty. Please try again.'));
179: } else {
180: while (is_dir($skel) === false) {
181: $skel = $this->in(
182: __d('cake_console', 'Directory path does not exist please choose another:'),
183: null,
184: CAKE . 'Console' . DS . 'Templates' . DS . 'skel'
185: );
186: }
187: }
188: }
189:
190: $app = basename($path);
191:
192: $this->out(__d('cake_console', '<info>Skel Directory</info>: ') . $skel);
193: $this->out(__d('cake_console', '<info>Will be copied to</info>: ') . $path);
194: $this->hr();
195:
196: $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
197:
198: switch (strtolower($looksGood)) {
199: case 'y':
200: $Folder = new Folder($skel);
201: if (!empty($this->params['empty'])) {
202: $skip = array();
203: }
204:
205: if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
206: $this->hr();
207: $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $app, $path));
208: $this->hr();
209: } else {
210: $this->err(__d('cake_console', "<error>Could not create</error> '%s' properly.", $app));
211: return false;
212: }
213:
214: foreach ($Folder->messages() as $message) {
215: $this->out(String::wrap(' * ' . $message), 1, Shell::VERBOSE);
216: }
217:
218: return true;
219: case 'n':
220: unset($this->args[0]);
221: $this->execute();
222: return false;
223: case 'q':
224: $this->out(__d('cake_console', '<error>Bake Aborted.</error>'));
225: return false;
226: }
227: }
228:
229: 230: 231: 232: 233: 234: 235:
236: public function consolePath($path) {
237: $File = new File($path . 'Console' . DS . 'cake.php');
238: $contents = $File->read();
239: if (preg_match('/(__CAKE_PATH__)/', $contents, $match)) {
240: $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " \$ds . '" : "'";
241: $replacement = $root . str_replace(DS, "' . \$ds . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
242: $result = str_replace($match[0], $replacement, $contents);
243: if ($File->write($result)) {
244: return true;
245: }
246: return false;
247: }
248: return false;
249: }
250:
251: 252: 253: 254: 255: 256:
257: public function securitySalt($path) {
258: $File = new File($path . 'Config' . DS . 'core.php');
259: $contents = $File->read();
260: if (preg_match('/([\s]*Configure::write\(\'Security.salt\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
261: $string = Security::generateAuthKey();
262: $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \'' . $string . '\');', $contents);
263: if ($File->write($result)) {
264: return true;
265: }
266: return false;
267: }
268: return false;
269: }
270:
271: 272: 273: 274: 275: 276:
277: public function securityCipherSeed($path) {
278: $File = new File($path . 'Config' . DS . 'core.php');
279: $contents = $File->read();
280: if (preg_match('/([\s]*Configure::write\(\'Security.cipherSeed\',[\s\'A-z0-9]*\);)/', $contents, $match)) {
281: App::uses('Security', 'Utility');
282: $string = substr(bin2hex(Security::generateAuthKey()), 0, 30);
283: $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.cipherSeed\', \'' . $string . '\');', $contents);
284: if ($File->write($result)) {
285: return true;
286: }
287: return false;
288: }
289: return false;
290: }
291:
292: 293: 294: 295: 296: 297:
298: public function cachePrefix($dir) {
299: $app = basename($dir);
300: $File = new File($dir . 'Config' . DS . 'core.php');
301: $contents = $File->read();
302: if (preg_match('/(\$prefix = \'myapp_\';)/', $contents, $match)) {
303: $result = str_replace($match[0], '$prefix = \'' . $app . '_\';', $contents);
304: return $File->write($result);
305: }
306: return false;
307: }
308:
309: 310: 311: 312: 313: 314: 315:
316: public function corePath($path, $hardCode = true) {
317: if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
318: $filename = $path . 'webroot' . DS . 'index.php';
319: if (!$this->_replaceCorePath($filename, $hardCode)) {
320: return false;
321: }
322: $filename = $path . 'webroot' . DS . 'test.php';
323: if (!$this->_replaceCorePath($filename, $hardCode)) {
324: return false;
325: }
326: return true;
327: }
328: }
329:
330: 331: 332: 333: 334: 335: 336:
337: protected function _replaceCorePath($filename, $hardCode) {
338: $contents = file_get_contents($filename);
339:
340: $root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
341: $corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
342:
343: $result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
344: if ($hardCode) {
345: $result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
346: }
347: if (!file_put_contents($filename, $result)) {
348: return false;
349: }
350: return (bool)$count;
351: }
352:
353: 354: 355: 356: 357: 358:
359: public function cakeAdmin($name) {
360: $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
361: $File = new File($path . 'core.php');
362: $contents = $File->read();
363: if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
364: $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\'' . $name . '\'));', $contents);
365: if ($File->write($result)) {
366: Configure::write('Routing.prefixes', array($name));
367: return true;
368: }
369: }
370: return false;
371: }
372:
373: 374: 375: 376: 377:
378: public function getPrefix() {
379: $admin = '';
380: $prefixes = Configure::read('Routing.prefixes');
381: if (!empty($prefixes)) {
382: if (count($prefixes) === 1) {
383: return $prefixes[0] . '_';
384: }
385: if ($this->interactive) {
386: $this->out();
387: $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
388: }
389: $options = array();
390: foreach ($prefixes as $i => $prefix) {
391: $options[] = $i + 1;
392: if ($this->interactive) {
393: $this->out($i + 1 . '. ' . $prefix);
394: }
395: }
396: $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
397: return $prefixes[$selection - 1] . '_';
398: }
399: if ($this->interactive) {
400: $this->hr();
401: $this->out(__d('cake_console', 'You need to enable %s in %s to use prefix routing.',
402: 'Configure::write(\'Routing.prefixes\', array(\'admin\'))',
403: '/app/Config/core.php'));
404: $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
405: $this->out(__d('cake_console', 'Example: %s', 'www.example.com/admin/controller'));
406: while (!$admin) {
407: $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
408: }
409: if ($this->cakeAdmin($admin) !== true) {
410: $this->out(__d('cake_console', '<error>Unable to write to</error> %s.', '/app/Config/core.php'));
411: $this->out(__d('cake_console', 'You need to enable %s in %s to use prefix routing.',
412: 'Configure::write(\'Routing.prefixes\', array(\'admin\'))',
413: '/app/Config/core.php'));
414: return $this->_stop();
415: }
416: return $admin . '_';
417: }
418: return '';
419: }
420:
421: 422: 423: 424: 425:
426: public function getOptionParser() {
427: $parser = parent::getOptionParser();
428: return $parser->description(
429: __d('cake_console', 'Generate a new CakePHP project skeleton.')
430: )->addArgument('name', array(
431: 'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
432: ))->addOption('empty', array(
433: 'boolean' => true,
434: 'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
435: ))->addOption('theme', array(
436: 'short' => 't',
437: 'help' => __d('cake_console', 'Theme to use when baking code.')
438: ))->addOption('skel', array(
439: 'default' => current(App::core('Console')) . 'Templates' . DS . 'skel',
440: '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.')
441: ));
442: }
443:
444: }
445: