|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Tool |
|
17 * @subpackage Framework |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: ControllerFile.php 23204 2010-10-21 15:35:21Z ralph $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * This class is the front most class for utilizing Zend_Tool_Project |
|
25 * |
|
26 * A profile is a hierarchical set of resources that keep track of |
|
27 * items within a specific project. |
|
28 * |
|
29 * @category Zend |
|
30 * @package Zend_Tool |
|
31 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
33 */ |
|
34 class Zend_Tool_Project_Context_Zf_ControllerFile extends Zend_Tool_Project_Context_Filesystem_File |
|
35 { |
|
36 |
|
37 /** |
|
38 * @var string |
|
39 */ |
|
40 protected $_controllerName = 'index'; |
|
41 |
|
42 /** |
|
43 * @var string |
|
44 */ |
|
45 protected $_moduleName = null; |
|
46 |
|
47 /** |
|
48 * @var string |
|
49 */ |
|
50 protected $_filesystemName = 'controllerName'; |
|
51 |
|
52 /** |
|
53 * init() |
|
54 * |
|
55 */ |
|
56 public function init() |
|
57 { |
|
58 $this->_controllerName = $this->_resource->getAttribute('controllerName'); |
|
59 $this->_moduleName = $this->_resource->getAttribute('moduleName'); |
|
60 $this->_filesystemName = ucfirst($this->_controllerName) . 'Controller.php'; |
|
61 parent::init(); |
|
62 } |
|
63 |
|
64 /** |
|
65 * getPersistentAttributes |
|
66 * |
|
67 * @return array |
|
68 */ |
|
69 public function getPersistentAttributes() |
|
70 { |
|
71 return array( |
|
72 'controllerName' => $this->getControllerName() |
|
73 ); |
|
74 } |
|
75 |
|
76 /** |
|
77 * getName() |
|
78 * |
|
79 * @return string |
|
80 */ |
|
81 public function getName() |
|
82 { |
|
83 return 'ControllerFile'; |
|
84 } |
|
85 |
|
86 /** |
|
87 * getControllerName() |
|
88 * |
|
89 * @return string |
|
90 */ |
|
91 public function getControllerName() |
|
92 { |
|
93 return $this->_controllerName; |
|
94 } |
|
95 |
|
96 /** |
|
97 * getContents() |
|
98 * |
|
99 * @return string |
|
100 */ |
|
101 public function getContents() |
|
102 { |
|
103 $className = ($this->_moduleName) ? ucfirst($this->_moduleName) . '_' : ''; |
|
104 $className .= ucfirst($this->_controllerName) . 'Controller'; |
|
105 |
|
106 $codeGenFile = new Zend_CodeGenerator_Php_File(array( |
|
107 'fileName' => $this->getPath(), |
|
108 'classes' => array( |
|
109 new Zend_CodeGenerator_Php_Class(array( |
|
110 'name' => $className, |
|
111 'extendedClass' => 'Zend_Controller_Action', |
|
112 'methods' => array( |
|
113 new Zend_CodeGenerator_Php_Method(array( |
|
114 'name' => 'init', |
|
115 'body' => '/* Initialize action controller here */', |
|
116 )) |
|
117 ) |
|
118 )) |
|
119 ) |
|
120 )); |
|
121 |
|
122 |
|
123 if ($className == 'ErrorController') { |
|
124 |
|
125 $codeGenFile = new Zend_CodeGenerator_Php_File(array( |
|
126 'fileName' => $this->getPath(), |
|
127 'classes' => array( |
|
128 new Zend_CodeGenerator_Php_Class(array( |
|
129 'name' => $className, |
|
130 'extendedClass' => 'Zend_Controller_Action', |
|
131 'methods' => array( |
|
132 new Zend_CodeGenerator_Php_Method(array( |
|
133 'name' => 'errorAction', |
|
134 'body' => <<<EOS |
|
135 \$errors = \$this->_getParam('error_handler'); |
|
136 |
|
137 if (!\$errors) { |
|
138 \$this->view->message = 'You have reached the error page'; |
|
139 return; |
|
140 } |
|
141 |
|
142 switch (\$errors->type) { |
|
143 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: |
|
144 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: |
|
145 case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: |
|
146 |
|
147 // 404 error -- controller or action not found |
|
148 \$this->getResponse()->setHttpResponseCode(404); |
|
149 \$this->view->message = 'Page not found'; |
|
150 break; |
|
151 default: |
|
152 // application error |
|
153 \$this->getResponse()->setHttpResponseCode(500); |
|
154 \$this->view->message = 'Application error'; |
|
155 break; |
|
156 } |
|
157 |
|
158 // Log exception, if logger available |
|
159 if (\$log = \$this->getLog()) { |
|
160 \$log->crit(\$this->view->message, \$errors->exception); |
|
161 } |
|
162 |
|
163 // conditionally display exceptions |
|
164 if (\$this->getInvokeArg('displayExceptions') == true) { |
|
165 \$this->view->exception = \$errors->exception; |
|
166 } |
|
167 |
|
168 \$this->view->request = \$errors->request; |
|
169 EOS |
|
170 )), |
|
171 new Zend_CodeGenerator_Php_Method(array( |
|
172 'name' => 'getLog', |
|
173 'body' => <<<EOS |
|
174 \$bootstrap = \$this->getInvokeArg('bootstrap'); |
|
175 if (!\$bootstrap->hasResource('Log')) { |
|
176 return false; |
|
177 } |
|
178 \$log = \$bootstrap->getResource('Log'); |
|
179 return \$log; |
|
180 EOS |
|
181 )), |
|
182 ) |
|
183 )) |
|
184 ) |
|
185 )); |
|
186 |
|
187 } |
|
188 |
|
189 // store the generator into the registry so that the addAction command can use the same object later |
|
190 Zend_CodeGenerator_Php_File::registerFileCodeGenerator($codeGenFile); // REQUIRES filename to be set |
|
191 return $codeGenFile->generate(); |
|
192 } |
|
193 |
|
194 /** |
|
195 * addAction() |
|
196 * |
|
197 * @param string $actionName |
|
198 */ |
|
199 public function addAction($actionName) |
|
200 { |
|
201 $classCodeGen = $this->getCodeGenerator(); |
|
202 $classCodeGen->setMethod(array('name' => $actionName . 'Action', 'body' => ' // action body here')); |
|
203 file_put_contents($this->getPath(), $classCodeGen->generate()); |
|
204 } |
|
205 |
|
206 /** |
|
207 * getCodeGenerator() |
|
208 * |
|
209 * @return Zend_CodeGenerator_Php_Class |
|
210 */ |
|
211 public function getCodeGenerator() |
|
212 { |
|
213 $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName($this->getPath()); |
|
214 $codeGenFileClasses = $codeGenFile->getClasses(); |
|
215 $class = array_shift($codeGenFileClasses); |
|
216 return $class; |
|
217 } |
|
218 |
|
219 } |