Passed
Push — master ( 3dcefc...33965f )
by Jean-Christophe
06:48
created

ScaffoldController::addAuthController()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 1
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
14
/**
15
 * Base class for Scaffolding
16
 * Ubiquity\scaffolding$ScaffoldController
17
 * This class is part of Ubiquity
18
 *
19
 * @author jcheron <[email protected]>
20
 * @version 1.0.2
21
 *
22
 */
23
abstract class ScaffoldController {
24
	public static $views = [
25
							"CRUD" => [ "index" => "@framework/crud/index.html","form" => "@framework/crud/form.html","display" => "@framework/crud/display.html" ],
26
							"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" ] ];
27
28
	protected abstract function getTemplateDir();
29
30
	protected abstract function storeControllerNameInSession($controller);
31
32
	public abstract function showSimpleMessage($content, $type, $title = null, $icon = "info", $timeout = NULL, $staticName = null);
33
34
	protected abstract function _addMessageForRouteCreation($path, $jsCallback = "");
35
36 2
	public function _createMethod($access, $name, $parameters = "", $return = "", $content = "", $comment = "") {
37 2
		$templateDir = $this->getTemplateDir ();
38 2
		$keyAndValues = [ "%access%" => $access,"%name%" => $name,"%parameters%" => $parameters,"%content%" => $content,"%comment%" => $comment,"%return%" => $return ];
39 2
		return UFileSystem::openReplaceInTemplateFile ( $templateDir . "method.tpl", $keyAndValues );
40
	}
41
42 5
	public function _createController($controllerName, $variables = [], $ctrlTemplate = 'controller.tpl', $hasView = false, $jsCallback = "") {
43 5
		$message = "";
44 5
		$templateDir = $this->getTemplateDir ();
45 5
		$controllersNS = \rtrim ( Startup::getNS ( "controllers" ), "\\" );
46 5
		$controllersDir = \ROOT . \DS . str_replace ( "\\", \DS, $controllersNS );
47 5
		$controllerName = \ucfirst ( $controllerName );
48 5
		$filename = $controllersDir . \DS . $controllerName . ".php";
49 5
		if (\file_exists ( $filename ) === false) {
50 5
			$namespace = "";
51 5
			if ($controllersNS !== "") {
52 5
				$namespace = "namespace " . $controllersNS . ";";
53
			}
54 5
			$msgView = "";
55 5
			$indexContent = "";
56 5
			if ($hasView) {
57 1
				$viewDir = \ROOT . \DS . "views" . \DS . $controllerName . \DS;
58 1
				UFileSystem::safeMkdir ( $viewDir );
59 1
				$viewName = $viewDir . \DS . "index.html";
60 1
				UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . "view.tpl", $viewName, [ "%controllerName%" => $controllerName,"%actionName%" => "index" ] );
61 1
				$msgView = "<br>The default view associated has been created in <b>" . UFileSystem::cleanPathname ( \ROOT . \DS . $viewDir ) . "</b>";
62 1
				$indexContent = "\$this->loadView(\"" . $controllerName . "/index.html\");";
63
			}
64 5
			$variables = \array_merge ( $variables, [ "%controllerName%" => $controllerName,"%indexContent%" => $indexContent,"%namespace%" => $namespace ] );
65 5
			UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . $ctrlTemplate, $filename, $variables );
66 5
			$msgContent = "The <b>" . $controllerName . "</b> controller has been created in <b>" . UFileSystem::cleanFilePathname ( $filename ) . "</b>." . $msgView;
67 5
			if (isset ( $variables ["%path%"] ) && $variables ["%path%"] !== "") {
68
				$msgContent .= $this->_addMessageForRouteCreation ( $variables ["%path%"], $jsCallback );
69
			}
70 5
			$this->storeControllerNameInSession ( $controllersNS . "\\" . $controllerName );
71 5
			$message = $this->showSimpleMessage ( $msgContent, "success", null, "checkmark circle", NULL, "msgGlobal" );
72
		} else {
73
			$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" );
74
		}
75 5
		return $message;
76
	}
77
78 1
	public function addCrudController($crudControllerName, $resource, $crudDatas = null, $crudViewer = null, $crudEvents = null, $crudViews = null, $routePath = '') {
79 1
		$crudController = new CrudControllerCreator ( $crudControllerName, $resource, $crudDatas, $crudViewer, $crudEvents, $crudViews, $routePath );
80 1
		$crudController->create ( $this );
81 1
	}
82
83 1
	public function addAuthController($authControllerName, $baseClass, $authViews = null, $routePath = "") {
84 1
		$authCreator = new AuthControllerCreator ( $authControllerName, $baseClass, $authViews, $routePath );
85 1
		$authCreator->create ( $this );
86 1
	}
87
88 2
	public function _createClass($template, $classname, $namespace, $uses, $extendsOrImplements, $classContent) {
89 2
		$namespaceVar = "";
90 2
		if (UString::isNotNull ( $namespace )) {
91 2
			$namespaceVar = "namespace {$namespace};";
92
		}
93 2
		$variables = [ "%classname%" => $classname,"%namespace%" => $namespaceVar,"%uses%" => $uses,"%extendsOrImplements%" => $extendsOrImplements,"%classContent%" => $classContent ];
94 2
		$templateDir = $this->getTemplateDir ();
95 2
		$directory = UFileSystem::getDirFromNamespace ( $namespace );
96 2
		UFileSystem::safeMkdir ( $directory );
97 2
		$filename = UFileSystem::cleanFilePathname ( $directory . \DS . $classname . ".php" );
98 2
		if (! file_exists ( $filename )) {
99 2
			UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . $template, $filename, $variables );
100 2
			$message = $this->showSimpleMessage ( "The <b>" . $classname . "</b> class has been created in <b>" . $filename . "</b>.", "success", "Creation", "checkmark circle" );
101
		} else {
102
			$message = $this->showSimpleMessage ( "The file <b>" . $filename . "</b> already exists.<br>Can not create the <b>" . $classname . "</b> class!", "warning", "Creation", "warning circle" );
103
		}
104 2
		return $message;
105
	}
106
107 1
	public function _newAction($controller, $action, $parameters = null, $content = '', $routeInfo = null, $createView = false) {
108 1
		$templateDir = $this->getTemplateDir ();
109 1
		$msgContent = "";
110 1
		$r = new \ReflectionClass ( $controller );
111 1
		if (! method_exists ( $controller, $action )) {
112 1
			$ctrlFilename = $r->getFileName ();
113 1
			$content = CodeUtils::indent ( $content, 2 );
114 1
			$fileContent = \implode ( "", UIntrospection::getClassCode ( $controller ) );
0 ignored issues
show
Bug introduced by
It seems like Ubiquity\utils\base\UInt...tClassCode($controller) can also be of type false; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
			$fileContent = \implode ( "", /** @scrutinizer ignore-type */ UIntrospection::getClassCode ( $controller ) );
Loading history...
115 1
			$fileContent = \trim ( $fileContent );
116 1
			$posLast = \strrpos ( $fileContent, "}" );
117 1
			if ($posLast !== false) {
118 1
				if ($createView) {
119 1
					$viewname = $this->_createViewOp ( ClassUtils::getClassSimpleName ( $controller ), $action );
120 1
					$content .= "\n\t\t\$this->loadView('" . $viewname . "');\n";
121 1
					$msgContent .= "<br>Created view : <b>" . $viewname . "</b>";
122
				}
123 1
				$routeAnnotation = "";
124 1
				if (is_array ( $routeInfo )) {
125 1
					$name = "route";
126 1
					$path = $routeInfo ["path"];
127 1
					$routeProperties = [ '"' . $path . '"' ];
128 1
					$methods = $routeInfo ["methods"];
129 1
					if (UString::isNotNull ( $methods )) {
130
						$routeProperties [] = '"methods"=>' . $this->getMethods ( $methods );
131
					}
132 1
					if (isset ( $routeInfo ["ck-Cache"] )) {
133
						$routeProperties [] = '"cache"=>true';
134
						if (isset ( $routeInfo ["duration"] )) {
135
							$duration = $routeInfo ["duration"];
136
							if (\ctype_digit ( $duration )) {
137
								$routeProperties [] = '"duration"=>' . $duration;
138
							}
139
						}
140
					}
141 1
					$routeProperties = \implode ( ",", $routeProperties );
142 1
					$routeAnnotation = UFileSystem::openReplaceInTemplateFile ( $templateDir . "annotation.tpl", [ "%name%" => $name,"%properties%" => $routeProperties ] );
143
144 1
					$msgContent .= $this->_addMessageForRouteCreation ( $path );
145
				}
146 1
				$parameters = CodeUtils::cleanParameters ( $parameters );
147 1
				$actionContent = UFileSystem::openReplaceInTemplateFile ( $templateDir . "action.tpl", [ "%route%" => "\n" . $routeAnnotation,"%actionName%" => $action,"%parameters%" => $parameters,"%content%" => $content ] );
1 ignored issue
show
Bug introduced by
Are you sure $routeAnnotation of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

147
				$actionContent = UFileSystem::openReplaceInTemplateFile ( $templateDir . "action.tpl", [ "%route%" => "\n" . /** @scrutinizer ignore-type */ $routeAnnotation,"%actionName%" => $action,"%parameters%" => $parameters,"%content%" => $content ] );
Loading history...
148 1
				$fileContent = \substr_replace ( $fileContent, "\n%content%", $posLast - 1, 0 );
149 1
				if (! CodeUtils::isValidCode ( '<?php ' . $content )) {
150
					echo $this->showSimpleMessage ( "Errors parsing action content!", "warning", "Creation", "warning circle", null, "msgControllers" );
151
					return;
152
				} else {
153 1
					if (UFileSystem::replaceWriteFromContent ( $fileContent . "\n", $ctrlFilename, [ '%content%' => $actionContent ] )) {
154 1
						$msgContent = "The action <b>{$action}</b> is created in controller <b>{$controller}</b>" . $msgContent;
155 1
						echo $this->showSimpleMessage ( $msgContent, "info", "Creation", "info circle", null, "msgControllers" );
156
					}
157
				}
158
			}
159
		} else {
160
			echo $this->showSimpleMessage ( "The action {$action} already exists in {$controller}!", "error", "Creation", "warning circle", null, "msgControllers" );
161
		}
162 1
	}
163
164
	protected function getMethods($strMethods) {
165
		$methods = \explode ( ",", $strMethods );
166
		$result = [ ];
167
		foreach ( $methods as $method ) {
168
			$result [] = '"' . $method . '"';
169
		}
170
		return "[" . \implode ( ",", $result ) . "]";
171
	}
172
173 1
	protected function _createViewOp($controller, $action) {
174 1
		$viewName = $controller . "/" . $action . ".html";
175 1
		UFileSystem::safeMkdir ( \ROOT . \DS . "views" . \DS . $controller );
176 1
		$templateDir = $this->getTemplateDir ();
177 1
		UFileSystem::openReplaceWriteFromTemplateFile ( $templateDir . "view.tpl", \ROOT . \DS . "views" . \DS . $viewName, [ "%controllerName%" => $controller,"%actionName%" => $action ] );
178 1
		return $viewName;
179
	}
180
181 2
	public function createAuthCrudView($frameworkName, $controllerName, $newName) {
182 2
		$folder = \ROOT . \DS . "views" . \DS . $controllerName;
183 2
		UFileSystem::safeMkdir ( $folder );
184
		try {
185 2
			$teInstance = Startup::getTempateEngineInstance ();
186 2
			if (isset ( $teInstance )) {
187 2
				$blocks = $teInstance->getBlockNames ( $frameworkName );
188 2
				if (sizeof ( $blocks ) > 0) {
189 2
					$content = [ "{% extends \"" . $frameworkName . "\" %}\n" ];
190 2
					foreach ( $blocks as $blockname ) {
191 2
						$content [] = "{% block " . $blockname . " %}\n\t{{ parent() }}\n{% endblock %}\n";
192
					}
193
				} else {
194 2
					$content = [ $teInstance->getCode ( $frameworkName ) ];
195
				}
196
			}
197 1
		} catch ( \Exception $e ) {
198 1
			$content = [ $teInstance->getCode ( $frameworkName ) ];
199
		}
200 2
		if (isset ( $content )) {
201 2
			return UFileSystem::save ( $folder . \DS . $newName . ".html", implode ( "", $content ) );
202
		}
203
	}
204
}
205
206