3.3 atm与后台语言协同工作方案 php类及使用示例

<?php
class atmjs{
    private $path = ‘/path/to/maps/remote‘;    //这里必须修改
    private $id = ‘‘;
    private $scripts = ‘‘;

    private $debugId = ‘‘;
    private $status = false;
    private $domain = ‘‘;
    private $isDebug = false;
    private $port = 1234;
    private $debugPath = ‘‘;
    private $settings = ‘‘;

    public function import($id){
        $this->id = $id;
        if (preg_match (‘/^([^:]+):([\d]+\.[\d]+\.[\d]+)/‘, $id, $m)) {
            $route =  $m[1].‘/‘.$m[2];
        } else {
            return;
        }
        $filePath = $this->path.‘/‘.$route.‘.json‘;
        $datas = @file_get_contents($filePath);
        if(!$datas){
            return;
        }
        $datas = json_decode($datas, true);

        if( !isset($datas[‘settings‘]) || !isset($datas) || !isset($datas[‘maps‘]) || !isset($datas[‘maps‘][$id]) ){
            return;
        }

        $this->settings = $datas[‘settings‘];
        $this->json = $datas[‘maps‘][$id];

        // 获取domain
        $this->setDomain();

        $this->status = true;

    }

    private function setDomain(){
        $settings = $this->settings;
        $param = $settings[‘debugParam‘];
        $debugId = $_REQUEST[$param];

        // 如果URL或post参数里面有debugParam则为调试模式
        if(isset($debugId)){
            $this->isDebug = true;

            // 如果调试参数值为true,则用127.0.0.1进行调试
            // 如果不是,则认为参数值即为指定的调试地址
            if($debugId==‘true‘ || empty($debugId)){
                $debugId = $this->debugId = ‘127.0.0.1‘;
            }else{
                $debugId = $this->debugId = $debugId;
            }
            $port = $this->port = $settings[‘port‘];
            $domain = ‘http://‘.$debugId.‘:‘.$port.‘/dev‘;
            $this->debugPath = ‘http://‘.$debugId.‘:‘.$port.‘/debug‘;
        }else{
            $domain = $settings[‘domain‘];
        }
        $this->domain = $domain;
    }

    public function loadJs(){
        // 如果入口文件是css类型,则onlyCss字段值为true
        if($this->json[‘onlyCss‘]){
            return;
        }
        // 如果有错误 则输出空字符串
        if(!$this->status){
            return;
        }

        // 如果是调试模式
        if($this->isDebug){
            $debugSrc = $this->debugPath.‘?id=‘.$this->id.‘&type=js&timestamp=‘.time();
            echo implode("\n", array(
                ‘<script type="text/javascript" id="atmjsnode" data-base="‘.$this->domain.‘" src="‘.$debugSrc.‘"></script>‘,
                $this->getUseTag()
            ));
        }else{
            echo implode(‘‘, array(
                $this->getLoaderTag(),
                $this->getMapTag(),
                $this->getJsTags(),
                $this->getUseTag()
            ));
        }
    }

    private function getUseTag(){
        if(!empty($this->scripts)){
            $scripts = ‘ ,‘.$this->scripts;
        }else{
            $scripts = ‘‘;
        }
        $codes = ‘atmjs.use(\‘‘.$this->id.‘\‘‘.$scripts.‘)‘;
        return ‘<script type="text/javascript">‘.$codes.‘</script>‘;
    }

    private function getLoaderTag(){
        $json = $this->json;
        $uri = $json[‘loader‘];
        if(!empty($uri)){
            $url = $this->domain.$uri;
            return ‘<script id="atmjsnode" data-base="‘.$this->domain.‘" type="text/javascript" src="‘.$url.‘"></script>‘;
        }else{
            return ‘‘;
        }
    }

    private function getMapTag(){
        $json = $this->json;
        $map = $json[‘map‘];
        if(!empty($map)){
            return ‘<script type="text/javascript">‘.$json[‘map‘].‘</script>‘;
        }else{
            return ‘‘;
        }
    }

    private function getJsTags(){
        $json = $this->json;
        $js = $json[‘js‘];
        $arr = array();
        if(isset($js)){
            foreach($js as $uri){
                $tag = $this->getJsTag($uri);
                array_push($arr, $tag);
            }
            return implode(‘‘, $arr);
        }else{
            return ‘‘;
        }
    }

    private function getJsTag($uri){
        $url = $this->domain.$uri;
        return ‘<script type="text/javascript" src="‘.$url.‘"></script>‘;
    }

    public function setJs($scripts=‘‘){
        $this->scripts = $scripts;
    }

    public function loadCss(){
        // 如果有错误 则输出空字符串
        if(!$this->status){ echo ‘‘; return;}
        // 如果是调试模式
        if($this->isDebug){
            $debugSrc = $this->debugPath.‘?id=‘.$this->id.‘&type=css&domain=‘.$this->domain.‘&timestamp=‘.time();
            echo ‘<link rel="stylesheet" type="text/css" href="‘.$debugSrc.‘" />‘;
        }else{
            echo $this->getCssTags();
        }
    }

    private function getCssTags(){
        $json = $this->json;
        $css = $json[‘css‘];
        $arr = array();
        if(isset($css)){
            foreach($css as $uri){
                $tag = $this->getCssTag($uri);
                array_push($arr, $tag);
            }
            return implode(‘‘, $arr);
        }else{
            return ‘‘;
        }
    }

    private function getCssTag($uri){
        $url = $this->domain.$uri;
        return ‘<link type="text/css" rel="stylesheet" href="‘.$url.‘" />‘;
    }
}

使用示例:

<?php    require_once(‘./atmjs.php‘);    $atmjs = new atmjs;    $atmjs->import(‘user/account:1.0.0/login‘);?><!doctype html><html><head>    <meta charset="UTF-8">    <title>Document</title>    <?php        $atmjs->loadCss();    ?></head><body><!--some html code-->

some code<?php$scripts= <<<EOTfunction (loginExport) {    //some code}EOT;    $atmjs->setJs($scripts);    $atmjs->loadJs();?></body></html>

  

3.生成的页面源代码:

<!doctype html><html><head>    <meta charset="UTF-8">    <title>Document</title>    <link type="text/css" rel="stylesheet" href="http://cn-style.gcimg.net/static/core/reset/1.0.0/reset_e5b6e95.css" />    <link type="text/css" rel="stylesheet" href="http://cn-style.gcimg.net/static/user/account/1.0.0/account_2e2d290.css" />    <link type="text/css" rel="stylesheet" href="http://cn-style.gcimg.net/static/user/account/1.0.0/css/login_9fa75f4.css" /></head><body><!--some html code-->

some code<script id="atmjsnode" data-base="http://cn-style.gcimg.net/static" type="text/javascript" src="http://cn-style.gcimg.net/static/lib/loader/1.0.0/loader_aa23401.js"></script><script type="text/javascript">atmjs.setMap({"_alias":{},"alias":{"user/account:1.0.0/other/ajax":"/user/account/1.0.0/other/ajax_49ac7f5.js"},"pkg":{},"cssDeps":{}});</script><script type="text/javascript" src="http://cn-style.gcimg.net/static/user/account/1.0.0/account_8c2acc1.js"></script><script type="text/javascript" src="http://cn-style.gcimg.net/static/user/account/1.0.0/exports/login_1074d04.js"></script><script type="text/javascript">atmjs.use(‘user/account:1.0.0/login‘ ,function (loginExport) {        //some code    })</script></body></html>

  

时间: 2024-08-03 15:18:03

3.3 atm与后台语言协同工作方案 php类及使用示例的相关文章

3.1 atm与后台语言协同工作方案

先以登陆页来分析 // ~/blog/user/account/1.0.0 文件夹产出的地图文件, 1.0.0/exports下的入口文件的依赖都生成到了maps里面 { "settings": { "port": 1234, "debugParam": "debugId", "domain": "http://cn-style.gcimg.net/static" }, "ma

3.2 atm与后台语言协同工作方案 -- 调试模式

json文件 // ~/blog/user/account/1.0.0 文件夹产出的地图文件, 1.0.0/exports下的入口文件的依赖都生成到了maps里面 { "settings": { "port": 1234, "debugParam": "debugId", "domain": "http://cn-style.gcimg.net/static" }, "maps

atitit.js&#160;javascript&#160;调用c#&#160;java&#160;php后台语言api&#160;html5交互的原理与总结p97

atitit.js javascript 调用c# java php后台语言api html5交互的原理与总结p97 1. 实现html5化界面的要解决的策略1 1.1. Js交互1 1.2. 动态参数个数2 1.3. 事件监听2 1.4. 异常转换2 2. dwrC.exec3 2.1. 支持 ajax 与browExt模式  支持反射,直接继承调用后台api3 2.2. sendNSCommand (nativeswing的实现)3 2.3. --------nativeswing的实现3

Day5作业,商城+ATM机+后台管理

晚来了....东西太多,需要写的blog内容太多,re讲的渣渣,不明白为什么oldboy经常换老师,吐槽下吧,真心不爱了.... github地址在这:https://github.com/ccorzorz/ATM-shoppmall 商城用原来的,先上图吧: 商城图: ATM后台管理: ATM终端: README: 1.测试帐号: 商城测试帐号:cc/123 或者自己注册 ATM 终端测试帐号:cc/123 alex/123 或者自己注册 ATM 管理员测试帐号:admin/admin 2.需

IOS开发语言Swift入门连载---类和结构体

IOS开发语言Swift入门连载-类和结构体 类和结构体是人们构建代码所用的一种通用且灵活的构造体.为了在类和结构体中实现各种功能,我们必须要严格按照常量.变量以及函数所规定的语法规则来定义属性和添加方法. 与其他编程语言所不同的是,Swift 并不要求你为自定义类和结构去创建独立的接口和实现文件.你所要做的是在一个单一文件中定义一个类或者结构体,系统将会自动生成面向其它代码的外部接口. 注意: 通常一个类 的实例被称为对象 .然而在Swift 中,类和结构体的关系要比在其他语言中更加的密切,本

嵌入式linux C++语言(四)——类与对象

嵌入式linux C++语言(四)--类与对象 类的设计和使用如下: #include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>using namespace std;class Stack{public:    Stack(int size=1024);    ~Stack();    void init();    bool isEmpty();    bool

二、Java语言的简单认识及Hello World示例

1. Java语言的简单认识 (1) Java有三个版本: a. JAVA SE (以前称J2SE):Standard Environment 标准版本: b. JAVA EE (以前称J2EE):Enterprise Environment 企业版: c. JAVA ME (以前称J2ME):Eicro Environment 微型版; (2) Java的安装目录 在前一讲中,提到安装目录中有两个文件夹,分别是jdk1.7.0_45和jre7.在"jdk1.7.0_45"文件夹的bi

初探swift语言的学习笔记(类对象,函数)

swift扩展了很多功能和属性,有些也比较奇P.只有慢慢学习,通过经验慢慢总结了. 下面将初步学习一下类的写法. 码工,最大爱好就是看码,而不是文字,太枯燥. // // computer.swift // swiftDemo // // Created by apple on 14-6-8. // Copyright (c) 2014年 fengsh. All rights reserved. /* 写本例子的目的在于快速学习swift类的写法,包括知识点: 1.属性设置 2.构造.释构 3.

Java 语言中的 StringBuffer类 硬伤ING

StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类. 所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入.删除等操作,使用StringBuffer要更加适合一些. 在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能是完全一样的. 但是有一个最显著的区别在于,