首页 > 作文

浅谈Laravel核心解读之Console内核

更新时间:2023-04-06 09:06:32 阅读: 评论:0

console内核

上一篇文章我们介绍了laravel的http内核,详细概述了网络请求从进入应用到应用处理完请求返回http响应整个生命周期讪笑的意思中http内核是如何调动laravel各个核心组件来完成任务的。除了处理http请求一个健壮的应用经常还会需要执行计划任务、异步队列这些。laravel为了能让应用满足这些场景设计了artisan工具,通过artisan工具定义各种命令来满足非http请求的各种场景,artisan命令通过laravel的console内核来完成对应用核心组件的调度来完成任务。 今天我们就来学习一下laravel console内核的核心代码。

内核绑定

跟http内核一样,在应用初始化阶有一个内核绑定的过程,将console内核注册到应用的服务容器里去,还subtotal函数是引用上一篇文章引用过的bootstrap/app.php里的代码

<?php// 第一部分: 创建应用实端午节来历和习俗例$app = new illuminate\foundation\application(  realpath(__dir__.'/../'));// 第二部分: 完成内核绑定$app->singleton(  illuminate\contracts\http\kernel::class,  app\http\kernel::class);// console内核绑定$app->singleton(  illuminate\contracts\console\kernel::class,  app\console\kernel::class);$app->singleton(  illuminate\contracts\debug\exceptionhandler::class,  app\exceptions\handler::class);return $app;

console内核 \app\console\kernel继承自illuminate\foundation\console, 在console内核中我们可以注册artisan命令和定义应用里要执行的计划任务。

/*** define the application's command schedule.** @param \illuminate\console\scheduling\schedule $schedule* @return void*/protected function schedule(schedule $schedule){  // $schedule->command('inspire')  //     ->hourly();}/*** register the commands for the application.** @return void*/protected function commands(){  $this->load(__dir__.'/commands');  require ba_path('routes/console.php');}

在实例化console内核的时候,内核会定义应用的命令计划任务(shedule方法中定义的计划任务)

public function __construct(application $app, dispatcher $events){  if (! defined('artisan_binary')) {    define('artisan_binary', 'artisan');  }  $this->app = $app;  $this->events = $events;  $this->app->booted(function () {    $this->defineconsoleschedule();  });}

应用解析console内核

查看aritisan文件的源码我们可以看到, 完成console内核绑定的绑定后,接下来就会通过服务容器解析出console内核对象

$kernel = $app->make(illuminate\contracts\console\kernel::class);$status = $kernel->handle(  $input = new symfony\component\console\input\argvinput,  new symfony\component\console\output\consoleoutput);

执行命令任务

解析出console内核对象后,接下来就要处理来自命令行的命令请求了, 我们都知道php是通过全局变量$_rver[‘argv’]来接收所有的命令行输入的, 和命令行里执行shell脚本一样(在shell脚本里可以通过$0获取脚本文件名,$1 $2这些依次获取后面传递给shell脚本的参数选项)索引0对应的是脚本文件名,接下来依次是命令行里传递给脚本的所有参数选项,所以在命令行里通过artisan脚本执行的命令,在artisan脚本中$_rver[‘argv’]数组里索引0对应的永远是artisan这个字符串,命令行里后面的参数会依次对应到$_rver[‘argv’]数组后续的元素里。

因为artisan命令的语法中可以指定命令参数选项、有的选项还可以指定实参,为了减少命令行输入参数解析的复杂度,laravel使用了symfony\component\console\input对象来解析命令行里这些参数选项(she团队拓展训练ll脚本里其实也是一样,会通过shell函数getopts来解析各种格式的命令行参数输入),同样地laravel使用了symfony\component\console\output对象来抽象化命令行的标准输出。

引导应用

在console内核的handle方法里我们可以看到和http内核处理请求前使用bootstrapper程序引用应用一样在开始处理命令任务之前也会有引导应用这一步操作

其父类 「illuminatefoundationconsolekernel」 内部定义了属性名为 「bootstrappers」 的 引导程序 数组:

protected $bootstrappers = [  \illuminate\foundation\bootstrap\loadenvironmentvariables::class,  \illuminate\foundation\bootstrap\loadconfiguration::class,  \illuminate\foundation\bootstrap\handleexceptions::class,  \illuminate\foundation\bootstrap\registerfacades::class,  \illuminate\foundation\bootstrap\trequestforconsole::class,  \illuminate\foundation\bootstrap\registerproviders::class,  \illuminate\foundation\bootstrap\bootproviders::class,];

数组中包括的引导程序基本上和http内核中定义的引导程序一样, 都是应用在初始化阶段要进行的环境变量、配置文件加载、注册异常处理器、设置console请求、注册应用中的服务容器、facade和启动服务。其中设置console请求是唯一区别于http内核的一个引导程序。

执行命令

执行命令是通过console application来执行的,它继承自symfony框架的symfony\component\console\application类, 通过对应的run方法来执行命令。

name illuminate\foundation\console;class kernel implements kernelcontract{  public function handle($input, $output = null)  {    try {      $this->bootstrap();      return $this->getartisan()->run($input, $output);    } catch (exception $e) {      $this->reportexception($e);      $this->renderexception($output, $e);      return 1;    } catch (throwable $e) {      $e = new fatalthrowableerror($e);      $this->reportexception($e);      $this->renderexception($output, $e);      return 1;    }  }}namespace symfony\component\console;class application{  //执行命令  public function run(inputinterface $input = null, outputinterface $output = null)  {    ......    try {      $exitcode = $this->dorun($input, $output);    } catch {      ......    }    ......    return $exitcode;  }    public function dorun(inputinterface $input, outputinterface $output)  {    //解析出命令名称    $name = $this->getcommandname($input);        //解析出入参    if (!$name) {      $name = $this->defaultcommand;      $definition = $this->getdefinition();      $definition->targuments(array_merge(        $definition->getarguments(),        array(          'command' => new inputargument('command', inputargument::optional, $definition->getargument('command')->getdescription(), $name),        )      ));    }    ......    try {      //通过命令名称查找出命令类(命名空间、类名等)      $command = $this->find($name);    }    ......    //运行命令类    $exitcode = $this->doruncommand($command, $input, $output);        return $exitcode;  }    protected function doruncommand(command $command, inputinterface $input, outputinterface $output)  {    ......    //执行命令类的run方法来处理任务    $exitcode = $command->run($input, $output);    ......        return $exitcode;  }}

执行命令时主要有三步操作:

通过命令行输入解析出命令名称和参数选项。通过命令名称查找命令类的命名空间和类名。执行命令类的run方法来完成任务处理并返回状态码。

和命令行脚本的规范一样,如果执行命令任务程序成功会返回0, 抛出异常退醋可以美白牙齿吗出则返回1。

还有就是打开命令类后我们可以看到并没有run方法,我们把处理逻辑都写在了handle方法中,仔细查看代码会发现run方法定义在父类中,在run方法会中会调用子类中定义的handle方法来完成任务处理。 严格遵循了面向对象程序设计的solid 原则。

结束应用

执行完命令程序返回状态码后, 在artisan中会直接通过exit($status)函数输出状态码并结束php进程,接下来shell进程会根据返回的状态码是否为0来判断脚本命令是否执行成功。

到这里通过命令行开启的程序进程到这里就结束了,跟http内核一样console内核在整个生命周期中也是负责调度,只不过http内核最终将请求落地到了controller程序中而console内核则是将命令行请求落地到了laravel中定义的各种命令类程序中,然后在命令类里面我们就可以写其他程序一样自由地使用laravel中的各个组件和注册到服务容器里的服务了。

本文已经收录在系列文章laravel源码学习里,欢迎访问阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-06 09:06:30,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/2006beab639af269675e81357f05550c.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:浅谈Laravel核心解读之Console内核.doc

本文 PDF 下载地址:浅谈Laravel核心解读之Console内核.pdf

标签:内核   命令   命令行   脚本
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图