I'm attending CakeFest 2010!

3.13.1 シェルやタスクを作成する

3.13.1.1 独自のシェルを作成する

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

コンソールで使用するシェルを作成しましょう。この例では、‘report' シェルを作成します。これはモデルのデータを出力します。まず始めに /vendors/shells に report.php を作成します。

<?php 
class ReportShell extends Shell {
	function main() {}
}
?>
  1. <?php
  2. class ReportShell extends Shell {
  3. function main() {}
  4. }
  5. ?>

ここからシェルを実行することができますが、十分ではありません。シェルにモデルを追加して、なんらかのレポートを作成することができます。これはコントローラ内でするのと同じようにできます: $uses 変数にモデル名を追加します

<?php
class ReportShell extends Shell {
	var $uses = array('Order');

	function main() {
	}
}
?>
  1. <?php
  2. class ReportShell extends Shell {
  3. var $uses = array('Order');
  4. function main() {
  5. }
  6. }
  7. ?>

$uses 配列にモデルを追加すると、main() メソッドで使用できます。この例では、Order モデルは新しいシェルの main() メソッドで $this->Order としてアクセスできます。

このシェルで使用するロジックの例です:

class ReportShell extends Shell {
	var $uses = array('Order');
	function main() {
		//Get orders shipped in the last    month
		$month_ago = date('Y-m-d H:i:s',    strtotime('-1 month'));
		$orders =    $this->Order->findAll("Order.shipped >= '$month_ago'");

		//Print out each order's information
		foreach($orders as $order) {
			$this->out('Order date:	' .    $order['created'] . "\n");
			$this->out('Amount: $' .    number_format($order['amount'], 2) . "\n");
			$this->out('----------------------------------------' .    "\n");
	 
			$total += $order['amount'];
		}

		//Print out total for the selected orders
		$this->out("Total: $" .    number_format($total, 2) . "\n"); 
	}
}
  1. class ReportShell extends Shell {
  2. var $uses = array('Order');
  3. function main() {
  4. //Get orders shipped in the last month
  5. $month_ago = date('Y-m-d H:i:s', strtotime('-1 month'));
  6. $orders = $this->Order->findAll("Order.shipped >= '$month_ago'");
  7. //Print out each order's information
  8. foreach($orders as $order) {
  9. $this->out('Order date: ' . $order['created'] . "\n");
  10. $this->out('Amount: $' . number_format($order['amount'], 2) . "\n");
  11. $this->out('----------------------------------------' . "\n");
  12. $total += $order['amount'];
  13. }
  14. //Print out total for the selected orders
  15. $this->out("Total: $" . number_format($total, 2) . "\n");
  16. }
  17. }

このコマンドを実行することでレポートを実行することができます。(cake コマンドが PATH にある場合):

$ cake report 

レポートは /vendor/shells/ の .php 拡張子がないシェルファイル名です。次のようになります。:

Hello user,
   Welcome to    CakePHP v1.2 Console
   ---------------------------------------------------------------
   App : app
   Path:    /path/to/cake/app
   ---------------------------------------------------------------
   Order date:    2007-07-30 10:31:12
   Amount:    $42.78
   ----------------------------------------
   Order date:    2007-07-30 21:16:03
   Amount:    $83.63
   ----------------------------------------
   Order date:    2007-07-29 15:52:42
   Amount:    $423.26
   ----------------------------------------
   Order date:    2007-07-29 01:42:22
   Amount:    $134.52
   ----------------------------------------
   Order date:    2007-07-29 01:40:52
   Amount:    $183.56
   ----------------------------------------
   Total:    $867.75

3.13.1.2 タスク

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

タスクはシェルのちょっとした拡張です。これを使用してシェル間でロジックを共有でき、特別な $tasks クラス変数を使用してシェルに追加されます。たとえばコア bake シェルでは、いくつかのタスクが定義されています:

<?php 
class BakeShell extends Shell {
   var $tasks = array('Project', 'DbConfig', 'Model', 'View', 'Controller');
}
?>
  1. <?php
  2. class BakeShell extends Shell {
  3. var $tasks = array('Project', 'DbConfig', 'Model', 'View', 'Controller');
  4. }
  5. ?>

タスクは、/vendors/shells/tasks/ 内で次にクラス名のファイルに保存されます。新しく ‘cool’ タスクを作成しているとします。CoolTask クラス(Shell を継承します)は /vendors/shells/tasks/cool.php に置きます。

各タスクはすくなくとも execute() メソッドを実装する必要があります。 - シェルはタスクロジックを開始するためにこのメソッドを呼び出します。

<?php
class SoundTask extends Shell {
   var $uses = array('Model'); // コントローラ変数 $uses と同じ
   function execute() {}
}
?>
  1. <?php
  2. class SoundTask extends Shell {
  3. var $uses = array('Model'); // コントローラ変数 $uses と同じ
  4. function execute() {}
  5. }
  6. ?>

シェルクラス内のタスクにアクセスし、実行できます:

<?php 
class SeaShell extends Shell // /vendors/shells/sea.php にあります
{
   var $tasks = array('Sound'); // /vendors/shells/tasks/sound.php にあります
   function main() {}
}
?>
  1. <?php
  2. class SeaShell extends Shell // /vendors/shells/sea.php にあります
  3. {
  4. var $tasks = array('Sound'); // /vendors/shells/tasks/sound.php にあります
  5. function main() {}
  6. }
  7. ?>

SeaShell クラスの “sound” というメソッドは、$tasks 配列で指定された Sound タスクの機能にアクセスする機能をオーバーライドします。

コマンドラインから直接タスクにアクセスできます:

$ cake sea sound