Subversion-Projekte lars-tiefland.cakephp

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* SVN FILE: $Id: project.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * The Project Task handles creating the base application
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
11
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
12
 *
13
 * Licensed under The MIT License
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @filesource
17
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
18
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
19
 * @package       cake
20
 * @subpackage    cake.cake.scripts.bake
21
 * @since         CakePHP(tm) v 1.2
22
 * @version       $Revision: 7945 $
23
 * @modifiedby    $LastChangedBy: gwoo $
24
 * @lastmodified  $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
25
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
26
 */
27
if (!class_exists('File')) {
28
	uses('file');
29
}
30
/**
31
 * Task class for creating new project apps and plugins
32
 *
33
 * @package       cake
34
 * @subpackage    cake.cake.console.libs.tasks
35
 */
36
class ProjectTask extends Shell {
37
/**
38
 * Checks that given project path does not already exist, and
39
 * finds the app directory in it. Then it calls bake() with that information.
40
 *
41
 * @param string $project Project path
42
 * @access public
43
 */
44
	function execute($project = null) {
45
		if ($project === null) {
46
			if (isset($this->args[0])) {
47
				$project = $this->args[0];
48
				$this->Dispatch->shiftArgs();
49
			}
50
		}
51
 
52
		if ($project) {
53
			$this->Dispatch->parseParams(array('-app', $project));
54
			$project = $this->params['working'];
55
		}
56
 
57
		if (empty($this->params['skel'])) {
58
			$this->params['skel'] = '';
59
			if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
60
				$this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
61
			}
62
		}
63
 
64
		while (!$project) {
65
			$project = $this->in("What is the full path for this app including the app directory name?\nExample: ".$this->params['working'] . DS . "myapp", null, $this->params['working'] . DS . 'myapp');
66
		}
67
 
68
		if ($project) {
69
			$response = false;
70
			while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
71
				$response = $this->in('A project already exists in this location: '.$project.' Overwrite?', array('y','n'), 'n');
72
				if (strtolower($response) === 'n') {
73
					$response = $project = false;
74
				}
75
			}
76
		}
77
 
78
		if ($this->bake($project)) {
79
			$path = Folder::slashTerm($project);
80
			if ($this->createHome($path)) {
81
				$this->out(__('Welcome page created', true));
82
			} else {
83
				$this->out(__('The Welcome page was NOT created', true));
84
			}
85
 
86
			if ($this->securitySalt($path) === true ) {
87
				$this->out(__('Random hash key created for \'Security.salt\'', true));
88
			} else {
89
				$this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
90
			}
91
 
92
			$corePath = $this->corePath($path);
93
			if ($corePath === true ) {
94
				$this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
95
				$this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', true), CAKE_CORE_INCLUDE_PATH));
96
				$this->out(__('Remember to check these value after moving to production server', true));
97
			} elseif ($corePath === false) {
98
				$this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' .DS .'index.php'));
99
			}
100
			$Folder = new Folder($path);
101
			if (!$Folder->chmod($path . 'tmp', 0777)) {
102
				$this->err(sprintf(__('Could not set permissions on %s', true), $path . DS .'tmp'));
103
				$this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS .'tmp'));
104
			}
105
 
106
			$this->params['working'] = $path;
107
			$this->params['app'] = basename($path);
108
			return true;
109
		}
110
	}
111
/**
112
 * Looks for a skeleton template of a Cake application,
113
 * and if not found asks the user for a path. When there is a path
114
 * this method will make a deep copy of the skeleton to the project directory.
115
 * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
116
 *
117
 * @param string $path Project path
118
 * @param string $skel Path to copy from
119
 * @param string $skip array of directories to skip when copying
120
 * @access private
121
 */
122
	function bake($path, $skel = null, $skip = array('empty')) {
123
		if (!$skel) {
124
			$skel = $this->params['skel'];
125
		}
126
 
127
		while (!$skel) {
128
			$skel = $this->in(sprintf(__("What is the path to the directory layout you wish to copy?\nExample: %s"), APP, null, ROOT . DS . 'myapp' . DS));
129
			if ($skel == '') {
130
				$this->out(__('The directory path you supplied was empty. Please try again.', true));
131
			} else {
132
				while (is_dir($skel) === false) {
133
					$skel = $this->in(__('Directory path does not exist please choose another:', true));
134
				}
135
			}
136
		}
137
 
138
		$app = basename($path);
139
 
140
		$this->out('Bake Project');
141
		$this->out("Skel Directory: $skel");
142
		$this->out("Will be copied to: {$path}");
143
		$this->hr();
144
 
145
		$looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
146
 
147
		if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
148
			$verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
149
 
150
			$Folder = new Folder($skel);
151
			if ($Folder->copy(array('to' => $path, 'skip' => $skip))) {
152
				$this->hr();
153
				$this->out(sprintf(__("Created: %s in %s", true), $app, $path));
154
				$this->hr();
155
			} else {
156
				$this->err(" '".$app."' could not be created properly");
157
				return false;
158
			}
159
 
160
			if (low($verbose) == 'y' || low($verbose) == 'yes') {
161
				foreach ($Folder->messages() as $message) {
162
					$this->out($message);
163
				}
164
			}
165
 
166
			return true;
167
		} elseif (low($looksGood) == 'q' || low($looksGood) == 'quit') {
168
			$this->out('Bake Aborted.');
169
		} else {
170
			$this->execute(false);
171
			return false;
172
		}
173
	}
174
/**
175
 * Writes a file with a default home page to the project.
176
 *
177
 * @param string $dir Path to project
178
 * @return boolean Success
179
 * @access public
180
 */
181
	function createHome($dir) {
182
		$app = basename($dir);
183
		$path = $dir . 'views' . DS . 'pages' . DS;
184
		include(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'views'.DS.'home.ctp');
185
		return $this->createFile($path.'home.ctp', $output);
186
	}
187
/**
188
 * Generates and writes 'Security.salt'
189
 *
190
 * @param string $path Project path
191
 * @return boolean Success
192
 * @access public
193
 */
194
	function securitySalt($path) {
195
		$File =& new File($path . 'config' . DS . 'core.php');
196
		$contents = $File->read();
197
		if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
198
			if (!class_exists('Security')) {
199
				uses('Security');
200
			}
201
			$string = Security::generateAuthKey();
202
			$result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents);
203
			if ($File->write($result)) {
204
				return true;
205
			}
206
			return false;
207
		}
208
		return false;
209
	}
210
/**
211
 * Generates and writes CAKE_CORE_INCLUDE_PATH
212
 *
213
 * @param string $path Project path
214
 * @return boolean Success
215
 * @access public
216
 */
217
	function corePath($path) {
218
		if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
219
			$File =& new File($path . 'webroot' . DS . 'index.php');
220
			$contents = $File->read();
221
			if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
222
				$result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '".CAKE_CORE_INCLUDE_PATH."');", $contents);
223
				if (!$File->write($result)) {
224
					return false;
225
				}
226
			} else {
227
				return false;
228
			}
229
 
230
			$File =& new File($path . 'webroot' . DS . 'test.php');
231
			$contents = $File->read();
232
			if (preg_match('/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) {
233
				$result = str_replace($match[0], "\t\tdefine('CAKE_CORE_INCLUDE_PATH', '".CAKE_CORE_INCLUDE_PATH."');", $contents);
234
				if (!$File->write($result)) {
235
					return false;
236
				}
237
			} else {
238
				return false;
239
			}
240
			return true;
241
		}
242
	}
243
/**
244
 * Enables Configure::read('Routing.admin') in /app/config/core.php
245
 *
246
 * @param string $name Name to use as admin routing
247
 * @return boolean Success
248
 * @access public
249
 */
250
	function cakeAdmin($name) {
251
		$File =& new File(CONFIGS . 'core.php');
252
		$contents = $File->read();
253
		if (preg_match('%([/\\t\\x20]*Configure::write\(\'Routing.admin\',[\\t\\x20\'a-z]*\\);)%', $contents, $match)) {
254
			$result = str_replace($match[0], "\t" . 'Configure::write(\'Routing.admin\', \''.$name.'\');', $contents);
255
			if ($File->write($result)) {
256
				Configure::write('Routing.admin', $name);
257
				return true;
258
			} else {
259
				return false;
260
			}
261
		} else {
262
			return false;
263
		}
264
	}
265
/**
266
 * Help
267
 *
268
 * @return void
269
 * @access public
270
 */
271
	function help() {
272
		$this->hr();
273
		$this->out("Usage: cake bake project <arg1>");
274
		$this->hr();
275
		$this->out('Commands:');
276
		$this->out("\n\tproject <name>\n\t\tbakes app directory structure.\n\t\tif <name> begins with '/' path is absolute.");
277
		$this->out("");
278
		$this->_stop();
279
	}
280
 
281
}
282
?>