XOOPS'ye Bağımlılık Enjeksiyonu
Sürüm Uyumluluğu
| Özellik | XOOPS 2.5.x | XOOPS 4.0 |
|---|---|---|
| Manuel DI (yapıcı enjeksiyonu) | ✅ Mevcut | ✅ Mevcut |
| PSR-11 Konteyner | ❌ Yerleşik değil | ✅ Yerel destek |
\Xmf\Module\Helper::getContainer() | ❌ yalnızca 4.0 | ✅ Mevcut |
XOOPS 2.5.x’de, manuel yapıcı enjeksiyonunu kullanın (bağımlılıkları açıkça iletmek). Aşağıdaki PSR-11 konteyner örnekleri XOOPS 4.0 içindir.
Genel Bakış
Section titled “Genel Bakış”Bağımlılık Enjeksiyonu (DI), bileşenlerin bağımlılıklarını dahili olarak oluşturmak yerine harici kaynaklardan almasına olanak tanıyan bir tasarım modelidir. XOOPS 4.0, PSR-11 uyumlu DI konteyner desteğini sunar.
Neden Bağımlılık Enjeksiyonu?
Section titled “Neden Bağımlılık Enjeksiyonu?”DI olmadan (Sıkı Kaplin)
Section titled “DI olmadan (Sıkı Kaplin)”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 ile (Gevşek Kaplin)
Section titled “DI ile (Gevşek Kaplin)”class ArticleService{ public function __construct( private readonly ArticleRepositoryInterface $repository, private readonly EventDispatcherInterface $dispatcher ) {}}PSR-11 Konteyner
Section titled “PSR-11 Konteyner”Temel Kullanım
Section titled “Temel Kullanım”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}Konteyner Yapılandırması
Section titled “Konteyner Yapılandırması”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(); },];Hizmet Kaydı
Section titled “Hizmet Kaydı”Otomatik kablolama
Section titled “Otomatik kablolama”// 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);Manuel Kayıt
Section titled “Manuel Kayıt”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 ],];Yapıcı Enjeksiyonu
Section titled “Yapıcı Enjeksiyonu”Tercih Edilen Yaklaşım
Section titled “Tercih Edilen Yaklaşım”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; }}Yöntem Enjeksiyonu
Section titled “Yöntem Enjeksiyonu”İsteğe Bağlı Bağımlılıklar İçin
Section titled “İsteğe Bağlı Bağımlılıklar İçin”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); }}Arayüz Bağlama
Section titled “Arayüz Bağlama”Arayüzleri Tanımla
Section titled “Arayüzleri Tanımla”interface ArticleRepositoryInterface{ public function findById(int $id): ?Article; public function save(Article $article): void; public function delete(Article $article): void;}Bağlama Uygulaması
Section titled “Bağlama Uygulaması”return [ ArticleRepositoryInterface::class => XoopsArticleRepository::class,
// Or with factory ArticleRepositoryInterface::class => function (ContainerInterface $c) { return new XoopsArticleRepository( $c->get('database') ); },];DI ile test etme
Section titled “DI ile test etme”Kolay Alay Etme
Section titled “Kolay Alay Etme”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 Eski Entegrasyon
Section titled “XOOPS Eski Entegrasyon”Eski ve Yeni Arasında Köprü Kurmak
Section titled “Eski ve Yeni Arasında Köprü Kurmak”// 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);}Eski İşleyicileri Sarma
Section titled “Eski İşleyicileri Sarma”return [ 'article.handler' => function () { return xoops_getModuleHandler('article', 'mymodule'); },
ArticleRepositoryInterface::class => function (ContainerInterface $c) { return new LegacyArticleRepository( $c->get('article.handler') ); },];En İyi Uygulamalar
Section titled “En İyi Uygulamalar”- Arayüzleri Enjekte Et - Uygulamalara değil soyutlamalara bağlıdır
- Yapıcı Enjeksiyonu - Ayarlayıcı enjeksiyonu yerine yapıcıyı tercih edin
- Tek Sorumluluk - Her sınıfın birkaç bağımlılığı olmalıdır
- Konteyner Farkındalığından Kaçının - Hizmetlerin konteyner hakkında bilgi sahibi olmaması gerekir
- Yapılandırın, Kodlamayın - Kablolama için yapılandırma dosyalarını kullanın
İlgili Belgeler
Section titled “İlgili Belgeler”- ../07-XOOPS-4.0/Implementation-Guides/PSR-11-Dependency-Injection-Guide - PSR-11 uygulaması
- ../03-Module-Development/Patterns/Service-Layer - Hizmet modeli
- ../03-Module-Development/Best-Practices/Testing - DI ile test etme
- ../07-XOOPS-4.0/XOOPS-4.0-Architecture - Mimariye genel bakış