不带PSR-4的旧版控制器的Symfony自动装配

时间:2019-06-12 20:12:50

标签: php symfony dependency-injection symfony4 symfony-dependency-injection

我有一个旧版应用程序,并且正在使用Symfony。 到目前为止一切正常。

现在,我想为我的Legacy-Controllers使用自动装配。

  • 它们是使用作曲者classmap功能加载的
  • 位于根命名空间中(例如\Controller_Page
  • 类名与文件名不同

是的。我知道这很糟糕。但这是遗留问题,我现在不想触摸每个控制器(该应用程序中存在更大的问题)。

我想使用Dependency-Injection和Autowiring来减少(可怕的)混乱。

以下是我已经尝试过的一些方法:

services:
 _defaults:
        autowire: true
        autoconfigure: true
    "\\":
        resource: '../legacy/Controller'
        tags: ['controller.service_arguments']
  

命名空间不是有效的PSR-4前缀

services:
 _defaults:
        autowire: true
        autoconfigure: true
    "":
        resource: '../legacy/Controller'
        tags: ['controller.service_arguments']
  

命名空间前缀必须以“ \”结尾

// in Kernel::configureContainer()
$container->registerForAutoconfiguration(\BaseController::class);

(我的\BaseController只有Symfony\Component\HttpFoundation\RequestStack作为__construct参数)

  

控制器“ BaseController”具有必需的构造函数参数,并且不存在于容器中。您忘了定义这样的服务吗?

// in Kernel::configureContainer()
$container->registerForAutoconfiguration(\Controller_Legacy::class);
  

无法加载资源“ 4208ad7faaf7d383f981bd32e92c4f2f”。

我不知道如何做到这一点。 感谢您的帮助。

编辑1

又走了一步。 我完成了其中一种传统控制器的自动配置:

// Kernel.php
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
    $container->addDefinitions([
        \Controller_Legacy::class => (new Definition(\Controller_Legacy::class))
            ->setAutowired(true)
            ->setAutoconfigured(true)
            ->addTag('controller.service_arguments'),
    ]);


    // ...
}

所以看来我以前的问题是由yaml配置或smth引起的,而不是由容器本身引起的。

现在,我必须找到一种方法来注册我所有的Legacy-Controllers。 如果我找到了一个好的解决方案,它将稍作调整并进行更新。 (好的解决方案非常受欢迎)

Edit2

好的,那不是YAML配置。如果我使用PHP配置 我遇到同样的问题。

/** @var $this \Symfony\Component\DependencyInjection\Loader\PhpFileLoader */

$definition = new Definition();

$definition
    ->setAutowired(true)
    ->setAutoconfigured(true)
    ->setPublic(false)
;

$this->registerClasses($definition, '\\', '../legacy/*');
  

命名空间不是有效的PSR-4前缀。

我现在尝试手动注册课程。

1 个答案:

答案 0 :(得分:0)

好的,我在原始问题中添加了导致该结果的步骤。 对我来说,这种解决方案有效。 它可能不是最好的,但是可以解决问题。 (不过可以打开以获得更好的建议)。

Kernel.php中,我滥用了composer-Autoloader来获取所需的类并将它们注册为Services。由于未使用的服务将被删除,所以我没有问题:-)

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
{
    /** @var ClassLoader $classLoader */
    $classLoader = require $this->getProjectDir().'/vendor/autoload.php';

    foreach (array_keys($classLoader->getClassMap()) as $class) {
        $definition = (new Definition($class))
            ->setAutowired(true)
            ->setAutoconfigured(true)
            ->setPublic(false);

        $container->setDefinition($class, $definition);
    }

    // Since my ClassMap contains not only controllers, I add the 'controller.service_arguments'-Tag
    // after the loop.
    $container
        ->registerForAutoconfiguration(\BaseController::class)
        ->addTag('controller.service_arguments');

    // ...
}
相关问题