在任务中使用CakePHP组件时如何编写代码?

在任务中使用CakePHP组件时如何编写代码?,cakephp,cakephp-1.3,Cakephp,Cakephp 1.3,我有一些我想在CakePHP任务中使用的组件代码,但我不确定我是否以正确的方式引导了控制器 作为一个简化版本,我有两个组件,通过扩展AppController访问时运行良好。一个组件包含另一个组件,因为它调用另一个组件 第一部分: class BigBrotherComponent extends Object { var $Controller = null; var $components = array('Sibling'); function startup

我有一些我想在CakePHP任务中使用的组件代码,但我不确定我是否以正确的方式引导了控制器

作为一个简化版本,我有两个组件,通过扩展AppController访问时运行良好。一个组件包含另一个组件,因为它调用另一个组件

第一部分:

class BigBrotherComponent extends Object {

    var $Controller = null;

    var $components = array('Sibling');

    function startup(&$controller) {
        $this->Controller =& $controller;
    }

    function doThis() {
       $this->Controller->loadModel('SampleModel');
       $this->Sibling->doThat();
    }
 }
class SiblingComponent extends Object {

    var $Controller = null;

    function startup(&$controller) {
        $this->Controller =& $controller;
    }

    function doThat() {
        /* Doing stuff */
    }
 }
第二部分:

class BigBrotherComponent extends Object {

    var $Controller = null;

    var $components = array('Sibling');

    function startup(&$controller) {
        $this->Controller =& $controller;
    }

    function doThis() {
       $this->Controller->loadModel('SampleModel');
       $this->Sibling->doThat();
    }
 }
class SiblingComponent extends Object {

    var $Controller = null;

    function startup(&$controller) {
        $this->Controller =& $controller;
    }

    function doThat() {
        /* Doing stuff */
    }
 }
为了让我的用户从命令行操作它,我定义了一个Shell和一个任务。这就是任务,尽管我不确定我是否做对了

App::import('Core', 'Controller');
App::import('Component', 'Session');
App::import('Component', 'BigBrother');

class BigBrotherTask extends Shell {
    var $Controller; 
    var $BigBrother;

    function initialize() {
        $this->Controller =& new Controller();

        // add session to controller, because some components access $this->Controller->Session->setFlash();
        $this->Controller->Session =& new SessionComponent();
        $this->Controller->Session->startup($this->Controller);

        // Initialise the component
        $this->WordToText =& new WordToTextComponent();
        $this->WordToText->startup($this->Controller);
    }

    function doThat() {
        $this->BigBrother->doThat();
    }

}
当BigBrother组件不包含任何它自己的组件,或者在BigBrother组件中引用控制器之外的任何其他组件时,这种类型的任务工作得很好。正如您所看到的,我不得不对会话组件做一些小小的修改


是否有更好的方法来初始化和使用任务中的组件,从而正确初始化组件和子组件?

我相信我已经找到了更好的方法,但仍然不确定这是否是最好的方法

首先,组件需要通过initialize()方法设置控制器。这可确保如果通过控制器或其他组件加载组件,则会指定控制器。其他组件加载的组件无法运行startup()

因此,组件应具有以下特性

function initialize(&controller, $settings = null) {
    $this->Controller =& $controller;
}
下一步是使控制器和组件初始化更像dispatcher所做的

App::import('Core', 'Controller');

class BigBrotherTask extends Shell {
    var $Controller; 

    function initialize() {
        $this->Controller =& new Controller();
        $this->Controller->components = array('Session', 'WordToText');
        $this->Controller->uses = null;

        $this->Controller->constructClasses();
        $this->Controller->startupProcess();
    }

    function doThat() {
        $this->Controller->BigBrother->doThat();
    }

}
任务流中似乎没有关机,因此如果组件需要正确关机,则必须在任务函数末尾或Shell中对其进行编码