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:
- Why is it checking for
$command
to be a subclass of an interface? Shouldn't it be iterating overclass_implements()
instead to check? - 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
\Cake\Console\Shell
doesn't implement an interface, soclass_implements()
wouldn't work.\Cake\Console\Command
(respectively the new\Cake\Command\Command
) does implement an interface, soclass_implements()
would work, but so doesis_subclass_of()
, it checks for interfaces too, so it doesn't really make a difference.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
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.