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: api.php 7945 2008-12-19 02:16:01Z gwoo $ */
3
/**
4
 * API shell to get CakePHP core method signatures.
5
 *
6
 * Implementation of a Cake Shell to show CakePHP core method signatures.
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.console.libs
21
 * @since         CakePHP(tm) v 1.2.0.5012
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
 
28
/**
29
 * API shell to show method signatures of CakePHP core classes.
30
 *
31
 * @package       cake
32
 * @subpackage    cake.cake.console.libs
33
 */
34
class ApiShell extends Shell {
35
/**
36
 * Map between short name for paths and real paths.
37
 *
38
 * @var array
39
 * @access public
40
 */
41
	var $paths = array();
42
/**
43
 * Override intialize of the Shell
44
 *
45
 * @access public
46
 */
47
	function initialize () {
48
		$this->paths = array_merge($this->paths, array(
49
			'behavior' => LIBS . 'model' . DS . 'behaviors' . DS,
50
			'cache' => LIBS . 'cache' . DS,
51
			'controller' => LIBS . 'controller' . DS,
52
			'component' => LIBS . 'controller' . DS . 'components' . DS,
53
			'helper' => LIBS . 'view' . DS . 'helpers' . DS,
54
			'model' => LIBS . 'model' . DS,
55
			'view' => LIBS . 'view' . DS,
56
			'core' => LIBS
57
		));
58
	}
59
/**
60
 * Override main() to handle action
61
 *
62
 * @access public
63
 */
64
	function main() {
65
		if (empty($this->args)) {
66
			return $this->help();
67
		}
68
 
69
		$type = low($this->args[0]);
70
 
71
		if (isset($this->paths[$type])) {
72
			$path = $this->paths[$type];
73
		} else {
74
			$path = $this->paths['core'];
75
		}
76
 
77
		if (count($this->args) == 1) {
78
			$file = $type;
79
			$class = Inflector::camelize($type);
80
		} elseif (count($this->args) > 1) {
81
			$file = Inflector::underscore($this->args[1]);
82
			$class = Inflector::camelize($file);
83
		}
84
 
85
		$objects = Configure::listObjects('class', $path);
86
		if (in_array($class, $objects)) {
87
			if (in_array($type, array('behavior', 'component', 'helper')) && $type !== $file) {
88
				if (!preg_match('/' . Inflector::camelize($type) . '$/', $class)) {
89
					$class .= Inflector::camelize($type);
90
				}
91
			}
92
 
93
		} else {
94
			$this->err(sprintf(__("%s not found", true), $class));
95
			$this->_stop();
96
		}
97
 
98
		$parsed = $this->__parseClass($path . $file .'.php');
99
 
100
		if (!empty($parsed)) {
101
			if (isset($this->params['m'])) {
102
				if (!isset($parsed[$this->params['m']])) {
103
					$this->err(sprintf(__("%s::%s() could not be found", true), $class, $this->params['m']));
104
					$this->_stop();
105
				}
106
				$method = $parsed[$this->params['m']];
107
				$this->out($class .'::'.$method['method'] . $method['parameters']);
108
				$this->hr();
109
				$this->out($method['comment'], true);
110
			} else {
111
				$this->out(ucwords($class));
112
				$this->hr();
113
				$i = 0;
114
				foreach ($parsed as $method) {
115
					$list[] = ++$i . ". " . $method['method'] . $method['parameters'];
116
				}
117
				$this->out($list);
118
 
119
				$methods = array_keys($parsed);
120
				while ($number = $this->in(__('Select a number to see the more information about a specific method. q to quit. l to list.', true), null, 'q')) {
121
					if ($number === 'q') {
122
						$this->out(__('Done', true));
123
						$this->_stop();
124
					}
125
 
126
					if ($number === 'l') {
127
						$this->out($list);
128
					}
129
 
130
					if (isset($methods[--$number])) {
131
						$method = $parsed[$methods[$number]];
132
						$this->hr();
133
						$this->out($class .'::'.$method['method'] . $method['parameters']);
134
						$this->hr();
135
						$this->out($method['comment'], true);
136
					}
137
				}
138
			}
139
		}
140
	}
141
 
142
/**
143
 * Show help for this shell.
144
 *
145
 * @access public
146
 */
147
	function help() {
148
		$head  = "Usage: cake api [<type>] <className> [-m <method>]\n";
149
		$head .= "-----------------------------------------------\n";
150
		$head .= "Parameters:\n\n";
151
 
152
		$commands = array(
153
			'path' => "\t<type>\n" .
154
						"\t\tEither a full path or type of class (model, behavior, controller, component, view, helper).\n".
155
						"\t\tAvailable values:\n\n".
156
						"\t\tbehavior\tLook for class in CakePHP behavior path\n".
157
						"\t\tcache\tLook for class in CakePHP cache path\n".
158
						"\t\tcontroller\tLook for class in CakePHP controller path\n".
159
						"\t\tcomponent\tLook for class in CakePHP component path\n".
160
						"\t\thelper\tLook for class in CakePHP helper path\n".
161
						"\t\tmodel\tLook for class in CakePHP model path\n".
162
						"\t\tview\tLook for class in CakePHP view path\n",
163
			'className' => "\t<className>\n" .
164
						"\t\tA CakePHP core class name (e.g: Component, HtmlHelper).\n"
165
		);
166
 
167
		$this->out($head);
168
		if (!isset($this->args[1])) {
169
			foreach ($commands as $cmd) {
170
				$this->out("{$cmd}\n\n");
171
			}
172
		} elseif (isset($commands[low($this->args[1])])) {
173
			$this->out($commands[low($this->args[1])] . "\n\n");
174
		} else {
175
			$this->out("Command '" . $this->args[1] . "' not found");
176
		}
177
	}
178
 
179
/**
180
 * Parse a given class (located on given file) and get public methods and their
181
 * signatures.
182
 *
183
 * @param object $File File object
184
 * @param string $class Class name
185
 * @return array Methods and signatures indexed by method name
186
 * @access private
187
 */
188
	function __parseClass($path) {
189
		$parsed = array();
190
 
191
		$File = new File($path);
192
		if (!$File->exists()) {
193
			$this->err(sprintf(__("%s could not be found", true), $File->name));
194
			$this->_stop();
195
		}
196
 
197
		$contents = $File->read();
198
 
199
		if (preg_match_all('%(/\\*\\*[\\s\\S]*?\\*/)(\\s+function\\s+\\w+)(\\(.+\\))%', $contents, $result, PREG_PATTERN_ORDER)) {
200
			foreach ($result[2] as $key => $method) {
201
				$method = str_replace('function ', '', trim($method));
202
 
203
				if (strpos($method, '__') === false && $method[0] != '_') {
204
					$parsed[$method] = array(
205
											'comment' => r(array('/*', '*/', '*'), '', trim($result[1][$key])),
206
											'method' => $method,
207
											'parameters' => trim($result[3][$key]),
208
											);
209
				}
210
			}
211
		}
212
		ksort($parsed);
213
		return $parsed;
214
	}
215
}
216
?>