Pular para o conteúdo

Injeção de Dependência no XOOPS

Injeção de Dependência (DI) é um padrão de design que permite aos componentes receber suas dependências de fontes externas em vez de criá-las internamente. O XOOPS 4.0 introduz suporte nativo de container compatível com PSR-11.

class ArticleService
{
private ArticleRepository $repository;
private EventDispatcher $dispatcher;
public function __construct()
{
// Dependências hard - difíceis de testar e modificar
$this->repository = new ArticleRepository(new XoopsDatabase());
$this->dispatcher = new EventDispatcher();
}
}
class ArticleService
{
public function __construct(
private readonly ArticleRepositoryInterface $repository,
private readonly EventDispatcherInterface $dispatcher
) {}
}
use Psr\Container\ContainerInterface;
// Obter o container
$container = \Xmf\Module\Helper::getHelper('mymodule')->getContainer();
// Recuperar um serviço
$articleService = $container->get(ArticleService::class);
// Verificar se serviço existe
if ($container->has(ArticleService::class)) {
// Usar o serviço
}
config/services.php
use Psr\Container\ContainerInterface;
return [
// Instanciação de classe simples
ArticleRepository::class => ArticleRepository::class,
// Vinculação de interface para implementação
ArticleRepositoryInterface::class => ArticleRepository::class,
// Função factory
ArticleService::class => function (ContainerInterface $c): ArticleService {
return new ArticleService(
$c->get(ArticleRepositoryInterface::class),
$c->get(EventDispatcherInterface::class)
);
},
// Instância compartilhada (singleton)
'database' => function (): XoopsDatabase {
return XoopsDatabaseFactory::getDatabaseConnection();
},
];
// O container resolve automaticamente as dependências
// quando type hints estão disponíveis
class ArticleController
{
public function __construct(
private readonly ArticleService $service,
private readonly ViewRenderer $renderer
) {}
}
// Container cria ArticleController com suas dependências
$controller = $container->get(ArticleController::class);
config/services.php
return [
ArticleService::class => [
'class' => ArticleService::class,
'arguments' => [
ArticleRepositoryInterface::class,
EventDispatcherInterface::class,
],
'shared' => true, // Singleton
],
'article.handler' => [
'factory' => [ArticleHandlerFactory::class, 'create'],
'arguments' => ['@database'], // Referência a outro serviço
],
];
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('Criando artigo', ['title' => $dto->title]);
$article = Article::create($dto);
$this->repository->save($article);
$this->dispatcher->dispatch(new ArticleCreatedEvent($article));
return $article;
}
}
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);
}
}
interface ArticleRepositoryInterface
{
public function findById(int $id): ?Article;
public function save(Article $article): void;
public function delete(Article $article): void;
}
config/services.php
return [
ArticleRepositoryInterface::class => XoopsArticleRepository::class,
// Ou com factory
ArticleRepositoryInterface::class => function (ContainerInterface $c) {
return new XoopsArticleRepository(
$c->get('database')
);
},
];
class ArticleServiceTest extends TestCase
{
public function testCreateArticle(): void
{
// Criar mocks
$repository = $this->createMock(ArticleRepositoryInterface::class);
$dispatcher = $this->createMock(EventDispatcherInterface::class);
$logger = $this->createMock(LoggerInterface::class);
// Injetar mocks
$service = new ArticleService($repository, $dispatcher, $logger);
// Definir expectativas
$repository->expects($this->once())->method('save');
$dispatcher->expects($this->once())->method('dispatch');
// Testar
$dto = new CreateArticleDTO('Título', 'Conteúdo');
$article = $service->create($dto);
$this->assertInstanceOf(Article::class, $article);
}
}
// Obter serviço do container em código legado
function mymodule_get_articles(int $limit): array
{
$container = \Xmf\Module\Helper::getHelper('mymodule')->getContainer();
$service = $container->get(ArticleService::class);
return $service->findRecent($limit);
}
config/services.php
return [
'article.handler' => function () {
return xoops_getModuleHandler('article', 'mymodule');
},
ArticleRepositoryInterface::class => function (ContainerInterface $c) {
return new LegacyArticleRepository(
$c->get('article.handler')
);
},
];
  1. Injetar Interfaces - Depender de abstrações, não implementações
  2. Injeção de Construtor - Preferir construtor sobre injeção por setter
  3. Responsabilidade Única - Cada classe deve ter poucas dependências
  4. Evitar Consciência de Container - Serviços não devem saber sobre o container
  5. Configurar, Não Codificar - Usar arquivos de configuração para wiring
  • ../07-XOOPS-4.0/Implementation-Guides/PSR-11-Dependency-Injection-Guide - Implementação PSR-11
  • ../03-Module-Development/Patterns/Service-Layer - Padrão de serviço
  • ../03-Module-Development/Best-Practices/Testing - Testando com DI
  • ../07-XOOPS-4.0/XOOPS-4.0-Architecture - Visão geral de arquitetura