Merge branch 'maint/7.0'
[ninja.git] / src / op5 / ninja_sdk / js_class_generator.php
blob8a5746562b67dcbad839f338ffd38d2a29df8772
1 <?php
3 require_once( 'generator_lib.php' );
5 abstract class js_class_generator {
6 protected $fp;
7 protected $indent_lvl = array();
8 protected $class_suffix = '';
9 protected $class_dir = 'js';
10 protected $class_basedir = '.';
11 protected $classname;
13 public function generate( $skip_generated_note = false ) {
14 $class_dir = dirname( $this->get_filename() );
16 if( !is_dir( $class_dir ) && !mkdir( $class_dir, 0755, true ) )
17 throw new GeneratorException( "Could not create dir $class_dir" );
19 $this->fp = fopen( $this->get_filename(), 'w' );
21 if( $this->fp === false )
22 throw new GeneratorException( "Could not open ".$this->get_filename()." for writing" );
24 if( !$skip_generated_note )
25 $this->comment( "\nNOTE!\n\nThis is an auto generated file. Changes to this file will be overwritten!\n" );
28 public function set_class_suffix( $class_suffix ) {
29 $this->class_suffix = $class_suffix;
32 public function set_class_dir( $class_dir ) {
33 $this->class_dir = $class_dir;
36 public function set_basedir( $class_basedir ) {
37 $this->class_basedir = $class_basedir;
40 public function exists() {
41 return file_exists( $this->get_filename() );
44 public function get_classname() {
45 return $this->classname . $this->class_suffix;
48 public function get_filename() {
49 return $this->class_basedir . DIRECTORY_SEPARATOR . $this->class_dir . DIRECTORY_SEPARATOR . $this->classname . '.js';
52 public function init_class( $args = array() ) {
53 $argstr = implode(', ', $args);
54 $this->write();
55 $this->write( 'var '.$this->get_classname() . ' = function '.$this->get_classname().'('.$argstr.'){' );
58 public function finish_class() {
59 $this->write( "};" );
62 public function variable( $name, $default = null ) {
63 $this->write( "this.$name = %s;", $default );
66 public function init_function( $name, $args = array() ) {
67 $argstr = implode(', ', $args);
68 if( $name === false ) {
69 $this->write( "function($argstr) {" );
70 } else {
71 $this->write( "this.$name = function($argstr) {" );
75 public function finish_function() {
76 $this->write( "};" );
77 $this->write();
80 public function comment( $comment ) {
81 $lines = explode( "\n", $comment );
82 $curlvl = array_sum( $this->indent_lvl );
83 foreach( $lines as $line ) {
84 fwrite( $this->fp, str_repeat( "\t", $curlvl ) . "// " . trim($line) . "\n" );
87 public function write( $block = '' ) {
88 $args = func_get_args();
89 $block = array_shift( $args );
90 $args_str = array_map( function($var){return json_encode($var);}, $args );
91 $block = vsprintf($block,$args_str);
93 $lines = explode( "\n", $block );
94 foreach( $lines as $line ) {
95 for($i=substr_count( $line, '}' ); $i>0; $i--)
96 array_pop( $this->indent_lvl );
97 $curlvl = array_sum( $this->indent_lvl );
98 if( substr( trim($line), 0, 4) == 'case' || substr( trim($line), 0, 8) == 'default:' )
99 $curlvl--;
100 fwrite( $this->fp, str_repeat( "\t", $curlvl ) . $line . "\n" );
101 for($i=substr_count( $line, '{' ); $i>0; $i--)
102 $this->indent_lvl[] = (strpos( $line, "switch" ) !== false) ? 2 : 1;