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