我正在创建一个Symfony 3控制台应用程序,并试图远离使用整个框架,依赖于此处和那里的组件。我已经扩展了默认的Symfony\Component\Console\Application
类以允许容器,并且进入该容器我正在加载app/config/services.yml
以尝试自动发现我的应用程序中的所有命令。配置文件如下所示:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
vagabond.command.config.get:
class: Vagabond\Command\Config\Get
tags: ['console.command']
vagabond.command.config.set:
class: Vagabond\Command\Config\Set
tags: ['console.command']
vagabond.command.site.add:
class: Vagabond\Command\Site\Add
tags: ['console.command']
vagabond.command.site.edit:
class: Vagabond\Command\Site\Edit
tags: ['console.command']
vagabond.command.site.remove:
class: Vagabond\Command\Site\Remove
tags: ['console.command']
在我的Application
课程中,我这样覆盖了getDefaultCommands()
:
protected function getDefaultCommands()
{
$commands = parent::getDefaultCommands();
foreach ($this->container->findTaggedServiceIds('console.command') as $commandId => $command) {
$commands[] = $this->container->get($commandId);
}
return $commands;
}
这一切都有效!我加载了services.yml
文件中指定的所有命令。但是......我希望能够使用资源加载而不是手动指定每个命令(否则我也可以使用new \Vagabond\Command\Config\Get
将它们放在我的shell命令中)。我尝试将其放在我的services.yml
文件中:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
vagabond.command.config.get:
class: Vagabond\Command\Config\Get
tags: ['console.command']
vagabond.command.config.set:
class: Vagabond\Command\Config\Set
tags: ['console.command']
vagabond.command.site.add:
class: Vagabond\Command\Site\Add
tags: ['console.command']
vagabond.command.site.edit:
class: Vagabond\Command\Site\Edit
tags: ['console.command']
vagabond.command.site.remove:
class: Vagabond\Command\Site\Remove
tags: ['console.command']
Vagabond\Command\Config\:
resource: '../../src/Command/Config/*'
tags: ['console.command2']
然后我修改了我的Application
课程,以找到console.command2
代码。得到了这个错误:
PHP Fatal error: Uncaught ReflectionException: Class does not exist in ~/Documents/projects/vagabond/vendor/symfony/dependency-injection/ContainerBuilder.php:1083
对于eagle-eyed,Class
之后的两个空格 - 当我使用资源时,发现的类的Symfony\Component\DependencyInjection\Definition
实例将其私有$class
值设置为{{ 1}},所以当应用程序尝试使用反射创建类时,它会使用null
作为它尝试创建的实际类名。
我的问题是,那么:有没有办法使用null
键一次抓取所有命令,和确保他们拥有必要的所有正确信息根据需要实例化?