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