Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a component in console/shell

Tags:

cakephp

In CakePHP, how do I load a component in a shell?

To use a component in a controller, you include an array of components in a property called $components. That doesn't work for my Shell. Neither does the "on-the-fly" loading suggested in the documentation.

class MakePdfsShell extends Shell {
    public $components = array('Document'); // <- this doesn't work
    public function main()
    {
        $this->Document = $this->Components->load('Document'); // <- this doesnt work either
        $this->Document->generate(); // <- this is what I want to do
    }
    ...
like image 853
MM. Avatar asked Feb 14 '15 00:02

MM.


2 Answers

I have some xml utilities that I use across some of my controllers. One of the controller launches a heavy task via the cake console, so that it can quietly run in the background via PHP CLI, while the user's request is immediately completed (once the task done, it will e-mail the results to the user).

The xml utilities are generic enough to be used in controller and shell, are too specific to the application to warrant them vendor status. The offered solution with the Lib folder does not work as in CakePhp v3 there seems to be no Lib folder.

After some quite some time spent, I managed to load my controller component to the shell task. Here is how to:

namespace App\Shell;

use Cake\Console\Shell;

use Cake\Core\App;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use App\Controller\Component\XmlUtilitiesComponent;   // <- resides in your app's src/Controller/Component folder

class XmlCheckShell extends Shell
{

    public function initialize() {
        $this->Utilities = new XmlUtilitiesComponent(new ComponentRegistry());
    }

...

$this->Utilities can now be used across my entire shell class.

like image 148
D. Rordorf Avatar answered Mar 13 '23 20:03

D. Rordorf


I assume that you have a component named YourComponent:

<?php

App::uses('Component', 'Controller');

class YourComponent extends Component {

    public function testMe() {
        return 'success';
    }
}

- with cake 2., you can load your component like this

App::uses('ComponentCollection', 'Controller');
App::uses('YourComponent', 'Controller/Component');

class YourShell extends AppShell {

    public function startup() {
        $collection = new ComponentCollection();
        $this->yourComponent = $collection->load('Your');
    }

    public function main() {
        $this->yourComponent->testMe();
    }
}

- with cake 3. you can load your component like this

<?php

namespace App\Shell;


use App\Controller\Component\YourComponent;
use Cake\Console\Shell;
use Cake\Controller\ComponentRegistry;

class YourShell extends Shell {

    public function initialize() {
        parent::initialize();
        $this->yourComponent = new YourComponent(new ComponentRegistry(), []);
    }

    public function main() {
        $pages = $this->yourComponent->testMe();
    }
}
like image 36
Hai Nguyen Avatar answered Mar 13 '23 19:03

Hai Nguyen