Last active
August 26, 2022 12:36
-
-
Save benblub/b799c4d46facb7ff89244dede3cac02c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class BarService | |
{ | |
public function bar(): string | |
{ | |
return 'bar'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use ApiPlatform\Core\JsonSchema\TypeFactoryInterface; | |
use Symfony\Component\DependencyInjection\Attribute\AsDecorator; | |
use Symfony\Component\DependencyInjection\Attribute\MapDecorated; | |
#[AsDecorator(decorates: BarService::class)] | |
class BarServiceDecorator | |
{ | |
private $inner; | |
public function __construct(#[MapDecorated] $inner) | |
{ | |
$this->inner = $inner; | |
} | |
public function bar(): string | |
{ | |
return $this->inner->bar() . ' Bar'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Service\BarService; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Routing\Annotation\Route; | |
class TestController extends AbstractController | |
{ | |
/** | |
* @Route("/foo") | |
*/ | |
public function foo(BarService $barService) | |
{ | |
return new Response('foo ' . $barService->bar()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is another example of how to use the whole thing with interfaces.
https://symfony.com/doc/current/service_container/autowiring.html#working-with-interfaces
https://symfony.com/doc/current/service_container/service_decoration.html