0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

いまさらLaravel4 Tips:Artisan::addしたくない

Last updated at Posted at 2019-05-23

#概要
commands クラス作成する度に、app/start/artisan.php に Artisan::add 追加するのめんどくさいな~と思って、commands ディレクトリ配下のphpファイル見て自動で追加することにしました。

もっとちゃんと考えて書けば namespace とかも ちゃんと取り よいか書き方あるんだと思いますが 取り急ぎで必要だったのでご勘弁ください。

artisan.php
<?php

$command_files = File::glob(app_path().'/commands/*.php');
if ($command_files !== false) {
    foreach($command_files as $file){
      $fp = fopen($file, 'r');
      $class = $buffer = '';
      $i = 0;
      while (!$class) {
          if (feof($fp)) break;
          $buffer .= fread($fp, 512);
          if (preg_match('/class\s+(\w+)(.*)?\{/', $buffer, $matches)) {
              $class = $matches[1];
              break;
          }
      }
      Artisan::add(new $class); 
    }
}

namespace とか取らないなら phpファイルの中身見なくてもファイル名でいいじゃんとか今更思ってますが...。

0
0
1

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?