1
1
<?php
2
-
3
2
namespace Ubiquity \scaffolding ;
4
3
5
4
use Ubiquity \controllers \Startup ;
23
22
*
24
23
*/
25
24
abstract class ScaffoldController {
25
+
26
26
protected $ config ;
27
+
27
28
public static $ views = [
28
- "CRUD " => [ "index " => "@framework/crud/index.html " ,"form " => "@framework/crud/form.html " ,"display " => "@framework/crud/display.html " ],
29
- "auth " => [ "index " => "@framework/auth/index.html " ,"info " => "@framework/auth/info.html " ,"noAccess " => "@framework/auth/noAccess.html " ,"disconnected " => "@framework/auth/disconnected.html " ,"message " => "@framework/auth/message.html " ,"baseTemplate " => "@framework/auth/baseTemplate.html " ] ];
29
+ "CRUD " => [
30
+ "index " => "@framework/crud/index.html " ,
31
+ "form " => "@framework/crud/form.html " ,
32
+ "display " => "@framework/crud/display.html "
33
+ ],
34
+ "auth " => [
35
+ "index " => "@framework/auth/index.html " ,
36
+ "info " => "@framework/auth/info.html " ,
37
+ "noAccess " => "@framework/auth/noAccess.html " ,
38
+ "disconnected " => "@framework/auth/disconnected.html " ,
39
+ "message " => "@framework/auth/message.html " ,
40
+ "baseTemplate " => "@framework/auth/baseTemplate.html "
41
+ ]
42
+ ];
30
43
31
44
public function getTemplateDir () {
32
45
return \dirname (__DIR__ ) . "/scaffolding/templates/ " ;
33
46
}
34
47
35
- public function _refreshRest ($ refresh = false ) {
36
- }
48
+ public function _refreshRest ($ refresh = false ) {}
37
49
38
- public function initRestCache ($ refresh = true ) {
39
- }
50
+ public function initRestCache ($ refresh = true ) {}
40
51
41
52
protected abstract function storeControllerNameInSession ($ controller );
42
53
@@ -45,19 +56,26 @@ public abstract function showSimpleMessage($content, $type, $title = null, $icon
45
56
protected abstract function _addMessageForRouteCreation ($ path , $ jsCallback = "" );
46
57
47
58
public function _createMethod ($ access , $ name , $ parameters = "" , $ return = "" , $ content = "" , $ comment = "" ) {
48
- $ templateDir = $ this ->getTemplateDir ();
49
- $ keyAndValues = [ "%access% " => $ access ,"%name% " => $ name ,"%parameters% " => $ parameters ,"%content% " => $ content ,"%comment% " => $ comment ,"%return% " => $ return ];
50
- return UFileSystem::openReplaceInTemplateFile ( $ templateDir . "method.tpl " , $ keyAndValues );
59
+ $ templateDir = $ this ->getTemplateDir ();
60
+ $ keyAndValues = [
61
+ "%access% " => $ access ,
62
+ "%name% " => $ name ,
63
+ "%parameters% " => $ parameters ,
64
+ "%content% " => $ content ,
65
+ "%comment% " => $ comment ,
66
+ "%return% " => $ return
67
+ ];
68
+ return UFileSystem::openReplaceInTemplateFile ($ templateDir . "method.tpl " , $ keyAndValues );
51
69
}
52
70
53
71
public function _createController ($ controllerName , $ variables = [], $ ctrlTemplate = 'controller.tpl ' , $ hasView = false , $ jsCallback = "" ) {
54
72
$ message = "" ;
55
- $ templateDir = $ this ->getTemplateDir ();
56
- $ controllersNS = \rtrim ( Startup::getNS ( "controllers " ), "\\" );
57
- $ controllersDir = \ROOT . \DS . str_replace ( "\\" , \DS , $ controllersNS );
58
- $ controllerName = \ucfirst ( $ controllerName );
73
+ $ templateDir = $ this ->getTemplateDir ();
74
+ $ controllersNS = \rtrim ( Startup::getNS ( "controllers " ), "\\" );
75
+ $ controllersDir = \ROOT . \DS . str_replace ( "\\" , \DS , $ controllersNS );
76
+ $ controllerName = \ucfirst ( $ controllerName );
59
77
$ filename = $ controllersDir . \DS . $ controllerName . ".php " ;
60
- if (\file_exists ( $ filename ) === false ) {
78
+ if (\file_exists ( $ filename ) === false ) {
61
79
$ namespace = "" ;
62
80
if ($ controllersNS !== "" ) {
63
81
$ namespace = "namespace " . $ controllersNS . "; " ;
@@ -66,165 +84,199 @@ public function _createController($controllerName, $variables = [], $ctrlTemplat
66
84
$ indexContent = "" ;
67
85
if ($ hasView ) {
68
86
$ viewDir = \ROOT . \DS . "views " . \DS . $ controllerName . \DS ;
69
- UFileSystem::safeMkdir ( $ viewDir );
87
+ UFileSystem::safeMkdir ( $ viewDir );
70
88
$ viewName = $ viewDir . \DS . "index.html " ;
71
- UFileSystem::openReplaceWriteFromTemplateFile ( $ templateDir . "view.tpl " , $ viewName , [ "%controllerName% " => $ controllerName ,"%actionName% " => "index " ] );
72
- $ msgView = "<br>The default view associated has been created in <b> " . UFileSystem::cleanPathname ( \ROOT . \DS . $ viewDir ) . "</b> " ;
89
+ UFileSystem::openReplaceWriteFromTemplateFile ($ templateDir . "view.tpl " , $ viewName , [
90
+ "%controllerName% " => $ controllerName ,
91
+ "%actionName% " => "index "
92
+ ]);
93
+ $ msgView = "<br>The default view associated has been created in <b> " . UFileSystem::cleanPathname (\ROOT . \DS . $ viewDir ) . "</b> " ;
73
94
$ indexContent = "\$this->loadView( \"" . $ controllerName . "/index.html \"); " ;
74
95
}
75
- $ variables = \array_merge ( $ variables , [ "%controllerName% " => $ controllerName ,"%indexContent% " => $ indexContent ,"%namespace% " => $ namespace ] );
76
- UFileSystem::openReplaceWriteFromTemplateFile ( $ templateDir . $ ctrlTemplate , $ filename , $ variables );
77
- $ msgContent = "The <b> " . $ controllerName . "</b> controller has been created in <b> " . UFileSystem::cleanFilePathname ( $ filename ) . "</b>. " . $ msgView ;
78
- if (isset ( $ variables ["%path% " ] ) && $ variables ["%path% " ] !== "" ) {
79
- $ msgContent .= $ this ->_addMessageForRouteCreation ( $ variables ["%path% " ], $ jsCallback );
96
+ $ variables = \array_merge ($ variables , [
97
+ "%controllerName% " => $ controllerName ,
98
+ "%indexContent% " => $ indexContent ,
99
+ "%namespace% " => $ namespace
100
+ ]);
101
+ UFileSystem::openReplaceWriteFromTemplateFile ($ templateDir . $ ctrlTemplate , $ filename , $ variables );
102
+ $ msgContent = "The <b> " . $ controllerName . "</b> controller has been created in <b> " . UFileSystem::cleanFilePathname ($ filename ) . "</b>. " . $ msgView ;
103
+ if (isset ($ variables ["%path% " ]) && $ variables ["%path% " ] !== "" ) {
104
+ $ msgContent .= $ this ->_addMessageForRouteCreation ($ variables ["%path% " ], $ jsCallback );
80
105
}
81
- $ this ->storeControllerNameInSession ( $ controllersNS . "\\" . $ controllerName );
82
- $ message = $ this ->showSimpleMessage ( $ msgContent , "success " , null , "checkmark circle " , NULL , "msgGlobal " );
106
+ $ this ->storeControllerNameInSession ( $ controllersNS . "\\" . $ controllerName );
107
+ $ message = $ this ->showSimpleMessage ( $ msgContent , "success " , null , "checkmark circle " , NULL , "msgGlobal " );
83
108
} else {
84
- $ message = $ this ->showSimpleMessage ( "The file <b> " . $ filename . "</b> already exists.<br>Can not create the <b> " . $ controllerName . "</b> controller! " , "warning " , null , "warning circle " , 100000 , "msgGlobal " );
109
+ $ message = $ this ->showSimpleMessage ( "The file <b> " . $ filename . "</b> already exists.<br>Can not create the <b> " . $ controllerName . "</b> controller! " , "warning " , null , "warning circle " , 100000 , "msgGlobal " );
85
110
}
86
111
return $ message ;
87
112
}
88
113
89
114
public function addCrudController ($ crudControllerName , $ resource , $ crudDatas = null , $ crudViewer = null , $ crudEvents = null , $ crudViews = null , $ routePath = '' ) {
90
- $ crudController = new CrudControllerCreator ( $ crudControllerName , $ resource , $ crudDatas , $ crudViewer , $ crudEvents , $ crudViews , $ routePath );
91
- $ crudController ->create ( $ this );
115
+ $ crudController = new CrudControllerCreator ( $ crudControllerName , $ resource , $ crudDatas , $ crudViewer , $ crudEvents , $ crudViews , $ routePath );
116
+ $ crudController ->create ( $ this );
92
117
}
93
118
94
119
public function addAuthController ($ authControllerName , $ baseClass , $ authViews = null , $ routePath = "" ) {
95
- $ authCreator = new AuthControllerCreator ( $ authControllerName , $ baseClass , $ authViews , $ routePath );
96
- $ authCreator ->create ( $ this );
120
+ $ authCreator = new AuthControllerCreator ( $ authControllerName , $ baseClass , $ authViews , $ routePath );
121
+ $ authCreator ->create ( $ this );
97
122
}
98
123
99
124
public function addRestController ($ restControllerName , $ baseClass , $ resource , $ routePath = "" , $ reInit = true ) {
100
- $ restCreator = new RestControllerCreator ( $ restControllerName , $ baseClass , $ resource , $ routePath );
101
- $ restCreator ->create ( $ this , $ reInit );
125
+ $ restCreator = new RestControllerCreator ( $ restControllerName , $ baseClass , $ resource , $ routePath );
126
+ $ restCreator ->create ( $ this , $ reInit );
102
127
}
103
128
104
129
public function _createClass ($ template , $ classname , $ namespace , $ uses , $ extendsOrImplements , $ classContent ) {
105
130
$ namespaceVar = "" ;
106
- if (UString::isNotNull ( $ namespace )) {
131
+ if (UString::isNotNull ( $ namespace )) {
107
132
$ namespaceVar = "namespace {$ namespace }; " ;
108
133
}
109
- $ variables = [ "%classname% " => $ classname ,"%namespace% " => $ namespaceVar ,"%uses% " => $ uses ,"%extendsOrImplements% " => $ extendsOrImplements ,"%classContent% " => $ classContent ];
110
- $ templateDir = $ this ->getTemplateDir ();
111
- $ directory = UFileSystem::getDirFromNamespace ( $ namespace );
112
- UFileSystem::safeMkdir ( $ directory );
113
- $ filename = UFileSystem::cleanFilePathname ( $ directory . \DS . $ classname . ".php " );
114
- if (! file_exists ( $ filename )) {
115
- UFileSystem::openReplaceWriteFromTemplateFile ( $ templateDir . $ template , $ filename , $ variables );
116
- $ message = $ this ->showSimpleMessage ( "The <b> " . $ classname . "</b> class has been created in <b> " . $ filename . "</b>. " , "success " , "Creation " , "checkmark circle " );
134
+ $ variables = [
135
+ "%classname% " => $ classname ,
136
+ "%namespace% " => $ namespaceVar ,
137
+ "%uses% " => $ uses ,
138
+ "%extendsOrImplements% " => $ extendsOrImplements ,
139
+ "%classContent% " => $ classContent
140
+ ];
141
+ $ templateDir = $ this ->getTemplateDir ();
142
+ $ directory = UFileSystem::getDirFromNamespace ($ namespace );
143
+ UFileSystem::safeMkdir ($ directory );
144
+ $ filename = UFileSystem::cleanFilePathname ($ directory . \DS . $ classname . ".php " );
145
+ if (! file_exists ($ filename )) {
146
+ UFileSystem::openReplaceWriteFromTemplateFile ($ templateDir . $ template , $ filename , $ variables );
147
+ $ message = $ this ->showSimpleMessage ("The <b> " . $ classname . "</b> class has been created in <b> " . $ filename . "</b>. " , "success " , "Creation " , "checkmark circle " );
117
148
} else {
118
- $ message = $ this ->showSimpleMessage ( "The file <b> " . $ filename . "</b> already exists.<br>Can not create the <b> " . $ classname . "</b> class! " , "warning " , "Creation " , "warning circle " );
149
+ $ message = $ this ->showSimpleMessage ( "The file <b> " . $ filename . "</b> already exists.<br>Can not create the <b> " . $ classname . "</b> class! " , "warning " , "Creation " , "warning circle " );
119
150
}
120
151
return $ message ;
121
152
}
122
153
123
154
public function _newAction ($ controller , $ action , $ parameters = null , $ content = '' , $ routeInfo = null , $ createView = false , $ theme = null ) {
124
- $ templateDir = $ this ->getTemplateDir ();
155
+ $ templateDir = $ this ->getTemplateDir ();
125
156
$ msgContent = "" ;
126
- $ r = new \ReflectionClass ( $ controller );
127
- if (! method_exists ( $ controller , $ action )) {
128
- $ ctrlFilename = $ r ->getFileName ();
129
- $ content = CodeUtils::indent ( $ content , 2 );
130
- $ classCode = UIntrospection::getClassCode ( $ controller );
157
+ $ r = new \ReflectionClass ( $ controller );
158
+ if (! method_exists ( $ controller , $ action )) {
159
+ $ ctrlFilename = $ r ->getFileName ();
160
+ $ content = CodeUtils::indent ( $ content , 2 );
161
+ $ classCode = UIntrospection::getClassCode ( $ controller );
131
162
if ($ classCode !== false ) {
132
- $ fileContent = \implode ( "" , $ classCode );
133
- $ fileContent = \trim ( $ fileContent );
134
- $ posLast = \strrpos ( $ fileContent , "} " );
163
+ $ fileContent = \implode ( "" , $ classCode );
164
+ $ fileContent = \trim ( $ fileContent );
165
+ $ posLast = \strrpos ( $ fileContent , "} " );
135
166
if ($ posLast !== false ) {
136
167
if ($ createView ) {
137
- $ viewname = $ this ->_createViewOp ( ClassUtils::getClassSimpleName ( $ controller ), $ action , $ theme );
168
+ $ viewname = $ this ->_createViewOp ( ClassUtils::getClassSimpleName ( $ controller ), $ action , $ theme );
138
169
$ content .= "\n\t\t\$this->loadView(' " . $ viewname . "'); \n" ;
139
170
$ msgContent .= "<br>Created view : <b> " . $ viewname . "</b> " ;
140
171
}
141
172
$ routeAnnotation = "" ;
142
- if (is_array ( $ routeInfo )) {
173
+ if (is_array ( $ routeInfo )) {
143
174
$ name = "route " ;
144
- $ path = $ routeInfo ["path " ];
145
- $ routeProperties = [ '" ' . $ path . '" ' ];
146
- $ methods = $ routeInfo ["methods " ];
147
- if (UString::isNotNull ( $ methods )) {
148
- $ routeProperties [] = '"methods"=> ' . $ this ->getMethods ( $ methods );
175
+ $ path = $ routeInfo ["path " ];
176
+ $ routeProperties = [
177
+ '" ' . $ path . '" '
178
+ ];
179
+ $ methods = $ routeInfo ["methods " ];
180
+ if (UString::isNotNull ($ methods )) {
181
+ $ routeProperties [] = '"methods"=> ' . $ this ->getMethods ($ methods );
149
182
}
150
- if (isset ( $ routeInfo ["ck-Cache " ] )) {
151
- $ routeProperties [] = '"cache"=>true ' ;
152
- if (isset ( $ routeInfo ["duration " ] )) {
153
- $ duration = $ routeInfo ["duration " ];
154
- if (\ctype_digit ( $ duration )) {
155
- $ routeProperties [] = '"duration"=> ' . $ duration ;
183
+ if (isset ( $ routeInfo ["ck-Cache " ])) {
184
+ $ routeProperties [] = '"cache"=>true ' ;
185
+ if (isset ( $ routeInfo ["duration " ])) {
186
+ $ duration = $ routeInfo ["duration " ];
187
+ if (\ctype_digit ( $ duration )) {
188
+ $ routeProperties [] = '"duration"=> ' . $ duration ;
156
189
}
157
190
}
158
191
}
159
- $ routeProperties = \implode ( ", " , $ routeProperties );
160
- $ routeAnnotation = UFileSystem::openReplaceInTemplateFile ( $ templateDir . "annotation.tpl " , [ "%name% " => $ name ,"%properties% " => $ routeProperties ] );
192
+ $ routeProperties = \implode (", " , $ routeProperties );
193
+ $ routeAnnotation = UFileSystem::openReplaceInTemplateFile ($ templateDir . "annotation.tpl " , [
194
+ "%name% " => $ name ,
195
+ "%properties% " => $ routeProperties
196
+ ]);
161
197
162
- $ msgContent .= $ this ->_addMessageForRouteCreation ( $ path );
198
+ $ msgContent .= $ this ->_addMessageForRouteCreation ( $ path );
163
199
}
164
- $ parameters = CodeUtils::cleanParameters ( $ parameters );
165
- $ actionContent = UFileSystem::openReplaceInTemplateFile ( $ templateDir . "action.tpl " , [ "%route% " => "\n" . $ routeAnnotation ?? '' ,"%actionName% " => $ action ,"%parameters% " => $ parameters ,"%content% " => $ content ] );
166
- $ fileContent = \substr_replace ( $ fileContent , "\n%content% " , $ posLast - 1 , 0 );
167
- if (! CodeUtils::isValidCode ( '<?php ' . $ content )) {
168
- echo $ this ->showSimpleMessage ( "Errors parsing action content! " , "warning " , "Creation " , "warning circle " , null , "msgControllers " );
200
+ $ parameters = CodeUtils::cleanParameters ($ parameters );
201
+ $ actionContent = UFileSystem::openReplaceInTemplateFile ($ templateDir . "action.tpl " , [
202
+ "%route% " => "\n" . $ routeAnnotation ?? '' ,
203
+ "%actionName% " => $ action ,
204
+ "%parameters% " => $ parameters ,
205
+ "%content% " => $ content
206
+ ]);
207
+ $ fileContent = \substr_replace ($ fileContent , "\n%content% " , $ posLast - 1 , 0 );
208
+ if (! CodeUtils::isValidCode ('<?php ' . $ content )) {
209
+ echo $ this ->showSimpleMessage ("Errors parsing action content! " , "warning " , "Creation " , "warning circle " , null , "msgControllers " );
169
210
return ;
170
211
} else {
171
- if (UFileSystem::replaceWriteFromContent ( $ fileContent . "\n" , $ ctrlFilename , [ '%content% ' => $ actionContent ] )) {
212
+ if (UFileSystem::replaceWriteFromContent ($ fileContent . "\n" , $ ctrlFilename , [
213
+ '%content% ' => $ actionContent
214
+ ])) {
172
215
$ msgContent = "The action <b> {$ action }</b> is created in controller <b> {$ controller }</b> " . $ msgContent ;
173
- echo $ this ->showSimpleMessage ( $ msgContent , "success " , "Creation " , "info circle " , null , "msgControllers " );
216
+ echo $ this ->showSimpleMessage ( $ msgContent , "success " , "Creation " , "info circle " , null , "msgControllers " );
174
217
}
175
218
}
176
219
}
177
220
} else {
178
- echo $ this ->showSimpleMessage ( "The action {$ action } already exists in {$ controller }! " , "error " , "Creation " , "warning circle " , null , "msgControllers " );
221
+ echo $ this ->showSimpleMessage ( "The action {$ action } already exists in {$ controller }! " , "error " , "Creation " , "warning circle " , null , "msgControllers " );
179
222
}
180
223
}
181
224
}
182
225
183
226
protected function getMethods ($ strMethods ) {
184
- $ methods = \explode ( ", " , $ strMethods );
185
- $ result = [ ];
186
- foreach ( $ methods as $ method ) {
187
- $ result [] = '" ' . $ method . '" ' ;
227
+ $ methods = \explode ( ", " , $ strMethods );
228
+ $ result = [];
229
+ foreach ($ methods as $ method ) {
230
+ $ result [] = '" ' . $ method . '" ' ;
188
231
}
189
- return "[ " . \implode ( ", " , $ result ) . "] " ;
232
+ return "[ " . \implode ( ", " , $ result ) . "] " ;
190
233
}
191
234
192
- protected function _createViewOp ($ controller , $ action , $ theme = null ) {
235
+ public function _createViewOp ($ controller , $ action , $ theme = null ) {
193
236
$ prefix = "" ;
194
- if (! isset ( $ theme ) || $ theme == '' ) {
195
- $ theme = $ this ->config ["templateEngineOptions " ] ["activeTheme " ] ?? null ;
237
+ if (! isset ( $ theme ) || $ theme == '' ) {
238
+ $ theme = $ this ->config ["templateEngineOptions " ]["activeTheme " ] ?? null ;
196
239
}
197
240
if ($ theme != null ) {
198
241
$ prefix = 'themes/ ' . $ theme . '/ ' ;
199
242
}
200
243
$ viewName = $ prefix . $ controller . "/ " . $ action . ".html " ;
201
- UFileSystem::safeMkdir ( \ROOT . \DS . "views " . \DS . $ prefix . $ controller );
202
- $ templateDir = $ this ->getTemplateDir ();
203
- UFileSystem::openReplaceWriteFromTemplateFile ( $ templateDir . "view.tpl " , \ROOT . \DS . "views " . \DS . $ viewName , [ "%controllerName% " => $ controller ,"%actionName% " => $ action ] );
244
+ UFileSystem::safeMkdir (\ROOT . \DS . "views " . \DS . $ prefix . $ controller );
245
+ $ templateDir = $ this ->getTemplateDir ();
246
+ UFileSystem::openReplaceWriteFromTemplateFile ($ templateDir . "view.tpl " , \ROOT . \DS . "views " . \DS . $ viewName , [
247
+ "%controllerName% " => $ controller ,
248
+ "%actionName% " => $ action
249
+ ]);
204
250
return $ viewName ;
205
251
}
206
252
207
253
public function createAuthCrudView ($ frameworkName , $ controllerName , $ newName ) {
208
254
$ folder = \ROOT . \DS . "views " . \DS . $ controllerName ;
209
- UFileSystem::safeMkdir ( $ folder );
255
+ UFileSystem::safeMkdir ( $ folder );
210
256
try {
211
- $ teInstance = Startup::getTempateEngineInstance ();
212
- if (isset ( $ teInstance )) {
213
- $ blocks = $ teInstance ->getBlockNames ( $ frameworkName );
214
- if (sizeof ( $ blocks ) > 0 ) {
215
- $ content = [ "{% extends \"" . $ frameworkName . "\" %} \n" ];
216
- foreach ( $ blocks as $ blockname ) {
217
- $ content [] = "{% block " . $ blockname . " %} \n\t{{ parent() }} \n{% endblock %} \n" ;
257
+ $ teInstance = Startup::getTempateEngineInstance ();
258
+ if (isset ($ teInstance )) {
259
+ $ blocks = $ teInstance ->getBlockNames ($ frameworkName );
260
+ if (sizeof ($ blocks ) > 0 ) {
261
+ $ content = [
262
+ "{% extends \"" . $ frameworkName . "\" %} \n"
263
+ ];
264
+ foreach ($ blocks as $ blockname ) {
265
+ $ content [] = "{% block " . $ blockname . " %} \n\t{{ parent() }} \n{% endblock %} \n" ;
218
266
}
219
267
} else {
220
- $ content = [ $ teInstance ->getCode ( $ frameworkName ) ];
268
+ $ content = [
269
+ $ teInstance ->getCode ($ frameworkName )
270
+ ];
221
271
}
222
272
}
223
- } catch ( \Exception $ e ) {
224
- $ content = [ $ teInstance ->getCode ( $ frameworkName ) ];
273
+ } catch (\Exception $ e ) {
274
+ $ content = [
275
+ $ teInstance ->getCode ($ frameworkName )
276
+ ];
225
277
}
226
- if (isset ( $ content )) {
227
- return UFileSystem::save ( $ folder . \DS . $ newName . ".html " , implode ( "" , $ content ) );
278
+ if (isset ( $ content )) {
279
+ return UFileSystem::save ( $ folder . \DS . $ newName . ".html " , implode ( "" , $ content) );
228
280
}
229
281
}
230
282
0 commit comments