0

I would like to produce an extra view file, inside the model's template folder when I bake a controller. I've copied (unchanged yet) ControllerTask.php into plugins/WetKit/src/Shell/Task in my plugin.

For instance, for the model Entries, I would like a new file to appear during baking: src/Templates/Entries/my_custom_file.ctp.

However, when I run cake bake controller entries -t WetKit, I get:

$ bin/cake bake controller entries -t WetKit
PHP Fatal error:  Cannot declare class Bake\Shell\Task\ControllerTask, because the name is already in use in C:\Users\me\Downloads\xampp3\xampp\htdocs\twofk\plugins\WetKit\src\Shell\Task\ControllerTask.php on line 28

What is the best way to accomplish this?

Edit 1: Would this be a namespace issue?

Edit 2:

Edits made to the WetKitControllerTask.php, kept as-is, but added "Yo!" in the output of the sprintf function:

namespace Wetkit\Bake\Shell\Task;

use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;

use Bake\Shell\Task\ControllerTask;

/**
 * Task class for creating and updating controller files.
 *
 * @property \Bake\Shell\Task\ModelTask $Model
 * @property \Bake\Shell\Task\BakeTemplateTask $BakeTemplate
 * @property \Bake\Shell\Task\TestTask $Test
 */
class WetKitControllerTask extends ControllerTask
...
    public function bake($controllerName)
    {
        $this->out("\n" . sprintf('Yo! Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
    

When I run the bake command, I don't seem to be getting "Yo!" to appear:

$ bin/cake bake controller entries -t WetKit

Baking controller class for Entries...

Edit 3

I get an error:

  • file: plugins/WetKit/src/Shell/Task/WetKitControllerTask.php
  • class name: ControllerTask
  • result: Cannot declare class Wetkit\Bake\Shell\Task\ControllerTask because the name is already in use

I get an error:

  • file: plugins/WetKit/src/Shell/Task/ControllerTask.php
  • class name: ControllerTask
  • result: Cannot declare class Wetkit\Bake\Shell\Task\ControllerTask because the name is already in use

No error, but doesn't output desired line:

  • file: plugins/WetKit/src/Shell/Task/ControllerTask.php
  • class name: WetKitControllerTask
  • result: no issues, but also not displaying added output command

Command

public function bake($controllerName)
    {
        $this->out("\n" . 'Override damn you!');
    

Under the Include tab in DebugKit, I see that my WetKit plugin is loaded.

To confirm, the override should happen in plugins/WetKit/src/Shell/Task/ of the plugin?

Edit 4

The namespace issue was my fault. I introduced this while trying to override the task.

In terms of autoloader, in composer.json:

"autoload": {
    "psr-4": {
        "WetKit\\": "src/"
    }
},

With:

  • Namespace: WetKit\Shell\Task
  • Filename: ControllerTask.php
  • Class name: WetKitControllerTask

I decided to refresh the autoload files:

$ composer dump-autoload -o
Generating optimized autoload files
Class WetKit\Shell\Task\WetKitControllerTask located in C:/Users/me/Downloads/xampp3/xampp/htdocs/twofk/plugins/WetKit/src\Shell\Task\ControllerTask.php does not comply with psr-4 autoloading standard. Skipping.
Generated optimized autoload files containing 3261 classes

If I change the filename to match the classname, composer will load the class and not output the PSR-4 issue. The baking still doesn't work, I don't see the class changes.

I tried to bake with the filename being ControllerTask.php and class name WetKitControllerTask. Composer would have skipped the file, but baking still doesn't show my class modifications. Strange.

Edit 5

Noting a change to targeting TemplateTask.php instead of ControllerTask.php. What I need to do fits better in TemplateTask.php.

With the class and filename matching, I cannot bake as I get the error shown below (which also happened with ControllerTask.php):

enter image description here

Edit 6

Solution:

...
use Bake\Shell\Task\TemplateTask as WetKitTemplateTask;
...
class TemplateTask extends WetKitTemplateTask
TechFanDan
  • 3,329
  • 6
  • 46
  • 89
  • 1
    If you didn't change anything in the file, then sure, that namespace won't work. No time for a thorough answer right now, but you're better off either extending the plugin's bake task, or using events instead: **https://book.cakephp.org/bake/1/en/development.html**. – ndm Oct 08 '21 at 13:54
  • I tried extending. Cake bake doesn't complain, but doesn't display the minor change to the class either. Probably missing some minor detail? – TechFanDan Oct 08 '21 at 18:34
  • You'd have to name your task class `ControllerTask`, otherwise its name will map to `wet_kit_controller`. Note that overriding a task has nothing to do with themes, it's a generic thing that just depends on whether the plugin is loaded, and it depends on the name of the plugin whether this works, to be specific, the plugin has to be named so that it alphabetically sorts _after_ `Bake`, as [**plugins are processed**](https://github.com/cakephp/bake/blob/1.12.0/src/Shell/BakeShell.php#L145-L152) in alphabetical order when building the tasks list! – ndm Oct 09 '21 at 14:48
  • Thanks for the tip. However, If I name my class ControllerTask, it complains. I don't seem to have another choice than to modify it. See edit 3. – TechFanDan Oct 10 '21 at 16:25
  • The path is not related to overriding, but generally bake tasks must go into `src/Shell/Task/` of the plugin, yes, otherwise Bake is not able to find them. I'm not sure what your autoloader config looks like, but the `Bake` in the namespace looks suspicious, out of the box (in case the plugin has been baked) the namespace would be mapped to only the plugin name, eg `WetKit\Shell\Task` (also note the capital `K` in `WetKit`, it is important that this matches the autoloader mapping). – ndm Oct 10 '21 at 19:00
  • Thanks again for the suggestions. See the changes under edit 4. I fixed the namespace issue, that's on me, it's what I thought was needed to override the class. I met a PSR-4 compliance message when dumping autoloads in composer. The message goes away if the file and class match. – TechFanDan Oct 10 '21 at 23:09
  • FWIW, I have the code needed and working in the vendor file. I just need to figure out how to override this file in the plugin. Gettin' closer :) – TechFanDan Oct 12 '21 at 13:06
  • 1
    It has to be `ControllerTask` (extending the bake plugin's `ControllerTask`) with a namespace of `WetKit\Shell\Task` in `src/Shell/Task/ControllerTask.php`. Debug **https://github.com/cakephp/bake/blob/1.12.0/src/Shell/BakeShell.php#L141** to figure whether the task is being found and correctly set in the tasks map. – ndm Oct 12 '21 at 14:30
  • See edit 5 with the screenshot. With the class and filename matching, I can't bake, nor can I debug. – TechFanDan Oct 12 '21 at 14:55
  • 1
    Look at those red squiggly line that PhpStorm shows you... you must use an alias for extending, or the fully qualified name. – ndm Oct 12 '21 at 15:19
  • Ah! Read up and found a solution, see Edit 6. If you want to provide this as an answer, I'll accept it. – TechFanDan Oct 12 '21 at 18:14

1 Answers1

0

I just had to change the namespace and class definition as such and place the file under plugin/WetKit/src/Shell/Task.

...
use Bake\Shell\Task\TemplateTask as WetKitTemplateTask;
...
class TemplateTask extends WetKitTemplateTask
TechFanDan
  • 3,329
  • 6
  • 46
  • 89