Skip to content

Commit

Permalink
[FEATURE] Allow override of preset configuration through site configu…
Browse files Browse the repository at this point in the history
…ration

Refs #4
  • Loading branch information
derhansen committed Nov 20, 2023
1 parent d5d70ae commit f135120
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 24 deletions.
20 changes: 14 additions & 6 deletions Classes/Command/PresetByPageUidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UniWue\UwA11yCheck\Check\Result\Impact;
use UniWue\UwA11yCheck\Check\ResultSet;
Expand All @@ -25,11 +26,12 @@ class PresetByPageUidCommand extends AbstractCheckCommand
*/
public function configure()
{
$this->addArgument(
'preset',
InputArgument::REQUIRED,
'ID of the preset'
)
$this->
addArgument(
'preset',
InputArgument::REQUIRED,
'ID of the preset'
)
->addArgument(
'uid',
InputArgument::REQUIRED,
Expand Down Expand Up @@ -57,8 +59,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$pageUid = (int)$input->getArgument('uid');
$levels = $input->hasOption('levels') ? (int)$input->getOption('levels') : 0;

$preset = $presetService->getPresetById($presetId);
try {
$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pageUid);
} catch (\Exception $exception) {
$io->error('Given page UID ' . $pageUid . ' has no connection to a site.');
return Command::FAILURE;
}

$preset = $presetService->getPresetById($presetId, $site);
if (!$preset) {
// @extensionScannerIgnoreLine False positive
$io->error('Preset "' . $presetId . '" not found or contains errors (check classNames!).');
Expand Down
24 changes: 17 additions & 7 deletions Classes/Command/PresetByRecordUidsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UniWue\UwA11yCheck\Check\Result\Impact;
use UniWue\UwA11yCheck\Check\ResultSet;
Expand All @@ -24,15 +25,21 @@ class PresetByRecordUidsCommand extends AbstractCheckCommand
*/
public function configure()
{
$this->addArgument(
'preset',
InputArgument::REQUIRED,
'ID of the preset'
)
$this
->addArgument(
'preset',
InputArgument::REQUIRED,
'ID of the preset'
)
->addArgument(
'uids',
InputArgument::REQUIRED,
'On or multiple record UIDs'
'One or multiple record UIDs'
)
->addArgument(
'rootPageUid',
InputArgument::REQUIRED,
'UID of root page where records are stored'
);
}

Expand All @@ -48,7 +55,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$presetId = $input->getArgument('preset');
$recordUids = GeneralUtility::intExplode(',', $input->getArgument('uids'), true);
$preset = $presetService->getPresetById($presetId);
$rootPageUid = (int)$input->getArgument('rootPageUid');

$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($rootPageUid);
$preset = $presetService->getPresetById($presetId, $site);

if (!$preset) {
// @extensionScannerIgnoreLine False positive
Expand Down
28 changes: 26 additions & 2 deletions Classes/Controller/A11yCheckController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter;
use UniWue\UwA11yCheck\Domain\Model\Dto\CheckDemand;
use UniWue\UwA11yCheck\Property\TypeConverter\PresetTypeConverter;
use UniWue\UwA11yCheck\Service\PresetService;
use UniWue\UwA11yCheck\Service\ResultsService;

Expand Down Expand Up @@ -105,9 +106,10 @@ public function indexAction(?CheckDemand $checkDemand = null): ResponseInterface
);
}

$site = $this->request->getAttribute('site');
$this->view->assignMultiple([
'checkDemand' => $checkDemand,
'presets' => $this->presetService->getPresets(),
'presets' => $this->presetService->getPresets($site),
'levelSelectorOptions' => $this->getLevelSelectorOptions(),
'savedResultsCount' => $this->resultsService->getSavedResultsCount($this->pid),
]);
Expand All @@ -118,7 +120,20 @@ public function indexAction(?CheckDemand $checkDemand = null): ResponseInterface
/**
* Ensure checkDemand array will be converted to an object
*/
public function initializeCheckAction(): void
protected function initializeIndexAction(): void
{
$this->initializeTypeConverterForArgument();
}

/**
* Ensure checkDemand array will be converted to an object
*/
protected function initializeCheckAction(): void
{
$this->initializeTypeConverterForArgument();
}

private function initializeTypeConverterForArgument(): void
{
if ($this->arguments->hasArgument('checkDemand')) {
$propertyMappingConfiguration = $this->arguments->getArgument('checkDemand')
Expand All @@ -129,9 +144,18 @@ public function initializeCheckAction(): void
PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED,
true
);

$propertyMappingConfiguration->forProperty('preset')->setTypeConverterOption(
PresetTypeConverter::class,
PresetTypeConverter::CONFIGURATION_REQUEST,
$this->request
);
$presetTypeConverter = GeneralUtility::makeInstance(PresetTypeConverter::class);
$propertyMappingConfiguration->forProperty('preset')->setTypeConverter($presetTypeConverter);
}
}


/**
* Check action
*
Expand Down
11 changes: 10 additions & 1 deletion Classes/Property/TypeConverter/PresetTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Error\Error;
use TYPO3\CMS\Extbase\Mvc\Request;
use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
use UniWue\UwA11yCheck\Check\Preset;
Expand All @@ -14,6 +15,8 @@
*/
class PresetTypeConverter extends AbstractTypeConverter
{
public const CONFIGURATION_REQUEST = 'request';

protected PresetService $presetService;

public function injectConfigurationService(PresetService $presetService): void
Expand All @@ -33,7 +36,13 @@ public function convertFrom(
array $convertedChildProperties = [],
PropertyMappingConfigurationInterface $configuration = null
) {
$preset = $this->presetService->getPresetById($source);
if (!$configuration) {
throw new \Exception('PresetTypeConverter has not configuration', 1700322071);
}
/** @var Request $request */
$request = $configuration->getConfigurationValue(self::class, self::CONFIGURATION_REQUEST);
$site = $request->getAttribute('site');
$preset = $this->presetService->getPresetById($source, $site);

if (!$preset) {
return GeneralUtility::makeInstance(Error::class, 'Preset not found', 1573053017);
Expand Down
24 changes: 21 additions & 3 deletions Classes/Service/PresetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteInterface;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UniWue\UwA11yCheck\Analyzers\AbstractAnalyzer;
Expand Down Expand Up @@ -34,10 +36,11 @@ public function __construct()
/**
* Returns all presets
*/
public function getPresets(): array
public function getPresets(SiteInterface $site): array
{
$yamlFile = $this->getConfigurationFile();
$yamlData = $this->yamlFileLoader->load($yamlFile);
$yamlData = $this->overrideFromSiteConfiguration($yamlData, $site);

$presets = [];

Expand Down Expand Up @@ -84,11 +87,11 @@ public function getPresets(): array
/**
* Returns a preset by the ID
*/
public function getPresetById(string $id): ?Preset
public function getPresetById(string $id, SiteInterface $site): ?Preset
{
$result = null;

$presets = $this->getPresets();
$presets = $this->getPresets($site);
foreach ($presets as $preset) {
if ($preset->getId() === $id) {
$result = $preset;
Expand Down Expand Up @@ -193,4 +196,19 @@ protected function getConfigurationFile()

return $file;
}

/**
* Overrides the given YAML data with the site configuration.
*/
private function overrideFromSiteConfiguration($yamlData, SiteInterface $site): array
{
if (!$site instanceof Site) {
return $yamlData;
}

$siteConfiguration = $site->getConfiguration()['settings']['uw_a11y_check'] ?? [];
ArrayUtility::mergeRecursiveWithOverrule($yamlData, $siteConfiguration);

return $yamlData;
}
}
4 changes: 3 additions & 1 deletion Classes/Service/ResultsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace UniWue\UwA11yCheck\Service;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use UniWue\UwA11yCheck\Check\ResultSet;

Expand Down Expand Up @@ -45,6 +46,7 @@ public function getResultsArrayByPid(int $pid): array

$queryResult = $query->execute()->fetchAllAssociative();

$site = GeneralUtility::makeInstance(SiteFinder::class)->getSiteByPageId($pid);
$dbResults = [];

foreach ($queryResult as $result) {
Expand All @@ -60,7 +62,7 @@ public function getResultsArrayByPid(int $pid): array
$checkDate = new \DateTime();
$checkDate->setTimestamp($result['check_date']);
$dbResults[$presetId] = [
'preset' => $this->presetService->getPresetById($presetId) ?? 'Unknown',
'preset' => $this->presetService->getPresetById($presetId, $site) ?? 'Unknown',
'results' => [$unserializedData],
'date' => $checkDate,
];
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,37 @@ an existing extension (e.g. website site package).

1. Overwrite default configuration

It is possible to overwrite the default configuration by eigher a custom YAML configuration file or by
using TYPO3 site settings, which are available since TYPO3 10.4

1.1 Overwrite configuration by custom YAML configuration file

This is recommended, if you want to overwrite or create a completely new configuration for the extension.

```
$GLOBALS['TYPO3_CONF_VARS']['UwA11yCheck']['Configuration'] = 'EXT:your_sitepackage/Configuration/A11y/Default.yaml';
```


1.2 Overwrite configuration by TYPO3 site settings

If you just want to override single configuration values from the default configuration, it is possible to use TYPO3
site configuration as shown below:

```
settings:
uw_a11y_check:
presets:
pageContent:
checkUrlGenerator:
configuration:
targetPid: 71
extNews:
checkUrlGenerator:
configuration:
targetPid: 73
```


Next you should copy the contents of the default configuration to your configuration file.

2. Create pages with plugins for page content and ext:news (if installed)
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 1,
'version' => '4.0.0',
'version' => '4.1.0',
'constraints' => [
'depends' => [
'typo3' => '11.5.0-11.5.99',
Expand Down
2 changes: 0 additions & 2 deletions ext_localconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
]
);

ExtensionUtility::registerTypeConverter(PresetTypeConverter::class);

// Register configuration yaml file
if (empty($GLOBALS['TYPO3_CONF_VARS']['UwA11yCheck']['Configuration'])) {
$GLOBALS['TYPO3_CONF_VARS']['UwA11yCheck']['Configuration'] = 'EXT:uw_a11y_check/Configuration/A11y/Default.yaml';
Expand Down

0 comments on commit f135120

Please sign in to comment.