“XOOPS中的依赖注入”
:::注意[版本兼容性]
| 特色 | XOOPS2.5.x | XOOPS4.0 |
|---|---|---|
| 手动 DI(构造函数注入) | ✅ 可用 | ✅ 可用 |
| PSR-11 集装箱 | ❌ 未建成-in | ✅ 原生支持 |
\XMF\Module\Helper::getContainer() | ❌仅限 4.0 | ✅ 可用 |
在 XOOPS 2.5.x 中,使用手动构造函数注入(显式传递依赖项)。下面的 PSR-11 容器示例适用于 XOOPS 4.0。 :::
依赖注入 (DI) 是一种设计模式,允许组件从外部源接收依赖项,而不是在内部创建它们。 XOOPS 4.0 引入了 PSR-11 兼容 DI 容器支持。
为什么需要依赖注入?
Section titled “为什么需要依赖注入?”不带 DI(紧耦合)
Section titled “不带 DI(紧耦合)”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(); }}带 DI(松耦合)
Section titled “带 DI(松耦合)”class ArticleService{ public function __construct( private readonly ArticleRepositoryInterface $repository, private readonly EventDispatcherInterface $dispatcher ) {}}PSR-11 容器
Section titled “PSR-11 容器”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}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(); },];汽车-wiring
Section titled “汽车-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);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 ],];构造函数注入
Section titled “构造函数注入”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; }}对于可选依赖项
Section titled “对于可选依赖项”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;}return [ ArticleRepositoryInterface::class => XoopsArticleRepository::class,
// Or with factory ArticleRepositoryInterface::class => function (ContainerInterface $c) { return new XoopsArticleRepository( $c->get('database') ); },];使用 DI 进行测试
Section titled “使用 DI 进行测试”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); }}XOOPS 旧版集成
Section titled “XOOPS 旧版集成”// 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);}包装遗留处理程序
Section titled “包装遗留处理程序”return [ 'article.handler' => function () { return xoops_getModuleHandler('article', 'mymodule'); },
ArticleRepositoryInterface::class => function (ContainerInterface $c) { return new LegacyArticleRepository( $c->get('article.handler') ); },];- 注入接口 - 依赖于抽象,而不是实现
- 构造函数注入 - 优先使用构造函数而不是 setter 注入
- 单一职责 - 每个类应该有很少的依赖关系
- 避免容器意识 - 服务不应该了解容器
- 配置,不要编码 - 使用配置文件进行连接
- ../07-XOOPS-4.0/Implementation-Guides/PSR-11-Dependency-Injection-Guide - PSR-11 实施
- ../03-Module-Development/Patterns/Service-Layer - 服务模式
- ../03-Module-Development/Best-Practices/Testing - 使用 DI 进行测试
- ../07-XOOPS-4.0/XOOPS-4.0-Architecture - 架构概述