3 class ORMObjectGenerator
extends class_generator
{
14 public function __construct( $name, $structure ) {
16 $this->structure
= $structure[$name];
17 $this->key
= $this->structure
['key'];
18 $this->classname
= 'Base'.$this->structure
['class'];
20 $this->associations
= array();
22 foreach( $structure as $table => $tbl_struct ) {
23 foreach( $tbl_struct['structure'] as $name => $type ) {
24 if( is_array( $type ) ) {
25 if( $type[0] == $this->structure
['class'] ) {
26 $this->associations
[] = array(
29 substr( $type[1], 0, -1 ) // Drop last _
44 public function generate($skip_generated_note = false) {
45 parent
::generate($skip_generated_note);
46 $this->init_class( 'Object', array('abstract') );
47 $this->variable( '_table', $this->name
, 'protected' );
51 foreach( $this->structure
['structure'] as $field => $type ) {
52 if( is_array($type) ) {
53 $this->{"storage_object"}( $field, $type );
55 $this->{"storage_$type"}( $field );
59 $this->generate_construct();
60 $this->generate_get_key();
62 /* Getters and setters */
63 foreach( $this->structure
['structure'] as $field => $type ) {
64 if( is_array($type) ) {
65 $this->{"get_object"}( $field, $type );
67 $this->{"get_$type"}( $field );
71 foreach( $this->associations
as $assoc ) {
72 $this->generate_association_get_set( $assoc[0], $assoc[1], $assoc[2] );
75 $this->finish_class();
79 * Generates a class construct
83 private function generate_construct() {
84 $this->init_function( "__construct", array( 'values', 'prefix', 'export' ) );
85 $this->write( '$this->export = array();' );
86 $this->write( '$subobj_export = array();' );
87 $this->write( 'if($export === false) $export = array();'); //FIXME
88 $this->write( 'foreach( $export as $expcol ) {');
89 $this->write( '$parts = explode(".", $expcol, 2);');
90 $this->write( 'if(count($parts) == 2) {');
91 $this->write( 'if(!isset($subobj_export[$parts[0]])) {');
92 $this->write( '$subobj_export[$parts[0]] = array();');
94 $this->write( '$subobj_export[$parts[0]][] = $parts[1];');
95 $this->write( '$this->export[] = $parts[0];');
96 $this->write( '} else {');
97 $this->write( '$this->export[] = $parts[0];');
100 $this->comment('If object fields exists, make sure the object only exists in the export array once');
101 $this->write( '$this->export = array_unique($this->export);');
102 foreach( $this->structure
['structure'] as $field => $type ) {
103 $backend_name = $field;
104 if(isset($this->structure
['rename']) && isset($this->structure
['rename'][$field])) {
105 $backend_name = $this->structure
['rename'][$field];
107 if( is_array($type) ) {
108 $this->{"fetch_object"}( $field, $backend_name, $type );
110 $this->{"fetch_$type"}( $field, $backend_name );
113 $this->write( 'parent::__construct( $values, $prefix, $export ); ');
114 $this->finish_function();
118 * Generate association get set
120 * @param $table string
121 * @param $class string
122 * @param field string
125 private function generate_association_get_set($table, $class, $field) {
126 $this->init_function('get_'.$table.'_set');
127 $this->write('$set = '.$class.'Pool'.self
::$model_suffix.'::all();');
128 foreach( $this->key
as $key_field ) {
129 $this->write('$set = $set->reduce_by(%s,$this->get_'.$key_field.'(),"=");', $field.'.'.$key_field);
131 $this->write('return $set;');
132 $this->finish_function();
140 private function generate_get_key() {
141 $this->init_function("get_key");
142 $matchline = '$key = $this%s;';
144 foreach( $this->key
as $keypart ) {
145 // Build getter sequence
147 foreach( explode('.',$keypart) as $part ) {
148 $call .= "->get_$part()";
151 // Use sprintf instead of embedded in write. write escapes
152 $this->write( sprintf( $matchline, $call ) );
154 $matchline = '$key .= ";".$this%s;';
157 $this->write('return $key;');
159 $this->write('return false;');
161 $this->finish_function();
165 * Writes a private variable to class
167 * @param $name string
168 * @param $type string Unused?
171 private function storage_object( $name, $type ) {
172 $this->write( "private \$$name = false;" );
176 * Writes an object fetcher
178 * @param $name string
179 * @param $type string
182 private function fetch_object( $name, $backend_name, $type ) {
183 list( $class, $prefix ) = $type;
184 // Livestatus handles only one level of prefixes... might change in future? (for example comments: service.host.name should be host.name
185 $this->write( "\$this->$name = new $class".self
::$model_suffix."( \$values, %s, isset(\$subobj_export[%s]) ? \$subobj_export[%s] : array() );", $prefix, $backend_name, $name );
189 * Writes function getting object named $name
191 * @param $name string
192 * @param $type string Unused?
195 private function get_object( $name, $type ) {
196 $this->init_function( "get_$name" );
197 $this->write( "return \$this->$name;" );
198 $this->finish_function();
202 * undocumented function
206 private function storage_string( $name ) {
207 $this->write( "private \$$name = false;" );
211 * undocumented function
215 private function fetch_string( $name, $backend_name ) {
216 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) { ");
217 $this->write( "\$this->$name = (string)\$values[\$prefix.'$backend_name'];" );
222 * undocumented function
226 private function get_string( $name ) {
227 $this->init_function( "get_$name" );
228 $this->write( "return \$this->$name;" );
229 $this->finish_function();
235 * undocumented function
239 private function storage_time( $name ) {
240 $this->write( "private \$$name = false;" );
244 * undocumented function
248 private function fetch_time( $name, $backend_name ) {
249 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) { ");
250 $this->write( "\$this->$name = \$values[\$prefix.'$backend_name'];" );
255 * undocumented function
259 private function get_time( $name ) {
260 $this->init_function( "get_$name" );
261 $this->write( "return \$this->$name;" );
262 $this->finish_function();
268 * undocumented function
272 private function storage_int( $name ) {
273 $this->write( "private \$$name = false;" );
277 * undocumented function
281 private function fetch_int( $name, $backend_name ) {
282 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
283 $this->write( "\$this->$name = intval( \$values[\$prefix.'$backend_name'] );" );
288 * undocumented function
292 private function get_int( $name ) {
293 $this->init_function( "get_$name" );
294 $this->write( "return \$this->$name;" );
295 $this->finish_function();
301 * undocumented function
305 private function storage_float( $name ) {
306 $this->write( "private \$$name = false;" );
310 * undocumented function
314 private function fetch_float( $name, $backend_name ) {
315 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
316 $this->write( "\$this->$name = floatval( \$values[\$prefix.'$backend_name'] );" );
321 * undocumented function
325 private function get_float( $name ) {
326 $this->init_function( "get_$name" );
327 $this->write( "return \$this->$name;" );
328 $this->finish_function();
334 * undocumented function
338 private function storage_list( $name ) {
339 $this->write( "private \$$name = false;" );
343 * undocumented function
347 private function fetch_list( $name, $backend_name ) {
348 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
349 $this->write( "\$this->$name = \$values[\$prefix.'$backend_name'];" );
354 * undocumented function
358 private function get_list( $name ) {
359 $this->init_function( "get_$name" );
360 $this->write( "return \$this->$name;" );
361 $this->finish_function();
367 * undocumented function
371 private function storage_dict( $name ) {
372 $this->write( "private \$$name = false;" );
376 * undocumented function
380 private function fetch_dict( $name, $backend_name ) {
381 $this->write( "if(array_key_exists(\$prefix.'$backend_name', \$values)) {" );
382 $this->write( "\$this->$name = \$values[\$prefix.'$backend_name'];" );
387 * undocumented function
391 private function get_dict( $name ) {
392 $this->init_function( "get_$name" );
393 $this->write( "return \$this->$name;" );
394 $this->finish_function();