Example #1
0
 /**
  * @param sPath string 前后都没有/
  */
 protected function writeSubMenu(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager, $sWidgetVarName)
 {
     $arrTagName = array('menu', 'item');
     foreach ($aObject->childElementsIterator() as $aChild) {
         if ($aChild instanceof Node and in_array($aChild->tagName(), $arrTagName)) {
             $aItemNode = $aChild;
             $aItemAttrs = $aItemNode->attributes();
             if ($aItemAttrs->has('id')) {
                 $sItemId = $aItemAttrs->string('id');
                 if ($aChild->tagName() === 'menu') {
                     $aAttrValue = AttributeValue::createInstance('instance', " \$aStack->get()->getMenuByPath( '{$sItemId}' ) ");
                 } else {
                     $aAttrValue = AttributeValue::createInstance('instance', " \$aStack->get()->getItemByPath( '{$sItemId}' ) ");
                 }
                 $aItemAttrs->add($aAttrValue);
                 $aAttrValue->setParent($aObjectContainer);
                 $aAttrValue = AttributeValue::createInstance('display', false);
                 $aItemAttrs->add($aAttrValue);
                 $aAttrValue->setParent($aObjectContainer);
             } else {
                 // @todo for add
             }
         }
     }
     if (!$aObjectContainer->variableDeclares()->hasDeclared('aStack')) {
         $aObjectContainer->variableDeclares()->declareVarible('aStack', 'new \\org\\jecat\\framework\\util\\Stack()');
     }
     $aDev->write("\t\$aStack->put({$sWidgetVarName});");
     $aDev->write("\t\$aVariables->aStack = \$aStack;");
     $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     $aDev->write("\t\$aStack->out();");
 }
Example #2
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     if ($aObject instanceof \org\jecat\framework\ui\xhtml\ObjectBase and !$aObject->count()) {
         Assert::type("org\\jecat\\framework\\ui\\xhtml\\Text", $aObject, 'aObject');
         $sText = $aObject->source();
         // locale translate
         do {
             if (!trim($sText)) {
                 break;
             }
             // 排除 script/style 等标签中的内容
             if ($aParent = $aObject->parent() and $aParent instanceof Node and in_array(strtolower($aParent->tagName()), array('script', 'style'))) {
                 break;
             }
             // 仅 title alt 等属性
             if ($aObject instanceof AttributeValue and !in_array(strtolower($aObject->name()), array('title', 'alt'))) {
                 break;
             }
             // 过滤 注释 和 doctype 声明
             if (preg_match('/^\\s*<\\!.*>\\s*$/', $sText)) {
                 break;
             }
             $sText = Locale::singleton()->trans($sText, null, 'ui');
         } while (0);
         $aDev->output($sText);
     } else {
         $this->compileChildren($aObject, $aObjectContainer, $aDev, $aCompilerManager);
     }
 }
Example #3
0
 protected function compileChildren(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     foreach ($aObject->iterator() as $aChild) {
         if ($aCompiler = $aCompilerManager->compiler($aChild)) {
             $aCompiler->compile($aChild, $aObjectContainer, $aDev, $aCompilerManager);
         }
     }
 }
Example #4
0
 public static function reflectXPath(IObject $aParentObject, $sParentXPath = '')
 {
     $arrChildIdxies = array();
     foreach ($aParentObject->iterator() as $aChildObject) {
         $sType = PatchSlotPathSegment::xpathType($aChildObject);
         if (!isset($arrChildIdxies[$sType])) {
             $arrChildIdxies[$sType] = 0;
         }
         $sXPath = $sParentXPath . '/' . $sType . '@' . $arrChildIdxies[$sType]++;
         $aChildObject->properties()->set('xpath', $sXPath);
         self::reflectXPath($aChildObject, $sXPath);
     }
 }
Example #5
0
 public function compile(IObject $aObject, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     $sSource = $aObject->source();
     //如果开头是变量
     if (substr($sSource, 0, 1) === '$') {
         //分辨是定义还是调用
         if ($nEqual = stripos($sSource, '=') and strlen(substr($sSource, $nEqual)) > 0) {
             //这是定义
             $sObjName = '$' . substr($sSource, 1, $nEqual - 1);
             $arrStrings = $this->getElementsBySource(substr($sSource, $nEqual + 1));
             $sArrName = '$' . NodeCompiler::assignVariableName('arrChangByLoopIndex');
             $aDev->write("{$sArrName} = " . var_export($arrStrings, true) . ";\n\t\t\t\t\t\t\t\tif(!isset({$sObjName}))\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t{$sObjName} = new org\\jecat\\framework\\ui\\xhtml\\compiler\\macro\\Cycle({$sArrName});\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t\$aVariables->set( '" . substr($sObjName, 1) . "' , {$sObjName} ) ;\n\t\t\t\t\t\t\t\t");
         } else {
             //这是调用
             $sObjName = '$' . substr($sSource, 1);
             $aDev->write("\n\t\t\t\t\t\t\t\tif(isset({$sObjName}))\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t{$sObjName}->printArr(\$aDevice);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t");
         }
     } else {
         $sArrName = '$' . NodeCompiler::assignVariableName('arrChangByLoopIndex');
         $sObjName = '$' . NodeCompiler::assignVariableName('aStrChangByLoopIndex');
         $aDev->write("{$sArrName} = " . var_export($this->getElementsBySource($sSource), true) . ";\n\t\t\t\tif(!isset({$sObjName}))\n\t\t\t\t{\n\t\t\t\t\t{$sObjName} = new org\\jecat\\framework\\ui\\xhtml\\compiler\\macro\\Cycle({$sArrName});\n\t\t\t\t}\n\t\t\t\t{$sObjName}->printArr(\$aDevice);\n\t\t\t\t\$aVariables->set( '" . substr($sObjName, 1) . "' ,{$sObjName} ) ;\n\t\t\t");
     }
 }
Example #6
0
    public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
    {
        $aObject instanceof \org\jecat\framework\ui\xhtml\Node;
        if (!($sFuncName = $aObject->attributes()->string('function'))) {
            throw new Exception("render:js 节点缺少function属性(line:%d)", $aObject->line());
        }
        $aDev->output(<<<JSCODE
<script>
// 模板渲染函数
function {$sFuncName}(aVariables)
{
\t// 输出缓存对像
\tvar aDevice = {
\t\t_buffer: ''
\t\t, write: function(data){
\t\t\tthis._buffer+= data ;
\t\t}
\t}
JSCODE
);
        // 指定的模板文件
        if ($aObject->attributes()->string('template')) {
        } else {
            if ($aObject->tailTag()) {
                $aChildCompiledBuff = new TargetCodeOutputStream();
                $aChildCompiledBuff->useHereDoc(false);
                $this->compileChildren($aObject, $aObjectContainer, $aChildCompiledBuff, $aCompilerManager);
                $aJsBuff = new OutputStreamBuffer();
                JavascriptTranslaterFactory::singleton()->create()->compile(new InputStreamCache('<?php ' . $aChildCompiledBuff->bufferBytes(false)), $aJsBuff);
                $aDev->output($aJsBuff);
            } else {
            }
        }
        $aDev->output(<<<JSCODE

\treturn aDevice._buffer ;
}
</script>\t\t\t
JSCODE
);
    }
Example #7
0
 public function apply(ObjectContainer $aObjectContainer, IObject &$aTargetObject)
 {
     if ($this->nKind == self::filter) {
         call_user_func_array($this->fnFilter, array($aObjectContainer, $aTargetObject));
     } else {
         $aWeaveinObject = new WeaveinObject($this->aCompiled, $aTargetObject);
         switch ($this->sType) {
             case self::insertBefore:
                 $aTargetObject->insertAfterByPosition(0, $aWeaveinObject);
                 break;
             case self::insertAfter:
                 $aTargetObject->add($aWeaveinObject);
                 break;
             case self::appendBefore:
                 $aParent = $aTargetObject->parent();
                 if (!$aParent) {
                     throw new Exception("遇到错误,无法将内容织入指定的路径");
                 }
                 $aParent->insertBefore($aTargetObject, $aWeaveinObject);
                 break;
             case self::appendAfter:
                 $aParent = $aTargetObject->parent();
                 if (!$aParent) {
                     throw new Exception("遇到错误,无法将内容织入指定的路径");
                 }
                 $aParent->insertAfter($aTargetObject, $aWeaveinObject);
                 break;
             case self::replace:
                 $aParent = $aTargetObject->parent();
                 if (!$aParent) {
                     throw new Exception("遇到错误,无法将内容织入指定的路径");
                 }
                 $aParent->replace($aTargetObject, $aWeaveinObject);
                 $aTargetObject = $aWeaveinObject;
                 break;
         }
     }
 }
Example #8
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     $aDev->write(self::compileExpression($aObject->source(), $aObjectContainer->variableDeclares()));
 }
Example #9
0
 protected function writeTemplate(IObject $aObject, Attributes $aAttrs, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager, $sWidgetVarName)
 {
     // template
     if ($aAttrs->has('subtemplate')) {
         $sFunName = $aAttrs->string('subtemplate');
         $aDev->write("\t{$sWidgetVarName}->setSubTemplateName('__subtemplate_{$sFunName}') ;");
     } else {
         if ($aAttrs->has('template')) {
             $sTemplateName = $aAttrs->string('template');
             $aDev->write("\t{$sWidgetVarName}->setTemplateName('{$sTemplateName}') ;");
         } else {
             if ($aTemplate = $aObject->getChildNodeByTagName('template')) {
                 $aAttributes = $aTemplate->headTag()->attributes();
                 if ($aAttributes->has('name')) {
                     $sFunName = $aAttributes->string('name');
                 } else {
                     $sFunName = md5(rand());
                 }
                 $aAttributes->set('name', $sFunName);
                 $aTemplate->headTag()->setAttributes($aAttributes);
                 $aDev->write("\t{$sWidgetVarName}->setSubTemplateName('__subtemplate_{$sFunName}') ;");
             }
         }
     }
 }
Example #10
0
 public function compile(IObject $aObject, ObjectContainer $aObjectContainer, TargetCodeOutputStream $aDev, CompilerManager $aCompilerManager)
 {
     $sContents = trim($aObject->source());
     if ($sContents == '*.uri') {
         $aDev->write("\$aDevice->write(\$aVariables->get('theRequest')->uri()) ;");
     } else {
         if (substr($sContents, 0, 5) == '*.url') {
             $sPart = strlen($sContents) > 5 ? substr($sContents, 5) : '';
             switch ($sPart) {
                 case '':
                     $aDev->write("\$aDevice->write(\$aVariables->get('theRequest')->url()) ;");
                     break;
                 case '.scheme':
                     $aDev->write("\$aDevice->write(\$aVariables->get('theRequest')->urlScheme()) ;");
                     break;
                 case '.host':
                     $aDev->write("\$aDevice->write(\$aVariables->get('theRequest')->urlHost()) ;");
                     break;
                 case '.path':
                     $aDev->write("\$aDevice->write(\$aVariables->get('theRequest')->urlPath()) ;");
                     break;
                 case '.query':
                     $aDev->write("\$aDevice->write(\$aVariables->get('theRequest')->urlQuery()) ;");
                     break;
                 default:
                     break;
             }
         }
     }
 }
Example #11
0
 public static function xpathType(IObject $aObject)
 {
     if ($aObject instanceof Text) {
         return '<text>';
     } else {
         if ($aObject instanceof Macro) {
             return '<macro>';
         } else {
             if ($aObject instanceof Node) {
                 return $aObject->tagName();
             } else {
                 return '<unknow>';
             }
         }
     }
 }