1 | <?php |
---|
2 | |
---|
3 | require_once "service.php"; |
---|
4 | |
---|
5 | class Controller { |
---|
6 | |
---|
7 | var $config; |
---|
8 | var $className; |
---|
9 | var $parent; |
---|
10 | var $target; |
---|
11 | var $cache; |
---|
12 | var $service; |
---|
13 | var $includes; |
---|
14 | |
---|
15 | public static function newInstance( $className, $id = false, $parents = false ) |
---|
16 | { |
---|
17 | return new Controller( $className, $id, $parents ); |
---|
18 | } |
---|
19 | |
---|
20 | public function __construct( $className, $id = false, $parents = false ) |
---|
21 | { |
---|
22 | $this->className = $className; |
---|
23 | |
---|
24 | if( is_array( $id ) ) |
---|
25 | { |
---|
26 | $parents = $id; |
---|
27 | $id = null; |
---|
28 | } |
---|
29 | |
---|
30 | $this->parents = $parents; |
---|
31 | $this->target = $id; |
---|
32 | $this->includes = array(); |
---|
33 | |
---|
34 | include_once( "cache/MemoryCache.php" ); |
---|
35 | $this->cache = new MemoryCache(); |
---|
36 | |
---|
37 | $this->config = $this->cache->get( "$className.ini" ); |
---|
38 | |
---|
39 | if( !$this->config ) |
---|
40 | { |
---|
41 | $this->config = parse_ini_file( "config/$className.ini", true ); |
---|
42 | |
---|
43 | $this->cache->put( "$className.ini", $this->config[$className] ); |
---|
44 | } |
---|
45 | |
---|
46 | $this->service = $this->load( "service" ); |
---|
47 | |
---|
48 | if( $this->service ) |
---|
49 | $this->service->connect( $this->config["config"] ); |
---|
50 | } |
---|
51 | |
---|
52 | public function __destruct() |
---|
53 | { |
---|
54 | if( $this->service ) |
---|
55 | $this->service->close(); |
---|
56 | } |
---|
57 | |
---|
58 | public function load( $type ) |
---|
59 | { |
---|
60 | if( !$this->config[$type] ) |
---|
61 | return( false ); |
---|
62 | |
---|
63 | return $this->import( $this->config[$type]["path"], |
---|
64 | $this->config[$type]["class"] ); |
---|
65 | } |
---|
66 | |
---|
67 | public function import( $path, $class ) |
---|
68 | { |
---|
69 | if( !$class ){ |
---|
70 | preg_match( "/^\/?.*\/([^\/]+).php$/", $path, $class ); |
---|
71 | $class = $class[1]; |
---|
72 | } |
---|
73 | |
---|
74 | if( !$this->includes[$path] ) |
---|
75 | { |
---|
76 | include_once( $path ); |
---|
77 | $this->includes[ $path ] = true; |
---|
78 | } |
---|
79 | |
---|
80 | $object = $this->cache->get( $class ); |
---|
81 | |
---|
82 | if( $object ) |
---|
83 | return( $object ); |
---|
84 | |
---|
85 | $object = new $class(); |
---|
86 | $this->cache->put( $class, $object ); |
---|
87 | |
---|
88 | return( $object ); |
---|
89 | } |
---|
90 | |
---|
91 | public function clearAll() |
---|
92 | { |
---|
93 | return $this->cache->clearAll(); |
---|
94 | } |
---|
95 | |
---|
96 | public function clear( $id ) |
---|
97 | { |
---|
98 | return $this->cache->clear( $id ); |
---|
99 | } |
---|
100 | |
---|
101 | public function get( $id ) |
---|
102 | { |
---|
103 | return $this->cache->get( $id ); |
---|
104 | } |
---|
105 | |
---|
106 | public function put( $id, $data, $expires, $compressed ) |
---|
107 | { |
---|
108 | return $this->cache->put( $id, $data, $expires, $compressed ); |
---|
109 | } |
---|
110 | |
---|
111 | public function fireEvent( $event, $method, $params ) |
---|
112 | { |
---|
113 | if( !$this->config["$event.$method"] ) |
---|
114 | return( $params ); |
---|
115 | |
---|
116 | $original = $params; |
---|
117 | |
---|
118 | foreach( $this->config["$event.$method"] as $intercept => $interceptor ) |
---|
119 | { |
---|
120 | if( is_string( $interceptor ) ) |
---|
121 | $this->config["$event.$method"][$intercept] = $interceptor = $this->import( $interceptor ); |
---|
122 | |
---|
123 | $return = $interceptor->$intercept( $original, $params ); |
---|
124 | |
---|
125 | if( $return === false ) |
---|
126 | return( false ); |
---|
127 | |
---|
128 | if( $return ) |
---|
129 | $params = $return; |
---|
130 | } |
---|
131 | |
---|
132 | return( $params ); |
---|
133 | } |
---|
134 | |
---|
135 | public function find( $params, $criteria = false ) |
---|
136 | { |
---|
137 | |
---|
138 | if( ($params = $this->fireEvent( "before", "find", $params )) === false ) |
---|
139 | return( false ); |
---|
140 | |
---|
141 | if( $this->service ) |
---|
142 | { |
---|
143 | if( $this->target ) |
---|
144 | $result = $this->service->retrieve( $this->className, $this->target, $this->parents, $params, $criteria ); |
---|
145 | else |
---|
146 | $result = $this->service->find( $this->className, $this->parents, $params, $criteria ); |
---|
147 | |
---|
148 | if( $result ) |
---|
149 | $params = $result; |
---|
150 | } |
---|
151 | |
---|
152 | if( ($result = $this->fireEvent( "after", "find", $params )) === false ) |
---|
153 | return( false ); |
---|
154 | |
---|
155 | $result = json_encode( $result ); |
---|
156 | |
---|
157 | return( $result ); |
---|
158 | } |
---|
159 | |
---|
160 | public function update( $params, $criteria = false ) |
---|
161 | { |
---|
162 | |
---|
163 | if( !($params = $this->fireEvent( "before", "update", $params )) ) |
---|
164 | return( false ); |
---|
165 | |
---|
166 | if( $this->service ) |
---|
167 | { |
---|
168 | if( is_string( $idOrfilter ) ) |
---|
169 | $result = $this->service->update( $this->className, $this->target, $this->parents, $params, $criteria ); |
---|
170 | else |
---|
171 | $result = $this->service->replace( $this->className, $this->parents, $params, $criteria ); |
---|
172 | |
---|
173 | if( $result ) |
---|
174 | $params = $result; |
---|
175 | } |
---|
176 | |
---|
177 | if( !($result = $this->fireEvent( "after", "update", $params )) ) |
---|
178 | return( false ); |
---|
179 | |
---|
180 | $result = json_encode( $result ); |
---|
181 | |
---|
182 | return( $result ); |
---|
183 | } |
---|
184 | |
---|
185 | public function delete( $params, $criteria = false ) |
---|
186 | { |
---|
187 | if( ($params = $this->fireEvent( "before", "delete", $params )) === false ) |
---|
188 | return( false ); |
---|
189 | |
---|
190 | if( $this->service ) |
---|
191 | { |
---|
192 | if( is_string( $idOrfilter ) ) |
---|
193 | $result = $this->service->delete( $this->className, $this->target, $this->parents, $params, $criteria ); |
---|
194 | else |
---|
195 | $result = $this->service->deleteAll( $this->className, $this->parents, $params, $criteria ); |
---|
196 | |
---|
197 | if( $result ) |
---|
198 | $params = $result; |
---|
199 | } |
---|
200 | |
---|
201 | if( ($result = $this->fireEvent( "after", "delete", $params )) === false ) |
---|
202 | return( false ); |
---|
203 | |
---|
204 | $result = json_encode( $result ); |
---|
205 | |
---|
206 | return( $result ); |
---|
207 | } |
---|
208 | |
---|
209 | public function create( $params ) |
---|
210 | { |
---|
211 | if( ($params = $this->fireEvent( "before", "create", $params )) === false ) |
---|
212 | return( false ); |
---|
213 | |
---|
214 | if( $this->service ) |
---|
215 | { |
---|
216 | $result = $this->service->create( $this->className, $this->parents, $params ); |
---|
217 | |
---|
218 | if( $result ) |
---|
219 | $params = $result; |
---|
220 | } |
---|
221 | |
---|
222 | if( ($result = $this->fireEvent( "after", "create", $params )) === false ) |
---|
223 | return( false ); |
---|
224 | |
---|
225 | $result = json_encode( $result ); |
---|
226 | |
---|
227 | return( $result ); |
---|
228 | } |
---|
229 | } |
---|
230 | ?> |
---|