Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for comma-separated configuration paths #667

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ indent_size = 2
[*.json]
indent_style = tab

[Makefile]
indent_style = tab

# ReST-Files
[*.rst]
indent_size = 3
Expand Down
6 changes: 5 additions & 1 deletion Build/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,11 @@ parameters:
count: 1
path: ../../Tests/Unit/Loader/JsonSplitLoaderTest.php

-
message: "#^Method MASK\\\\Mask\\\\Tests\\\\Unit\\\\Loader\\\\JsonSplitLoaderTest\\:\\:getExpectedConfigurationArray2\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../Tests/Unit/Loader/JsonSplitLoaderTest.php

-
message: "#^Method MASK\\\\Mask\\\\Test\\\\Utility\\\\OverrideFieldsUtilityTest\\:\\:restructuringFieldsWorks\\(\\) has parameter \\$expected with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -1284,4 +1289,3 @@ parameters:
message: "#^Method MASK\\\\Mask\\\\Tests\\\\Unit\\\\TemplatePathUtilityTest\\:\\:getTemplatePathDataProvider\\(\\) return type has no value type specified in iterable type iterable\\.$#"
count: 1
path: ../../Tests/Unit/Utility/TemplatePathUtilityTest.php

36 changes: 27 additions & 9 deletions Classes/CodeGenerator/TyposcriptCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,24 @@ public function generateSetupTyposcript(): string
// for base paths to fluid templates configured in extension settings
$paths = [];
if ($this->maskExtensionConfiguration['content'] ?? false) {
$paths['templateRootPaths'] = [
10 => $this->maskExtensionConfiguration['content'],
];
$paths['templateRootPaths'] = $this->getTyposcriptPathArray(
$this->maskExtensionConfiguration['content'],
10
);
}

if ($this->maskExtensionConfiguration['partials'] ?? false) {
$paths['partialRootPaths'] = [
10 => $this->maskExtensionConfiguration['partials'],
];
$paths['partialRootPaths'] = $this->getTyposcriptPathArray(
$this->maskExtensionConfiguration['partials'],
10
);
}

if ($this->maskExtensionConfiguration['layouts'] ?? false) {
$paths['layoutRootPaths'] = [
10 => $this->maskExtensionConfiguration['layouts'],
];
$paths['layoutRootPaths'] = $this->getTyposcriptPathArray(
$this->maskExtensionConfiguration['layouts'],
10
);
}

$setupContent[] = ArrayToTypoScriptConverter::convert($paths, 'lib.maskContentElement');
Expand All @@ -250,4 +253,19 @@ public function generateSetupTyposcript(): string

return implode("\n\n", $setupContent) . "\n\n";
}

/**
* @return string[]
*/
protected function getTyposcriptPathArray(string $commaSeparatedPaths, int $startKey): array
{
$paths = TemplatePathUtility::getPaths($commaSeparatedPaths);
if (count($paths) === 0) {
return [];
}
return array_combine(
range($startKey, $startKey + count($paths) - 1),
$paths
);
}
}
17 changes: 13 additions & 4 deletions Classes/Controller/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1128,12 +1128,21 @@ protected function getMissingFolders(): array
if (!isset($this->maskExtensionConfiguration[$key])) {
continue;
}
$path = GeneralUtility::getFileAbsFileName($this->maskExtensionConfiguration[$key]);
if ($path === '') {
$origPaths = TemplatePathUtility::getPaths($this->maskExtensionConfiguration[$key]);
if (count($origPaths) === 0) {
continue;
}
if (!file_exists($path)) {
$missingFolders[$key] = $this->maskExtensionConfiguration[$key];
foreach ($origPaths as $origPath) {
$path = GeneralUtility::getFileAbsFileName($origPath);
if (!file_exists($path)) {
$suffix = '';
$num = 1;
while (isset($missingFolders[$key . $suffix])) {
$num++;
$suffix = ' #' . $num;
}
$missingFolders[$key . $suffix] = $origPath;
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions Classes/EventListeners/MaskBackendPreviewEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public function __invoke(PageContentPreviewRenderingEvent $event): void
$view = GeneralUtility::makeInstance(StandaloneView::class, $renderingContext);
$view->setTemplatePathAndFilename($templatePathAndFilename);
if (!empty($this->maskExtensionConfiguration['layouts_backend'])) {
$layoutRootPath = GeneralUtility::getFileAbsFileName($this->maskExtensionConfiguration['layouts_backend']);
$view->setLayoutRootPaths([$layoutRootPath]);
$layoutRootPaths = TemplatePathUtility::getAbsolutePaths($this->maskExtensionConfiguration['layouts_backend']);
$view->setLayoutRootPaths($layoutRootPaths);
}
if (!empty($this->maskExtensionConfiguration['partials_backend'])) {
$partialRootPath = GeneralUtility::getFileAbsFileName($this->maskExtensionConfiguration['partials_backend']);
$view->setPartialRootPaths([$partialRootPath]);
$partialRootPaths = TemplatePathUtility::getAbsolutePaths($this->maskExtensionConfiguration['partials_backend']);
$view->setPartialRootPaths($partialRootPaths);
}

// Fetch and assign some useful variables
Expand Down
22 changes: 13 additions & 9 deletions Classes/Imaging/PreviewIconResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MASK\Mask\Imaging;

use MASK\Mask\Utility\TemplatePathUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;

Expand All @@ -43,20 +44,23 @@ public function isPreviewIconAvailable(string $key): bool

public function getPreviewIconPath(string $key): string
{
if (!($this->maskExtensionConfiguration['preview'] ?? false)) {
$previewPaths = TemplatePathUtility::getPaths($this->maskExtensionConfiguration['preview']);
if (!count($previewPaths)) {
return '';
}
// search a fitting png or svg file in this path
$fileExtensions = ['png', 'svg'];
$previewPath = rtrim($this->maskExtensionConfiguration['preview'], '/');
foreach ($fileExtensions as $fileExtension) {
$extPathToIcon = $previewPath . '/' . $key . '.' . $fileExtension;
$absolutePathToIcon = GeneralUtility::getFileAbsFileName($extPathToIcon);
if ($absolutePathToIcon === '' || !file_exists($absolutePathToIcon)) {
continue;
foreach ($previewPaths as $previewPath) {
$previewPath = rtrim($previewPath, '/');
foreach ($fileExtensions as $fileExtension) {
$extPathToIcon = $previewPath . '/' . $key . '.' . $fileExtension;
$absolutePathToIcon = GeneralUtility::getFileAbsFileName($extPathToIcon);
if ($absolutePathToIcon === '' || !file_exists($absolutePathToIcon)) {
continue;
}
$resource = PathUtility::getPublicResourceWebPath($extPathToIcon);
return '/' . ltrim($resource, '/');
}
$resource = PathUtility::getPublicResourceWebPath($extPathToIcon);
return '/' . ltrim($resource, '/');
}

return '';
Expand Down
68 changes: 47 additions & 21 deletions Classes/Loader/JsonSplitLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use MASK\Mask\Definition\TcaFieldDefinition;
use MASK\Mask\Enumeration\FieldType;
use MASK\Mask\Migrations\MigrationManager;
use MASK\Mask\Utility\TemplatePathUtility;
use Symfony\Component\Finder\Finder;
use TYPO3\CMS\Core\Configuration\Features;
use TYPO3\CMS\Core\Utility\ArrayUtility;
Expand Down Expand Up @@ -63,17 +64,21 @@ public function load(): TableDefinitionCollection
$this->tableDefinitionCollection = new TableDefinitionCollection();
$definitionArray = [];

$contentElementsFolder = $this->validateFolderPath('tt_content');
if (file_exists($contentElementsFolder)) {
$definitionArray = $this->mergeElementDefinitions($definitionArray, $contentElementsFolder);
$contentElementsFolders = $this->validateFolderPaths('tt_content');
foreach ($contentElementsFolders as $contentElementsFolder) {
if (file_exists($contentElementsFolder)) {
$definitionArray = $this->mergeElementDefinitions($definitionArray, $contentElementsFolder);
}
}

// If optional backendLayoutsFolder is not empty, validate the path.
$backendLayoutsFolder = $this->getAbsolutePath('pages');
if ($backendLayoutsFolder !== '') {
$backendLayoutsFolder = $this->validateFolderPath('pages');
if (file_exists($backendLayoutsFolder)) {
$definitionArray = $this->mergeElementDefinitions($definitionArray, $backendLayoutsFolder);
$backendLayoutsFolders = $this->getAbsolutePaths('pages');
if (count($backendLayoutsFolders)) {
$backendLayoutsFolders = $this->validateFolderPaths('pages');
foreach ($backendLayoutsFolders as $backendLayoutsFolder) {
if (file_exists($backendLayoutsFolder)) {
$definitionArray = $this->mergeElementDefinitions($definitionArray, $backendLayoutsFolder);
}
}
}

Expand All @@ -91,32 +96,38 @@ public function write(TableDefinitionCollection $tableDefinitionCollection): voi
{
// Write content elements and backend layouts (if a folder is defined).
$this->writeElementsForTable($tableDefinitionCollection, 'tt_content');
if ($this->getAbsolutePath('pages') !== '') {
if (count($this->getAbsolutePaths('pages'))) {
$this->writeElementsForTable($tableDefinitionCollection, 'pages');
}

// Save new definition in memory.
$this->tableDefinitionCollection = $tableDefinitionCollection;
}

protected function validateFolderPath(string $table): string
/**
* @return string[]
*/
protected function validateFolderPaths(string $table): array
{
$path = $this->getAbsolutePath($table);
if ($path === '' && isset($this->maskExtensionConfiguration[self::FOLDER_KEYS[$table]]) && $this->maskExtensionConfiguration[self::FOLDER_KEYS[$table]] !== '') {
throw new \InvalidArgumentException('Expected ' . self::FOLDER_KEYS[$table] . ' to be a correct file system path. The value "' . $path . '" was given.', 1639218892);
$paths = $this->getAbsolutePaths($table);
if (count($paths) === 0 && isset($this->maskExtensionConfiguration[self::FOLDER_KEYS[$table]]) && $this->maskExtensionConfiguration[self::FOLDER_KEYS[$table]] !== '') {
throw new \InvalidArgumentException('Expected ' . self::FOLDER_KEYS[$table] . ' to be a correct file system path. The value "" was given.', 1639218892);
}

return $path;
return $paths;
}

protected function getPath(string $table): string
{
return $this->maskExtensionConfiguration[self::FOLDER_KEYS[$table]] ?? '';
}

protected function getAbsolutePath(string $table): string
/**
* @return string[]
*/
protected function getAbsolutePaths(string $table): array
{
return GeneralUtility::getFileAbsFileName($this->getPath($table));
return TemplatePathUtility::getAbsolutePaths($this->getPath($table));
}

protected function mergeElementDefinitions(array &$definitionArray, string $folder): array
Expand All @@ -138,10 +149,12 @@ protected function writeElementsForTable(TableDefinitionCollection $tableDefinit
}

$overrideSharedFields = $this->features->isFeatureEnabled('overrideSharedFields');
$absolutePath = $this->validateFolderPath($table);
$absoluteFolderPaths = $this->validateFolderPaths($table);

if (!file_exists($absolutePath)) {
GeneralUtility::mkdir_deep($absolutePath);
foreach ($absoluteFolderPaths as $absoluteFolderPath) {
if (!file_exists($absoluteFolderPath)) {
GeneralUtility::mkdir_deep($absoluteFolderPath);
}
}

$elements = [];
Expand All @@ -150,7 +163,7 @@ protected function writeElementsForTable(TableDefinitionCollection $tableDefinit
}

// Delete removed elements
foreach ((new Finder())->files()->in($absolutePath) as $file) {
foreach ((new Finder())->files()->in($absoluteFolderPaths) as $file) {
if ($file->getFileInfo()->getExtension() !== 'json') {
continue;
}
Expand Down Expand Up @@ -219,7 +232,20 @@ protected function writeElementsForTable(TableDefinitionCollection $tableDefinit
if ($overrideSharedFields) {
$elementTableDefinitionCollection->setRestructuringDone();
}
$filePath = $absolutePath . '/' . $element->key . '.json';
$fileName = $element->key . '.json';
$filePath = null;
foreach ($absoluteFolderPaths as $absolutePath) {
$filePath = $absolutePath . '/' . $fileName;
if (file_exists($filePath)) {
break;
}
$filePath = null;
}
if ($filePath === null) {
//use the first folder for new files
$filePath = $absoluteFolderPaths[0] . '/' . $fileName;
}

$result = GeneralUtility::writeFile($filePath, json_encode($elementTableDefinitionCollection->toArray(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT) . "\n");

if (!$result) {
Expand Down
80 changes: 62 additions & 18 deletions Classes/Utility/TemplatePathUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,36 +33,80 @@ public static function getTemplatePath(
array $settings,
string $elementKey,
bool $onlyTemplateName = false,
?string $path = null,
?string $commaSeparatedPaths = null,
bool $removeExtension = false
): string {
if ($path === null) {
$path = GeneralUtility::getFileAbsFileName(rtrim($settings['content'] ?? '', '/') . '/');
if ($commaSeparatedPaths === null) {
$paths = static::getAbsolutePaths($settings['content']);
} else {
$paths = static::getAbsolutePaths($commaSeparatedPaths);
}
if ($path === '' || $elementKey === '') {
if (count($paths) === 0 || $elementKey === '') {
return '';
}
$path = rtrim($path, '/') . '/';
$fileExtension = '.html';

// check if a html file with underscores exist
if (file_exists($path . GeneralUtility::underscoredToUpperCamelCase($elementKey) . $fileExtension)) {
$fileName = GeneralUtility::underscoredToUpperCamelCase($elementKey);
} elseif (file_exists($path . ucfirst($elementKey) . $fileExtension)) {
$fileName = ucfirst($elementKey);
} elseif (file_exists($path . $elementKey . $fileExtension)) {
$fileName = $elementKey;
} else {
$fileName = GeneralUtility::underscoredToUpperCamelCase($elementKey);
}
foreach ($paths as $path) {
$path = rtrim($path, '/') . '/';
$fileExtension = '.html';

// check if a html file with underscores exist
$exists = false;
if (file_exists($path . GeneralUtility::underscoredToUpperCamelCase($elementKey) . $fileExtension)) {
$fileName = GeneralUtility::underscoredToUpperCamelCase($elementKey);
$exists = true;
} elseif (file_exists($path . ucfirst($elementKey) . $fileExtension)) {
$fileName = ucfirst($elementKey);
$exists = true;
} elseif (file_exists($path . $elementKey . $fileExtension)) {
$fileName = $elementKey;
$exists = true;
} else {
$fileName = GeneralUtility::underscoredToUpperCamelCase($elementKey);
}

if ($removeExtension) {
$fileExtension = '';
if ($removeExtension) {
$fileExtension = '';
}

if ($exists) {
if ($onlyTemplateName) {
return $fileName . $fileExtension;
}
return $path . $fileName . $fileExtension;
}
}

//non-existing template file
if ($onlyTemplateName) {
return $fileName . $fileExtension;
}
return $path . $fileName . $fileExtension;
}

/**
* Split a string of comma-separated paths and make them absolute.
* Remove empty paths.
*
* @return string[]
*/
public static function getAbsolutePaths(string $commaSeparatedPaths): array
{
$paths = GeneralUtility::trimExplode(',', $commaSeparatedPaths);
foreach ($paths as $key => $path) {
$paths[$key] = GeneralUtility::getFileAbsFileName($path);
}
return array_filter($paths);
}

/**
* Split a string of comma-separated paths into an array.
* Remove empty values.
*
* @return string[]
*/
public static function getPaths(string $commaSeparatedPaths): array
{
$paths = GeneralUtility::trimExplode(',', $commaSeparatedPaths);
return array_filter($paths);
}
}
Loading