Skip to content

Commit

Permalink
Merge pull request #27 from helhum/user-config
Browse files Browse the repository at this point in the history
  • Loading branch information
helhum authored Oct 27, 2021
2 parents 739010e + e8abd6b commit fefbd85
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ jobs:
run: |
rm -rf var/cache/*
vendor/bin/typo3cms site:list | grep my-fancy-host
- name: BE Config Test
if: ${{ matrix.typo3 != '^9.5.0' }}
run: |
rm -rf var/cache/*
vendor/bin/typo3cms configuration:showlocal BE/explicitADmode --json | grep explicitAllow
2 changes: 1 addition & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ return PhpCsFixer\Config::create()
PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
->exclude('.Build')
->exclude('public')
);
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ finder:
- "*.php"
exclude:
- "vendor"
- "public"
33 changes: 33 additions & 0 deletions src/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use Helhum\ConfigLoader\CachedConfigurationLoader;
use Helhum\ConfigLoader\ConfigurationLoader;
use Helhum\ConfigLoader\InvalidConfigurationFileException;
use Helhum\ConfigLoader\Processor\PlaceholderValue;
use Helhum\TYPO3\ConfigHandling\Processor\ExtensionSettingsSerializer;
use Helhum\Typo3Console\Mvc\Cli\Symfony\Input\ArgvInput;
Expand Down Expand Up @@ -87,16 +88,48 @@ private function shouldCache(): bool
return $shouldCache && preg_match($lowLevelNamespaces, $input->getFirstArgument() ?? 'list') === 0;
}

/**
* Complete config
* Cached in production
*
* @throws InvalidConfigurationFileException
* @return array
*/
public function load(): array
{
return $this->buildLoader()->load();
}

/**
* Complete config, but without overrides config
*
* @return array
*/
public function loadBase(): array
{
return (new Typo3Config($this->settingsFile))->readBaseConfig();
}

/**
* Config with overrides file, but without TYPO3 defaults
*
* @return array
*/
public function loadOwn(): array
{
return (new Typo3Config($this->settingsFile))->readOwnConfig();
}

/**
* Config from overrides file
*
* @return array
*/
public function loadOverrides(): array
{
return (new Typo3Config($this->settingsFile))->readOverridesConfig();
}

public function flushCache(): void
{
if (!$this->isProduction) {
Expand Down
55 changes: 55 additions & 0 deletions src/Typo3Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Helhum\ConfigLoader\ConfigurationReaderFactory;
use Helhum\ConfigLoader\Reader\CollectionReader;
use Helhum\ConfigLoader\Reader\ConfigReaderInterface;
use Helhum\TYPO3\ConfigHandling\ConfigReader\ArrayReader;
use Helhum\TYPO3\ConfigHandling\ConfigReader\CustomProcessingReader;
use Helhum\TYPO3\ConfigHandling\ConfigReader\Typo3BaseConfigReader;
use Helhum\TYPO3\ConfigHandling\ConfigReader\Typo3DefaultConfigPresenceReader;
Expand All @@ -42,6 +43,16 @@ class Typo3Config implements ConfigReaderInterface
*/
private $reader;

/**
* @var ConfigReaderInterface
*/
private $ownConfigReader;

/**
* @var ConfigReaderInterface
*/
private $overridesReader;

public function __construct(string $configFile, ConfigurationReaderFactory $readerFactory = null)
{
$readerFactory = $readerFactory ?? new ConfigurationReaderFactory(dirname($configFile));
Expand All @@ -61,23 +72,67 @@ function (string $resource) {
$readerFactory->createRootReader(SettingsFiles::getOverrideSettingsFile())
)
);
$readerFactory->setReaderFactoryForType(
'typo3',
function () {
return new ArrayReader([]);
},
false
);
$this->ownConfigReader = new CustomProcessingReader(
new CollectionReader(
$readerFactory->createRootReader($configFile),
$readerFactory->createRootReader(SettingsFiles::getOverrideSettingsFile())
)
);
$this->overridesReader = $readerFactory->createReader(SettingsFiles::getOverrideSettingsFile());
}

public function hasConfig(): bool
{
return $this->reader->hasConfig();
}

/**
* Complete config
*
* @return array
*/
public function readConfig(): array
{
return $this->reader->readConfig();
}

/**
* Complete config, but without overrides config
*
* @return array
*/
public function readBaseConfig(): array
{
return $this->baseReader->readConfig();
}

/**
* Config with overrides file, but without TYPO3 defaults
*
* @return array
*/
public function readOwnConfig(): array
{
return $this->ownConfigReader->readConfig();
}

/**
* Config of overrides file only
*
* @return array
*/
public function readOverridesConfig(): array
{
return $this->overridesReader->hasConfig() ? $this->overridesReader->readConfig() : [];
}

public function getValue(string $path)
{
return Config::getValue($this->readConfig(), $path);
Expand Down
31 changes: 10 additions & 21 deletions src/Xclass/ConfigurationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@

use Composer\InstalledVersions;
use Helhum\ConfigLoader\Config;
use Helhum\ConfigLoader\ConfigurationReaderFactory;
use Helhum\ConfigLoader\PathDoesNotExistException;
use Helhum\TYPO3\ConfigHandling\ConfigCleaner;
use Helhum\TYPO3\ConfigHandling\ConfigDumper;
use Helhum\TYPO3\ConfigHandling\ConfigExtractor;
use Helhum\TYPO3\ConfigHandling\ConfigLoader;
use Helhum\TYPO3\ConfigHandling\Processor\RemoveSettingsProcessor;
use Helhum\TYPO3\ConfigHandling\SettingsFiles;
Expand Down Expand Up @@ -72,11 +70,6 @@ class ConfigurationManager
*/
private $configLoader;

/**
* @var ConfigExtractor
*/
private $configExtractor;

/**
* @var ConfigDumper
*/
Expand Down Expand Up @@ -144,13 +137,11 @@ class ConfigurationManager

public function __construct(
ConfigLoader $configLoader = null,
ConfigExtractor $configExtractor = null,
ConfigDumper $configDumper = null,
ConfigCleaner $configCleaner = null
) {
$this->configLoader = $configLoader ?: new ConfigLoader(Environment::getContext()->isProduction());
$this->configDumper = $configDumper ?: new ConfigDumper();
$this->configExtractor = $configExtractor ?: new ConfigExtractor($this->configDumper);
$this->configCleaner = $configCleaner ?: new ConfigCleaner();
}

Expand All @@ -163,8 +154,6 @@ public function getDefaultConfiguration()
{
if (!$this->defaultConfig) {
$this->defaultConfig = require $this->getDefaultConfigurationFileLocation();
// TYPO3 core expects this value to be set in LocalConfiguration.php (see SilentConfigurationUpgradeService::configureBackendLoginSecurity)
unset($this->defaultConfig['BE']['loginSecurityLevel']);
}

return $this->defaultConfig;
Expand Down Expand Up @@ -201,7 +190,7 @@ public function getDefaultConfigurationDescriptionFileLocation()
*/
public function getLocalConfiguration()
{
return $this->configCleaner->cleanConfig($this->getMergedLocalConfiguration(), $this->getDefaultConfiguration());
return $this->configLoader->loadOwn();
}

/**
Expand Down Expand Up @@ -294,17 +283,18 @@ public function updateLocalConfiguration(array $configurationToMerge): void
$remainingPaths = array_diff($removedPaths, $addedPaths);
$this->updateRemovalPaths($remainingPaths);
}

if ($this->configExtractor->extractConfig($configurationToMerge, $this->getMergedLocalConfiguration(), $overrideSettingsFile)) {
$this->configLoader->flushCache();
$remainingConfigToWrite = $this->configCleaner->cleanConfig(
$configurationToMerge,
$this->getLocalConfiguration()
);
if (!empty($remainingConfigToWrite)) {
$this->configDumper->dumpToFile(array_replace_recursive($this->configLoader->loadOverrides(), $remainingConfigToWrite), $overrideSettingsFile);
}
}

private function findRemovedPaths(): array
{
$overrideSettingsFile = SettingsFiles::getOverrideSettingsFile();
$factory = new ConfigurationReaderFactory();
$overrides = file_exists($overrideSettingsFile) ? $factory->createReader($overrideSettingsFile)->readConfig() : [];
$overrides = $this->configLoader->loadOverrides();
$removedPaths = [];
if (isset($overrides['processors'])) {
foreach ($overrides['processors'] as $index => $processorConfig) {
Expand All @@ -321,8 +311,7 @@ private function findRemovedPaths(): array
private function updateRemovalPaths(array $pathsToRemove): void
{
$overrideSettingsFile = SettingsFiles::getOverrideSettingsFile();
$factory = new ConfigurationReaderFactory();
$overrides = file_exists($overrideSettingsFile) ? $factory->createReader($overrideSettingsFile)->readConfig() : [];
$overrides = $this->configLoader->loadOverrides();
$processorPosition = 0;
if (isset($overrides['processors'])) {
foreach ($overrides['processors'] as $index => $processorConfig) {
Expand Down Expand Up @@ -497,7 +486,7 @@ public function exportConfiguration()
*/
public function writeLocalConfiguration(array $configuration)
{
$configuration = $this->configCleaner->cleanConfig($configuration, $this->getMergedLocalConfiguration());
$configuration = $this->configCleaner->cleanConfig($configuration, $this->getLocalConfiguration());
$this->updateLocalConfiguration($configuration);

// Too many places require this file to exist, so we make sure to create it
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/ConfigLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use Composer\InstalledVersions;
use Helhum\TYPO3\ConfigHandling\ConfigLoader;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit fefbd85

Please sign in to comment.