|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\scaffolding; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\controllers\Startup; |
|
6
|
|
|
use Ubiquity\utils\base\UFileSystem; |
|
7
|
|
|
use Ubiquity\utils\base\UString; |
|
8
|
|
|
use Ubiquity\controllers\admin\utils\CodeUtils; |
|
9
|
|
|
use Ubiquity\utils\base\UIntrospection; |
|
10
|
|
|
use Ubiquity\cache\ClassUtils; |
|
11
|
|
|
use Ubiquity\scaffolding\creators\AuthControllerCreator; |
|
12
|
|
|
use Ubiquity\scaffolding\creators\CrudControllerCreator; |
|
13
|
|
|
use Ubiquity\scaffolding\creators\RestControllerCreator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Base class for Scaffolding |
|
17
|
|
|
* Ubiquity\scaffolding$ScaffoldController |
|
18
|
|
|
* This class is part of Ubiquity |
|
19
|
|
|
* |
|
20
|
|
|
* @author jcheron <[email protected]> |
|
21
|
|
|
* @version 1.0.2 |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class ScaffoldController { |
|
25
|
|
|
public static $views = [ |
|
26
|
|
|
"CRUD" => [ "index" => "@framework/crud/index.html","form" => "@framework/crud/form.html","display" => "@framework/crud/display.html" ], |
|
27
|
|
|
"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" ] ]; |
|
28
|
|
|
|
|
29
|
|
|
public abstract function getTemplateDir(); |
|
30
|
|
|
|
|
31
|
|
|
public function _refreshRest($refresh = false) { |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function initRestCache($refresh = true) { |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected abstract function storeControllerNameInSession($controller); |
|
38
|
|
|
|
|
39
|
|
|
public abstract function showSimpleMessage($content, $type, $title = null, $icon = "info", $timeout = NULL, $staticName = null); |
|
40
|
|
|
|
|
41
|
|
|
protected abstract function _addMessageForRouteCreation($path, $jsCallback = ""); |
|
42
|
|
|
|
|
43
|
2 |
|
public function _createMethod($access, $name, $parameters = "", $return = "", $content = "", $comment = "") { |
|
44
|
2 |
|
$templateDir = $this->getTemplateDir (); |
|
45
|
2 |
|
$keyAndValues = [ "%access%" => $access,"%name%" => $name,"%parameters%" => $parameters,"%content%" => $content,"%comment%" => $comment,"%return%" => $return ]; |
|
46
|
2 |
|
return UFileSystem::openReplaceInTemplateFile ( $templateDir . "method.tpl", $keyAndValues ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
public function _createController($controllerName, $variables = [], $ctrlTemplate = 'controller.tpl', $hasView = false, $jsCallback = "") { |
|
50
|
5 |
|
$message = ""; |
|
51
|
5 |
|
$templateDir = $this->getTemplateDir (); |
|
52
|
5 |
|
$controllersNS = \rtrim ( Startup::getNS ( "controllers" ), "\\" ); |
|
53
|
5 |
|
$controllersDir = \ROOT . \DS . str_replace ( "\\", \DS, $controllersNS ); |
|
54
|
5 |
|
$controllerName = \ucfirst ( $controllerName ); |
|
55
|
5 |
|
$filename = $controllersDir . \DS . $controllerName . ".php"; |
|
56
|
5 |
|
if (\file_exists ( $filename ) === false) { |
|
57
|
5 |
|
$namespace = ""; |
|
58
|
5 |
|
if ($controllersNS !== "") { |
|
59
|
5 |
|
$namespace = "namespace " . $controllersNS . ";"; |
|
60
|
|
|
} |
|
61
|
5 |
|
$msgView = ""; |
|
62
|
5 |
|
$indexContent = ""; |
|
63
|
5 |
|
if ($hasView) { |
|
64
|
1 |
|
$viewDir = \ROOT . \DS . "views" . \DS . $controllerName . \DS; |
|
65
|
1 |
|
UFileSystem::safeMkdir ( $viewDir ); |
|
66
|
1 |
|
$viewName = $viewDir . \DS . "index.html"; |
|
67
|
1 |
|
UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . "view.tpl", $viewName, [ "%controllerName%" => $controllerName,"%actionName%" => "index" ] ); |
|
68
|
1 |
|
$msgView = "<br>The default view associated has been created in <b>" . UFileSystem::cleanPathname ( \ROOT . \DS . $viewDir ) . "</b>"; |
|
69
|
1 |
|
$indexContent = "\$this->loadView(\"" . $controllerName . "/index.html\");"; |
|
70
|
|
|
} |
|
71
|
5 |
|
$variables = \array_merge ( $variables, [ "%controllerName%" => $controllerName,"%indexContent%" => $indexContent,"%namespace%" => $namespace ] ); |
|
72
|
5 |
|
UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . $ctrlTemplate, $filename, $variables ); |
|
73
|
5 |
|
$msgContent = "The <b>" . $controllerName . "</b> controller has been created in <b>" . UFileSystem::cleanFilePathname ( $filename ) . "</b>." . $msgView; |
|
74
|
5 |
|
if (isset ( $variables ["%path%"] ) && $variables ["%path%"] !== "") { |
|
75
|
|
|
$msgContent .= $this->_addMessageForRouteCreation ( $variables ["%path%"], $jsCallback ); |
|
76
|
|
|
} |
|
77
|
5 |
|
$this->storeControllerNameInSession ( $controllersNS . "\\" . $controllerName ); |
|
78
|
5 |
|
$message = $this->showSimpleMessage ( $msgContent, "success", null, "checkmark circle", NULL, "msgGlobal" ); |
|
79
|
|
|
} else { |
|
80
|
|
|
$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" ); |
|
81
|
|
|
} |
|
82
|
5 |
|
return $message; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function addCrudController($crudControllerName, $resource, $crudDatas = null, $crudViewer = null, $crudEvents = null, $crudViews = null, $routePath = '') { |
|
86
|
1 |
|
$crudController = new CrudControllerCreator ( $crudControllerName, $resource, $crudDatas, $crudViewer, $crudEvents, $crudViews, $routePath ); |
|
87
|
1 |
|
$crudController->create ( $this ); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function addAuthController($authControllerName, $baseClass, $authViews = null, $routePath = "") { |
|
91
|
1 |
|
$authCreator = new AuthControllerCreator ( $authControllerName, $baseClass, $authViews, $routePath ); |
|
92
|
1 |
|
$authCreator->create ( $this ); |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function addRestController($restControllerName, $resource, $routePath = "", $reInit = true) { |
|
96
|
1 |
|
$restCreator = new RestControllerCreator ( $restControllerName, $resource, $routePath ); |
|
97
|
1 |
|
$restCreator->create ( $this, $reInit ); |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function _createClass($template, $classname, $namespace, $uses, $extendsOrImplements, $classContent) { |
|
101
|
2 |
|
$namespaceVar = ""; |
|
102
|
2 |
|
if (UString::isNotNull ( $namespace )) { |
|
103
|
2 |
|
$namespaceVar = "namespace {$namespace};"; |
|
104
|
|
|
} |
|
105
|
2 |
|
$variables = [ "%classname%" => $classname,"%namespace%" => $namespaceVar,"%uses%" => $uses,"%extendsOrImplements%" => $extendsOrImplements,"%classContent%" => $classContent ]; |
|
106
|
2 |
|
$templateDir = $this->getTemplateDir (); |
|
107
|
2 |
|
$directory = UFileSystem::getDirFromNamespace ( $namespace ); |
|
108
|
2 |
|
UFileSystem::safeMkdir ( $directory ); |
|
109
|
2 |
|
$filename = UFileSystem::cleanFilePathname ( $directory . \DS . $classname . ".php" ); |
|
110
|
2 |
|
if (! file_exists ( $filename )) { |
|
111
|
2 |
|
UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . $template, $filename, $variables ); |
|
112
|
2 |
|
$message = $this->showSimpleMessage ( "The <b>" . $classname . "</b> class has been created in <b>" . $filename . "</b>.", "success", "Creation", "checkmark circle" ); |
|
113
|
|
|
} else { |
|
114
|
|
|
$message = $this->showSimpleMessage ( "The file <b>" . $filename . "</b> already exists.<br>Can not create the <b>" . $classname . "</b> class!", "warning", "Creation", "warning circle" ); |
|
115
|
|
|
} |
|
116
|
2 |
|
return $message; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
public function _newAction($controller, $action, $parameters = null, $content = '', $routeInfo = null, $createView = false) { |
|
120
|
1 |
|
$templateDir = $this->getTemplateDir (); |
|
121
|
1 |
|
$msgContent = ""; |
|
122
|
1 |
|
$r = new \ReflectionClass ( $controller ); |
|
123
|
1 |
|
if (! method_exists ( $controller, $action )) { |
|
124
|
1 |
|
$ctrlFilename = $r->getFileName (); |
|
125
|
1 |
|
$content = CodeUtils::indent ( $content, 2 ); |
|
126
|
1 |
|
$classCode = UIntrospection::getClassCode ( $controller ); |
|
127
|
1 |
|
if ($classCode !== false) { |
|
128
|
1 |
|
$fileContent = \implode ( "", $classCode ); |
|
129
|
1 |
|
$fileContent = \trim ( $fileContent ); |
|
130
|
1 |
|
$posLast = \strrpos ( $fileContent, "}" ); |
|
131
|
1 |
|
if ($posLast !== false) { |
|
132
|
1 |
|
if ($createView) { |
|
133
|
1 |
|
$viewname = $this->_createViewOp ( ClassUtils::getClassSimpleName ( $controller ), $action ); |
|
134
|
1 |
|
$content .= "\n\t\t\$this->loadView('" . $viewname . "');\n"; |
|
135
|
1 |
|
$msgContent .= "<br>Created view : <b>" . $viewname . "</b>"; |
|
136
|
|
|
} |
|
137
|
1 |
|
$routeAnnotation = ""; |
|
138
|
1 |
|
if (is_array ( $routeInfo )) { |
|
139
|
1 |
|
$name = "route"; |
|
140
|
1 |
|
$path = $routeInfo ["path"]; |
|
141
|
1 |
|
$routeProperties = [ '"' . $path . '"' ]; |
|
142
|
1 |
|
$methods = $routeInfo ["methods"]; |
|
143
|
1 |
|
if (UString::isNotNull ( $methods )) { |
|
144
|
|
|
$routeProperties [] = '"methods"=>' . $this->getMethods ( $methods ); |
|
145
|
|
|
} |
|
146
|
1 |
|
if (isset ( $routeInfo ["ck-Cache"] )) { |
|
147
|
|
|
$routeProperties [] = '"cache"=>true'; |
|
148
|
|
|
if (isset ( $routeInfo ["duration"] )) { |
|
149
|
|
|
$duration = $routeInfo ["duration"]; |
|
150
|
|
|
if (\ctype_digit ( $duration )) { |
|
151
|
|
|
$routeProperties [] = '"duration"=>' . $duration; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
1 |
|
$routeProperties = \implode ( ",", $routeProperties ); |
|
156
|
1 |
|
$routeAnnotation = UFileSystem::openReplaceInTemplateFile ( $templateDir . "annotation.tpl", [ "%name%" => $name,"%properties%" => $routeProperties ] ); |
|
157
|
|
|
|
|
158
|
1 |
|
$msgContent .= $this->_addMessageForRouteCreation ( $path ); |
|
159
|
|
|
} |
|
160
|
1 |
|
$parameters = CodeUtils::cleanParameters ( $parameters ); |
|
161
|
1 |
|
$actionContent = UFileSystem::openReplaceInTemplateFile ( $templateDir . "action.tpl", [ "%route%" => "\n" . $routeAnnotation ?? '',"%actionName%" => $action,"%parameters%" => $parameters,"%content%" => $content ] ); |
|
|
|
|
|
|
162
|
1 |
|
$fileContent = \substr_replace ( $fileContent, "\n%content%", $posLast - 1, 0 ); |
|
163
|
1 |
|
if (! CodeUtils::isValidCode ( '<?php ' . $content )) { |
|
164
|
|
|
echo $this->showSimpleMessage ( "Errors parsing action content!", "warning", "Creation", "warning circle", null, "msgControllers" ); |
|
165
|
|
|
return; |
|
166
|
|
|
} else { |
|
167
|
1 |
|
if (UFileSystem::replaceWriteFromContent ( $fileContent . "\n", $ctrlFilename, [ '%content%' => $actionContent ] )) { |
|
168
|
1 |
|
$msgContent = "The action <b>{$action}</b> is created in controller <b>{$controller}</b>" . $msgContent; |
|
169
|
1 |
|
echo $this->showSimpleMessage ( $msgContent, "info", "Creation", "info circle", null, "msgControllers" ); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} else { |
|
174
|
|
|
echo $this->showSimpleMessage ( "The action {$action} already exists in {$controller}!", "error", "Creation", "warning circle", null, "msgControllers" ); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
1 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
protected function getMethods($strMethods) { |
|
180
|
|
|
$methods = \explode ( ",", $strMethods ); |
|
181
|
|
|
$result = [ ]; |
|
182
|
|
|
foreach ( $methods as $method ) { |
|
183
|
|
|
$result [] = '"' . $method . '"'; |
|
184
|
|
|
} |
|
185
|
|
|
return "[" . \implode ( ",", $result ) . "]"; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
1 |
|
protected function _createViewOp($controller, $action) { |
|
189
|
1 |
|
$viewName = $controller . "/" . $action . ".html"; |
|
190
|
1 |
|
UFileSystem::safeMkdir ( \ROOT . \DS . "views" . \DS . $controller ); |
|
191
|
1 |
|
$templateDir = $this->getTemplateDir (); |
|
192
|
1 |
|
UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . "view.tpl", \ROOT . \DS . "views" . \DS . $viewName, [ "%controllerName%" => $controller,"%actionName%" => $action ] ); |
|
193
|
1 |
|
return $viewName; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
2 |
|
public function createAuthCrudView($frameworkName, $controllerName, $newName) { |
|
197
|
2 |
|
$folder = \ROOT . \DS . "views" . \DS . $controllerName; |
|
198
|
2 |
|
UFileSystem::safeMkdir ( $folder ); |
|
199
|
|
|
try { |
|
200
|
2 |
|
$teInstance = Startup::getTempateEngineInstance (); |
|
201
|
2 |
|
if (isset ( $teInstance )) { |
|
202
|
2 |
|
$blocks = $teInstance->getBlockNames ( $frameworkName ); |
|
203
|
2 |
|
if (sizeof ( $blocks ) > 0) { |
|
204
|
2 |
|
$content = [ "{% extends \"" . $frameworkName . "\" %}\n" ]; |
|
205
|
2 |
|
foreach ( $blocks as $blockname ) { |
|
206
|
2 |
|
$content [] = "{% block " . $blockname . " %}\n\t{{ parent() }}\n{% endblock %}\n"; |
|
207
|
|
|
} |
|
208
|
|
|
} else { |
|
209
|
2 |
|
$content = [ $teInstance->getCode ( $frameworkName ) ]; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
1 |
|
} catch ( \Exception $e ) { |
|
213
|
1 |
|
$content = [ $teInstance->getCode ( $frameworkName ) ]; |
|
214
|
|
|
} |
|
215
|
2 |
|
if (isset ( $content )) { |
|
216
|
2 |
|
return UFileSystem::save ( $folder . \DS . $newName . ".html", implode ( "", $content ) ); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
|