Skip to content

Commit

Permalink
Merge branch 'main' into search
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndr-w authored May 25, 2024
2 parents 21a382e + dfec13f commit 990d1d4
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
4 changes: 4 additions & 0 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ static function ($a) {

/* Wenn quick_navigation installiert, dann */
ButtonRegistry::registerButton(new QuickNavigationButton(), 5);

if (rex::isDebugMode() && rex_addon::get('developer')->isAvailable()) {
FragmentsScanner::scan();
}
120 changes: 120 additions & 0 deletions lib/FragmentScanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Alexplusde\Wildcard;

use rex_file;
use rex_path;

use function array_key_exists;

use const DIRECTORY_SEPARATOR;
use const GLOB_BRACE;
use const JSON_PRETTY_PRINT;

class FragmentScanner
{
/** @var string */
private $packageDir;
/** @var string */
private $wildcardFile;

/** @var array */
private $wildcardContent;

/**
* FragmentScanner constructor.
*/
public function __construct(string $packageDir)
{
$this->packageDir = $packageDir;
$this->wildcardFile = $packageDir . 'wildcard' . DIRECTORY_SEPARATOR . 'translations.json';
$this->wildcardContent = $this->getWildcardContent();
}

/**
* Liefert alle Fragment-Dateien, die Platzhalter enthalten könnten.
*/
public function getFiles(): array
{
return array_merge(
glob($this->packageDir . 'fragments' . DIRECTORY_SEPARATOR . '*.php', GLOB_BRACE),
glob($this->packageDir . 'fragments' . DIRECTORY_SEPARATOR . '**' . DIRECTORY_SEPARATOR . '*.php', GLOB_BRACE),
);
}

/**
* Erstellt die Wildcard-Datei, wenn sie nicht existiert.
*/
private function ensureWildcardFileExists(): void
{
if (!file_exists($this->wildcardFile)) {
rex_file::put($this->wildcardFile, json_encode(['wildcards' => []], JSON_PRETTY_PRINT));
}
}

/**
* Gibt den Inhalt der Wildcard-Datei als PHP-Array zurück.
*/
private function getWildcardContent(): ?array
{
return json_decode(rex_file::get($this->wildcardFile), true);
}

/**
* Überprüft, ob der Key bereits in Wildcard vorhanden ist.
*/
public function keyExists(string $key): bool
{
if (!isset($this->wildcardContent['wildcards'])) {
return false;
}
return array_key_exists($key, $this->wildcardContent['wildcards']);
}

/**
* Fügt den Key zu den Wildcards hinzu.
*/
public function addKey(string $key): void
{
if (!$this->keyExists($key)) {
$this->wildcardContent['wildcards'][$key] = [
'timestamp' => date('Y-m-d H:i:s'),
'translations' => [],
];
}
}

/**
* Schreibt die Wildcard-Datei in das zugehörige Addon-Verzeichnis.
*/
public function writeWildcardFile(): void
{
$this->ensureWildcardFileExists();
rex_file::put($this->wildcardFile, json_encode($this->wildcardContent, JSON_PRETTY_PRINT));
}

/**
* Führt die Scan-Funktion für alle Addons oder ein spezifisches Addon aus und aktualisiert die Wildcard-Dateien.
*/
public static function scan(?string $packageName = null): void
{
$dirs = glob(rex_path::addon('*'));
if ($packageName) {
$dirs = [rex_path::addon($packageName)];
}

foreach ($dirs as $dir) {
$scanner = new self($dir);
$files = $scanner->getFiles();
foreach ($files as $file) {
$content = rex_file::get($file);
preg_match_all('/{{(.*?)}}/', $content, $matches);
$keys = $matches[1];
foreach ($keys as $key) {
$scanner->addKey($key);
}
}
$scanner->writeWildcardFile();
}
}
}

0 comments on commit 990d1d4

Please sign in to comment.