interface ExecutorInterface
{
public function exec(): string;
}
class ConcreteImplementationA implements ExecutorInterface
{
public function exec(): string
{
return 'A';
}
}
class ConcreteImplementationB implements ExecutorInterface
{
public function exec(): string
{
return 'B';
}
}
class VariantExecutor
{
public function __construct(private ServiceLocator $locator)
{
}
public function exec(string $variant): string
{
return $this->locator->get($variant)->exec();
}
}
class Controller
{
public function call(VariantExecutor $executor, string $variant): Response
{
return new Response($executor->exec($variant));
}
}
#services.yaml
app.executor.locator:
class: Symfony\Component\DependencyInjection\ServiceLocator
arguments:
-
a: App\ConcreteImplementationA
b: App\ConcreteImplementationB
tags:
- { name: container.service_locator }
"autoload": {
"files": ["src/helpers.php"]
}
$container->set('\app\components\SearchEngineInterface', '\app\components\SearchEngine');
$container->set('\app\components\SearchEngineInterface', '\app\components\SuperSearchEngine');
public function load(array $configs, ContainerBuilder $container)
{
$container->setDefinition('emailsender', new Definition('MailerBundle\Sender\EmailSender', [
new Reference('swiftmailer.mailer'),
new Reference('swiftmailer.transport')]));
$container->setDefinition('emailnotifier', new Definition('MailerBundle\Controller\EmailController', [
new Reference('emailsender')]));
}
class SomeService
/**
* @var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
*/
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
public function doSome()
{
$template = $this->templating->render('AppBundle::foo.html.twig', array(
'foo' => 'bar'
));
}
services:
app.service.foo:
class: %app.service.foo.class%
arguments:
- @templating
symfony.com/doc/current/cookbook/controller/servic...
В настройках роута надо заменитьdefaults: { _controller: TestTestBundle:Test:index }
наdefaults: { _controller: test:indexAction }