Re-use Symfony Command¶
If your Castor project lives near a Symfony application, you may want to use
some of its commands directly as Castor tasks.
This is possible with the #[AsSymfonyTask()] attribute you can set on your
command class.
Note
Thanks to how PHP attributes works, your application will work even if it does not find this attribute class (which will probably not be available on your vendor directory - unless you installed Castor with Composer).
<?php
namespace App\Command;
use Castor\Attribute\AsSymfonyTask;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand('hello', 'Says hello from a Symfony application')]
#[AsSymfonyTask(name: 'symfony:hello')]
class HelloCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Hello');
return Command::SUCCESS;
}
}
By default, the attribute will re-use the same name as the one defined in the
Symfony application, but you can override it with the name parameter.
If Symfony command does not own a #[AsCommand()] attribute, you must set the
originName parameter, and it must be the same as the same in the symfony
application.
And finally, you can give a way to access the Symfony application entry point
with the console parameter. Some examples:
['bin/console'], this is the default, when Symfony and Castor live in the very same directory['path/to/symfony/bin/console'], when Symfony is in another directory['docker', 'exec', 'foobar-backend-1', '/app/server/backend/bin/console'], when your Symfony application lives in a docker container
Note
We try to detect automatically if the executable is a PHP script, and if so,
we will use a special runner that can also work without the PHP binary being
available on the system when using the static Castor binary.
You can override this behavior with the usePhpRunner parameter, where true
will force to consider the executable as a PHP script.