Skip to content

Commit ea2d91e

Browse files
committed
set _createViewOp public
1 parent 48e3a32 commit ea2d91e

File tree

1 file changed

+151
-99
lines changed

1 file changed

+151
-99
lines changed

src/Ubiquity/scaffolding/ScaffoldController.php

Lines changed: 151 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Ubiquity\scaffolding;
43

54
use Ubiquity\controllers\Startup;
@@ -23,20 +22,32 @@
2322
*
2423
*/
2524
abstract class ScaffoldController {
25+
2626
protected $config;
27+
2728
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+
];
3043

3144
public function getTemplateDir() {
3245
return \dirname(__DIR__) . "/scaffolding/templates/";
3346
}
3447

35-
public function _refreshRest($refresh = false) {
36-
}
48+
public function _refreshRest($refresh = false) {}
3749

38-
public function initRestCache($refresh = true) {
39-
}
50+
public function initRestCache($refresh = true) {}
4051

4152
protected abstract function storeControllerNameInSession($controller);
4253

@@ -45,19 +56,26 @@ public abstract function showSimpleMessage($content, $type, $title = null, $icon
4556
protected abstract function _addMessageForRouteCreation($path, $jsCallback = "");
4657

4758
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);
5169
}
5270

5371
public function _createController($controllerName, $variables = [], $ctrlTemplate = 'controller.tpl', $hasView = false, $jsCallback = "") {
5472
$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);
5977
$filename = $controllersDir . \DS . $controllerName . ".php";
60-
if (\file_exists ( $filename ) === false) {
78+
if (\file_exists($filename) === false) {
6179
$namespace = "";
6280
if ($controllersNS !== "") {
6381
$namespace = "namespace " . $controllersNS . ";";
@@ -66,165 +84,199 @@ public function _createController($controllerName, $variables = [], $ctrlTemplat
6684
$indexContent = "";
6785
if ($hasView) {
6886
$viewDir = \ROOT . \DS . "views" . \DS . $controllerName . \DS;
69-
UFileSystem::safeMkdir ( $viewDir );
87+
UFileSystem::safeMkdir($viewDir);
7088
$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>";
7394
$indexContent = "\$this->loadView(\"" . $controllerName . "/index.html\");";
7495
}
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);
80105
}
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");
83108
} 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");
85110
}
86111
return $message;
87112
}
88113

89114
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);
92117
}
93118

94119
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);
97122
}
98123

99124
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);
102127
}
103128

104129
public function _createClass($template, $classname, $namespace, $uses, $extendsOrImplements, $classContent) {
105130
$namespaceVar = "";
106-
if (UString::isNotNull ( $namespace )) {
131+
if (UString::isNotNull($namespace)) {
107132
$namespaceVar = "namespace {$namespace};";
108133
}
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");
117148
} 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");
119150
}
120151
return $message;
121152
}
122153

123154
public function _newAction($controller, $action, $parameters = null, $content = '', $routeInfo = null, $createView = false, $theme = null) {
124-
$templateDir = $this->getTemplateDir ();
155+
$templateDir = $this->getTemplateDir();
125156
$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);
131162
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, "}");
135166
if ($posLast !== false) {
136167
if ($createView) {
137-
$viewname = $this->_createViewOp ( ClassUtils::getClassSimpleName ( $controller ), $action, $theme );
168+
$viewname = $this->_createViewOp(ClassUtils::getClassSimpleName($controller), $action, $theme);
138169
$content .= "\n\t\t\$this->loadView('" . $viewname . "');\n";
139170
$msgContent .= "<br>Created view : <b>" . $viewname . "</b>";
140171
}
141172
$routeAnnotation = "";
142-
if (is_array ( $routeInfo )) {
173+
if (is_array($routeInfo)) {
143174
$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);
149182
}
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;
156189
}
157190
}
158191
}
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+
]);
161197

162-
$msgContent .= $this->_addMessageForRouteCreation ( $path );
198+
$msgContent .= $this->_addMessageForRouteCreation($path);
163199
}
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");
169210
return;
170211
} else {
171-
if (UFileSystem::replaceWriteFromContent ( $fileContent . "\n", $ctrlFilename, [ '%content%' => $actionContent ] )) {
212+
if (UFileSystem::replaceWriteFromContent($fileContent . "\n", $ctrlFilename, [
213+
'%content%' => $actionContent
214+
])) {
172215
$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");
174217
}
175218
}
176219
}
177220
} 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");
179222
}
180223
}
181224
}
182225

183226
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 . '"';
188231
}
189-
return "[" . \implode ( ",", $result ) . "]";
232+
return "[" . \implode(",", $result) . "]";
190233
}
191234

192-
protected function _createViewOp($controller, $action, $theme = null) {
235+
public function _createViewOp($controller, $action, $theme = null) {
193236
$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;
196239
}
197240
if ($theme != null) {
198241
$prefix = 'themes/' . $theme . '/';
199242
}
200243
$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+
]);
204250
return $viewName;
205251
}
206252

207253
public function createAuthCrudView($frameworkName, $controllerName, $newName) {
208254
$folder = \ROOT . \DS . "views" . \DS . $controllerName;
209-
UFileSystem::safeMkdir ( $folder );
255+
UFileSystem::safeMkdir($folder);
210256
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";
218266
}
219267
} else {
220-
$content = [ $teInstance->getCode ( $frameworkName ) ];
268+
$content = [
269+
$teInstance->getCode($frameworkName)
270+
];
221271
}
222272
}
223-
} catch ( \Exception $e ) {
224-
$content = [ $teInstance->getCode ( $frameworkName ) ];
273+
} catch (\Exception $e) {
274+
$content = [
275+
$teInstance->getCode($frameworkName)
276+
];
225277
}
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));
228280
}
229281
}
230282

0 commit comments

Comments
 (0)