浏览代码

Preparing hooks & router

isundil 10 年之前
父节点
当前提交
fbec34e894
共有 6 个文件被更改,包括 84 次插入17 次删除
  1. 18 13
      core/models/ModelBase.php
  2. 8 1
      core/models/ModuleHook.php
  3. 4 0
      core/tools/Context.php
  4. 31 0
      core/tools/Hooks.php
  5. 16 3
      core/tools/ModuleManager.php
  6. 7 0
      core/tools/Router.php

+ 18 - 13
core/models/ModelBase.php

@@ -97,6 +97,8 @@ abstract class ModelBase
 
     public function save()
     {
+        \Tools\Hook::trigger("onBeforeEntitySave");
+        \Tools\Hook::trigger("onBeforeEntitySave".$this->getTableName());
         //TODO send pre-hook
         if ($this->id === null)
         {
@@ -117,21 +119,24 @@ abstract class ModelBase
         }
         else
         {
-            if (empty($this->changed))
-                return true;
-            if ($this->id === false)
-                throw new \Exception("Cannot update private row");
-            $query = "UPDATE {$this->getTableName()} SET ";
-            $newValues = array();
-            foreach ($this->changed as $i => $j)
-                $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
-            $query .= implode(",",$newValues)." WHERE id={$this->id}";
-            $result = self::$dbo->exec($query);
-            if (!$result)
-                throw new \Exception(self::$dbo->errorInfo()[2]);
-            $this->changed = array();
+            if (!empty($this->changed))
+            {
+                if ($this->id === false)
+                    throw new \Exception("Cannot update private row");
+                $query = "UPDATE {$this->getTableName()} SET ";
+                $newValues = array();
+                foreach ($this->changed as $i => $j)
+                    $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
+                $query .= implode(",",$newValues)." WHERE id={$this->id}";
+                $result = self::$dbo->exec($query);
+                if (!$result)
+                    throw new \Exception(self::$dbo->errorInfo()[2]);
+                $this->changed = array();
+            }
         }
         //TODO send hook
+        \Tools\Hook::trigger("onAfterEntitySave");
+        \Tools\Hook::trigger("onAfterEntitySave".$this->getTableName());
     }
 
     public function selects($criteria = null)

+ 8 - 1
core/models/ModuleHook.php

@@ -7,7 +7,7 @@ class ModuleHook extends ModelBase
     protected function install()
     {
         $dbPrefix = $this->getDbPrefix();
-        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}module_hook` (
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}modulehook` (
             `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
             `module_id` INTEGER(11) UNSIGNED NOT NULL,
             `hookName` VARCHAR(32) NOT NULL,
@@ -19,5 +19,12 @@ class ModuleHook extends ModelBase
             throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
         return true;
     }
+
+    public static function getModules($moduleIds)
+    {
+        $fetcher = new self();
+        //TODO order by
+        return $fetcher->selects(array("module_id" => $moduleIds));
+    }
 }
 

+ 4 - 0
core/tools/Context.php

@@ -4,6 +4,7 @@ namespace Tools;
 
 class Context
 {
+    private $hookManager;
     private $moduleManager;
     private $router;
     private $cart;
@@ -11,6 +12,7 @@ class Context
 
     public function __construct()
     {
+        $this->hookManager = new Hooks($this);
         $this->router = new Router();
         if (!\Entity\ModelBase::init())
         {
@@ -19,6 +21,7 @@ class Context
         }
         $this->router->init();
         $this->moduleManager = new ModuleManager($this);
+        $this->hookManager->trigger("routerSetup");
         $this->router->serveUrl();
     }
 
@@ -30,6 +33,7 @@ class Context
             case "cart": return $this->cart; break;
             case "user": return $this->user; break;
             case "moduleManager": return $this->moduleManager; break;
+            case "hookManager": return $this->hookManager; break;
         }
         throw new \Exception("Cannot access attribute {$key}");
     }

+ 31 - 0
core/tools/Hooks.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace Tools;
+
+class Hooks
+{
+    private $context;
+
+    public function __construct($context)
+    {
+        $this->context = $context;
+    }
+
+    public function register($module, $hookName)
+    {
+        if (!\Tools\ModuleManager::isInstalling())
+            throw new \Exception("You can only register hooks while installing");
+        //TODO
+    }
+
+    public function trigger($hookName)
+    {
+        echo "Triggering hook `{$hookName}'";
+        //TODO
+    }
+
+    public function loadHooks($hookEntities)
+    {
+    }
+}
+

+ 16 - 3
core/tools/ModuleManager.php

@@ -12,18 +12,21 @@ class ModuleManager
         $this->context = $context;
         $modulesRoot = $context->router->getModulesPath();
         $modules = \Entity\Module::getActivated();
+        $ids = array();
         foreach ($modules as $i)
         {
             $modulePath = "{$modulesRoot}{$i->directory}/main.php";
-            if (file_exists($modulePath))
-                $this->loadModule($modulePath, $i);
+            if (file_exists($modulePath) && $this->loadModule($modulePath, $i))
+            {
+                $ids[] = $i->id;
+            }
             else
             {
                 $i->active = false;
                 $i->save();
             }
         }
-        var_dump($this->modules);
+        \Tools\Hooks::loadHooks(\Entity\ModuleHook::getModules($ids));
     }
 
     private function loadModule($path, $module)
@@ -44,5 +47,15 @@ class ModuleManager
             $this->loadModule($path);
         }
     }
+
+    public function __get($key)
+    {
+        switch ($key)
+        {
+            case "modules":
+                return $this->modules; break;
+        }
+        throw new \Exception("Cannot access attribute {$key}");
+    }
 }
 

+ 7 - 0
core/tools/Router.php

@@ -30,6 +30,13 @@ class Router
 
     public function serveUrl()
     {
+        $this->prepareUrl();
+        //TODO
+    }
+
+    private function prepareUrl()
+    {
+        //TODO add internal route config
     }
 }