我的目标
在 Plesk 中,我想经常使用 PHP 7.2 运行 PHP 脚本。它必须是 PHP 脚本而不是控制台命令(有关更多详细信息,请参阅“我的环境”)。我当前基于 Symfony 4.2 的实现工作正常,但它被标记为deprecated。
如此处所述,在 Symfony 4.2中ContainerAwareCommand
被标记。deprecated
不幸的是,有关将来如何解决此问题的参考文章不包含有关此问题的信息。
我的环境
我的共享虚拟主机 (Plesk) 使用 PHP 7.0 运行,但允许脚本使用 PHP 7.2 运行。如果它直接运行 PHP 脚本而不是作为控制台命令,则以后才有可能。我需要 PHP 7.2。
我知道 Symfony 中的注入类型。根据我目前的知识,这个问题只能通过使用该getContainer
方法或手动提供所有服务来解决,例如通过构造函数,这会导致代码混乱。
当前解决方案
文件: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();
文件: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');
// ...
}
}