PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] How do you create a custom command in a plugin in CakePHP 4?

 March 11, 2022     cakephp     No comments   

Issue

I am building a Command in a plugin, but having difficulty reconciling the plugin example in the book with the Command one. I get this:

Exception: Cannot use 'MyNamespace\MyCommand' for command 'my_namespace my'. It is not a subclass of Cake\Console\Shell or Cake\Command\CommandInterface.
In [/path/to/vendor/cakephp/cakephp/src/Console/CommandCollection.php, line 68]

In looking closer at CommandCollection::add() I see

    /**
     * Add a command to the collection
     *
     * @param string $name The name of the command you want to map.
     * @param string|\Cake\Console\Shell|\Cake\Console\CommandInterface $command The command to map.
     *   Can be a FQCN, Shell instance or CommandInterface instance.
     * @return $this
     * @throws \InvalidArgumentException
     */
    public function add(string $name, $command)
    {
        if (!is_subclass_of($command, Shell::class) && !is_subclass_of($command, CommandInterface::class)) {
            $class = is_string($command) ? $command : get_class($command);
            throw new InvalidArgumentException(sprintf(
                "Cannot use '%s' for command '%s'. " .
                "It is not a subclass of Cake\Console\Shell or Cake\Command\CommandInterface.",
                $class,
                $name
            ));
        }
...

Two things jump out:

  1. Why is it checking for $command to be a subclass of an interface? Shouldn't it be iterating over class_implements() instead to check?
  2. The exception message looks like it has a typo: shouldn't it reference Cake\Console\CommandInterface instead?

If there's a more complete example of creating a custom plugin with a Command, I'd appreciate a pointer to it.


Solution

  1. \Cake\Console\Shell doesn't implement an interface, so class_implements() wouldn't work. \Cake\Console\Command (respectively the new \Cake\Command\Command) does implement an interface, so class_implements() would work, but so does is_subclass_of(), it checks for interfaces too, so it doesn't really make a difference.

  2. Yes, it should. This might have been changed accidentally when commands have been moved into the Command namespace (\Cake\Command\Command was previously \Cake\Console\Command).

The error message suggests that your custom command class neither extends \Cake\Command\Command (or \Cake\Console\BaseCommand), nor implements \Cake\Console\CommandInterface, or that the value passed to CommandCollection::add() points to a wrong/non-existent class.



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing