やりたいこと
Laravelをコマンドラインから操作・実行したい。
今回は以下のように実行すると、コマンド名・引数・オプションを表示するだけのコマンドを作成する。
1 |
$ php artisan foo:bar arg1 arg2 --option1=opt1 --option2=opt2 |
環境
さくらのレンタルサーバ スタンダード
PHP 5.4.29
Laravel 4.2.8
1 2 3 4 5 6 7 8 9 10 |
$ php --version PHP 5.4.29 (cli) (built: Jun 11 2014 18:20:32) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies $ php artisan --version Laravel Framework version 4.2.8 $ composer --version Composer version a8adbfeb9fc7861deade782938222714168a22a8 2014-09-05 16:28:50 |
やり方
Laravelのルートディレクトリを「/LARAVEL-ROOT/」とする。
1. artisanでコマンドのスタブを作成する。
2. 作成したスタブをartisan.phpに登録する。
3. スタブに処理を記述する。
4. 確認する。
1. artisanでコマンドのスタブを作成する。
「php artisan command:make <スタブ(クラス)名>」を実行する。
1 2 3 |
$ cd /LARAVEL-ROOT/ $ php artisan command:make CommandTest Command created successfully. |
「command:make」はArtisanコマンドを作成する際のオプション。
「CommandTest」はスタブとなるクラス名(ファイル名)なのでなんでもOK。
上記の場合だと「/LARAVEL-ROOT/app/commands/CommandTest.php」が作成される。
この「CommandTest.php」に実行したい処理を記述する。
2. 作成したスタブをartisan.phpに登録する。
「/LARAVEL-ROOT/app/start/artisan.php」に「CommandTest」を登録する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php /* |-------------------------------------------------------------------------- | Register The Artisan Commands |-------------------------------------------------------------------------- | | Each available Artisan command must be registered with the console so | that it is available to be called. We'll register every command so | the console gets access to each of the command object instances. | */ Artisan::add(new CommandTest); |
3. スタブに処理を記述する。
1で作成された「/LARAVEL-ROOT/app/commands/CommandTest.php」を変更する。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php use Illuminate\Console\Command; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; class CommandTest extends Command { /** * The console command name. * * @var string */ protected $name = 'foo:bar'; /** * The console command description. * * @var string */ protected $description = 'command foo:bar description.'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function fire() { foreach ($this->argument() as $key => $val) { echo "{$key} = {$val}" . PHP_EOL; } foreach ($this->option() as $key => $val) { echo "{$key} = {$val}" . PHP_EOL; } } /** * Get the console command arguments. * * @return array */ protected function getArguments() { return array( array('argument1', InputArgument::OPTIONAL, 'An example argument1.'), array('argument2', InputArgument::OPTIONAL, 'An example argument2.'), ); } /** * Get the console command options. * * @return array */ protected function getOptions() { return array( array('option1', null, InputOption::VALUE_OPTIONAL, 'An example option1.', null), array('option2', null, InputOption::VALUE_OPTIONAL, 'An example option2.', null), ); } } |
14行目: protected $name
php artisanでコマンド実行する際のコマンド名になる。
今回は「foo:bar」としているが、「hoge」でも「hoge:fuga:piyo」でもなんでもよい。(多分)
1 2 3 |
$ php artisan foo:bar $ php artisna hoge $ php artisan hoge:fuga:piyo |
21行目: protected $description
「php artisan list」を実行した際の説明に出てくる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
$ php artisan list Laravel Framework version 4.2.8 Usage: [options] command [arguments] Options: --help -h Display this help message. --quiet -q Do not output any message. --verbose -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug --version -V Display this application version. --ansi Force ANSI output. --no-ansi Disable ANSI output. --no-interaction -n Do not ask any interactive question. --env The environment the command should run under. Available commands: changes Display the framework change list clear-compiled Remove the compiled class file down Put the application into maintenance mode dump-autoload Regenerate framework autoload files env Display the current framework environment help Displays help for a command list Lists commands migrate Run the database migrations optimize Optimize the framework for better performance routes List all registered routes serve Serve the application on the PHP development server tail Tail a log file on a remote server tinker Interact with your application up Bring the application out of maintenance mode workbench Create a new package workbench asset asset:publish Publish a package's assets to the public directory auth auth:clear-reminders Flush expired reminders. auth:reminders-controller Create a stub password reminder controller auth:reminders-table Create a migration for the password reminders table cache cache:clear Flush the application cache command command:make Create a new Artisan command config config:publish Publish a package's configuration to the application controller controller:make Create a new resourceful controller db db:seed Seed the database with records foo foo:bar command foo:bar description. key key:generate Set the application key migrate migrate:install Create the migration repository migrate:make Create a new migration file migrate:publish Publish a package's migrations to the application migrate:refresh Reset and re-run all migrations migrate:reset Rollback all database migrations migrate:rollback Rollback the last database migration queue queue:failed List all of the failed queue jobs queue:failed-table Create a migration for the failed queue jobs database table queue:flush Flush all of the failed queue jobs queue:forget Delete a failed queue job queue:listen Listen to a given queue queue:restart Restart queue worker daemons after their current job. queue:retry Retry a failed queue job queue:subscribe Subscribe a URL to an Iron.io push queue queue:work Process the next job on a queue session session:table Create a migration for the session database table view view:publish Publish a package's views to the application |
40-45行目: public function fire()
実際に実行されるコマンド処理の本体。
今回は引数とオプションを表示しているだけ。
56,57行目: protected function getArguments()
コマンド引数を設定するところ。
細かいことはArtisan Developmentの「Arguments & Options」で。
69,70行目: protected function getOptions()
コマンドオプションを設定するところ。
細かいことはArtisan Developmentの「Arguments & Options」で。
4. 確認する。
実際にコマンドを叩いてみる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ php artisan foo:bar arg1 arg2 --option1=opt1 --option2=opt2 command = foo:bar argument1 = arg1 argument2 = arg2 option1 = opt1 option2 = opt2 help = quiet = verbose = version = ansi = no-ansi = no-interaction = env = |
helpとかquietはデフォルトで設定されているものなのかな。