Example #1
0
 /**
  * $aObject 这是一个Node对象.它是模板引擎分析模板文件后的产品之一.Node对象包含了标签中的所有内容,包括Node的类型,内容,参数,等等,这些信息都是模板引擎分析模板得来.
  * 			比如这个if标签,你可以通过Node对象拿到它的源码,if的模板源码类似:
  * 			<if '(bool)$nTrue'>
  * 				<span>true</span>
  * 			</if>
  * 			也可以取得if标签的参数,if标签的参数就是上面源码中if后面的部分:
  * 			(bool)$nTrue
  * $aDev 输出设备,一般指网页
  * $aCompilerManager 编译管理器
  */
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     //确保传入的$aObject参数是node对象
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     //以下是编译过程
     //把<if>标签转换成php代码,也就是"if("
     //获得<if>标签中的条件语句,原封不动的放到if后面的括号中充当条件
     //但是这里并没有给代码块结尾,因为结尾在别的编译器中了,对于if标签来说,它的结尾工作放在</if>编译器那里了.是的,if标签是至少需要两个编译器才能完整编译
     $aDev->write('if(' . ExpressionCompiler::compileExpression($aObject->attributes()->anonymous()->source(), $aObjectContainer->variableDeclares()) . '){');
     /* 
      * 处理单行标签.单行格式是为了解决跨模板文件问题
      * if标签的多行格式是:
      * 			<if>
      * 			<else/>
      * 			</if>
      * 单行格式是
      * 			<if/>
      * 			<else/>
      * 			<if:end/>
      */
     if (!$aObject->headTag()->isSingle()) {
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         $aDev->write("} ");
     }
 }
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttributes = $aObject->attributes();
     if ($aAttributes->has("name")) {
         $sSubTemplateName = $aAttributes->string("name");
     } else {
         if (!($aNameVal = $aAttributes->anonymous())) {
             throw new Exception("subtemplate:define 节点缺少name属性(line:%d)", $aObject->line());
         }
         $sSubTemplateName = $aNameVal->source();
     }
     if (!is_callable($sSubTemplateName, true)) {
         throw new Exception("subtemplate:define 节点的name属性使用了无效的字符:%d", $sSubTemplateName);
     }
     $aDev->write("\r\n\r\n// -- subtemplate start ----------------------");
     $aDev->write("function __subtemplate_{$sSubTemplateName}(\$aVariables,\$aDevice){ ");
     // 准备 VariableDeclares
     $aOldVars = $aObjectContainer->variableDeclares();
     $aDeclareVariables = new VariableDeclares();
     $aObjectContainer->setVariableDeclares($aDeclareVariables);
     $aBuff = new OutputStreamBuffer();
     $aDev->write($aBuff);
     // 编译子对像
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     // 声明用到的变量
     $aDeclareVariables->make($aBuff);
     $aObjectContainer->setVariableDeclares($aOldVars);
     $aDev->write("}// -- subtemplate end ----------------------\r\n\r\n");
 }
Example #3
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Assert::type("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject, 'aObject');
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStackForLoopIsEnableToRun')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStackForLoopIsEnableToRun', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     $aAttrs = $aObject->attributes();
     $sIdx = $aAttrs->has('idx') ? $aAttrs->string('idx') : '';
     $sItem = $aAttrs->has('item') ? $aAttrs->string('item') : 'theModel';
     $sFor = $aAttrs->has('for') ? $aAttrs->get('for') : "\$aVariables->get('theModel')";
     $aDev->write("if(\$aForModel={$sFor}){\r\n");
     if ($sIdx) {
         $aDev->write("\t\${$sIdx}=0;\r\n");
     }
     $aDev->write("\t\$aStackForLoopIsEnableToRun->put(false);");
     $aDev->write("\t\tforeach(\$aForModel->childIterator() as \$__aChildModel){\r\n");
     $aDev->write("\t\t\t\$aVariables->set('{$sItem}',\$__aChildModel) ;\r\n\t\t\$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();\r\n\t\t\$bLoopIsEnableToRun = true;\r\n");
     if ($sIdx) {
         $aDev->write("\t\t\t\$aVariables->set('{$sIdx}',\${$sIdx}++) ;\r\n");
     }
     if (!$aObject->headTag()->isSingle()) {
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         $aDev->write("\t}\r\n}\r\n");
     }
 }
Example #4
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write(' }elseif(');
     $aDev->write(ExpressionCompiler::compileExpression($aObject->attributes()->anonymous()->source(), $aObjectContainer->variableDeclares()));
     $aDev->write("){ ");
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
 }
Example #5
0
 /**
  * return IObject
  */
 public function parse(string $aSource, ObjectContainer $aObjectContainer, UI $aUI)
 {
     $sTempateName = $aObjectContainer->ns() . ':' . $aObjectContainer->templateName();
     $aWeaveMgr = $this->weaveManager();
     if (!$aWeaveMgr->hasPatchSlot($sTempateName)) {
         return;
     }
     foreach ($aWeaveMgr->patchSlotIterator($sTempateName) as $aPatchSlot) {
         $aPatchSlot->applyPatchs($aObjectContainer, $aUI);
     }
 }
Example #6
0
 /**
  * @return ICompiled
  */
 public function compile(ObjectContainer $aObjectContainer, TargetCodeOutputStream $aTargetCodeStream)
 {
     // 变量声明 buffer
     $aBuffVarsDeclare = new OutputStreamBuffer();
     $aTargetCodeStream->write($aBuffVarsDeclare);
     // 编译正文
     foreach ($aObjectContainer->iterator() as $aObject) {
         $aCompiler = $this->compiler($aObject);
         if ($aCompiler) {
             $aCompiler->compile($aObject, $aObjectContainer, $aTargetCodeStream, $this);
         }
     }
     // 变量声明
     $aObjectContainer->variableDeclares()->make($aBuffVarsDeclare);
 }
Example #7
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStackForLoopIsEnableToRun')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStackForLoopIsEnableToRun', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     $sIdxUserName = $aObject->attributes()->has('idx') ? $aObject->attributes()->get('idx') : '';
     $sIdxAutoName = NodeCompiler::assignVariableName('$__dowhile_idx_');
     if (!empty($sIdxUserName)) {
         $aDev->write(" {$sIdxAutoName} = -1; \$aStackForLoopIsEnableToRun->put(false);");
     }
     $aDev->write(" do{ \$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();\r\n\t\t\t\$bLoopIsEnableToRun = true;");
     if (!empty($sIdxUserName)) {
         $aDev->write(" {$sIdxAutoName}++; \r\n\t\t\t\t\t\t\t\$aVariables[{$sIdxUserName}]={$sIdxAutoName}; ");
     }
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     $aDev->write(" }while(");
     $aDev->write(ExpressionCompiler::compileExpression($aObject->attributes()->anonymous()->source(), $aObjectContainer->variableDeclares()));
     $aDev->write(");");
 }
Example #8
0
 /**
  * return IObject
  */
 public function parse(string $aSource, ObjectContainer $aObjectContainer, UI $aUI)
 {
     $nProcIndex = 0;
     $aState = ParserStateDefault::singleton();
     $aCurrentObject = $aRootObject = new ObjectBase(0, $aSource->length() - 1, 0, '');
     while ($nProcIndex < $aSource->length()) {
         $aNewState = $aState->examineStateChange($aSource, $nProcIndex, $aCurrentObject);
         if ($aNewState) {
             // 切换状态
             if ($aNewState != $aState) {
                 //
                 $aCurrentObject = $aState->sleep($aCurrentObject, $aSource, $nProcIndex - 1);
                 $aCurrentObject = $aNewState->active($aCurrentObject, $aSource, $nProcIndex);
                 // 状态变化
                 $aState = ParserState::queryState($aCurrentObject);
             }
         } else {
             $aCurrentObject = $aState->complete($aCurrentObject, $aSource, $nProcIndex);
             if (!$aCurrentObject) {
                 break;
             }
             $aCurrentObject = ParserState::queryState($aCurrentObject)->wakeup($aCurrentObject, $aSource, $nProcIndex);
             // 状态变化
             $aState = ParserState::queryState($aCurrentObject);
         }
         $nProcIndex++;
     }
     // 未完成的对象
     if ($aCurrentObject != $aRootObject) {
         $aBuff = new OutputStreamBuffer();
         $aRootObject->printStruct($aBuff);
         throw new Exception("<pre>\r\n分析UI模板时遇到未完成的对象:%s\r\n%s\r\n</pre>", array($aCurrentObject->summary(), $aBuff));
     }
     $aObjectContainer->clear();
     foreach ($aRootObject->iterator() as $aObject) {
         $aObjectContainer->add($aObject);
     }
 }
Example #9
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttrs = $aObject->attributes();
     $sType = strtolower($aAttrs->string('type'));
     if (in_array($sType, array('text/php', 'php'))) {
         foreach ($aObject->iterator() as $aChild) {
             if ($aChild instanceof AttributeValue) {
                 continue;
             }
             $aDev->write(ExpressionCompiler::compileExpression($aChild->source(), $aObjectContainer->variableDeclares(), false, true));
         }
     } else {
         if ($aAttrs->has('src') and !$aAttrs->bool('ignore')) {
             $sSrc = $aAttrs->get('src');
             $aDev->preprocessStream()->write("\\org\\jecat\\framework\\resrc\\HtmlResourcePool::singleton()->addRequire({$sSrc},\\org\\jecat\\framework\\resrc\\HtmlResourcePool::RESRC_JS) ;");
             // 清除后文中的空白字符
             ClearCompiler::clearAfterWhitespace($aObject);
         } else {
             parent::compile($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         }
     }
 }
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttributes = $aObject->attributes();
     if ($aAttributes->has("name")) {
         $sSubTemplateName = $aAttributes->string("name");
     } else {
         if (!($aSubTemplateNameVal = $aAttributes->anonymous())) {
             throw new Exception("subtemplate:define 节点缺少name属性(line:%d)", $aObject->line());
         }
         $sSubTemplateName = $aSubTemplateNameVal->source();
     }
     if (!is_callable($sSubTemplateName, true)) {
         throw new Exception("subtemplate:define 节点的name属性使用了无效的字符:%d", $sSubTemplateName);
     }
     $sSubTemplateFuncName = '__subtemplate_' . $sSubTemplateName;
     $aDev->write("\r\n// -- call subtemplate:{$sSubTemplateName} start---------------------");
     // 是否继承父模板中的变量
     $bExtendParentVars = $aAttributes->has("vars") ? $aAttributes->bool('vars') : false;
     // variables
     if (!$bExtendParentVars) {
         $aDev->write("\$__subtemplate_aVariables = new \\org\\jecat\\framework\\util\\DataSrc() ;");
         $aDev->write("\$__subtemplate_aVariables->addChild(\$aVariables) ;");
     } else {
         $aDev->write("\$__subtemplate_aVariables = \$aVariables ;");
     }
     // other variables
     foreach ($aAttributes as $sName => $aValue) {
         if (substr($sName, 0, 4) == 'var.' and $sVarName = substr($sName, 4)) {
             $sVarName = '"' . addslashes($sVarName) . '"';
             $sValue = ExpressionCompiler::compileExpression($aValue->source(), $aObjectContainer->variableDeclares());
             $aDev->write("\$__subtemplate_aVariables->set({$sVarName},{$sValue}) ;");
         }
     }
     $aDev->write("if( !function_exists('{$sSubTemplateFuncName}') ){");
     $aDev->write("\t\$aDevice->write(\"正在调用无效的子模板:{$sSubTemplateName}\") ;");
     $aDev->write("} else {");
     $aDev->write("\tcall_user_func_array('{$sSubTemplateFuncName}',array(\$__subtemplate_aVariables,\$aDevice)) ;");
     $aDev->write("}");
     $aDev->write("// -- call subtemplate:{$sSubTemplateName} end ---------------------\r\n\r\n");
 }
Example #11
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     $aDev->write(self::compileExpression($aObject->source(), $aObjectContainer->variableDeclares()));
 }
Example #12
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write(' continue ' . ExpressionCompiler::compileExpression($aObject->attributes()->source(), $aObjectContainer->variableDeclares()) . '; ');
 }
Example #13
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Assert::type("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject, 'aObject');
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStackForLoopIsEnableToRun')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStackForLoopIsEnableToRun', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     $aAttrs = $aObject->attributes();
     $sStartValue = $aAttrs->has("start") ? $aAttrs->expression("start") : '0';
     $sEndValue = $aAttrs->expression("end");
     $sStepValue = $aAttrs->has("step") ? $aAttrs->expression("step") : '1';
     $sVarAutoName = NodeCompiler::assignVariableName('$__loop_var_');
     $sIdxAutoName = NodeCompiler::assignVariableName('$__loop_idx_');
     $sEndName = NodeCompiler::assignVariableName('$__loop_end_');
     $sStepName = NodeCompiler::assignVariableName('$__loop_step_');
     $aDev->write("\t\t{$sEndName}  = {$sEndValue} ; \n\t\t\t\t\t\t\t\t{$sStepName}  = {$sStepValue}  ;\n\t\t\t\t\t\t\t\t{$sIdxAutoName} = 0;\n\t\t\t\t\t\t\t\t\$aStackForLoopIsEnableToRun->put(false);\n\t\t\t\t\t\t\t\tfor( {$sVarAutoName} = {$sStartValue} ; {$sVarAutoName} <= {$sEndName} ; {$sVarAutoName} += {$sStepName} ){\n\t\t\t\t\t\t\t\t\$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();\n\t\t\t\t\t\t\t\t\$bLoopIsEnableToRun = true;  \n\t\t\t\t\t\t");
     if ($aAttrs->has("var")) {
         $sVarUserName = $aAttrs->string("var");
         $aDev->write("\t\t\t\$aVariables->{$sVarUserName} = {$sVarAutoName} ;");
     }
     if ($aAttrs->has("idx")) {
         $sIdxUserName = $aAttrs->string("idx");
         $aDev->write("\t\t\t\$aVariables->{$sIdxUserName} = {$sIdxAutoName} ;\n\t\t\t\t\t\t\t\t\t\t{$sIdxAutoName}++;");
     }
     // 		$aDev->write ( "\$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();
     // 			\$bLoopIsEnableToRun = true;" );
     if (!$aObject->headTag()->isSingle()) {
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         $aDev->write('} ');
     }
 }
Example #14
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStackForLoopIsEnableToRun')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStackForLoopIsEnableToRun', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aAttrs = $aObject->attributes();
     if ($aAttrs->has('for')) {
         $sForUserExp = $aAttrs->expression('for');
     } else {
         throw new Exception("foreach tag can not run without 'for' attribute");
     }
     $sKeyUserName = $aAttrs->has('key') ? $aAttrs->get('key') : '';
     $sItemUserName = $aAttrs->has('item') ? $aAttrs->get('item') : '';
     $bItemRef = $aAttrs->has('item.ref') ? $aAttrs->bool('item.ref') : false;
     $sIdxUserName = $aAttrs->has('idx') ? $aAttrs->get('idx') : '';
     $sForAutoName = NodeCompiler::assignVariableName('$__foreach_Arr_');
     $sItemAutoName = NodeCompiler::assignVariableName('$__foreach_item_');
     $sKeyAutoName = NodeCompiler::assignVariableName('$__foreach_key_');
     $sIdxAutoName = NodeCompiler::assignVariableName('$__foreach_idx_');
     $sItemRef = $bItemRef ? '&' : '';
     $aDev->write("\r\n// foreach start ");
     $aDev->write("{$sForAutoName} = {$sForUserExp};\r\n\$aStackForLoopIsEnableToRun->put(false);\r\n{$sIdxAutoName} = -1;\r\nforeach({$sForAutoName} as {$sKeyAutoName}=>{$sItemRef}{$sItemAutoName}){");
     $aDev->write("\$bLoopIsEnableToRun = & \$aStackForLoopIsEnableToRun->getRef();\r\n\t\$bLoopIsEnableToRun = true;\r\n\t{$sIdxAutoName}++;");
     if (!empty($sKeyUserName)) {
         $aDev->write("\t\t\$aVariables[{$sKeyUserName}]={$sKeyAutoName}; ");
     }
     if (!empty($sItemUserName)) {
         $aDev->write("\t\t\$aVariables[{$sItemUserName}]={$sItemAutoName}; ");
     }
     if (!empty($sIdxUserName)) {
         $aDev->write("\t\t\$aVariables[{$sIdxUserName}]={$sIdxAutoName}; ");
     }
     //是否是单行标签?
     if (!$aObject->headTag()->isSingle()) {
         //循环体,可能会包含foreach:else标签
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         $aDev->write("}\r\n");
         // end if   (如果foreach的内容包含foreach:else标签,则此处为foreach:else的end)
     }
 }