Ubiquity  2.0.2
php rapid development framework
CheckTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
13 use Ajax\JsUtils;
18 
27 class CheckTrait{
28  protected $messages=[ "error" => [ ],"info" => [ ] ];
29 
30  abstract protected function getModelSteps();
31 
32  abstract protected function getActiveModelStep();
33 
34  abstract protected function getNextModelStep();
35 
36  abstract protected function displayModelsMessages($type, $messagesToDisplay);
37 
38  abstract protected function showSimpleMessage($content, $type, $icon="info", $timeout=NULL, $staticName=null);
39 
40  abstract public function _isModelsCompleted();
41 
46  abstract public function _getAdminFiles();
47 
48  public function createModels($singleTable=null) {
49  $config=Startup::getConfig();
50  \ob_start();
51  (new DbModelsCreator())->create($config, false, $singleTable);
52  $result=\ob_get_clean();
53  $message=$this->showSimpleMessage("", "success", "check mark", null, "msg-create-models");
54  $message->addHeader("Models creation");
55  $message->addList(\explode("\n", \str_replace("\n\n", "\n", \trim($result))));
56  $this->models(true);
57  echo $message;
58  }
59 
60  protected function _checkStep($niveau=null) {
61  $nbChecked=1;
62  $niveauMax=$niveau;
63  if (!isset($niveau))
64  $niveauMax=$this->activeStep - 1;
65  $steps=$this->getModelSteps();
66  while ( $nbChecked <= $niveauMax && $this->hasNoError() && isset($steps[$nbChecked]) ) {
67  $this->_modelCheckOneNiveau($steps[$nbChecked][1]);
68  $nbChecked++;
69  }
70  if ($this->hasError() && !isset($niveau)) {
71  $this->activeStep=$nbChecked - 1;
72  $this->steps[$this->engineering][$this->activeStep][0]="warning sign red";
73  }
74  }
75 
76  protected function _modelCheckOneNiveau($name) {
77  $config=Startup::getConfig();
78  switch($name) {
79  case "Conf":
80  if ($this->missingKeyInConfigMessage("Database is not well configured in <b>app/config/config.php</b>", Startup::checkDbConfig()) === false) {
81  $this->_addInfoMessage("settings", "Database is well configured");
82  }
83  break;
84  case "Connexion":
85  case "Database":
86  $this->checkDatabase($config, "database");
87  break;
88  case "Models":
89  $this->checkModels($config);
90  break;
91  case "Cache":
92  $this->checkModelsCache($config);
93  break;
94  }
95  }
96 
97  protected function checkDatabase($config, $infoIcon="database") {
98  $db=$config["database"];
99  if (!DAO::isConnected()) {
100  $this->_addErrorMessage("warning", "connection to the database is not established (probably in <b>app/config/services.php</b> file).");
101  try {
102  if ($db["dbName"] !== "") {
103  $this->_addInfoMessage($infoIcon, "Attempt to connect to the database <b>" . $db["dbName"] . "</b> ...");
104  $db=new Database($db["type"], $db["dbName"], @$db["serverName"], @$db["port"], @$db["user"], @$db["password"], @$db["options"], @$db["cache"]);
105  $db->connect();
106  }
107  } catch ( \Exception $e ) {
108  $this->_addErrorMessage("warning", $e->getMessage());
109  }
110  } else {
111  $this->_addInfoMessage($infoIcon, "The connection to the database <b>" . $db["dbName"] . "</b> is established.");
112  }
113  }
114 
115  protected function checkModels($config, $infoIcon="sticky note") {
116  if ($this->missingKeyInConfigMessage("Models namespace is not well configured in <b>app/config/config.php</b>", Startup::checkModelsConfig()) === false) {
117  $modelsNS=@$config["mvcNS"]["models"];
118  $this->_addInfoMessage($infoIcon, "Models namespace <b>" . $modelsNS . "</b> is ok.");
119  $dir=UFileSystem::cleanPathname(ROOT . DS . $modelsNS);
120  if (\file_exists($dir) === false) {
121  $this->_addErrorMessage("warning", "The directory <b>" . $dir . "</b> does not exists.");
122  } else {
123  $this->_addInfoMessage($infoIcon, "The directory for models namespace <b>" . $dir . "</b> exists.");
124  $files=CacheManager::getModelsFiles($config, true);
125  if (\sizeof($files) === 0) {
126  $this->_addErrorMessage("warning", "No file found in <b>" . $dir . "</b> folder.");
127  } else {
128  foreach ( $files as $file ) {
129  $completeName=ClassUtils::getClassFullNameFromFile($file);
130  $parts=\explode("\\", $completeName);
131  $classname=\array_pop($parts);
132  $ns=\implode("\\", $parts);
133  if ($ns !== $modelsNS) {
134  $this->_addErrorMessage("warning", "The namespace <b>" . $ns . "</b> would be <b>" . $modelsNS . "</b> for the class <b>" . $classname . "</b>.");
135  } else {
136  $this->_addInfoMessage($infoIcon, "The namespace for the class <b>" . $classname . "</b> is ok.");
137  }
138  }
139  }
140  }
141  }
142  }
143 
144  protected function checkModelsCache($config, $infoIcon="lightning") {
145  $instanceCache=CacheManager::$cache;
146  if ($instanceCache === null) {
147  $this->_addErrorMessage("warning", "Cache instance is not created.");
148  } else {
149  $this->_addInfoMessage($infoIcon, $instanceCache->getCacheInfo());
150  if (CacheManager::$cache instanceof ArrayCache) {
151  $this->checkArrayCache($config, $infoIcon);
152  } else {
153  $this->checkModelsCacheFiles($config, $infoIcon);
154  }
155  }
156  }
157 
158  protected function checkArrayCache($config, $infoIcon="lightning") {
159  if (!isset($config["cache"]) || UString::isNull($config["cache"])) {
160  self::missingKeyInConfigMessage("Cache directory is not well configured in <b>app/config/config.php</b>", [ "cache" ]);
161  } else {
162  if (!isset($config["cache"]["directory"]) || UString::isNull($config["cache"]["directory"])) {
163  self::missingKeyInConfigMessage("Cache directory is not well configured in <b>app/config/config.php</b>", [ "directory" ]);
164  } else {
165  $cacheDir=UFileSystem::cleanPathname(ROOT . DS . $config["cache"]["directory"]);
166  $this->_addInfoMessage($infoIcon, "Models cache directory is well configured in config file.");
167  $cacheDirs=CacheManager::getCacheDirectories($config, true);
168  if (\file_exists($cacheDir) === false) {
169  $this->_addErrorMessage("warning", "The cache directory <b>" . $cacheDir . "</b> does not exists.");
170  } else {
171  $modelsCacheDir=UFileSystem::cleanPathname($cacheDirs["models"]);
172  $this->_addInfoMessage($infoIcon, "Cache directory <b>" . $cacheDir . "</b> exists.");
173  if (\file_exists($modelsCacheDir) === false) {
174  $this->_addErrorMessage("warning", "The models cache directory <b>" . $modelsCacheDir . "</b> does not exists.");
175  } else {
176  $this->_addInfoMessage($infoIcon, "Models cache directory <b>" . $modelsCacheDir . "</b> exists.");
177  $this->checkModelsCacheFiles($config, $infoIcon);
178  }
179  }
180  }
181  }
182  }
183 
184  protected function checkModelsCacheFiles($config, $infoIcon="lightning") {
185  CacheManager::startProd($config);
186  $files=CacheManager::getModelsFiles($config, true);
187  foreach ( $files as $file ) {
188  $classname=ClassUtils::getClassFullNameFromFile($file);
189  if (!CacheManager::modelCacheExists($classname)) {
190  $this->_addErrorMessage("warning", "The models cache entry does not exists for the class <b>" . $classname . "</b>.");
191  } else {
192  $this->_addInfoMessage($infoIcon, "The models cache entry for <b>" . $classname . "</b> exists.");
193  }
194  }
195  }
196 
197  protected function displayAllMessages() {
198  if ($this->hasNoError()) {
199  $this->_addInfoMessage("checkmark", "everything is fine here");
200  }
201  if ($this->hasMessages()) {
202  $messagesElmInfo=$this->displayModelsMessages($this->hasNoError() ? "success" : "info", $this->messages["info"]);
203  echo $messagesElmInfo;
204  }
205  if ($this->hasError()) {
206  $messagesElmError=$this->displayModelsMessages("error", $this->messages["error"]);
207  echo $messagesElmError;
208  }
209  $this->showActions();
210  }
211 
212  protected function showActions() {
213  $buttons=$this->jquery->semantic()->htmlButtonGroups("step-actions");
214  $step=$this->getActiveModelStep();
215  switch($step[1]) {
216  case "Conf":
217  $buttons->addItem("Show config file")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/config", "#action-response")->addIcon("settings");
218  $buttons->addItem("Edit config file")->addClass("orange")->addIcon("edit");
219  break;
220  case "Connexion":
221  case "Database":
222  if ($this->engineering === "reverse")
223  $buttons->addItem("(Re-)Create database")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/showDatabaseCreation", "#main-content")->addIcon("database");
224  break;
225  case "Models":
226  if ($this->engineering === "forward")
227  $buttons->addItem("(Re-)Create models")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/createModels", "#main-content", [ "attr" => "" ])->addIcon("sticky note");
228  else {
229  $buttons->addItem("Import from Yuml")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/_importFromYuml", "#models-main", [ "attr" => "" ])->addIcon("sticky note");
230  }
231  $bt=$buttons->addItem("Classes diagram")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/_showAllClassesDiagram", "#action-response", [ "attr" => "","ajaxTransition" => "random" ]);
232  $bt->addIcon("sticky note outline");
233  if ($this->hasError())
234  $bt->addClass("disabled");
235  break;
236  case "Cache":
237  $buttons->addItem("(Re-)Init models cache")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/_initModelsCache", "#main-content")->addIcon("lightning");
238  break;
239  }
240  $nextStep=$this->getNextModelStep();
241  if (isset($nextStep)) {
242  $bt=$buttons->addItem($nextStep[1]);
243  $bt->addIcon("angle double right", false);
244  $bt->addLabel($nextStep[2], true, $nextStep[0]);
245  $bt->getContent()[1]->addClass("green");
246  if ($this->hasNoError()) {
247  $bt->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/_loadModelStep/" . $this->engineering . "/" . ($this->activeStep + 1), "#models-main");
248  } else {
249  $bt->addClass("disabled");
250  }
251  } else {
252  $bt=$buttons->addItem("See datas")->addClass("black");
253  $bt->addIcon("unhide");
254  if ($this->hasNoError()) {
255  $bt->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/models/noHeader/", "#models-main");
256  } else {
257  $bt->addClass("disabled");
258  }
259  }
260  echo "<div>" . $buttons . "</div><br>";
261  echo "<div id='action-response'></div>";
262  }
263 
264  protected function missingKeyInConfigMessage($message, $keys) {
265  if (\sizeof($keys) > 0) {
266  $this->_addErrorMessage("warning", $message . " : parameters <b>[" . \Ajax\service\JArray::implode(",", $keys) . "]</b> are required.");
267  return true;
268  }
269  return false;
270  }
271 
272  protected function _addErrorMessage($type, $content) {
273  $this->_addMessage("error", $type, $content);
274  }
275 
276  protected function _addInfoMessage($type, $content) {
277  $this->_addMessage("info", $type, $content);
278  }
279 
280  protected function _addMessage($key, $type, $content) {
281  $this->messages[$key][]=new InfoMessage($type, $content);
282  }
283 
284  protected function hasError() {
285  return \sizeof($this->messages["error"]) > 0;
286  }
287 
288  protected function hasNoError() {
289  return \sizeof($this->messages["error"]) == 0;
290  }
291 
292  protected function hasMessages() {
293  return \sizeof($this->messages["info"]) > 0;
294  }
295 
296  protected function displayMessages($type, $messagesToDisplay, $header="", $icon="") {
297  $messagesElm=$this->jquery->semantic()->htmlMessage("modelsMessages-" . $type);
298  $messagesElm->addHeader($header);
299  if ($this->hasError() && $type === "info")
300  $messagesElm->setIcon("info circle");
301  else
302  $messagesElm->setIcon($icon);
303  $messages=[ ];
304  foreach ( $messagesToDisplay as $msg ) {
305  $elm=new HtmlSemDoubleElement("", "li", "", $msg->getContent());
306  $elm->addIcon($msg->getType());
307  $messages[]=$elm;
308  }
309  $messagesElm->addList($messages);
310  $messagesElm->addClass($type);
311  return $messagesElm;
312  }
313 }
static getClassFullNameFromFile($filePathName)
get the full name (name \ namespace) of a class from its file path result example: (string) "I\Am\The...
Definition: ClassUtils.php:15
showSimpleMessage($content, $type, $icon="info", $timeout=NULL, $staticName=null)
checkModelsCacheFiles($config, $infoIcon="lightning")
Definition: CheckTrait.php:184
static startProd(&$config)
Starts the cache for production.
static getModelsFiles(&$config, $silent=false)
checkDatabase($config, $infoIcon="database")
Definition: CheckTrait.php:97
displayModelsMessages($type, $messagesToDisplay)
This class is responsible for storing Arrays in PHP files.
Definition: ArrayCache.php:13
static isConnected()
Returns true if the connection to the database is estabished.
Definition: DAO.php:390
displayMessages($type, $messagesToDisplay, $header="", $icon="")
Definition: CheckTrait.php:296
checkArrayCache($config, $infoIcon="lightning")
Definition: CheckTrait.php:158
static getCacheDirectories(&$config, $silent=false)
checkModels($config, $infoIcon="sticky note")
Definition: CheckTrait.php:115
checkModelsCache($config, $infoIcon="lightning")
Definition: CheckTrait.php:144