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 AppShell;
use CakeConsoleShell;
use CakeCoreApp;
use CakeControllerComponent;
use CakeControllerComponentRegistry;
use AppControllerComponentXmlUtilitiesComponent; // <- 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…