---
# System prepended metadata

title: '如何自行建立 php artisan make: 模板 PART(2)'

---

# 如何自行建立 php artisan make: 模板 PART(2)
>製作:Tinn
>參考:Google各位大神、laravel官網...

---
在PART(1) 建立repository後 現在難度要再增加了
目前我們所要建立的 services 程式裡會 use 到 repository
預設讓use的repository 名稱與services相同

Ex:
要讓 TestServices.php use TestRepository
TestServices.php
```php=
<?php
namespace App\Services;

use App\Repository\TestRepository;

class DummyClass
{
    public function __construct()
    {

    }
}
?>
```
---

## 目標?

- [php aritisan make:repository](https://hackmd.io/X1z5ahqDQli6PbVf8IOLFw?both)
- [php aritisan make:services](https://hackmd.io/pf2a8UYHTBWIJRkwXUL3xg) 
- php aritisan make:autoclass (一次性創建controller、repository、services)

---

### 生成文件

在app\Console\Commands資料夾下建立ServicesMakeCommand.php檔案

---

```php=
<?php
/**
 * Created by PhpStorm.
 * User: 20181201
 * Date: 2019/2/22
 * Time: 下午 01:46
 */

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;

class ServicesMakeCommand extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:services';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new services class';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Services';

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__ . '/stubs/services.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Services';
    }

    /**
     * Replace the namespace for the given stub.
     *
     * @param  string $stub
     * @param  string $name
     * @return $this
     */
    protected function replaceNamespace(&$stub, $name)
    {

        $stub = str_replace(
            [/*'RepositoryNamespace', */
                'NameRepository'],
            [/*$this->getNamespace($name),*/
                $this->setModel()],
            $stub
        );

        return $this;
    }

    /**
     * set Model
     *
     */
    private function setModel()
    {
        if (!empty($this->option('repository'))) {
            return $this->option('repository') . "Repository";
        } else {
            $name = explode('/', $this->getNameInput('name'));
            $new = str_replace('Services', '', $name[0]);
//            return str_replace('Services"', '', $new . "Repository");
            return $new . "Repository";
        }
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            ['repository', 'r', InputOption::VALUE_OPTIONAL, 'Injection  repository.'],
        ];
    }

}

```

---

### 建立模版檔案

在app\Console\Commands\stubs下建立模版檔案 .stub檔案是make命令生成的類檔案的模版，用來定義要生成的類檔案的通用部分建立Services.stub模版檔案：

我的範例檔如下 <font color='red'>目前看專案與架構 未來可將常用的class 引入</font>:

---


```php=
namespace App\Services;

use App\Repository\NameRepository;
use App\Repository\EmployeeRepository;

class DummyClass
{
    protected $_employeeRepository;
    protected $_NameRepository;
    public $employeeId;
    public $companyId;

    public function __construct()
    {
        $this->_employeeRepository = new EmployeeRepository;
        $this->_NameRepository = new NameRepository;
    }
}

```

---

### 註冊命令類

---

將ServicesMakeCommand新增到App\Console\Kernel.php中

---

```php=
    protected $commands = [
        //
        Commands\RepositoryMakeCommand::class,
        Commands\ServicesMakeCommand::class,
        Commands\AutoMakeCommand::class

    ];

```

---

# 好了完成可以開始食用 :fork_and_knife: 

---


### Thank you! :sheep: 
