Symfony 4.2+:替换ContainerAwareCommand的getContainer-> get() [英] Symfony 4.2+: Replacement for ContainerAwareCommand's getContainer->get()

查看:62
本文介绍了Symfony 4.2+:替换ContainerAwareCommand的getContainer-> get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Plesk中,我想经常使用PHP 7.2运行一个PHP脚本.它必须作为PHP脚本而不是控制台命令(有关更多详细信息,请参见我的环境". ).我当前基于Symfony 4.2的实现工作正常,但已标记为已弃用.

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.

此处,在Symfony 4.2中, ContainerAwareCommand 被标记为 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.

我的共享虚拟主机(Plesk)在PHP 7.0上运行,但允许脚本在PHP 7.2上运行.仅当它直接运行PHP脚本而不作为控制台命令运行 not 时,以后才有可能.我需要PHP 7.2.

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.

我知道Symfony中的注入类型.根据我目前的知识,只能使用 getContainer 方法或手动提供所有服务(例如通过构造函数)来解决此问题,这会导致代码混乱.

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();

文件: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');

        // ...
    }
}

推荐答案

现在回答

就我而言,复制ContainerAwareCommand类似乎是最好的方法,只要没有另外说明(感谢"Cerad").这使我可以保留当前功能并摆脱不推荐使用的警告.对我来说,它也是临时解决方案(直到Hoster升级到PHP 7.2),因此对Symfony的未来重大升级没有影响.

Answer for now

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.

根据symfony网站上的此博客文章

According to this blog post on the symfony website Deprecated ContainerAwareCommand

另一种方法是从Command类扩展命令,并在命令构造函数中使用适当的服务注入

The alternative is to extend commands from the Command class and use proper service injection in the command constructor

所以正确的方法是:

<?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();

        // ...
    }
}

这篇关于Symfony 4.2+:替换ContainerAwareCommand的getContainer-> get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆