Injection de dépendances dans XOOPS
L’injection de dépendances (DI) est un modèle de conception qui permet aux composants de recevoir leurs dépendances à partir de sources externes plutôt que de les créer en interne. XOOPS 4.0 introduit le support du conteneur DI compatible PSR-11.
Pourquoi l’injection de dépendances ?
Section intitulée « Pourquoi l’injection de dépendances ? »Sans DI (couplage serré)
Section intitulée « Sans DI (couplage serré) »class ArticleService{ private ArticleRepository $repository; private EventDispatcher $dispatcher;
public function __construct() { // Hard dependencies - difficult to test and modify $this->repository = new ArticleRepository(new XoopsDatabase()); $this->dispatcher = new EventDispatcher(); }}Avec DI (couplage faible)
Section intitulée « Avec DI (couplage faible) »class ArticleService{ public function __construct( private readonly ArticleRepositoryInterface $repository, private readonly EventDispatcherInterface $dispatcher ) {}}Conteneur PSR-11
Section intitulée « Conteneur PSR-11 »Utilisation basique
Section intitulée « Utilisation basique »use Psr\Container\ContainerInterface;
// Get the container$container = \Xmf\Module\Helper::getHelper('mymodule')->getContainer();
// Retrieve a service$articleService = $container->get(ArticleService::class);
// Check if service existsif ($container->has(ArticleService::class)) { // Use the service}Configuration du conteneur
Section intitulée « Configuration du conteneur »use Psr\Container\ContainerInterface;
return [ // Simple class instantiation ArticleRepository::class => ArticleRepository::class,
// Interface to implementation binding ArticleRepositoryInterface::class => ArticleRepository::class,
// Factory function ArticleService::class => function (ContainerInterface $c): ArticleService { return new ArticleService( $c->get(ArticleRepositoryInterface::class), $c->get(EventDispatcherInterface::class) ); },
// Shared instance (singleton) 'database' => function (): XoopsDatabase { return XoopsDatabaseFactory::getDatabaseConnection(); },];Enregistrement du service
Section intitulée « Enregistrement du service »Auto-wiring
Section intitulée « Auto-wiring »// The container automatically resolves dependencies// when type hints are available
class ArticleController{ public function __construct( private readonly ArticleService $service, private readonly ViewRenderer $renderer ) {}}
// Container creates ArticleController with its dependencies$controller = $container->get(ArticleController::class);Enregistrement manuel
Section intitulée « Enregistrement manuel »return [ ArticleService::class => [ 'class' => ArticleService::class, 'arguments' => [ ArticleRepositoryInterface::class, EventDispatcherInterface::class, ], 'shared' => true, // Singleton ],
'article.handler' => [ 'factory' => [ArticleHandlerFactory::class, 'create'], 'arguments' => ['@database'], // Reference other service ],];Injection de constructeur
Section intitulée « Injection de constructeur »Approche préférée
Section intitulée « Approche préférée »final class ArticleService{ public function __construct( private readonly ArticleRepositoryInterface $repository, private readonly EventDispatcherInterface $dispatcher, private readonly LoggerInterface $logger ) {}
public function create(CreateArticleDTO $dto): Article { $this->logger->info('Creating article', ['title' => $dto->title]);
$article = Article::create($dto); $this->repository->save($article); $this->dispatcher->dispatch(new ArticleCreatedEvent($article));
return $article; }}Injection de méthode
Section intitulée « Injection de méthode »Pour les dépendances optionnelles
Section intitulée « Pour les dépendances optionnelles »class ArticleController{ public function __construct( private readonly ArticleService $service ) {}
public function show(int $id, ?CacheInterface $cache = null): Response { $cacheKey = "article_{$id}";
if ($cache && $cached = $cache->get($cacheKey)) { return $this->render($cached); }
$article = $this->service->findById($id);
$cache?->set($cacheKey, $article, 3600);
return $this->render($article); }}Liaison d’interface
Section intitulée « Liaison d’interface »Définir les interfaces
Section intitulée « Définir les interfaces »interface ArticleRepositoryInterface{ public function findById(int $id): ?Article; public function save(Article $article): void; public function delete(Article $article): void;}Lier l’implémentation
Section intitulée « Lier l’implémentation »return [ ArticleRepositoryInterface::class => XoopsArticleRepository::class,
// Or with factory ArticleRepositoryInterface::class => function (ContainerInterface $c) { return new XoopsArticleRepository( $c->get('database') ); },];Test avec DI
Section intitulée « Test avec DI »Mocking facile
Section intitulée « Mocking facile »class ArticleServiceTest extends TestCase{ public function testCreateArticle(): void { // Create mocks $repository = $this->createMock(ArticleRepositoryInterface::class); $dispatcher = $this->createMock(EventDispatcherInterface::class); $logger = $this->createMock(LoggerInterface::class);
// Inject mocks $service = new ArticleService($repository, $dispatcher, $logger);
// Set expectations $repository->expects($this->once())->method('save'); $dispatcher->expects($this->once())->method('dispatch');
// Test $dto = new CreateArticleDTO('Title', 'Content'); $article = $service->create($dto);
$this->assertInstanceOf(Article::class, $article); }}Intégration Legacy XOOPS
Section intitulée « Intégration Legacy XOOPS »Intégration de l’ancien et du nouveau
Section intitulée « Intégration de l’ancien et du nouveau »// Get service from container in legacy codefunction mymodule_get_articles(int $limit): array{ $container = \Xmf\Module\Helper::getHelper('mymodule')->getContainer(); $service = $container->get(ArticleService::class);
return $service->findRecent($limit);}Envelopper les gestionnaires legacy
Section intitulée « Envelopper les gestionnaires legacy »return [ 'article.handler' => function () { return xoops_getModuleHandler('article', 'mymodule'); },
ArticleRepositoryInterface::class => function (ContainerInterface $c) { return new LegacyArticleRepository( $c->get('article.handler') ); },];Meilleures pratiques
Section intitulée « Meilleures pratiques »- Injecter des interfaces - Dépendre des abstractions, pas des implémentations
- Injection de constructeur - Préférer l’injection de constructeur à l’injection de setter
- Responsabilité unique - Chaque classe doit avoir peu de dépendances
- Éviter la conscience du conteneur - Les services ne doivent pas connaître le conteneur
- Configurer, ne pas coder - Utiliser les fichiers de configuration pour le câblage
Documentation connexe
Section intitulée « Documentation connexe »- ../07-XOOPS-4.0/Implementation-Guides/PSR-11-Dependency-Injection-Guide - Implémentation PSR-11
- ../03-Module-Development/Patterns/Service-Layer - Modèle de service
- ../03-Module-Development/Best-Practices/Testing - Test avec DI
- ../07-XOOPS-4.0/XOOPS-4.0-Architecture - Aperçu de l’architecture