简介 我们继续承接上节课说的,接着学习 database-seeding 的工作原理:
database-seeding 命令行参数
首先,我们执行 php artisan help db:seed 看看发生什么,结果如下图所示:
默认情况下,我们没有写 –class 参数即启动 seeder,会自动调用 databaseseeder;
seedcommand seed命令行 通过 ide(猜测类名为seedercommand或位置) 定位到 seedcommad.php ,执行入口方法为 fire() 后续会详细说明,先总体了解类中方法,具体代码及注释如下:
class seedcommand extends command{ //引用确认提示 use confirmabletrait; /** * the console command name. * * @var string */ protected $name = 'db:seed'; /** * the console command description. * * @var string */ protected $description = 'seed the database with records'; /** * the connection resolver instance. * * @var \illuminate\database\connectionresolverinterface */ protected $resolver; /** * create a new database seed command instance. * * @param \illuminate\database\connectionresolverinterface $resolver * @return void */ public function __construct(resolver $resolver) { parent::__construct(); $this->resolver = $resolver; } /** * execute the console command. * 执行console命令行 * @return void */ public function fire() { if (!$this->confirmtoproceed()) { return; } $this->resolver->setdefaultconnection($this->getdatabase()); // 获取相应 seeder,并调用其 run 方法 $this->getseeder()->run(); } /** * get a seeder instance from the container. * 获取输入的class参数, * @return \illuminate\database\seeder */ protected function getseeder() { // 从输入的class参数,获取seeder;相当于app::make('classname'),默认为 databaseseeder $class = $this->laravel->make($this->input->getoption('class')); return $class->setcontainer($this->laravel)->setcommand($this); } /** * get the name of the database connection to use. * 获取要使用的数据库链接名称 * @return string */ protected function getdatabase() { // 获取输入database 参数值 $database = $this->input->getoption('database'); // 如果没有输入database 参数值,则获取配置文件中默认数据库即config\database.php中default参数value return $database ?: $this->laravel['config']['database.default']; } /** * get the console command options. * 获取console命令行参数 * @return array */ protected function getoptions() { return [ ['class', null, inputoption::value_optional, 'the class name of the root seeder', 'databaseseeder'], ['database', null, inputoption::value_optional, 'the database connection to seed'], ['force', null, inputoption::value_none, 'force the operation to run when in production.'], ]; }}
fire 方法 用户执行 console 命令,即调用 seedcommand 类中 fire 方法,fire 方法中先通过引用 use confirmabletrait , 调用相关 confirmtoproceed 方法,来确认是否继续,具体代码及注释如下:
trait confirmabletrait{ /** * confirm before proceeding with the action. * 在进行动作之前确认 * @param string $warning * @param \closure|bool|null $callback * @return bool */ public function confirmtoproceed($warning = 'application in production!', $callback = null) { // 获取默认确认信息,如果$callback为空则查找默认环境变量,production则为ture $callback = is_null($callback) ? $this->getdefaultconfirmcallback() : $callback; // 是否需要确认信息 $shouldconfirm = $callbackinstanceof closure ? call_user_func($callback) : $callback; if ($shouldconfirm) { // 如果参数有force,则直接返回true,标识已确认 if ($this->option('force')) { return true; } // 输出确认提示告警信息 $this->comment(str_repeat('*', strlen($warning) + 12)); $this->comment('* ' . $warning . ' *'); $this->comment(str_repeat('*', strlen($warning) + 12)); $this->output->writeln(''); $confirmed = $this->confirm('do you really wish to run this command? [y/n]'); // 通过用户输入的y或者n判断是否执行 if (!$confirmed) { $this->comment('command cancelled!'); return false; } } return true; } /** * get the default confirmation callback. * * @return \closure */ protected function getdefaultconfirmcallback() { return function () { //判断容器环境是否=production(即.env中app_env对应值),等于则返回true return $this->getlaravel()->environment() == 'production'; }; }}
将.env中的app_env值由默认local置为production,执行 php artisan db:seed ,结果如下图所示:
执行 php artisan db:seed --force 则跳过提示,直接执行 seed 命令。
databaseseeder默认seeder 命令 php artisan db:seed 默认执行 databaseseeder 中的 run 方法,回头来看下call方法如何执行:
class databaseseeder extends seeder{ protected $totruncate = ['users','lessons']; /** * run the database seeds. * * @return void */ public function run() { model::unguard(); // 循环删除表 foreach($this->totruncateas $table) { db::table($table)->truncate(); } // 调用seeder父类中 call 方法: $this->call('userstableseeder'); $this->call('lessonstableseeder'); }}
seed 类中 call 和 resolve 方法:
/*** seed类中call方法* seed the given connection from the given path.* 通过给出的路径进行seed操作* @param string $class* @return void*/public function call($class){ // 容器存在则app::make($class)->run(),否则new一个$class,调用run $this->resolve($class)->run(); // 通过接受$class,输出执行seeded的类名 if (isset($this->command)) { $this->command->getoutput()->writeln(seeded: $class); }} /*** resolve an instance of the given seeder class.* 获取一个给定seeder类的实例* @param string $class* @return \illuminate\database\seeder*/protected function resolve($class){ if (isset($this->container)) { $instance = $this->container->make($class); $instance->setcontainer($this->container); } else { $instance = new $class; } if (isset($this->command)) { $instance->setcommand($this->command); } return $instance;}
