nouveau module observers dans env
authordj3c1t <dj3c1t@free.fr>
Sat, 18 May 2013 20:03:57 +0000 (22:03 +0200)
committerdj3c1t <dj3c1t@free.fr>
Sat, 18 May 2013 20:03:57 +0000 (22:03 +0200)
mw/env/modules/mw_env_observers.php [new file with mode: 0644]
mw/env/modules/mw_env_run.php

diff --git a/mw/env/modules/mw_env_observers.php b/mw/env/modules/mw_env_observers.php
new file mode 100644 (file)
index 0000000..6b5a83c
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+
+  class mw_env_observers extends mw_env{
+
+    var $observers;
+
+    function init_observers(){
+      $observers_files = array();
+      if(($plugins = $this->plugins()) !== false){
+        foreach($plugins as $plugin_name => $plugin){
+          $observers_path = $this->path("mw_dir")."plugins/".$plugin_name."/app/observers/";
+          if(
+               $plugin["installed"]
+            && $plugin["enabled"]
+            && file_exists($observers_path)
+            && is_dir($observers_path)
+          ){
+            if($dh = opendir($observers_path)){
+              while(($file = readdir($dh)) !== false){
+                if(
+                     substr($file, 0, 1) != "."
+                  && !is_dir($observers_path.$file)
+                  && strcmp(substr($file, -4), ".php") == 0
+                  && !isset($observers_files[$file])
+                ) $observers_files[$file] = $observers_path;
+              }
+              closedir($dh);
+            }
+            else return false;
+          }
+        }
+        $observers_path = $this->path("mw_dir")."app/observers/";
+        if(
+             file_exists($observers_path)
+          && is_dir($observers_path)
+        ){
+          if($dh = opendir($observers_path)){
+            while(($file = readdir($dh)) !== false){
+              if(
+                   substr($file, 0, 1) != "."
+                && !is_dir($observers_path.$file)
+                && strcmp(substr($file, -4), ".php") == 0
+                && !isset($observers_files[$file])
+              ) $observers_files[$file] = $observers_path;
+            }
+            closedir($dh);
+          }
+          else return false;
+        }
+      }
+      $observers = array();
+      foreach($observers_files as $file => $observers_path){
+        $observer_name = substr($file, 0, -4);
+        if(class_exists($observer_name)) return false;
+        if(!file_exists($observer_file = $observers_path.$file)) return false;
+        require_once $observer_file;
+        if(!class_exists($observer_name)) return false;
+        $observers[$observer_name] = new $observer_name();
+      }
+      return $observers;
+    }
+
+    function call_observers($observer_call){
+      if(!isset($this->observers)){
+        $this->observers = $this->init_observers();
+      }
+      if($this->observers){
+        foreach($this->observers as $observer_name => $observer){
+          if(method_exists($observer, $observer_call)){
+            $observer->$observer_call($this);
+          }
+        }
+      }
+    }
+
+  }
+
+?>
\ No newline at end of file
index d4b63ab..30d89cf 100644 (file)
               }
               if(($controller_validate = $controller->validate($this)) === true){
                 if(($controller_prepare_inputs = $controller->prepare_inputs($this)) === true){
+                  $etat_before = $this->etat;
+                  $this->call_observers("before_action");
                   $controller->$action_method($this);
+                  $etat_after = $this->etat;
+                  $this->etat = $etat_before;
+                  $this->call_observers("after_action");
+                  $this->etat = $etat_after;
                 }
                 else $this->erreur($controller_prepare_inputs);
               }
       else $this->erreur("Impossible d'effectuer cette action");
     }
 
-    function etat($name){
+    function is_running($etat){
+      if($etat = $this->valid_etat($etat)){
+        return
+            ($etat["mod"] == $this->etat("mod"))
+        &&  ($etat["controller"] == $this->etat("controller"))
+        &&  ($etat["action"] == $this->etat("action"));
+      }
+      return false;
+    }
+
+    function etat($name = null){
+      if(!isset($name)) return $this->etat;
       return $this->etat[$name];
     }