3 require_once( 'generator_lib.php' );
5 abstract class class_generator
{
7 protected $indent_lvl = array();
8 protected $class_suffix = '';
9 protected $class_dir = false;
10 protected $class_basedir = false;
13 /* Overwrite those to make compatible with framework naming convention */
14 public static $model_suffix = '';
15 public static $library_suffix = '';
16 public static $library_dir = false;
17 public static $model_dir = false;
18 public static $manifest_dir = false;
20 protected $filename_lowercase = true;
22 public function generate( $skip_generated_note = false ) {
23 $class_dir = dirname( $this->get_filename() );
25 if( !is_dir( $class_dir ) && !mkdir( $class_dir, 0755, true ) )
26 throw new GeneratorException( "Could not create dir $class_dir" );
28 $this->fp
= fopen( $this->get_filename(), 'w' );
30 if( $this->fp
=== false )
31 throw new GeneratorException( "Could not open ".$this->get_filename()." for writing" );
33 /* Hardcode, so we don't accidentaly add whitespace before < */
34 fwrite( $this->fp
, "<?php\n\n" );
36 if( !$skip_generated_note )
37 $this->comment( "\nNOTE!\n\nThis is an auto generated file. Changes to this file will be overwritten!\n" );
40 public function generate_getter($name) {
41 $this->init_function('get_'.$name);
42 $this->write('return $this->'.$name.';');
43 $this->finish_function();
46 public function set_class_suffix( $class_suffix ) {
47 $this->class_suffix
= $class_suffix;
50 public function set_class_dir( $class_dir ) {
51 $this->class_dir
= $class_dir;
54 public function set_basedir( $class_basedir ) {
55 $this->class_basedir
= $class_basedir;
58 public function set_library() {
59 $this->set_class_suffix( self
::$library_suffix );
60 $this->set_basedir( self
::$library_dir );
61 $this->filename_lowercase
= false;
64 public function set_model() {
65 $this->set_class_suffix( self
::$model_suffix );
66 $this->set_basedir( self
::$model_dir );
69 public function set_manifest() {
70 $this->set_class_suffix( '' );
71 $this->set_basedir( self
::$manifest_dir );
74 public function exists() {
75 return file_exists( $this->get_filename() );
78 public function get_classname() {
79 return $this->classname
. $this->class_suffix
;
82 public function get_include_path() {
83 $filename = $this->classname
;
84 if( $this->filename_lowercase
) {
85 $filename = strtolower( $this->classname
);
88 if($this->class_dir
!== false) {
89 $path .= $this->class_dir
. DIRECTORY_SEPARATOR
;
91 return $path . $filename . '.php';
94 public function get_filename() {
95 $filename = $this->classname
;
96 if( $this->filename_lowercase
) {
97 $filename = strtolower( $this->classname
);
100 if($this->class_basedir
!== false) {
101 $path .= $this->class_basedir
. DIRECTORY_SEPARATOR
;
103 if($this->class_dir
!== false) {
104 $path .= $this->class_dir
. DIRECTORY_SEPARATOR
;
106 return $path . $filename . '.php';
109 public function classfile( $path, $relative_to_file=false ) {
110 if($relative_to_file) {
111 $filephp = var_export(DIRECTORY_SEPARATOR
.$path, true);
112 $filephp = 'dirname(__FILE__).'.$filephp;
114 $filephp = var_export($path, true);
116 $this->write( 'require_once( '.$filephp.' );' );
119 public function init_class( $parent = false, $modifiers = array(), $interfaces = false ) {
120 if( is_array( $modifiers ) ) {
121 $modifiers = implode( ' ', $modifiers );
123 if( !empty( $modifiers ) ) {
124 $modifiers = trim($modifiers)." ";
127 if( $interfaces !== false ) {
128 $interface_str = " implements ".implode(', ',$interfaces);
132 $this->write( "/**\n * Autogenerated class ".$this->get_classname()."\n *\n * @todo: documentation\n */" );
133 $this->write( $modifiers."class ".$this->get_classname().($parent===false?
"":" extends ".$parent.$this->class_suffix
).$interface_str." {" );
136 public function finish_class() {
140 public function variable( $name, $default = null, $visibility = 'private' ) {
142 if( false!==strpos( $visibility, 'public' ) ||
false!==strpos( $visibility, 'protected' ) ) {
143 $this->write( "/**\n * Autogenerated varible\n *\n * @todo: documentation\n */" );
145 $this->write( "$visibility \$$name = " . var_export( $default, true ) . ";" );
148 public function abstract_function( $name, $args = array(), $modifiers = array(), $defaults = array() ) {
149 if( !is_array( $modifiers ) ) {
150 $modifiers = array_filter( array_map( 'trim', explode(' ',$modifiers) ) );
152 if( !in_array('public',$modifiers) && !in_array('private',$modifiers) && !in_array('protected',$modifiers) ) {
153 $modifiers[] = 'public';
157 foreach( $args as $arg ) {
158 $argstr .= $argdelim.'$'.$arg;
159 if( isset($defaults[$arg]) )
160 $argstr .= '='.var_export($defaults[$arg],true);
164 if( in_array('public',$modifiers) ||
in_array('protected',$modifiers) ) {
165 $this->write( "/**\n * Autogenerated function\n *\n * @todo: documentation\n */" );
167 $modifiers = implode( ' ', $modifiers );
168 if( !empty( $modifiers ) ) {
169 $modifiers = trim($modifiers)." ";
171 $this->write( "abstract ${modifiers}function $name($argstr);" );
175 public function init_function( $name, $args = array(), $modifiers = array(), $defaults = array() ) {
176 if( !is_array( $modifiers ) ) {
177 $modifiers = array_filter( array_map( 'trim', explode(' ',$modifiers) ) );
179 if( !in_array('public',$modifiers) && !in_array('private',$modifiers) && !in_array('protected',$modifiers) ) {
180 $modifiers[] = 'public';
184 foreach( $args as $arg ) {
185 $argstr .= $argdelim.'$'.$arg;
186 if( isset($defaults[$arg]) )
187 $argstr .= '='.var_export($defaults[$arg],true);
191 if( in_array('public',$modifiers) ||
in_array('protected',$modifiers) ) {
192 $this->write( "/**\n * Autogenerated function\n *\n * @todo: documentation\n */" );
194 $modifiers = implode( ' ', $modifiers );
195 if( !empty( $modifiers ) ) {
196 $modifiers = trim($modifiers)." ";
198 $this->write( "${modifiers}function $name($argstr) {" );
201 public function finish_function() {
206 public function comment( $comment ) {
207 $lines = explode( "\n", $comment );
208 $curlvl = array_sum( $this->indent_lvl
);
209 foreach( $lines as $line ) {
210 fwrite( $this->fp
, str_repeat( "\t", $curlvl ) . "// " . trim($line) . "\n" );
213 public function write( $block = '' ) {
214 $args = func_get_args();
215 $block = array_shift( $args );
216 $args_str = array_map( function($var){
217 return var_export($var,true);
219 $block = vsprintf($block,$args_str);
221 $lines = explode( "\n", $block );
222 foreach( $lines as $line ) {
223 for($i=substr_count( $line, '}' ); $i>0; $i--)
224 array_pop( $this->indent_lvl
);
225 $curlvl = array_sum( $this->indent_lvl
);
226 if( substr( trim($line), 0, 4) == 'case' ) $curlvl--;
227 fwrite( $this->fp
, str_repeat( "\t", $curlvl ) . $line . "\n" );
228 for($i=substr_count( $line, '{' ); $i>0; $i--)
229 $this->indent_lvl
[] = (strpos( $line, "switch" ) !== false) ?
2 : 1;