Reputation: 364
In Plesk i want to run a PHP script frequently using PHP 7.2. It has to be as a PHP script and not console command (see "my environment" for more details). My current Symfony 4.2 based implementation works fine, but it is marked deprecated.
As stated here, the ContainerAwareCommand
is marked deprecated
in Symfony 4.2. Unfortunately, the referenced article about how to solve this issue in the future doesn't contain information about it.
My shared webhosting (Plesk) runs with PHP 7.0 but allows scripts to run with PHP 7.2. Later is only possible, if it directly runs the PHP script and not as a console command. I require PHP 7.2.
I know the injection types in Symfony. Based on my current knowledge, this issue only can be solved by using the getContainer
approach or providing all services by hand, for instance via constructor, which would result in a code mess.
<?php
// namespaces, Dotenv and gathering $env and $debug
// ...
$kernel = new Kernel($env, $debug);
$app = new Application($kernel);
$app->add(new FillCronjobQueueCommand());
$app->setDefaultCommand('fill_cronjob_queue');
$app->run();
<?php
// ...
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class FillCronjobQueueCommand extends ContainerAwareCommand
{
protected function configure()
{
$this->setName('fill_cronjob_queue');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// using "$this->getContainer()" is deprecated since Symfony 4.2
$manager = $this->getContainer()->get('doctrine')->getManager();
$cron_queue_repo = $manager->getRepository(CronjobQueue::class);
$cronjobs = $manager->getRepository(Cronjob::class)->findAll();
$logger = $this->getContainer()->get('logger');
// ...
}
}
Upvotes: 3
Views: 6966
Reputation: 1130
For my case it seems copying the ContainerAwareCommand class is the best way, as long as not stated otherwise (Thanks "Cerad"). This allows me to keep the current functionality and get rid of deprecated warnings. For me its also temporary solution (until Hoster upgrades to PHP 7.2) and has therefore no impact for future major upgrades of Symfony.
Nevertheless, i recommend the answer below and will implement it in the future.
According to this blog post on the symfony website Deprecated ContainerAwareCommand
The alternative is to extend commands from the Command class and use proper service injection in the command constructor
So the correct way is :
<?php
// ...
use Symfony\Component\Console\Command\Command
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use PSR\Log\LoggerInterface;
class FillCronjobQueueCommand extends Command
{
public function __construct(EntityManagerInterface $manager, LoggerInterface $logger)
{
$this->manager = $manager;
$this->logger = $logger;
}
protected function configure()
{
$this->setName('fill_cronjob_queue');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$cron_queue_repo = $this->manager->getRepository(CronjobQueue::class);
$cronjobs = $this->manager->getRepository(Cronjob::class)->findAll();
// ...
}
}
Upvotes: 9