thinkphp5源码分析2

接着上篇 thinkphp5源码分析1 继续thinkphp的源码分析。

上一章我们讲到了run和send方法,这两个方法到底干了些什么呢?
我跟别人的分析方法是有区别的,他们只发一些代码上来分析。这和源代码中的注释区别不大,并不能让读者或者php的开发者能一下子看懂整个代码。反而越看越不明白。失去了方向和兴趣。
当然代码还是要看的,更重要的是看代码后面的逻辑。

// 2. 执行应用
App::run()->send();

App->run()

public static function run(Request $request = null)
    {
        //获取了一个request对象(实例)
        $request = is_null($request) ? Request::instance() : $request;
        try {
        //初始化配置,后面的代码都围绕着$config做的操作
            $config = self::initCommon();
        ...
       return $response;
    }

都写到这了,你也该知道下面要干什么了吧。
对,把这个对象,和配置输出来。看看到底是什么样子。后面的代码看起来就so easy了。

Array
(
    [app_host] => 
    [app_debug] => 
    [app_trace] => 
    [app_status] => 
    [app_multi_module] => 1
    [auto_bind_module] => 
    [root_namespace] => Array
        (
        )

    [extra_file_list] => Array
        (
            [0] => /Users/alert/Desktop/thinkphp/thinkphp/helper.php
        )

    [default_return_type] => html
    [default_ajax_return] => json
    [default_jsonp_handler] => jsonpReturn
    [var_jsonp_handler] => callback
    [default_timezone] => PRC
    [lang_switch_on] => 
    [default_filter] => 
    [default_lang] => zh-cn
    [class_suffix] => 
    [controller_suffix] => 
    [default_module] => index
    [deny_module_list] => Array
        (
            [0] => common
        )

    [default_controller] => Index
    [default_action] => index
    [default_validate] => 
    [empty_controller] => Error
    [use_action_prefix] => 
    [action_suffix] => 
    [controller_auto_search] => 
    [var_pathinfo] => s
    [pathinfo_fetch] => Array
        (
            [0] => ORIG_PATH_INFO
            [1] => REDIRECT_PATH_INFO
            [2] => REDIRECT_URL
        )

    [pathinfo_depr] => /
    [https_agent_name] => 
    [url_html_suffix] => html
    [url_common_param] => 
    [url_param_type] => 0
    [url_route_on] => 1
    [route_config_file] => Array
        (
            [0] => route
        )

    [route_complete_match] => 
    [url_route_must] => 
    [url_domain_deploy] => 
    [url_domain_root] => 
    [url_convert] => 1
    [url_controller_layer] => controller
    [var_method] => _method
    [var_ajax] => _ajax
    [var_pjax] => _pjax
    [request_cache] => 
    [request_cache_expire] => 
    [request_cache_except] => Array
        (
        )

    [template] => Array
        (
            [type] => Think
            [view_path] => 
            [view_suffix] => html
            [view_depr] => /
            [tpl_begin] => {
            [tpl_end] => }
            [taglib_begin] => {
            [taglib_end] => }
        )

    [view_replace_str] => Array
        (
        )

    [dispatch_success_tmpl] => /Users/alert/Desktop/thinkphp/thinkphp/tpl/dispatch_jump.tpl
    [dispatch_error_tmpl] => /Users/alert/Desktop/thinkphp/thinkphp/tpl/dispatch_jump.tpl
    [exception_tmpl] => /Users/alert/Desktop/thinkphp/thinkphp/tpl/think_exception.tpl
    [error_message] => 页面错误!请稍后再试~
    [show_error_msg] => 
    [exception_handle] => 
    [record_trace] => 
    [log] => Array
        (
            [type] => File
            [path] => /Users/alert/Desktop/thinkphp/runtime/log/
            [level] => Array
                (
                )

        )

    [trace] => Array
        (
            [type] => Html
        )

    [cache] => Array
        (
            [type] => File
            [path] => /Users/alert/Desktop/thinkphp/runtime/cache/
            [prefix] => 
            [expire] => 0
        )

    [session] => Array
        (
            [id] => 
            [var_session_id] => 
            [prefix] => think
            [type] => 
            [auto_start] => 1
        )

    [cookie] => Array
        (
            [prefix] => 
            [expire] => 0
            [path] => /
            [domain] => 
            [secure] => 
            [httponly] => 
            [setcookie] => 1
        )

    [database] => Array
        (
            [type] => mysql
            [dsn] => 
            [hostname] => 127.0.0.1
            [database] => 
            [username] => root
            [password] => 
            [hostport] => 
            [params] => Array
                (
                )

            [charset] => utf8
            [prefix] => 
            [debug] => 1
            [deploy] => 0
            [rw_separate] => 
            [master_num] => 1
            [slave_no] => 
            [fields_strict] => 1
            [resultset_type] => array
            [auto_timestamp] => 
            [datetime_format] => Y-m-d H:i:s
            [sql_explain] => 
        )

    [paginate] => Array
        (
            [type] => bootstrap
            [var_page] => page
            [list_rows] => 15
        )

    [console] => Array
        (
            [name] => Think Console
            [version] => 0.1
            [user] => 
        )

)

通过self::initCommon()方法把文件convention.php的配置读到了$config数组中了。里面的内容是不是很快就看懂了呢?

再看Request对象

think\Request Object
(
    [method:protected] => GET
    [domain:protected] => 
    [url:protected] => 
    [baseUrl:protected] => 
    [baseFile:protected] => 
    [root:protected] => 
    [pathinfo:protected] => /
    [path:protected] => /
    [routeInfo:protected] => Array
        (
        )

    [env:protected] => 
    [dispatch:protected] => Array
        (
            [type] => module
            [module] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                )

        )

    [module:protected] => index
    [controller:protected] => Index
    [action:protected] => index
    [langset:protected] => zh-cn
    [param:protected] => Array
        (
        )

    [get:protected] => Array
        (
        )

    [post:protected] => Array
        (
        )

    [request:protected] => Array
        (
        )

    [route:protected] => Array
        (
        )

    [put:protected] => 
    [session:protected] => Array
        (
        )

    [file:protected] => Array
        (
        )

    [cookie:protected] => Array
        (
        )

    [server:protected] => Array
        (
            [HTTP_HOST] => localhost:8888
            [HTTP_CONNECTION] => keep-alive
            [HTTP_CACHE_CONTROL] => max-age=0
            [HTTP_UPGRADE_INSECURE_REQUESTS] => 1
            [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
            [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
            [HTTP_REFERER] => http://localhost:8888/
            [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
            [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.9,en;q=0.8
            [HTTP_COOKIE] => Phpstorm-3a8f796c=7fdba4f6-f510-4bae-ba6c-d793a31084ea; __utmz=111872281.1522708417.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); KXnd_2132_widthauto=1; KXnd_2132_saltkey=sp7PNpii; KXnd_2132_lastvisit=1523585123; KXnd_2132_ulastactivity=9714C6ZrmbU65nf4SXyZdVeYmZNXjs3HLIl7wOjC%2FNCWAr6elOiR; KXnd_2132_lastcheckfeed=1%7C1523588755; KXnd_2132_nofavfid=1; UM_distinctid=162eaa500235ed-0cb2adee1adbca-336b7b05-1aeaa0-162eaa50024e3d; CNZZDATA1256279252=518537777-1524354653-http%253A%252F%252Flocalhost%253A8888%252F%7C1524404828; __utmc=111872281; __utma=111872281.1830718118.1522708417.1524462548.1524473402.21; __utmt=1; __utmb=111872281.15.10.1524473402
            [PATH] => /usr/bin:/bin:/usr/sbin:/sbin
            [SERVER_SIGNATURE] => 
            [SERVER_SOFTWARE] => Apache/2.2.34 (Unix) mod_wsgi/3.5 Python/2.7.13 PHP/7.2.1 mod_ssl/2.2.34 OpenSSL/1.0.2j DAV/2 mod_fastcgi/2.4.6 mod_perl/2.0.9 Perl/v5.24.0
            [SERVER_NAME] => localhost
            [SERVER_ADDR] => ::1
            [SERVER_PORT] => 8888
            [REMOTE_ADDR] => ::1
            [DOCUMENT_ROOT] => /Applications/MAMP/htdocs
            [SERVER_ADMIN] => [email protected]
            [SCRIPT_FILENAME] => /Applications/MAMP/htdocs/thp/index.php
            [REMOTE_PORT] => 56667
            [GATEWAY_INTERFACE] => CGI/1.1
            [SERVER_PROTOCOL] => HTTP/1.1
            [REQUEST_METHOD] => GET
            [QUERY_STRING] => 
            [REQUEST_URI] => /thp/
            [SCRIPT_NAME] => /thp/index.php
            [PHP_SELF] => /thp/index.php
            [REQUEST_TIME_FLOAT] => 1524476053.033
            [REQUEST_TIME] => 1524476053
            [argv] => Array
                (
                )

            [argc] => 0
        )

    [header:protected] => Array
        (
        )

    [mimeType:protected] => Array
        (
            [xml] => application/xml,text/xml,application/x-xml
            [json] => application/json,text/x-json,application/jsonrequest,text/json
            [js] => text/javascript,application/javascript,application/x-javascript
            [css] => text/css
            [rss] => application/rss+xml
            [yaml] => application/x-yaml,text/yaml
            [atom] => application/atom+xml
            [pdf] => application/pdf
            [text] => text/plain
            [image] => image/png,image/jpg,image/jpeg,image/pjpeg,image/gif,image/webp,image/*
            [csv] => text/csv
            [html] => text/html,application/xhtml+xml,*/*
        )

    [content:protected] => 
    [filter:protected] => 
    [bind:protected] => Array
        (
        )

    [input:protected] => 
    [cache:protected] => 
    [isCheckCache:protected] => 
)

找到点感觉没有,在run()的结尾返回了一个response。那就顺便看看response是个什么。so easy!

think\Response Object
(
    [data:protected] => 

:)

ThinkPHP V5
十年磨一剑 - 为API开发设计的高性能框架

[ V5.0 版本由 七牛云 独家赞助发布 ]
[contentType:protected] => text/html [charset:protected] => utf-8 [code:protected] => 200 [options:protected] => Array ( ) [header:protected] => Array ( [Content-Type] => text/html; charset=utf-8 ) [content:protected] => )

天啦!你看到没有内容已经在run()里面都处理完了。那send()干了些啥。
莫非真的是发送了不一样的东西么?

Response->send()

public function send()
    {
        // 处理输出数据
        $data = $this->getContent();
        echo $data;

没错,精简下来的代码就这点,用getContent()方法读取Response对象里的data数据。[data:protected] 这个内容是protected的。类外是无法访问的。所以写了个getContent()的方法。

到目前为止从URL访问,到数据返回整个框架里面处理都写完了。
是不是比想象的要简单了,记住分析事物,不要上来就事无巨细的分析。否则你会陷入代码的迷宫里无法自拔。最后匆匆的放弃!什么也没发现。

你可能感兴趣的:(thinkphp5源码分析2)