我目前正在尝试找到一个可靠的解决方案来动态更改Symfony2服务的依赖关系.详细信息:我有一个使用HTTP驱动程序与外部API通信的服务.
class myAwesomeService
{
private $httpDriver;
public function __construct(
HTTDriverInterface $httpDriver
) {
$this->httpDriver = $httpDriver;
}
public function transmitData($data)
{
$this->httpDriver->dispatch($data);
}
}
Run Code Online (Sandbox Code Playgroud)
在CI上运行Behat测试时,我想使用httpMockDriver而不是真正的驱动程序,因为外部API可能会崩溃,缓慢甚至破坏,我不想破坏构建.
目前我正在做这样的事情:
<?php
namespace MyAwesome\TestBundle\DependencyInjection;
class MyAwesomeTestExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new
FileLocator(__DIR__.'/../Resources/config'));
$environment = //get environment
if ($environment == 'test') {
$loader->load('services_mock.yml');
} else {
$loader->load('services.yml');
}
}
}
Run Code Online (Sandbox Code Playgroud)
这现在有效,但肯定会打破.那么,是否有更优雅/可靠的方式来动态更改HTTPDriver?