Coverage Report - org.webslinger.ext.quercus.bsf.QuercusCompiler
 
Classes in this File Line Coverage Branch Coverage Complexity
QuercusCompiler
78%
54/69
100%
5/5
0
 
 1  
 package org.webslinger.ext.quercus.bsf;
 2  
 
 3  
 import java.io.OutputStreamWriter;
 4  
 import java.util.HashMap;
 5  
 
 6  
 import org.apache.bsf.BSFDeclaredBean;
 7  
 import org.apache.bsf.BSFException;
 8  
 
 9  
 import com.caucho.quercus.Quercus;
 10  
 import com.caucho.quercus.env.Env;
 11  
 import com.caucho.quercus.env.QuercusClass;
 12  
 import com.caucho.quercus.env.Value;
 13  
 import com.caucho.quercus.program.QuercusProgram;
 14  
 import com.caucho.quercus.program.ClassDef;
 15  
 import com.caucho.quercus.program.AbstractFunction;
 16  
 
 17  
 import org.webslinger.bsf.ApplyKey;
 18  
 import org.webslinger.bsf.Compiler;
 19  
 import org.webslinger.bsf.EvalKey;
 20  
 import org.webslinger.bsf.ExecKey;
 21  
 import org.webslinger.bsf.Key;
 22  
 import org.webslinger.util.GeneratedResult;
 23  
 
 24  
 import org.webslinger.ext.quercus.LocalEnv;
 25  
 
 26  9
 public final class QuercusCompiler extends Compiler<Value, QuercusClass> {
 27  1
     private static final HashMap<Class, String> compiledClassNamesMap = new HashMap<Class, String>();
 28  
     static {
 29  1
         compiledClassNamesMap.put(ApplyKey.class, "BSFQuercusApply");
 30  1
         compiledClassNamesMap.put(EvalKey.class, "BSFQuercusEval");
 31  1
         compiledClassNamesMap.put(ExecKey.class, "BSFQuercusExec");
 32  1
     }
 33  
 
 34  
     private final QuercusEngine phpEngine;
 35  
 
 36  
     public QuercusCompiler(QuercusEngine engine) {
 37  3
         super(engine);
 38  3
         phpEngine = engine;
 39  3
     }
 40  
 
 41  
     public Quercus getQuercus() {
 42  9
         return phpEngine.getQuercus();
 43  
     }
 44  
 
 45  
     public void declareBean(BSFDeclaredBean bean) throws BSFException {
 46  0
         clear();
 47  0
     }
 48  
 
 49  
     public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
 50  0
         clear();
 51  0
     }
 52  
 
 53  
     protected CompilerContext newContext(Key<QuercusClass> key) throws BSFException {
 54  3
         return new CompilerContext(key);
 55  
     }
 56  
 
 57  
     public QuercusClass compile(CompilerContext context) throws Throwable {
 58  3
         String className = compiledClassNamesMap.get(context.key.getClass());
 59  3
         StringBuilder code = context.code;
 60  3
         code.append("class ").append(className).append(" {");
 61  3
         code.append("var $bsf;");
 62  3
         for (BSFDeclaredBean bean: context.beans) {
 63  0
             code.append("var $").append(bean.name).append(';');
 64  
         }
 65  3
         code.append("public function __construct($engine, $beans) {");
 66  3
         code.append("$this->bsf = $engine->getBSFFunctions();");
 67  3
         for (int i = 0; i < context.beans.length; i++) {
 68  0
             BSFDeclaredBean bean = context.beans[i];
 69  0
             code.append("$this->").append(bean.name).append(" = $beans[").append(i).append("];");
 70  
         }
 71  3
         code.append('}');
 72  3
         code.append("public function method");
 73  3
         super.compile(context);
 74  3
         code.append('}');
 75  3
         code.append('}');
 76  3
         if (engine.isDebugOn()) System.err.println(code);
 77  3
         Quercus php = getQuercus();
 78  3
         synchronized (php) {
 79  3
             LocalEnv env = newEnv();
 80  
             try {
 81  3
                 QuercusProgram program = php.parseCode(code.toString());
 82  3
                 program.execute(env);
 83  3
                 return new QuercusClass((ClassDef) program.getClasses().iterator().next(), null);
 84  
             } finally {
 85  3
                 env.getOriginalOut().flush();
 86  3
                 env.close();
 87  
             }
 88  0
         }
 89  
     }
 90  
 
 91  
     public Env getGlobalEnv() {
 92  15
         return phpEngine.getGlobalEnv();
 93  
     }
 94  
 
 95  
     public LocalEnv newEnv() throws Throwable {
 96  6
         LocalEnv env = new LocalEnv(getQuercus(), null, null, null);
 97  6
         env.getWriterStreamImpl().setWriter(new OutputStreamWriter(System.err));
 98  6
         return env;
 99  
     }
 100  
 
 101  
     public Value init(CompilerContext context, QuercusClass compiled) throws Throwable {
 102  3
         LocalEnv env = newEnv();
 103  
         try {
 104  3
             Value[] args = new Value[] {
 105  
                 (Value) wrap(engine, QuercusEngine.class),
 106  
                 (Value) wrap(context.beans, context.beans.getClass())
 107  
             };
 108  3
             return compiled.callNew(env, args);
 109  
         } finally {
 110  3
             env.getOriginalOut().flush();
 111  3
             env.close();
 112  
         }
 113  
     }
 114  
 
 115  
     public QuercusClass compileKey(ApplyKey<QuercusClass> key, CompilerContext context) throws BSFException {
 116  3
         StringBuilder code = context.code;
 117  3
         code.append('(');
 118  3
         String[] names = key.getNames();
 119  12
         for (int i = 0; i < names.length; i++) {
 120  9
             if (i != 0) code.append(", ");
 121  9
             code.append('$').append(names[i]);
 122  
         }
 123  3
         code.append(") {");
 124  3
         code.append(context.text);
 125  3
         return null;
 126  
     }
 127  
 
 128  
     public QuercusClass compileKey(EvalKey<QuercusClass> key, CompilerContext context) throws BSFException {
 129  0
         context.code.append("() {");
 130  0
         context.code.append(context.text);
 131  0
         return null;
 132  
     }
 133  
 
 134  
     public QuercusClass compileKey(ExecKey<QuercusClass> key, CompilerContext context) throws BSFException {
 135  0
         context.code.append("() {");
 136  0
         context.code.append(context.text);
 137  0
         return null;
 138  
     }
 139  
 
 140  
     public Object[] newWrapArray(int length) {
 141  0
         return new Value[length];
 142  
     }
 143  
 
 144  
     public Object[] newWrapArray(Object[] array) {
 145  3
         return new Value[array.length];
 146  
     }
 147  
 
 148  
     public Object wrap(Object object, Class type) {
 149  15
         return getGlobalEnv().wrapJava(object);
 150  
     }
 151  
 
 152  
     public Object unwrap(Object object) {
 153  3
         return ((Value) object).toJavaObject();
 154  
     }
 155  
 }