Skip to content

Commit

Permalink
[TASK] Use service locator for optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Feb 17, 2023
1 parent 288edd9 commit 4849a12
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 71 deletions.
42 changes: 15 additions & 27 deletions Classes/Hooks/PageRenderer/SchemaMarkupInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,43 @@
use Brotkrueml\Schema\Adapter\ExtensionAvailability;
use Brotkrueml\Schema\Cache\PagesCacheService;
use Brotkrueml\Schema\Event\RenderAdditionalTypesEvent;
use Brotkrueml\Schema\Extension;
use Brotkrueml\Schema\Manager\SchemaManager;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

final class SchemaMarkupInjection
{
private readonly ApplicationType $applicationType;

/**
* @var array<string, mixed>
* @var array<string, string>
*/
private array $configuration;

private SchemaManager $schemaManager;
private PagesCacheService $pagesCacheService;
private ExtensionAvailability $extensionAvailability;
private EventDispatcherInterface $eventDispatcher;
private ExtensionAvailability $extensionAvailability;
private PagesCacheService $pagesCacheService;
private SchemaManager $schemaManager;

public function __construct(
ExtensionConfiguration $extensionConfiguration = null,
SchemaManager $schemaManager = null,
PagesCacheService $pagesCacheService = null,
ApplicationType $applicationType = null,
ExtensionAvailability $extensionAvailability = null,
EventDispatcherInterface $eventDispatcher = null,
private readonly ApplicationType $applicationType,
private readonly ContainerInterface $locator,
) {
$this->applicationType = $applicationType ?? new ApplicationType();
if (! $this->applicationType->isBackend()) {
$extensionConfiguration ??= GeneralUtility::makeInstance(ExtensionConfiguration::class);
$this->configuration = $extensionConfiguration->get('schema') ?? [];
$this->schemaManager = $schemaManager ?? GeneralUtility::makeInstance(SchemaManager::class);
$this->pagesCacheService = $pagesCacheService ?? GeneralUtility::makeInstance(PagesCacheService::class);
$this->extensionAvailability = $extensionAvailability ?? new ExtensionAvailability();
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::makeInstance(EventDispatcherInterface::class);
}
}

/**
* @noinspection PhpUnusedParameterInspection
*/
public function execute(?array &$params, PageRenderer &$pageRenderer): void
public function execute(?array &$params, PageRenderer $pageRenderer): void
{
if ($this->applicationType->isBackend()) {
return;
}

$this->configuration = $this->locator->get(ExtensionConfiguration::class)->get(Extension::KEY) ?? [];
$this->eventDispatcher = $this->locator->get(EventDispatcherInterface::class);
$this->extensionAvailability = $this->locator->get(ExtensionAvailability::class);
$this->pagesCacheService = $this->locator->get(PagesCacheService::class);
$this->schemaManager = $this->locator->get(SchemaManager::class);

if (! $this->isMarkupToBeEmbedded()) {
return;
}
Expand Down
11 changes: 11 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ services:
Brotkrueml\Schema\AdminPanel\TypesInformation:
public: true

Brotkrueml\Schema\Hooks\PageRenderer\SchemaMarkupInjection:
public: true
arguments:
- '@Brotkrueml\Schema\Adapter\ApplicationType'
- !service_locator
Psr\EventDispatcher\EventDispatcherInterface: '@Psr\EventDispatcher\EventDispatcherInterface'
Brotkrueml\Schema\Adapter\ExtensionAvailability: '@Brotkrueml\Schema\Adapter\ExtensionAvailability'
TYPO3\CMS\Core\Configuration\ExtensionConfiguration: '@TYPO3\CMS\Core\Configuration\ExtensionConfiguration'
Brotkrueml\Schema\Cache\PagesCacheService: '@Brotkrueml\Schema\Cache\PagesCacheService'
Brotkrueml\Schema\Manager\SchemaManager: '@Brotkrueml\Schema\Manager\SchemaManager'

Brotkrueml\Schema\JsonLd\Renderer:
public: true

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ xlf-lint:

.PHONY: yaml-lint
yaml-lint: vendor
find -regex '.*\.ya?ml' ! -path "./.Build/*" -exec .Build/bin/yaml-lint -v {} \;
find -regex '.*\.ya?ml' ! -path "./.Build/*" -exec .Build/bin/yaml-lint --parse-tags -v {} \;

.PHONY: zip
zip:
Expand Down
105 changes: 62 additions & 43 deletions Tests/Unit/Hooks/PageRenderer/SchemaMarkupInjectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Package\PackageManager;
Expand All @@ -42,6 +44,7 @@ final class SchemaMarkupInjectionTest extends TestCase
private ExtensionAvailability&Stub $extensionAvailabilityStub;
private EventDispatcher&Stub $eventDispatcherStub;
private SchemaManager $schemaManager;
private ContainerInterface $locator;

protected function setUp(): void
{
Expand All @@ -60,13 +63,17 @@ protected function setUp(): void
->method('dispatch')
->willReturn(new RenderAdditionalTypesEvent(false));

$this->subject = new SchemaMarkupInjection(
$this->locator = $this->buildLocator(
$this->eventDispatcherStub,
$this->extensionAvailabilityStub,
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->schemaManager,
);

$this->subject = new SchemaMarkupInjection(
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$this->pageRendererMock = $this->createMock(PageRenderer::class);
Expand Down Expand Up @@ -165,12 +172,8 @@ public function executeWithMarkupDefinedCallsAddHeaderDataIfShouldEmbeddedIntoHe
->willReturn(false);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$params = [];
Expand Down Expand Up @@ -213,12 +216,8 @@ public function executeWithSchemaCallsAddFooterDataOnceIfShouldEmbeddedIntoBody(
->willReturn(false);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$params = [];
Expand Down Expand Up @@ -253,12 +252,8 @@ public function seoExtensionIsNotInstalledAddsHeaderData(): void
->willReturn(false);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$this->pageRendererMock
Expand Down Expand Up @@ -401,12 +396,8 @@ public function whenPageShouldNotBeIndexedAndConfigurationOptionIsNotDefinedThen
->willReturn(true);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$params = [];
Expand Down Expand Up @@ -454,12 +445,8 @@ public function whenPageShouldNotBeIndexedAndConfigurationOptionIsActivatedThenM
->willReturn(true);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$params = [];
Expand Down Expand Up @@ -499,12 +486,8 @@ public function whenPageShouldNotBeIndexedAndConfigurationOptionIsDeactivatedThe
->willReturn(true);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$params = [];
Expand Down Expand Up @@ -552,12 +535,8 @@ public function whenSeoExtensionIsNotLoadedMarkupIsAlwaysEmbedded(): void
->willReturn(false);

$subject = new SchemaMarkupInjection(
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$this->eventDispatcherStub,
$this->locator,
);

$packageManagerStub = $this->createStub(PackageManager::class);
Expand Down Expand Up @@ -611,13 +590,17 @@ public function additionalTypeAddedViaEventDispatcherIsAddedCorrectly(): void
->method('dispatch')
->willReturn($event);

$subject = new SchemaMarkupInjection(
$locator = $this->buildLocator(
$eventDispatcherStub,
$this->extensionAvailabilityStub,
$this->extensionConfigurationMock,
$this->schemaManager,
$this->pagesCacheServiceMock,
$this->schemaManager,
);

$subject = new SchemaMarkupInjection(
$this->applicationTypeStub,
$this->extensionAvailabilityStub,
$eventDispatcherStub,
$locator,
);

$packageManagerStub = $this->createStub(PackageManager::class);
Expand All @@ -630,4 +613,40 @@ public function additionalTypeAddedViaEventDispatcherIsAddedCorrectly(): void
$params = [];
$subject->execute($params, $this->pageRendererMock);
}

private function buildLocator(
EventDispatcherInterface $eventDispatcher,
ExtensionAvailability $extensionAvailability,
ExtensionConfiguration $extensionConfiguration,
PagesCacheService $pagesCacheService,
SchemaManager $schemaManager,
): ContainerInterface {
return new class($eventDispatcher, $extensionAvailability, $extensionConfiguration, $pagesCacheService, $schemaManager) implements ContainerInterface {
public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
private readonly ExtensionAvailability $extensionAvailability,
private readonly ExtensionConfiguration $extensionConfiguration,
private readonly PagesCacheService $pagesCacheService,
private readonly SchemaManager $schemaManager,
) {
}

public function get(string $id)
{
return match ($id) {
EventDispatcherInterface::class => $this->eventDispatcher,
ExtensionAvailability::class => $this->extensionAvailability,
ExtensionConfiguration::class => $this->extensionConfiguration,
PagesCacheService::class => $this->pagesCacheService,
SchemaManager::class => $this->schemaManager,
};
}

public function has(string $id): bool
{
// Not used
return true;
}
};
}
}

0 comments on commit 4849a12

Please sign in to comment.