Skip to content

Instantly share code, notes, and snippets.

@satomixx
Created April 11, 2014 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satomixx/10443297 to your computer and use it in GitHub Desktop.
Save satomixx/10443297 to your computer and use it in GitHub Desktop.
[CakePHP 2.x] shell(シェル)からComponent(コンポーネント)を呼び出す ref: http://qiita.com/tsumekoara/items/d302cc0eeec3adc18456
<?php
App::uses('Shell', 'Console');
class AppShell extends Shell {
}
object(OnakahettaShell) {
uses => array()
.
.
Tamagoyaki => object(TamagoyakiComponent) {} <-いた!!
.
.
}
object(TamagoyakiComponent) {
components => array()
settings => array()
[protected] _Collection => object(ComponentCollection) {}
[protected] _componentMap => array()
}
<?php
App::uses('ComponentCollection', 'Controller'); //これが大事
App::uses('TamagoyakiComponent', 'Controller/Component');
// ↑このHogeComponentとapp/Controller/Component以下の呼び出したいファイルの『クラスの名前』が一致しているか確認。(まあ基本はクラスとファイル名は一致させる)
/**
* こういうクラス ちなみにAppShellがShellを継承している
*/
class OnakahettaShell extends AppShell {
/**
* TamagoyakiComponent.phpの呼び出し
*/
public function startup() {
$collection = new ComponentCollection(); //これが大事です。
$this->Hoge = new TamagoyakiComponent($collection); //コンポーネントをインスタンス化
parent::startup();
}
/**
* メイン処理
*/
public function main() {
$monya = $this->Tamagoyaki->__fuga();
//↑こんな感じで使えます◎
Debugger::log($this); // 出力内容は下に記載
Debugger::log($this->Tamagoyaki); //出力内容は下に
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment