統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Console / Command / Task / ProjectTask.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (14.805 KB)

1 635eef61 spyder1211
<?php
2
/**
3
 * The Project Task handles creating the base application
4
 *
5
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://cakephp.org CakePHP(tm) Project
14
 * @since         CakePHP(tm) v 1.2
15
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
16
 */
17
18
App::uses('AppShell', 'Console/Command');
19
App::uses('File', 'Utility');
20
App::uses('Folder', 'Utility');
21
App::uses('CakeText', 'Utility');
22
App::uses('Security', 'Utility');
23
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
/**
32
 * configs path (used in testing).
33
 *
34
 * @var string
35
 */
36
        public $configPath = null;
37
38
/**
39
 * Checks that given project path does not already exist, and
40
 * finds the app directory in it. Then it calls bake() with that information.
41
 *
42
 * @return mixed
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
 * Checks PHP's include_path for CakePHP.
144
 *
145
 * @return bool Indicates whether or not CakePHP exists on include_path
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
 * Looks for a skeleton template of a Cake application,
159
 * and if not found asks the user for a path. When there is a path
160
 * this method will make a deep copy of the skeleton to the project directory.
161
 *
162
 * @param string $path Project path
163
 * @param string $skel Path to copy from
164
 * @param string $skip array of directories to skip when copying
165
 * @return mixed
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(CakeText::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
 * Generates the correct path to the CakePHP libs that are generating the project
231
 * and points app/console/cake.php to the right place
232
 *
233
 * @param string $path Project path.
234
 * @return bool success
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
 * Generates and writes 'Security.salt'
253
 *
254
 * @param string $path Project path
255
 * @return bool Success
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
 * Generates and writes 'Security.cipherSeed'
273
 *
274
 * @param string $path Project path
275
 * @return bool Success
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
 * Writes cache prefix using app's name
294
 *
295
 * @param string $dir Path to project
296
 * @return bool Success
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
 * Generates and writes CAKE_CORE_INCLUDE_PATH
311
 *
312
 * @param string $path Project path
313
 * @param bool $hardCode Whether or not define calls should be hardcoded.
314
 * @return bool Success
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
 * Replaces the __CAKE_PATH__ placeholder in the template files.
332
 *
333
 * @param string $filename The filename to operate on.
334
 * @param bool $hardCode Whether or not the define should be uncommented.
335
 * @return bool Success
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
                $composer = ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib';
344
                if (file_exists($composer)) {
345
                        $corePath = " ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib'";
346
                }
347
348
                $result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
349
                if ($hardCode) {
350
                        $result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
351
                }
352
                if (!file_put_contents($filename, $result)) {
353
                        return false;
354
                }
355
                return (bool)$count;
356
        }
357
358
/**
359
 * Enables Configure::read('Routing.prefixes') in /app/Config/core.php
360
 *
361
 * @param string $name Name to use as admin routing
362
 * @return bool Success
363
 */
364
        public function cakeAdmin($name) {
365
                $path = (empty($this->configPath)) ? APP . 'Config' . DS : $this->configPath;
366
                $File = new File($path . 'core.php');
367
                $contents = $File->read();
368
                if (preg_match('%(\s*[/]*Configure::write\(\'Routing.prefixes\',[\s\'a-z,\)\(]*\);)%', $contents, $match)) {
369
                        $result = str_replace($match[0], "\n" . 'Configure::write(\'Routing.prefixes\', array(\'' . $name . '\'));', $contents);
370
                        if ($File->write($result)) {
371
                                Configure::write('Routing.prefixes', array($name));
372
                                return true;
373
                        }
374
                }
375
                return false;
376
        }
377
378
/**
379
 * Checks for Configure::read('Routing.prefixes') and forces user to input it if not enabled
380
 *
381
 * @return string Admin route to use
382
 */
383
        public function getPrefix() {
384
                $admin = '';
385
                $prefixes = Configure::read('Routing.prefixes');
386
                if (!empty($prefixes)) {
387
                        if (count($prefixes) === 1) {
388
                                return $prefixes[0] . '_';
389
                        }
390
                        if ($this->interactive) {
391
                                $this->out();
392
                                $this->out(__d('cake_console', 'You have more than one routing prefix configured'));
393
                        }
394
                        $options = array();
395
                        foreach ($prefixes as $i => $prefix) {
396
                                $options[] = $i + 1;
397
                                if ($this->interactive) {
398
                                        $this->out($i + 1 . '. ' . $prefix);
399
                                }
400
                        }
401
                        $selection = $this->in(__d('cake_console', 'Please choose a prefix to bake with.'), $options, 1);
402
                        return $prefixes[$selection - 1] . '_';
403
                }
404
                if ($this->interactive) {
405
                        $this->hr();
406
                        $this->out(__d('cake_console', 'You need to enable %s in %s to use prefix routing.',
407
                                        'Configure::write(\'Routing.prefixes\', array(\'admin\'))',
408
                                        '/app/Config/core.php'));
409
                        $this->out(__d('cake_console', 'What would you like the prefix route to be?'));
410
                        $this->out(__d('cake_console', 'Example: %s', 'www.example.com/admin/controller'));
411
                        while (!$admin) {
412
                                $admin = $this->in(__d('cake_console', 'Enter a routing prefix:'), null, 'admin');
413
                        }
414
                        if ($this->cakeAdmin($admin) !== true) {
415
                                $this->out(__d('cake_console', '<error>Unable to write to</error> %s.', '/app/Config/core.php'));
416
                                $this->out(__d('cake_console', 'You need to enable %s in %s to use prefix routing.',
417
                                        'Configure::write(\'Routing.prefixes\', array(\'admin\'))',
418
                                        '/app/Config/core.php'));
419
                                return $this->_stop();
420
                        }
421
                        return $admin . '_';
422
                }
423
                return '';
424
        }
425
426
/**
427
 * Gets the option parser instance and configures it.
428
 *
429
 * @return ConsoleOptionParser
430
 */
431
        public function getOptionParser() {
432
                $parser = parent::getOptionParser();
433
434
                $parser->description(
435
                        __d('cake_console', 'Generate a new CakePHP project skeleton.')
436
                )->addArgument('name', array(
437
                        'help' => __d('cake_console', 'Application directory to make, if it starts with "/" the path is absolute.')
438
                ))->addOption('empty', array(
439
                        'boolean' => true,
440
                        'help' => __d('cake_console', 'Create empty files in each of the directories. Good if you are using git')
441
                ))->addOption('theme', array(
442
                        'short' => 't',
443
                        'help' => __d('cake_console', 'Theme to use when baking code.')
444
                ))->addOption('skel', array(
445
                        'default' => current(App::core('Console')) . 'Templates' . DS . 'skel',
446
                        'help' => __d('cake_console', 'The directory layout to use for the new application skeleton.' .
447
                                ' Defaults to cake/Console/Templates/skel of CakePHP used to create the project.')
448
                ));
449
450
                return $parser;
451
        }
452
453
}