Skip to content

Commit 7e92d53

Browse files
committed
1 parent 8cbabce commit 7e92d53

File tree

7 files changed

+126
-0
lines changed

7 files changed

+126
-0
lines changed

config/services.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,23 @@ services:
2222

2323
# add more service definitions when explicit configuration is needed
2424
# please note that last definitions always *replace* previous ones
25+
26+
foo:
27+
class: App\Service\Foo
28+
tags: ['my_tag']
29+
30+
debug.foo:
31+
decorates: foo
32+
class: App\Service\Decorator
33+
34+
debug.debug.foo:
35+
decorates: debug.foo
36+
class: App\Service\Decorator
37+
38+
bar:
39+
class: App\Service\Bar
40+
tags: ['my_tag']
41+
42+
debug.App\Service\Bar:
43+
decorates: App\Service\Bar
44+
class: App\Service\Decorator

src/Controller/HomepageController.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6+
use Symfony\Component\DependencyInjection\Argument\ServiceLocator;
7+
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\Routing\Annotation\Route;
10+
11+
class HomepageController extends AbstractController
12+
{
13+
public function __construct(
14+
#[TaggedLocator('my_tag')]
15+
private readonly ServiceLocator $serviceLocator,
16+
) {
17+
}
18+
19+
#[Route('/')]
20+
public function index(): Response
21+
{
22+
$services = [];
23+
foreach ($this->serviceLocator->getProvidedServices() as $key => $class) {
24+
$services[] = [
25+
'$key' => $key,
26+
'$class' => $class,
27+
'instance' => $this->serviceLocator->get($key),
28+
];
29+
}
30+
31+
dd($services);
32+
33+
return $this->render('homepage/index.html.twig', [
34+
'controller_name' => 'HomepageController',
35+
]);
36+
}
37+
}

src/Service/Bar.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
6+
7+
#[AutoconfigureTag('my_tag')]
8+
final readonly class Bar implements MyInterface
9+
{
10+
public function myMethod(): string
11+
{
12+
return 'Hello from Bar!';
13+
}
14+
}

src/Service/Decorator.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
final readonly class Decorator implements MyInterface
6+
{
7+
public function __construct(
8+
private MyInterface $myInterface,
9+
) {
10+
}
11+
12+
public function myMethod(): string
13+
{
14+
return 'Decorated: ' . $this->myInterface->myMethod();
15+
}
16+
}

src/Service/Foo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
final readonly class Foo implements MyInterface
6+
{
7+
public function myMethod(): string
8+
{
9+
return 'Hello from Foo!';
10+
}
11+
}

src/Service/MyInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
interface MyInterface
6+
{
7+
public function myMethod(): string;
8+
}

templates/homepage/index.html.twig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block title %}Hello HomepageController!{% endblock %}
4+
5+
{% block body %}
6+
<style>
7+
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
8+
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
9+
</style>
10+
11+
<div class="example-wrapper">
12+
<h1>Hello {{ controller_name }}! ✅</h1>
13+
14+
This friendly message is coming from:
15+
<ul>
16+
<li>Your controller at <code><a href="{{ '/home/gregoire/dev/labs/symfony/symfony-5.4/src/Controller/HomepageController.php'|file_link(0) }}">src/Controller/HomepageController.php</a></code></li>
17+
<li>Your template at <code><a href="{{ '/home/gregoire/dev/labs/symfony/symfony-5.4/templates/homepage/index.html.twig'|file_link(0) }}">templates/homepage/index.html.twig</a></code></li>
18+
</ul>
19+
</div>
20+
{% endblock %}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy