Prechádzať zdrojové kódy

Update Extension\Extensions

Visman 1 rok pred
rodič
commit
2c94e1bc69
1 zmenil súbory, kde vykonal 116 pridanie a 10 odobranie
  1. 116 10
      app/Models/Extension/Extensions.php

+ 116 - 10
app/Models/Extension/Extensions.php

@@ -35,10 +35,8 @@ class Extensions extends Manager
      */
     protected string|array $error = '';
 
-    /**
-     * Путь до файла, который содержит данные из всех установленных расширений
-     */
     protected string $commonFile;
+    protected string $preFile;
 
     /**
      * Возвращает action (или свойство) по его имени
@@ -54,6 +52,7 @@ class Extensions extends Manager
     public function init(): Extensions
     {
         $this->commonFile = $this->c->DIR_CONFIG . '/ext/common.php';
+        $this->preFile    = $this->c->DIR_CONFIG . '/ext/pre.php';
 
         $this->fromDB();
 
@@ -237,6 +236,8 @@ class Extensions extends Manager
 
         $this->updateCommon($ext);
 
+        $this->updateIndividual();
+
         return true;
     }
 
@@ -251,6 +252,8 @@ class Extensions extends Manager
             return false;
         }
 
+        $oldStatus = $ext->dbStatus;
+
         $vars = [
             ':name' => $ext->name,
         ];
@@ -269,6 +272,10 @@ class Extensions extends Manager
 
         $this->updateCommon($ext);
 
+        if ($oldStatus) {
+            $this->updateIndividual();
+        }
+
         return true;
     }
 
@@ -283,7 +290,8 @@ class Extensions extends Manager
             return false;
         }
 
-        $result = $ext->prepare();
+        $oldStatus = $ext->dbStatus;
+        $result    = $ext->prepare();
 
         if (true !== $result) {
             $this->error = $result;
@@ -309,6 +317,10 @@ class Extensions extends Manager
 
         $this->updateCommon($ext);
 
+        if ($oldStatus) {
+            $this->updateIndividual();
+        }
+
         return true;
     }
 
@@ -323,7 +335,8 @@ class Extensions extends Manager
             return false;
         }
 
-        $result = $ext->prepare();
+        $oldStatus = $ext->dbStatus;
+        $result    = $ext->prepare();
 
         if (true !== $result) {
             $this->error = $result;
@@ -349,6 +362,10 @@ class Extensions extends Manager
 
         $this->updateCommon($ext);
 
+        if ($oldStatus) {
+            $this->updateIndividual();
+        }
+
         return true;
     }
 
@@ -378,6 +395,8 @@ class Extensions extends Manager
             'fileData' => $ext->fileData,
         ]);
 
+        $this->updateIndividual();
+
         return true;
     }
 
@@ -407,19 +426,29 @@ class Extensions extends Manager
             'fileData' => $ext->fileData,
         ]);
 
+        $this->updateIndividual();
+
         return true;
     }
 
     /**
-     * Обновляет файл с общими данными по расширениям
+     * Возвращает данные из файла с общими данными по расширениям
      */
-    protected function updateCommon(Extension $ext): bool
+    protected function loadDataFromFile(string $file): array
     {
-        if (\is_file($this->commonFile)) {
-            $data = include $this->commonFile;
+        if (\is_file($file)) {
+            return include $file;
         } else {
-            $data = [];
+            return [];
         }
+    }
+
+    /**
+     * Обновляет файл с общими данными по расширениям
+     */
+    protected function updateCommon(Extension $ext): bool
+    {
+        $data = $this->loadDataFromFile($this->commonFile);
 
         if ($ext::NOT_INSTALLED === $ext->status) {
             unset($data[$ext->name]);
@@ -449,4 +478,81 @@ class Extensions extends Manager
             return true;
         }
     }
+
+    /**
+     * Обновляет индивидуальные файлы с данными по расширениям
+     */
+    protected function updateIndividual(): bool
+    {
+        $oldPre     = $this->loadDataFromFile($this->preFile);
+        $templates  = [];
+        $commonData = $this->loadDataFromFile($this->commonFile);
+        $pre        = [];
+        $newPre     = [];
+
+        // выделение данных
+        foreach ($this->repository as $ext) {
+            if (1 !== $ext->dbStatus) {
+                continue;
+            }
+
+            if (isset($commonData[$ext->name]['templates']['pre'])) {
+                $pre = \array_merge_recursive($pre, $commonData[$ext->name]['templates']['pre']);
+            }
+        }
+
+        // PRE-данные шаблонов
+        foreach ($pre as $template => $names) {
+            $templates[$template] = $template;
+
+            foreach ($names as $name => $list) {
+                \uasort($list, function (array $a, array $b) {
+                    return $b['priority'] <=> $a['priority'];
+                });
+
+                $result = '';
+
+                foreach ($list as $value) {
+                    $result .= $value['data'];
+                }
+
+                $newPre[$template][$name] = $result;
+            }
+        }
+
+        $this->putData($this->preFile, $newPre);
+
+        // удаление скомпилированных шаблонов
+        foreach (\array_merge($this->diffPre($oldPre, $newPre), $this->diffPre($newPre, $oldPre)) as $template) {
+            $this->c->View->delete($template);
+        }
+
+        return true;
+    }
+
+    protected function diffPre(array $a, array $b): array
+    {
+        $result = [];
+
+        foreach ($a as $template => $names) {
+            if (! isset($b[$template])) {
+                $result[$template] = $template;
+
+                continue;
+            }
+
+            foreach ($names as $name => $value) {
+                if (
+                    ! isset($b[$template][$name])
+                    || $value !== $b[$template][$name]
+                ) {
+                    $result[$template] = $template;
+
+                    continue 2;
+                }
+            }
+        }
+
+        return $result;
+    }
 }