Example #1
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write('break ;');
     // 						 . ExpressionCompiler::compileExpression ( $aObject->attributes ()->source (), $aObjectContainer->variableDeclares() )
     // 						 . ';' );
 }
Example #2
0
 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("file")) {
         $sFileName = '"' . addslashes($aAttributes->string("file")) . '"';
     } else {
         if (!($aFileVal = $aAttributes->anonymous())) {
             throw new Exception("include 节点缺少file属性(line:%d)", $aObject->line());
         }
         $sFileName = '"' . addslashes($aFileVal->source()) . '"';
     }
     // 是否继承父模板中的变量
     $bExtendParentVars = $aAttributes->has("vars") ? $aAttributes->bool('vars') : true;
     // start
     $aDev->write("\r\n");
     // variables
     if (!$bExtendParentVars) {
         $aDev->write("\$__include_aVariables = new \\org\\jecat\\framework\\util\\DataSrc() ; \r\n");
         $aDev->write("\$__include_aVariables->addChild(\$aVariables) ;");
     } else {
         $aDev->write("\$__include_aVariables = \$aVariables ; \r\n");
     }
     // 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()) ;
             $sValue = $aAttributes->get($sName);
             $aDev->write("\$__include_aVariables->set({$sVarName},{$sValue}) ; \r\n");
         }
     }
     $aDev->write("\$this->display({$sFileName},\$__include_aVariables,\$aDevice) ; ");
 }
Example #3
0
 public function __construct($sType, $sMessage, $arrMessageArgs = null)
 {
     parent::__construct();
     $this->sType = $sType;
     $this->sMessage = $sMessage;
     $this->arrMessageArgs = Type::toArray($arrMessageArgs);
 }
Example #4
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $sLang = strtolower($aObject->attributes()->string('lang'));
     if (in_array($sLang, array('text/php', 'php')) and $aTailTag = $aObject->tailTag()) {
         // 编译头标签
         $this->compileTag($aObject->headTag(), $aObjectContainer, $aDev, $aCompilerManager);
         // 设置代码"上色器"
         $sVarName = parent::assignVariableName();
         $aDev->write("\r\n");
         $aDev->write("\${$sVarName} = new \\org\\jecat\\framework\\ui\\xhtml\\compiler\\node\\CodeColor() ;\r\n");
         $aDev->write("\\org\\jecat\\framework\\io\\StdOutputFilterMgr::singleton()->add(array(\${$sVarName},'outputFilter')) ;\r\n");
         $aDev->write("");
         // 编译 node body
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
         // 输出代码
         $aDev->write("\r\n");
         $aDev->write("\\org\\jecat\\framework\\io\\StdOutputFilterMgr::singleton()->remove( array(\${$sVarName},'outputFilter') ) ;\r\n");
         $aDev->write("\${$sVarName}->output(\$aDevice) ;");
         $aDev->write("");
         // 编译尾标签
         $this->compileTag($aTailTag, $aObjectContainer, $aDev, $aCompilerManager);
     } else {
         parent::compile($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     }
 }
Example #5
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 #7
0
 public function __construct(IContainer $aParent, $aCallback)
 {
     if (!is_callable($aCallback)) {
         throw new Exception(__CLASS__ . "() 的参数 \$aCallback 必须是一个回调函数,传入的参数类型为:%s", Type::reflectType($aCallback));
     }
     $arrBingo = self::searching($aParent, $aCallback);
     parent::__construct($arrBingo);
 }
Example #8
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 #9
0
 public function __construct(ParseState $aParseState, $sMessage, $argvs = null)
 {
     $argvs = Type::toArray($argvs, Type::toArray_emptyForNull);
     $sMessage .= "\r\n遇到问题的地方:%s";
     $argvs[] = implode(' ', array_slice($aParseState->arrTokenList, key($aParseState->arrTokenList), 30));
     $sMessage .= "\r\n完整的SQL:%s";
     $argvs[] = implode(' ', $aParseState->arrTokenList);
     parent::__construct($sMessage, $argvs);
 }
Example #10
0
 public function add($callback, $arrArgvs = array())
 {
     if (!is_callable($callback)) {
         throw new Exception(__METHOD__ . "()的参数\$callback必须为回调函数类型,传入的类型为:%s", Type::reflectType($callback));
     }
     if (!is_array($arrArgvs)) {
         $arrArgvs = array($arrArgvs);
     }
     array_unshift($this->arrFilters, array($callback, $arrArgvs));
 }
Example #11
0
 /**
  * @wiki /MVC模式/数据交换和数据校验/数据校验
  * ==Email格式校验==
  * 验证输入的Email格式的正确性
  */
 public function buildBean(array &$arrConfig, $sNamespace = '*', \org\jecat\framework\bean\BeanFactory $aBeanFactory = null)
 {
     if (!is_callable($arrConfig['callback'])) {
         throw new BeanConfException("callback 类型的校验器的 callback 属性必须是一个回调函数");
     }
     $this->fnCallback = $arrConfig['callback'];
     if (array_key_exists('argvs', $arrConfig)) {
         $this->arrArgvs = Type::toArray($arrConfig['argvs'], Type::toArray_normal);
     }
     $this->arrBeanConfig = $arrConfig;
 }
Example #12
0
 public function add($object, $sName = null, $bTakeover = true)
 {
     if ($object === $this) {
         return;
     }
     if (!$this->accept($object)) {
         throw new Exception(__METHOD__ . "() 方法无法接受 %s 类型的参数", Type::reflectType($object));
     }
     if (!in_array($object, $this->arrObjects, is_object($object))) {
         $this->arrObjects[] = $object;
         $this->attach($object, $sName, $bTakeover);
     }
     return $object;
 }
Example #13
0
 public function remove($req)
 {
     if (is_string($req)) {
         parent::remove($req);
     } else {
         if ($req instanceof \org\jecat\framework\ui\xhtml\AttributeValue) {
             $this->removeByValue($req);
             // anonymous attribute
             $key = array_search($req, $this->arrAnonymous);
             if ($key !== false) {
                 unset($this->arrAnonymous[$key]);
             }
         } else {
             Type::check(array('string', __NAMESPACE__ . '\\AttributeValue'), $req);
         }
     }
 }
Example #14
0
 public function compile(array &$arrRawSql, array &$arrInFactors = null, array &$arrTokenTree = null, $sTreeType = 'subtree')
 {
     $sSql = '';
     if (!$arrTokenTree) {
         $arrTokenTree =& $arrRawSql;
     }
     if (!empty($arrRawSql[$sTreeType])) {
         // data factors
         if (!empty($arrRawSql['factors'])) {
             $arrFactors =& $arrRawSql['factors'];
         } else {
             $arrFactors =& $arrInFactors;
         }
         foreach ($arrRawSql[$sTreeType] as &$token) {
             if (is_string($token) or is_numeric($token)) {
                 if ($arrFactors and array_key_exists($token, $arrFactors)) {
                     $sSql .= " '" . addslashes($arrFactors[$token]) . "'";
                 } else {
                     $sSql .= ' ' . $token;
                 }
             } else {
                 if (is_array($token)) {
                     if ($this->arrTokenCompilers and !empty($this->arrTokenCompilers[$token['expr_type']])) {
                         $sSql .= ' ' . $this->arrTokenCompilers[$token['expr_type']]->compile($this, $arrTokenTree, $token, $arrFactors);
                     } else {
                         if (!empty($token['subtree'])) {
                             $sSqlSubtree = $this->compile($token, $arrFactors, $arrTokenTree);
                             // 仅在 subtree 不为空的时候,处理 pretree
                             if ($sSqlSubtree and !empty($token['pretree'])) {
                                 $sSql .= ' ' . $this->compile($token, $arrFactors, $arrTokenTree, 'pretree');
                             }
                             $sSql .= ' ' . $sSqlSubtree;
                         }
                     }
                 } else {
                     throw new SqlCompileException($arrTokenTree, $token, "遇到类型无效的 sql token: %s", Type::detectType($token));
                 }
             }
         }
     }
     return $sSql ? substr($sSql, 1) : '';
 }
Example #15
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(");");
 }
 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 #17
0
 protected function processNameSeparator(&$sToken, ParseState $aParseState)
 {
     $prevToken = end($aParseState->arrTree);
     if (is_array($prevToken)) {
         if (!($nextToken = next($aParseState->arrTokenList))) {
             throw new SqlParserException($aParseState, "遇到无效的名称分隔符“.” , “.” 后面没有内容了");
         }
         if (!($nextName = $this->parseName($nextToken))) {
             if ($nextToken === '*') {
                 $nextName = $nextToken;
             } else {
                 throw new SqlParserException($aParseState, "遇到无效的名称分隔符“.” , “.” 后面不是一个合法的名称:%s", $nextToken);
             }
         }
         switch ($prevToken['expr_type']) {
             case 'column':
                 if (!empty($prevToken['table'])) {
                     $prevToken['db'] = $prevToken['table'];
                 }
                 $prevToken['table'] = $prevToken['column'];
                 $prevToken['column'] = $nextName;
                 break;
             case 'table':
                 $prevToken['db'] = $prevToken['table'];
                 $prevToken['table'] = $nextName;
                 break;
             default:
                 throw new SqlParserException($aParseState, "遇到无效的名称分隔符“.” , “.” 前不是一个字段或数据表的表达式:%s", $prevToken['expr_type']);
                 break;
         }
         array_pop($aParseState->arrTree);
         array_push($aParseState->arrTree, $prevToken);
     } else {
         if (is_string($prevToken)) {
             throw new SqlParserException($aParseState, "遇到无效的名称分隔符“.” , “.” 前不是一个字段或数据表的表达式:%s", $prevToken);
         } else {
             throw new SqlParserException($aParseState, "遇到遇到意外的token类型:%s", Type::detectType($prevToken));
         }
     }
 }
Example #18
0
 private function &findJoinedTable(&$sToken, ParseState $aParseState)
 {
     for (end($aParseState->arrTree); $prevToken = current($aParseState->arrTree); prev($aParseState->arrTree)) {
         if (!is_array($prevToken)) {
             throw new SqlParserException($aParseState, "join 前的 token 类型无效:%s", Type::reflectType($prevToken));
         } else {
             switch ($prevToken['expr_type']) {
                 // bingo
                 case 'table':
                 case 'subquery':
                     $pos = key($aParseState->arrTree);
                     return $aParseState->arrTree[$pos];
                     break;
                 case 'join_expression':
                     // nothing todo
                     break;
                 default:
                     break;
             }
         }
     }
     throw new SqlParserException($aParseState, "遇到无效的join,无法为 join 子句确定对应的数据表表达式");
 }
Example #19
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);
         }
     }
 }
Example #20
0
 /**
  * 通过传入的对象配置数组,创建一个 IBean 对象
  * @return org\jecat\framework\bean\IBean
  */
 public function createBean(array &$arrConfig, $sNamespace = '*', $bAutoBuild = true)
 {
     if ($sNamespace == '*' and !empty($arrConfig['namespace'])) {
         $sNamespace = $arrConfig['namespace'];
     }
     // ins
     if (!empty($arrConfig['ins']) or !empty($arrConfig['instance'])) {
         $sInstance = isset($arrConfig['ins']) ? $arrConfig['ins'] : $arrConfig['instance'];
         if (!($aFile = $this->beanFolders()->find($sInstance . '.ins.php', $sNamespace))) {
             throw new BeanConfException("Bean对象配置数组中的 ins 属性无效: %s,找不到指定的实例文件", $arrConfig['ins']);
         }
         return $aFile->includeFile(false, false);
     }
     $this->loadConfig($arrConfig, $sNamespace);
     if (!empty($arrConfig['class'])) {
         $arrConfig['class'] = $this->beanClassNameByAlias($arrConfig['class']) ?: $arrConfig['class'];
         if (!class_exists($arrConfig['class'])) {
             throw new BeanConfException("Bean对象配置数组中的 class 属性无效:%s,不存在该名称的类和别名", $arrConfig['class']);
         }
         if (!Type::hasImplements($arrConfig['class'], 'org\\jecat\\framework\\bean\\IBean')) {
             throw new BeanConfException("Bean对象配置数组中的 class 属性无效:%s,必须是一个实现 org\\jecat\\framework\\bean\\IBean 接口的类", $arrConfig['class']);
         }
         return $arrConfig['class']::createBean($arrConfig, $sNamespace, $bAutoBuild, $this);
     } else {
         throw new BeanConfException("无法根据配置数组创建 Bean 对象,缺少必须的 ins, config 或 class 属性: %s。", var_export($arrConfig, true));
     }
 }
Example #21
0
 public function trans($sOriWords, $argvs = null, $sLibName = 'base')
 {
     $arrArgvs = Type::toArray($argvs);
     return $this->sentenceLibrary($sLibName)->trans($sOriWords, $argvs);
 }
Example #22
0
 public function setExclude($exclude)
 {
     Type::toArray($exclude);
     $this->arrExclude = $exclude;
 }
Example #23
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     Type::check("org\\jecat\\framework\\ui\\xhtml\\Node", $aObject);
     $aDev->write("}");
 }
Example #24
0
 public function verifyFile(File $file, $bThrowException)
 {
     if (!$file instanceof File) {
         throw new Exception(__CLASS__ . "的" . __METHOD__ . "传入了错误的data参数(得到的参数是%s类型)", array(Type::detectType($file)));
     }
     $nDataSize = $file->length();
     if ($this->nMaxSize != -1 && $nDataSize > $this->nMaxSize) {
         if ($bThrowException) {
             throw new VerifyFailed("上传的文件大小大于网站限制的" . $this->nMaxSize . "字节");
         }
         return false;
     }
     if ($this->nMinSize != -1 && $nDataSize < $this->nMinSize) {
         if ($bThrowException) {
             throw new VerifyFailed("上传的文件大小小于网站限制的" . $this->nMaxSize . "字节");
         }
         return false;
     }
     return true;
 }
Example #25
0
 public function setValue($data = null)
 {
     Type::check("org\\jecat\\framework\\fs\\File", $data);
     parent::setValue($data);
 }
 public function generateTargetCode(TokenPool $aTokenPool, Token $aObject)
 {
     if (!$aObject instanceof ClassDefine) {
         throw new ClassCompileException(null, $aObject, "传入的类新必须为 ClassDefine: %s", Type::detectType($aObject));
     }
     $aClassEnd = $aObject->bodyToken()->theOther();
     $sTargetClassName = $aObject->fullName();
     // 反射父类的所有方法
     $arrParentMethodNames = array();
     $aRefParentClass = null;
     if ($aObject->isClass()) {
         foreach ($aObject->parentClassNameIterator() as $sParentClass) {
             if (!class_exists($sParentClass)) {
                 throw new ClassCompileException(null, $aObject, "编译class时遇到错误,class %s 的父类 %s 不存在 ", array($sTargetClassName, $sParentClass));
             }
             $aRefParentClass = new \ReflectionClass($sParentClass);
             foreach ($aRefParentClass->getMethods() as $aRefParentMethod) {
                 if (!$aRefParentMethod->isPrivate() and !$aRefParentMethod->isAbstract() and !$aRefParentMethod->isFinal()) {
                     $arrParentMethodNames[$aRefParentMethod->getName()] = $aRefParentMethod;
                 }
             }
         }
     }
     // 需要编入的方法
     $arrNeedWeaveMethods = array();
     foreach ($this->aop()->aspects()->iterator() as $aAspect) {
         foreach ($aAspect->pointcuts()->iterator() as $aPointcut) {
             foreach ($aPointcut->jointPoints()->iterator() as $aJointPoint) {
                 if (!$aJointPoint instanceof JointPointMethodDefine) {
                     continue;
                 }
                 // 模糊匹配函数名 -----------------------
                 if ($aJointPoint->weaveMethodIsPattern()) {
                     foreach ($aTokenPool->functionIterator($aObject->fullName()) as $aMethodToken) {
                         // bingo !
                         if ($aJointPoint->matchExecutionPoint($aMethodToken)) {
                             $sMethodName = $aMethodToken->name();
                             if (empty($arrNeedWeaveMethods[$sMethodName])) {
                                 $arrNeedWeaveMethods[$sMethodName] = new GenerateStat($aTokenPool, $aMethodToken);
                             }
                             $arrNeedWeaveMethods[$sMethodName]->addAdvices($aPointcut->advices()->iterator());
                         }
                     }
                 } else {
                     if (!$aJointPoint->matchClass($sTargetClassName)) {
                         continue;
                     }
                     $sFuncName = $aJointPoint->weaveMethod();
                     if (empty($arrNeedWeaveMethods[$sFuncName])) {
                         $arrNeedWeaveMethods[$sFuncName] = new GenerateStat($aTokenPool);
                     }
                     $arrNeedWeaveMethods[$sFuncName]->addAdvices($aPointcut->advices()->iterator());
                     // 目标类的方法
                     if ($aMethodToken = $aTokenPool->findFunction($sFuncName, $sTargetClassName)) {
                         $arrNeedWeaveMethods[$sFuncName]->aExecutePoint = $aMethodToken;
                     } else {
                         if (isset($arrParentMethodNames[$sFuncName])) {
                             $aMethodRef = $arrParentMethodNames[$sFuncName];
                             $aMethodRef instanceof \ReflectionMethod;
                             // 产生函数定义和函数调用的参数表
                             $this->generateArgvsByReflection($arrNeedWeaveMethods[$sFuncName], $aMethodRef);
                             if ($aMethodRef->isPublic()) {
                                 $sAccess = 'public';
                             } else {
                                 if ($aMethodRef->isProtected()) {
                                     $sAccess = 'protected';
                                 } else {
                                     if ($aMethodRef->isPrivate()) {
                                         $sAccess = 'private';
                                     }
                                 }
                             }
                             // 创建一个覆盖父类的方法用于 aop
                             $aMethodToken = $this->createMethod($sFuncName, $arrNeedWeaveMethods[$sFuncName]->sOriginCallArgvsLit, $sAccess, $aMethodRef->isStatic(), $aTokenPool, $aClassEnd, 'insertBefore');
                             $aMethodToken->setBelongsClass($aObject);
                             $aMethodToken->setBelongsNamespace($aObject->belongsNamespace());
                             $arrNeedWeaveMethods[$sFuncName]->aExecutePoint = $aMethodToken;
                             // 创建函数内容
                             $aTokenPool->insertAfter($aMethodToken->bodyToken(), new Token(T_STRING, "\n\t\t\t// 调用父类方法\n\t\t\treturn parent::{$sFuncName}({$arrNeedWeaveMethods[$sFuncName]->sOriginCallArgvsLit}) ;\n\t\t"));
                         } else {
                             // 创建一个全新的方法用于 aop
                             $aMethodToken = $this->createMethod($sFuncName, $arrNeedWeaveMethods[$sFuncName]->sOriginCallArgvsLit, 'private', false, $aTokenPool, $aClassEnd, 'insertBefore');
                             $aMethodToken->setBelongsClass($aObject);
                             $aMethodToken->setBelongsNamespace($aObject->belongsNamespace());
                             $arrNeedWeaveMethods[$sFuncName]->aExecutePoint = $aMethodToken;
                             $aTokenPool->insertAfter($aMethodToken->bodyToken(), new Token(T_STRING, " // 这只是一个影子方法 "));
                         }
                     }
                 }
             }
         }
     }
     // 开始编织
     foreach ($arrNeedWeaveMethods as $aState) {
         if (!empty($aState->arrAdvices)) {
             $this->weave($aState);
         }
     }
 }
Example #27
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)
     }
 }
Example #28
0
 public function __construct(&$arrTokenTree, &$token, $sMessage, $argvs = null)
 {
     $argvs = Type::toArray($argvs, Type::toArray_emptyForNull);
     $sMessage .= "\r\n 正在处理的Sql Raw:" . var_export($arrTokenTree, true);
     parent::__construct($sMessage, $argvs);
 }
Example #29
0
 public static function setSingleton(self $aInstance = null, $sClass = null)
 {
     if (!$sClass) {
         $sClass = get_called_class();
     }
     // 移除全局实例
     if (!$aInstance) {
         unset(self::$arrGlobalInstancs[$sClass]);
     }
     if (!$aInstance instanceof static) {
         throw new Exception('%s::setSingleton() 设置的单件实例必须为一个 %s 类型的对象,传入的对象类型为:%s', array($sClass, $sClass, Type::reflectType($aInstance)));
     }
     self::$arrGlobalInstancs[$sClass] = $aInstance;
 }
Example #30
0
 public function trans($sOriWords, $argvs = null)
 {
     $arrArgvs = Type::toArray($argvs);
     $sKey = md5($sOriWords);
     if (!isset($this->arrSentences[$sKey])) {
         $sSentence = $sOriWords;
         $this->arrNewSentences[$sKey] = $sOriWords;
     } else {
         $sSentence = $this->arrSentences[$sKey];
     }
     return $arrArgvs ? call_user_func_array('sprintf', array_merge(array($sSentence), $arrArgvs)) : $sSentence;
 }