在Laravel 5.1中使用Artisan Command创建文件

时间:2015-09-26 14:16:23

标签: php laravel laravel-5.1 artisan

我使用此命令为我的项目创建了Artisan Command

`php artisan make:console Repositories`

上述自定义命令的签名是:

protected $signature = 'make:repository {modelName : The name of the model}';

执行/触发此命令时,会创建2个文件:

  1. app/Http/Repositories/Contracts/modelNameRepositoryContract.php
  2. app/Http/Repositories/Eloquent/modelNameRepository.php
  3. 现在,我想要名称空间,默认情况下应该写入className。就像我们在触发make:controller ModelControllermake:model Model时获得的内容一样。这些文件已经编写了它需要的默认内容。我只想要那样。

    我想默认使用命名空间,use命名空间和类/合约名称填充文件。为此,这是来自 Repositories.php 文件的handle()方法:

    /**
     * Execute the console command.
     *
     * @return mixed
     */
     public function handle()
     {
         $modelName = $this->argument('modelName');
    
         if ($modelName === '' || is_null($modelName) || empty($modelName)) {
             $this->error('Model Name Invalid..!');
         }
    
         if (! file_exists('app/Http/Repositories/Contracts') && ! file_exists('app/Http/Repositories/Eloquent')) {
    
             mkdir('app/Http/Repositories/Contracts', 0775, true);
             mkdir('app/Http/Repositories/Eloquent', 0775, true);
    
             $contractFileName = 'app/Http/Repositories/Contracts/' . $modelName . 'RepositoryContract.php';
             $eloquentFileName = 'app/Http/Repositories/Eloquent/' . $modelName . 'Repository.php';
    
             if(! file_exists($contractFileName) && ! file_exists($eloquentFileName)) {
                 $contractFileContent = "<?php\n\nnamespace App\\Http\\Repositories\\Contracts;\n\ninterface " . $modelName . "RepositoryContract\n{\n}";
    
                 file_put_contents($contractFileName, $contractFileContent);
    
                 $eloquentFileContent = "<?php\n\nnamespace App\\Http\\Repositories\\Eloquent;\n\nuse App\\Repositories\\Contracts\\".$modelName."RepositoryContract;\n\nclass " . $modelName . "Repository implements " . $modelName . "RepositoryContract\n{\n}";
    
                 file_put_contents($eloquentFileName, $eloquentFileContent);
    
                 $this->info('Repository Files Created Successfully.');
    
             } else {
                 $this->error('Repository Files Already Exists.');
             }
         }
     }
    

    我知道上面的方法不是使用Artisan命令创建文件的正确方法。那么,我应该如何创建文件并使用默认的东西填充它。我在文档中找不到与此相关的任何内容。

    那么有人可以帮我解决这个问题吗?

    提前致谢。

2 个答案:

答案 0 :(得分:0)

我认为最好实施现有的Laravel Generator - \ Illuminate \ Console \ GeneratorCommand

看看\ Illuminate \ Foundation \ Console \ ModelMakeCommand,了解它是如何完成的。我相信你可以创建模板并注入一些可以即时替换的东西。

答案 1 :(得分:0)

这是我创建的代码,以帮助我使用Artisan命令重新创建View Composer文件,因为默认情况下未提供该命令。也许这可能对您有帮助。 注意:这是针对Laravel 8

<?php

namespace viewmodelsimple\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;

class ViewComposer extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'view:composer {composer}';

    protected $files;
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Generate a view composer';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    // public function __construct()
    public function __construct(Filesystem $files)
    {
        $this->files=$files;
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $viewComposer=$this->argument('composer');

        if ($viewComposer === '' || is_null($viewComposer) || empty($viewComposer)) {
            return $this->error('Composer Name Invalid..!');
        }

$contents=
'<?php
namespace App\ViewComposers;
    
use Illuminate\View\View;
        
class '.$viewComposer.'
{
        
    /**
    * Create a new '.$viewComposer.' composer.
    *
    * @return void
    */
    public function __construct()
    {
        // Dependencies automatically resolved by service container...
                
    }
        
    /**
    * Bind data to the view.
    *
    * @param  View  $view
    * @return void
    */
    public function compose(View $view)
    {
        //Bind data to view
    }
    
}';
    if ($this->confirm('Do you wish to create '.$viewComposer.' Composer file?')) {
        $file = "${viewComposer}.php";
        $path=app_path();
        
        $file=$path."/ViewComposers/$file";
        $composerDir=$path."/ViewComposers";

        if($this->files->isDirectory($composerDir)){
            if($this->files->isFile($file))
                return $this->error($viewComposer.' File Already exists!');
            
            if(!$this->files->put($file, $contents))
                return $this->error('Something went wrong!');
            $this->info("$viewComposer generated!");
        }
        else{
            $this->files->makeDirectory($composerDir, 0777, true, true);

            if(!$this->files->put($file, $contents))
                return $this->error('Something went wrong!');
            $this->info("$viewComposer generated!");
        }

    }
    }
}