My goal
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 environment
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.
Current solution
File: cron1.php
<?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();
File: FillCronjobQueueCommand.php
<?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');
// ...
}
}