Laravel学习笔记之Artisan命令生成自定义模板的方法
说明:本文主要讲述laravel的artisan命令来实现自定义模板,就如经常输入的php artisan make:controller shopcontroller
就会自动生成一个shopcontroller.php
模板文件一样,通过命令生成模板也会提高开发效率。同时,作者会将开发过程中的一些截图和代码黏上去,提高阅读效率。
备注:个人平时在写repository代码时会这样写,如先写上shoprepositoryinterface并定义好接口方法如all()
、create()
、update()
、delete()
、findby()
等等,然后再写上接口对应的实现shoprepository并注入对应的model即shop。别的postrepository、tagrepository也会是这么写(当然,对于很多重用的repository方法可以集体拿到abstractrepository抽象类里供子类继承,实现代码复用
)。那能不能直接命令行生成模板文件呢,就不用自己一个个的写了,就像输入php artisan make:controller postcontroller
给我一个controller模板来。
关于使用repository模式来封装下model逻辑,不让controller里塞满了很多model逻辑,这样做是有很多好处的,最主要的就是好测试和代码架构清晰,也符合solid原则。如果使用phpunit来做测试就知道了为啥说好测试了。segmentfault上也有相关的文章描述。作者也打算最近新开一篇文章聊一聊这个,phpunit也打算过段时间聊一聊。
个人研究了下artisan命令行,是可以的。经过开发后,结果是输入自定义指令php artisan make:repository postrepository --model=post(这个option可要可不要)
,就会帮我生成一个postrepositoryinterface和对应的接口实现postrepository。
模板文件stub
由于个人需要生成一个repositoryinterface和对应接口实现repository,那就需要两个模板文件了。在resources/stubs新建两个模板文件,以下是个人经常需要的两个模板文件(你可以自定义):
/** * @param array $columns * @return \illuminate\database\eloquent\collection|static[] */ public function all($columns = array('*')) { return $this->$model_var_name->all($columns); } /** * @param int $perpage * @param array $columns * @return \illuminate\contracts\pagination\lengthawarepaginator */ public function paginate($perpage = 15, $columns = array('*')) { return $this->$model_var_name->paginate($perpage, $columns); } /** * create a new $model_var_name * @param array $data * @return \$model_namespace */ public function create(array $data) { return $this->$model_var_name->create($data); } /** * update a $model_var_name * @param array $data * @param $id * @return \$model_namespace */ public function update($data = [], $id) { return $this->$model_var_name->whereid($id)->update($data); } /** * store a $model_var_name * @param array $data * @return \$model_namespace */ public function store($data = []) { $this->$model_var_name->id = $data['id']; //... $this->$model_var_name->save(); } /** * delete a $model_var_name * @param array $data * @param $id * @return \$model_namespace */ public function delete($data = [], $id) { $this->$model_var_name->whereid($id)->delete(); } /** * @param $id * @param array $columns * @return array|\illuminate\database\eloquent\collection|static[] */ public function find($id, $columns = array('*')) { $$model_name = $this->$model_var_name->whereid($id)->get($columns); return $$model_name; } /** * @param $field * @param $value * @param array $columns * @return \illuminate\database\eloquent\collection|static[] */ public function findby($field, $value, $columns = array('*')) { $$model_name = $this->$model_var_name->where($field, '=', $value)->get($columns); return $$model_name; } }
模板文件里包括参数,这些参数将会根据命令行中输入的参数和选项被相应替换:
artisan命令生成repository模板文件
生成artisan命令并注册
laravel提供了artisan命令自定义,输入指令:
php artisan make:console makerepositorycommand
然后改下签名和描述:
// app/console/commands/makerepositorycommand /** * the name and signature of the console command. * * @var string */ protected $signature = 'make:repository {repository} {--model=}'; /** * the console command description. * * @var string */ protected $description = 'make a repository and interface';
这里{repository}是必填参数并指明(选填参数加个?
,就和路由参数一样),将会被$this->argument('repository')方法捕捉到,{--model=}是选项,可填可不填,将会被$this->option('model')方法捕捉到。填上这个命令的描述,最后在console的kernel里注册下命令:
// app/console/kernel protected $commands = [ // commands\inspire::class, // commands\redissubscribe::class, // commands\redispublish::class, // commands\maketestrepositorycommand::class, commands\makerepositorycommand::class, ];
然后输入php artisan命令后就能看到这个make:repository命令了。
自动化生成repositoryinterface和repository文件
在makerepositorycommand.php命令执行文件里写上模板自动生成逻辑,代码也不长,有些逻辑也有注释,可看:
use config; use illuminate\console\command; use illuminate\filesystem\filesystem; use illuminate\support\composer; class makerepositorycommand extends command { /** * the name and signature of the console command. * * @var string */ protected $signature = 'make:repository {repository} {--model=}'; /** * the console command description. * * @var string */ protected $description = 'make a repository and interface'; /** * @var */ protected $repository; /** * @var */ protected $model; /** * create a new command instance. * * @param filesystem $filesystem * @param composer $composer */ public function __construct(filesystem $filesystem, composer $composer) { parent::__construct(); $this->files = $filesystem; $this->composer = $composer; } /** * execute the console command. * * @return mixed */ public function handle() { //获取repository和model两个参数值 $argument = $this->argument('repository'); $option = $this->option('model'); //自动生成repositoryinterface和repository文件 $this->writerepositoryandinterface($argument, $option); //重新生成autoload.php文件 $this->composer->dumpautoloads(); } private function writerepositoryandinterface($repository, $model) { if($this->createrepository($repository, $model)){ //若生成成功,则输出信息 $this->info('success to make a '.ucfirst($repository).' repository and a '.ucfirst($repository).'interface interface'); } } private function createrepository($repository, $model) { // getter/setter 赋予成员变量值 $this->setrepository($repository); $this->setmodel($model); // 创建文件存放路径, repositoryinterface放在app/repositories,repository个人一般放在app/repositories/eloquent里 $this->createdirectory(); // 生成两个文件 return $this->createclass(); } private function createdirectory() { $directory = $this->getdirectory(); //检查路径是否存在,不存在创建一个,并赋予775权限 if(! $this->files->isdirectory($directory)){ return $this->files->makedirectory($directory, 0755, true); } } private function getdirectory() { return config::get('repository.directory_eloquent_path'); } private function createclass() { //渲染模板文件,替换模板文件中变量值 $templates = $this->templatestub(); $class = null; foreach ($templates as $key => $template) { //根据不同路径,渲染对应的模板文件 $class = $this->files->put($this->getpath($key), $template); } return $class; } private function getpath($class) { // 两个模板文件,对应的两个路径 $path = null; switch($class){ case 'eloquent': $path = $this->getdirectory().directory_separator.$this->getrepositoryname().'.php'; break; case 'interface': $path = $this->getinterfacedirectory().directory_separator.$this->getinterfacename().'.php'; break; } return $path; } private function getinterfacedirectory() { return config::get('repository.directory_path'); } private function getrepositoryname() { // 根据输入的repository变量参数,是否需要加上'repository' $repositoryname = $this->getrepository(); if((strlen($repositoryname) < strlen('repository')) || strrpos($repositoryname, 'repository', -11)){ $repositoryname .= 'repository'; } return $repositoryname; } private function getinterfacename() { return $this->getrepositoryname().'interface'; } /** * @return mixed */ public function getrepository() { return $this->repository; } /** * @param mixed $repository */ public function setrepository($repository) { $this->repository = $repository; } /** * @return mixed */ public function getmodel() { return $this->model; } /** * @param mixed $model */ public function setmodel($model) { $this->model = $model; } private function templatestub() { // 获取两个模板文件 $stubs = $this->getstub(); // 获取需要替换的模板文件中变量 $templatedata = $this->gettemplatedata(); $renderstubs = []; foreach ($stubs as $key => $stub) { // 进行模板渲染 $renderstubs[$key] = $this->getrenderstub($templatedata, $stub); } return $renderstubs; } private function getstub() { $stubs = [ 'eloquent' => $this->files->get(resource_path('stubs/repository').directory_separator.'eloquent'.directory_separator.'repository.stub'), 'interface' => $this->files->get(resource_path('stubs/repository').directory_separator.'repository_interface.stub'), ]; return $stubs; } private function gettemplatedata() { $repositorynamespace = config::get('repository.repository_namespace'); $modelnamespace = 'app\\'.$this->getmodelname(); $repositoryinterfacenamespace = config::get('repository.repository_interface_namespace'); $repositoryinterface = $this->getinterfacename(); $classname = $this->getrepositoryname(); $modelname = $this->getmodelname(); $templatevar = [ 'repository_namespace' => $repositorynamespace, 'model_namespace' => $modelnamespace, 'repository_interface_namespace' => $repositoryinterfacenamespace, 'repository_interface' => $repositoryinterface, 'class_name' => $classname, 'model_name' => $modelname, 'model_var_name' => strtolower($modelname), ]; return $templatevar; } private function getrenderstub($templatedata, $stub) { foreach ($templatedata as $search => $replace) { $stub = str_replace('$'.$search, $replace, $stub); } return $stub; } private function getmodelname() { $modelname = $this->getmodel(); if(isset($modelname) && !empty($modelname)){ $modelname = ucfirst($modelname); }else{ // 若option选项没写,则根据repository来生成model name $modelname = $this->getmodelfromrepository(); } return $modelname; } private function getmodelfromrepository() { $repository = strtolower($this->getrepository()); $repository = str_replace('repository', '', $repository); return ucfirst($repository); } }
这里把一些常量值放在config/repository.php配置文件里了:
<?php /** * created by phpstorm. * user: liuxiang * date: 16/6/22 * time: 17:06 */ return [ 'directory_path' => 'app'.directory_separator.'repositories', 'directory_eloquent_path' => 'app'.directory_separator.'repositories'.directory_separator.'eloquent', 'repository_namespace' => 'app\repositories\eloquent', 'repository_interface_namespace' => 'app\repositories', ];
运行一下看可不可以吧,这里截个图:
it is working!!!
是可以生成repositoryinterface和对应的接口实现文件,这里一个是加了--model选项一个没加的,没加的话这里第一个指令就默认model的名称是shop。
生成的文件内容不截图了,看下新生成的shoprepository.php文件,的确是我想要的模板文件:
<?php /** * created by phpstorm. * user: liuxiang */ namespace app\repositories\eloquent; use app\shop; use app\repositories\shoprepositoryinterface; class shoprepository implements shoprepositoryinterface { /** * @var \app\shop */ public $shop; public function __construct(shop $shop) { $this->shop = $shop; } /** * @param array $columns * @return \illuminate\database\eloquent\collection|static[] */ public function all($columns = array('*')) { return $this->shop->all($columns); } /** * @param int $perpage * @param array $columns * @return \illuminate\contracts\pagination\lengthawarepaginator */ public function paginate($perpage = 15, $columns = array('*')) { return $this->shop->paginate($perpage, $columns); } /** * create a new shop * @param array $data * @return \app\shop */ public function create(array $data) { return $this->shop->create($data); } /** * update a shop * @param array $data * @param $id * @return \app\shop */ public function update($data = [], $id) { return $this->shop->whereid($id)->update($data); } /** * store a shop * @param array $data * @return \app\shop */ public function store($data = []) { $this->shop->id = $data['id']; //... $this->shop->save(); } /** * delete a shop * @param array $data * @param $id * @return \app\shop */ public function delete($data = [], $id) { $this->shop->whereid($id)->delete(); } /** * @param $id * @param array $columns * @return array|\illuminate\database\eloquent\collection|static[] */ public function find($id, $columns = array('*')) { $shop = $this->shop->whereid($id)->get($columns); return $shop; } /** * @param $field * @param $value * @param array $columns * @return \illuminate\database\eloquent\collection|static[] */ public function findby($field, $value, $columns = array('*')) { $shop = $this->shop->where($field, '=', $value)->get($columns); return $shop; } }
总结:本文主要用laravel的artisan命令来自动生成个人需要的模板,减少平时开发中重复劳动。就像laravel自带了很多模板生成命令,用起来会节省很多时间。这是作者在平时开发中遇到的问题,通过利用laravel artisan命令解决了,所以laravel还是挺好玩的。有兴趣的可以把代码扒下来玩一玩,并根据你自己想要的模板做修改。这两天想就repository模式封装model逻辑的方法和好处聊一聊,到时见。希望对大家的学习有所帮助,也希望大家多多支持