LoginSignup
1
1

More than 5 years have passed since last update.

【Laravel】make系コマンドで作成したクラスのインデントをスペースにする

Last updated at Posted at 2015-02-11

追記:
@ytake さんがgulpを使った解決方法を書かれています。とても参考になります。
http://blog.comnect.jp.net/blog/120


Laravelのmakeコマンド便利ですよね。
コマンド1発でControllerでもCommandでもクラスのひな形を作ってくれます。

でも、出来上がったクラスを見てみるとインデントはタブなんですよね。
個人のプロダクトなら気にするほどの事じゃないんですが、仕事で使うとなるとコーディング規約という壁が立ちはだかるのでどうしてもスペースに統一したりする必要があります。

という事で以下の変更を加えてmakeコマンドで作られたクラスのインデントを半角スペース4つにしてみました。

<?php namespace Illuminate\Console;

use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputArgument;

abstract class GeneratorCommand extends Command {

    
    
    

    /**
     * Build the class with the given name.
     *
     * @param  string  $name
     * @return string
     */
    protected function buildClass($name)
    {
        $stub = $this->files->get($this->getStub());

        // replaceIndent($stub)を追加
        return $this->replaceIndent($stub)->replaceNamespace($stub, $name)->replaceClass($stub, $name);
    }

    /**
     * Replace the indend
     * このメソッドは追加
     *
     * @return $this
     */
    protected function replaceIndent(&$stub)
    {
        $stub = str_replace("\t", '    ', $stub);

        return $this;
    }

でもこれだとIlluminateのクラス書き換えちゃうから別環境でinstallした時に消えてしまう。
それに、ホントはコマンド叩く時にオプションで好きなインデントを指定出来るようにしたかった。
という事でこれはかなりその場しのぎ、あとで時間がある時にちゃんとしたの作ってみよう。。。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1