• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Laravel5 源码解析 (一)

php 搞代码 3年前 (2022-01-23) 10次浏览 已收录 0个评论

Laravel5 源码解析

autoload.php 用的是composer的autoload

这里就不做说明了

app.php 启动整个项目

首先看下Application这个类

class Application extends Container implements ApplicationContract, HttpKernelInterface
Container中方法很多,实现了ArrayAccess,主要方法都是是用于绑定对象的(利于重用)。
ApplicationContract 需要实现注册service provider。
HttpKernelInterface 就一个handle方法,接受一个Request, 返回一个Response。

Application的构造方法

  • registerBaseBindings()
    把app对象自身绑定到instance属性数组。

  • registerBaseServiceProviders()
    注册两个service provider, 一个是EventServiceProvider,注册一个单例Dispatcher,名字为’events’(TODO),另一个是RoutingServiceProvider(), 包含了Router, RouterGenerator, Redirector, ResponseFactory, 构造ResponseFactory接受两个参数,一个是ViewFactory,一个是Redirecer

  • registerCoreContainerAliases(), 把一系列key和对应的类名,接口名加入alias属性数组
    'app' => ['Illuminate\Foundation\Application', 'Illuminate\Contracts\Container\Container', 'Illuminate\Contracts\Foundation\Application']
    ‘app’是key, 数组中的三个是别名(猜测:估计是每当用

    2本文来源gao!daima.com搞$代!码网

    搞代gaodaima码app?>make(Illuminate\Foundation\Application),<script type=”math/tex”>app->make(‘Illuminate\Foundation\Application’),都会返回</script>app[‘app’])

构造完App后开始创建重要的instance

  1. 在app容器中共享一个App\Http\Kernel单例。
    看一下这个类,继承了一个Illuminate\Foundation\Http\Kernel,实现了bootstrap, handle, terminate, getApplication这四个方法,可以想象,当收到request时候,流程就是前三个依次执行,非常简化。
    protected $bootstrappers数组,包含了bootstrap的一系列启动项目。
    他的__construct接受两个参数,一个Application,一个Router。需要再看一下$app->singleton是如何初始化他的,莫非已经包含了IoC的功能?(TODO)
    这里先假设这两个参数已经被正确传入了。接着,立即把protected $routeMiddleware中的给router的middleware方法调用。middleware方法只是把key和Class名称加入到router的middleware属性数组中。

  2. 在app容器中共享一个App\Console\Kernel单例。
    继承自Illuminate\Foundation\Console\Kernal。他的构造方法接受一个appDispatcher<script type=”math/tex”>app,一个Dispatcher </script>event。并且设置了一个scheduler,用于每隔一个时间段执行任务。protected $commands属性和protected function schedule方法都是用来重写的。

  3. 在app容器中共享一个App\Exceptions\Handler单例。
    继承自Illuminate\Foundation\Exceptions\Handler。他的构造方法接受一格Psr\Log\LoggerInterface

至此,app算是构造完成。接下来就要调用他的方法了。

调用Application

kernel=<script type=”math/tex”>kernel = </script>app->make(‘Illuminate\Contracts\Http\Kernel’);

response=<script type=”math/tex”>response = </script>kernel->handle(
$request = Illuminate\Http\Request::capture()
);

$response->send();

kernel?>terminate(<script type=”math/tex”>kernel->terminate(</script>request, $response);

这里的代码非常明确,首先make一个Kernal,之前已经将其绑定为singleton了。然后kernal handle一个request, 得到一个response, response调用send方法,最后kernal terminate。这里只是一个高纬度的概括,具体其中实现的方法,还需要进一步深入。

看到这里,我的感觉是,整个项目最重要的部分就是$app这个容器,或者说Container这个类,绑定的是什么(Closure),有哪些绑定方法,各个方法的作用是什么,make 和 build有什么区别,alias的作用是什么,等。了解了这些,对写框架会有帮助。

<script type=”text/javascript”> $(function () { $(‘pre.prettyprint code’).each(function () { var lines = $(this).text().split(‘\n’).length; var $numbering = $(‘

    ‘).addClass(‘pre-numbering’).hide(); $(this).addClass(‘has-numbering’).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('

  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

    以上就介绍了Laravel5 源码解析 (一),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。


    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:Laravel5 源码解析 (一)

    喜欢 (0)
    [搞代码]
    分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址