|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------------+ |
|
4
|
|
|
// | This file is part of the Agavi package. | |
|
5
|
|
|
// | Copyright (c) 2005-2010 the Agavi Project. | |
|
6
|
|
|
// | | |
|
7
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
|
8
|
|
|
// | file that was distributed with this source code. You can also view the | |
|
9
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|
10
|
|
|
// | vi: set noexpandtab: | |
|
11
|
|
|
// | Local Variables: | |
|
12
|
|
|
// | indent-tabs-mode: t | |
|
13
|
|
|
// | End: | |
|
14
|
|
|
// +---------------------------------------------------------------------------+ |
|
15
|
|
|
|
|
16
|
|
|
require_once(__DIR__ . '/AgaviTask.php'); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Creates the methods to handle requests for an agavi controller. |
|
20
|
|
|
* |
|
21
|
|
|
* @package agavi |
|
22
|
|
|
* @subpackage build |
|
23
|
|
|
* |
|
24
|
|
|
* @author Felix Gilcher <[email protected]> |
|
25
|
|
|
* @copyright Authors |
|
26
|
|
|
* @copyright The Agavi Project |
|
27
|
|
|
* |
|
28
|
|
|
* @since 1.1.0 |
|
29
|
|
|
* |
|
30
|
|
|
* @version $Id$ |
|
31
|
|
|
*/ |
|
32
|
|
|
class GenerateControllerMethodsTask extends AgaviTask |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var string The property to modify. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $property = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array the list of request methods to generate handlers for |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $methods = array(); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var boolean whether the generated controller should be simple |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $isSimple = false; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string the template to use for the handler methods |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $requestMethodTemplate; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var string the template to use for the isSimple method |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $simpleMethodTemplate; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Sets the property that this task will modify. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string The property to modify. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setProperty($property) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->property = $property; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets the methods to generate code for. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string a space separated list of methodnames. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setMethods($methodNames) |
|
75
|
|
|
{ |
|
76
|
|
|
if ("" == trim($methodNames)) { |
|
77
|
|
|
$this->methods = array(); |
|
78
|
|
|
} else { |
|
79
|
|
|
$this->methods = explode(" ", $methodNames); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets if the controller should be simple. |
|
85
|
|
|
* |
|
86
|
|
|
* @param boolean true if the controller is simple. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setSimple($flag) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->isSimple = 'y' == $flag; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Sets the template to use for the request method handling methods. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string the template path |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setRequestMethodTemplate($path) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->requestMethodTemplate = $path; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Sets the template to use for the isSimple() method. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string the template path |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setSimpleMethodTemplate($path) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->simpleMethodTemplate = $path; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Executes the task. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function main() |
|
117
|
|
|
{ |
|
118
|
|
|
if($this->property === null) { |
|
119
|
|
|
throw new \Agavi\Build\Exception\BuildException('The property attribute must be specified'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
if(count($this->methods) > 0 && $this->isSimple) { |
|
123
|
|
|
throw new \Agavi\Build\Exception\BuildException('A controller cannot serve request methods and be simple at the same time.'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if($this->requestMethodTemplate === null || !is_readable($this->requestMethodTemplate)) { |
|
127
|
|
|
throw new \Agavi\Build\Exception\BuildException( |
|
128
|
|
|
sprintf( |
|
129
|
|
|
'The requestMethodTemplate attribute must be specified and must point to a readable template file. Current value is "%1$s".', |
|
130
|
|
|
$this->requestMethodTemplate |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if($this->simpleMethodTemplate === null || !is_readable($this->simpleMethodTemplate)) { |
|
136
|
|
|
throw new \Agavi\Build\Exception\BuildException( |
|
137
|
|
|
sprintf( |
|
138
|
|
|
'The simpleMethodTemplate attribute must be specified and must point to a readable template file. Current value is "%1$s".', |
|
139
|
|
|
$this->simpleMethodTemplate |
|
140
|
|
|
) |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$template = file_get_contents($this->requestMethodTemplate); |
|
145
|
|
|
|
|
146
|
|
|
$methodDeclarations = ''; |
|
147
|
|
|
foreach($this->methods as $methodName) { |
|
148
|
|
|
$methodDeclarations .= str_replace('%%METHOD_NAME%%', ucfirst($methodName), $template); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if($this->isSimple) { |
|
152
|
|
|
$methodDeclarations .= file_get_contents($this->simpleMethodTemplate); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$this->project->setUserProperty($this->property, $methodDeclarations); |
|
156
|
|
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
?> |
|
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.