public attr / function, constructeurs __construct
[mtweb] / mw / env / modules / mw_env_helpers.php
1 <?php
2
3   class mw_env_helpers extends mw_env{
4
5     public $helpers;
6
7     public function init_helpers(){
8       $this->helpers = false;
9       $helpers_files = array();
10       if(($plugins = $this->plugins()) !== false){
11         foreach($plugins as $plugin_name => $plugin){
12           $helpers_path = $this->path("mw_dir")."plugins/".$plugin_name."/app/helpers/";
13           if(
14                $plugin["installed"]
15             && $plugin["enabled"]
16             && file_exists($helpers_path)
17             && is_dir($helpers_path)
18           ){
19             if($dh = opendir($helpers_path)){
20               while(($file = readdir($dh)) !== false){
21                 if(
22                      substr($file, 0, 1) != "."
23                   && !is_dir($helpers_path.$file)
24                   && strcmp(substr($file, -4), ".php") == 0
25                   && !isset($helpers_files[$file])
26                 ) $helpers_files[$file] = $helpers_path;
27               }
28               closedir($dh);
29             }
30             else return false;
31           }
32         }
33         $helpers_path = $this->path("mw_dir")."app/helpers/";
34         if(
35              file_exists($helpers_path)
36           && is_dir($helpers_path)
37         ){
38           if($dh = opendir($helpers_path)){
39             while(($file = readdir($dh)) !== false){
40               if(
41                    substr($file, 0, 1) != "."
42                 && !is_dir($helpers_path.$file)
43                 && strcmp(substr($file, -4), ".php") == 0
44                 && !isset($helpers_files[$file])
45               ) $helpers_files[$file] = $helpers_path;
46             }
47             closedir($dh);
48           }
49           else return false;
50         }
51       }
52       $this->helpers = array();
53       foreach($helpers_files as $file => $helpers_path){
54         $this->helpers[substr($file, 0, -4)] = array("file" => $helpers_path.$file);
55       }
56       return true;
57     }
58
59     public function helper($helper_name){
60       if(!isset($this->helpers)) $this->init_helpers();
61       if($this->helpers === false || !isset($this->helpers[$helper_name])) return false;
62       if(!isset($this->helpers[$helper_name]["file"])) return false;
63       if($this->init_helper($helper_name)) return $this->helpers[$helper_name]["impl"];
64       return false;
65     }
66
67     public function init_helper($helper_name){
68       if(!isset($this->helpers[$helper_name]["impl"])){
69         if(!class_exists($helper_name)){
70           if(
71                 !isset($this->helpers[$helper_name]["file"])
72             ||  !$this->helpers[$helper_name]["file"]
73             ||  !file_exists($this->helpers[$helper_name]["file"])
74           ) return false;
75           require_once $this->helpers[$helper_name]["file"];
76           if(!class_exists($helper_name)) return false;
77         }
78         $this->helpers[$helper_name]["impl"] = new $helper_name();
79         $this->helpers[$helper_name]["impl"]->set_env($this);
80         return true;
81       }
82       if($this->helpers[$helper_name]["impl"] === false) return false;
83       return true;
84     }
85
86   }
87
88   // -------------------------------------------------------------------------------------------
89   //                                                                             class mw_helper
90   //
91
92   abstract class mw_helper{
93
94     public $env;
95
96     public function set_env(&$env){
97       $this->env = &$env;
98     }
99
100     public function env(){
101       return $this->env;
102     }
103
104   }