【问题标题】:Creating file using Artisan Command in Laravel 5.1在 Laravel 5.1 中使用 Artisan 命令创建文件
【发布时间】:2015-12-24 05:28:01
【问题描述】:

我用这个命令为我的项目创建了一个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

现在,我希望命名空间,className 应该默认写入。就像我们在触发make:controller ModelControllermake:model Model 时得到的一样。这些文件已经写入了它需要的默认内容。我只想要类似的。

我想默认使用命名空间、使用命名空间和类/合同名称填充文件。为此,这是 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 命令创建文件的正确方法。那么,我应该如何创建文件并使用默认内容填充它。我在文档中找不到与此相关的任何内容。

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

提前致谢。

【问题讨论】:

    标签: php laravel laravel-5.1 laravel-artisan


    【解决方案1】:

    这是我创建的代码,用于帮助我使用 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!");
            }
    
        }
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-04-01
        • 2015-11-13
        • 2015-04-14
        • 2016-04-06
        • 2016-08-19
        • 2019-04-02
        • 2015-02-21
        • 1970-01-01
        • 2015-12-07
        相关资源
        最近更新 更多