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