zoukankan      html  css  js  c++  java
  • 理解Flight框架核心

    http://blog.csdn.net/sky_zhe/article/details/38906689

    Flight 框架

    Flight类

    1、加载 autoload.php ,启动框架的自动加载机制

    实际执行的是 Loader::autoload 函数, 关键代码

    $class_file = str_replace(array('\', '_'), '/', $class).'.php';

    可以看到是依据PSR-0标准来做的,_ 也被转化为目录

    另外同时定义了,根目录是项目根目录

    2、启动 Engine.php 

     使用 __callStatic 函数将对调用转到 Engine 类上:

    return flightcoreDispatcher::invokeMethod(array(self::$engine, $name), $params); 

    Engine类

    1、首先是个容器

        protected $vars; //变量
        protected $loader; //
        protected $dispatcher; //函数或方法,(称为事件)

    对变量的储存和使用,是通过 has,get,set,clear 等方法

    2、对类(component)的管理,通过 Loader 类

    Loader 通过 register 方法注册类及加载方法,通过 load 方法加载(实现了单例模式)

    框架本身已经实现了 request,response,router和view等类

    也可以使用Engine的register 方法添加自定义的类

    3、对事件(函数或方法)的调用,通过 Dispatcher 类

    对函数或方法命名,定义一个事件

    Dispatcher 通过 set 注册事件,get获得事件,并通过 run 执行事件,

    在run过程中,很重要的,就是可以hook一个事件,

        public function hook($name, $type, $callback) {
            $this->filters[$name][$type][] = $callback; // type 可以是before or after
        }

    也就是在事件被实际调用(execute)前后,调用(filter)所有的自定义函数(在filters数组里面)

    (最后执行函数的是callFunction,方法的是invokeMethod)

    框架本身定义了多个事件:

    start 是框架开始执行,stop 是结束,route是添加路由,还用redirect等等

    render是显示页面,json,jsonp等等

    这些事件的实际执行代码也在Engine类中(以_开头)

    可以用map添加自定义事件,用before,after分别hook事件前后

    4、类和事件的调用

    如上,类和事件管理在Loader 和 Dispatcher 中,但都是通过Engine类调用的

        public function register($name, $class, array $params = array(), $callback = null) {
            $this->loader->register($name, $class, $params, $callback);
        }
    
        public function map($name, $callback) {
            $this->dispatcher->set($name, $callback);
        }
    
        public function before($name, $callback) {
            $this->dispatcher->hook($name, 'before', $callback);
        }
    
        public function after($name, $callback) {
            $this->dispatcher->hook($name, 'after', $callback);
        }

    要获得类(如上,实际存在dispatcher中),需要通过方法调用(因为是通过 __call 实现的):

        $request = $this->request();
        $response = $this->response();
        $router = $this->router();

    变量可以直接通过 get 获得

    5、错误处理 handleErrors 和 配置

    通过 set_error_handler 和set_exception_handler 设置自定义错误处理代替php默认的处理,也是php框架的标配了。

    配置 就保存在 Engine 类的vars 里面,可以通过 set,get方法处理

    Flight类 只有静态方法,且全部委托 Engine 对象,

    Engine对象 

    用来管理变量,类或事件的方法,如 set,get,register,map,before,after 就是所谓核心方法,

    而所谓扩展方法,指注册到 Dispatcher中的事件,其实也是函数。而系统的事件,也是在 Engine中实现的。

    Engine对象中,核心方法管理对象和事件,扩展方法则把事件任务分给了dispatcher ,对象任务分给loader

    系统事件

    1、start

    读取request,路由

    设置输出缓存 和配置(就是变量,这些设置是在engine->ini中设置的,且硬编码无配置文件)

    添加 after hook,这个事件也就是调用stop事件,这个此时动态添加的hook事件也会执行,最终输出缓存

    匹配分发路由,

    2. stop

    简单的输出缓存(根据上面,此事件作为 start事件的hook after事件被自动调用的。

  • 相关阅读:
    9.11 eventbus
    9.10,,,实现new instanceof apply call 高阶函数,偏函数,柯里化
    9.9 promise实现 写完了传到gitee上面了,这里这个不完整
    9.5cors配置代码
    9.5 jsonp 实现
    9.5 http tcp https总结
    9.3 es6 class一部分 and es5 class 发布订阅
    8.30 cookie session token jwt
    8.30vue响应式原理
    warning: LF will be replaced by CRLF in renard-wx/project.config.json. The file will have its original line endings in your working directory
  • 原文地址:https://www.cnblogs.com/mitang/p/5185541.html
Copyright © 2011-2022 走看看