From: dj3c1t Date: Sat, 18 May 2013 20:03:57 +0000 (+0200) Subject: nouveau module observers dans env X-Git-Tag: mtweb.0.8.0~1 X-Git-Url: http://git.dj3c1t.com/index.cgi?a=commitdiff_plain;h=e3d5bcff19bdf8bed721635096d71503f0a3fd6d;p=mtweb nouveau module observers dans env --- diff --git a/mw/env/modules/mw_env_observers.php b/mw/env/modules/mw_env_observers.php new file mode 100644 index 0000000..6b5a83c --- /dev/null +++ b/mw/env/modules/mw_env_observers.php @@ -0,0 +1,78 @@ +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 diff --git a/mw/env/modules/mw_env_run.php b/mw/env/modules/mw_env_run.php index d4b63ab..30d89cf 100644 --- a/mw/env/modules/mw_env_run.php +++ b/mw/env/modules/mw_env_run.php @@ -152,7 +152,13 @@ } 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); } @@ -167,7 +173,18 @@ 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]; }