source: Dev/branches/cakephp/cake/console/libs/tasks/project.php @ 126

Last change on this file since 126 was 126, checked in by fpvanagthoven, 14 years ago

Cakephp branch.

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