From 73800871feba6cff2f056beecfae57be3a748722 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Wed, 2 Oct 2024 13:12:20 -0500 Subject: [PATCH] Internal: Theme: Use chamilo theme assets as fallback for assets not found in current theme --- src/CoreBundle/Controller/ThemeController.php | 9 +++-- src/CoreBundle/ServiceHelper/ThemeHelper.php | 36 +++++++++++++------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/CoreBundle/Controller/ThemeController.php b/src/CoreBundle/Controller/ThemeController.php index 350941a611..67aeac8918 100644 --- a/src/CoreBundle/Controller/ThemeController.php +++ b/src/CoreBundle/Controller/ThemeController.php @@ -6,6 +6,7 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Controller; +use Chamilo\CoreBundle\ServiceHelper\ThemeHelper; use League\Flysystem\FilesystemException; use League\Flysystem\FilesystemOperator; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -20,6 +21,10 @@ use const DIRECTORY_SEPARATOR; #[Route('/themes')] class ThemeController extends AbstractController { + public function __construct( + private readonly ThemeHelper $themeHelper + ) {} + /** * @throws FilesystemException */ @@ -36,9 +41,9 @@ class ThemeController extends AbstractController throw $this->createNotFoundException('The folder name does not exist.'); } - $filePath = $themeDir.DIRECTORY_SEPARATOR.$path; + $filePath = $this->themeHelper->getFileLocation($path); - if (!$filesystem->fileExists($filePath)) { + if (!$filePath) { throw $this->createNotFoundException('The requested file does not exist.'); } diff --git a/src/CoreBundle/ServiceHelper/ThemeHelper.php b/src/CoreBundle/ServiceHelper/ThemeHelper.php index b3cd233d64..7fd7985471 100644 --- a/src/CoreBundle/ServiceHelper/ThemeHelper.php +++ b/src/CoreBundle/ServiceHelper/ThemeHelper.php @@ -81,18 +81,40 @@ final class ThemeHelper return $visualTheme; } - public function getThemeAssetUrl(string $path, bool $absoluteUrl = false): string + /** + * @throws FilesystemException + * @throws UnableToCheckExistence + */ + public function getFileLocation(string $path): ?string { $themeName = $this->getVisualTheme(); + $locations = [ + $themeName.DIRECTORY_SEPARATOR.$path, + self::DEFAULT_THEME.DIRECTORY_SEPARATOR.$path, + ]; + + foreach ($locations as $location) { + if ($this->filesystem->fileExists($location)) { + return $location; + } + } + + return null; + } + + public function getThemeAssetUrl(string $path, bool $absoluteUrl = false): string + { try { - if (!$this->filesystem->fileExists($themeName.DIRECTORY_SEPARATOR.$path)) { + if (!$this->getFileLocation($path)) { return ''; } } catch (FilesystemException) { return ''; } + $themeName = $this->getVisualTheme(); + return $this->router->generate( 'theme_asset', ['name' => $themeName, 'path' => $path], @@ -113,11 +135,8 @@ final class ThemeHelper public function getAssetContents(string $path): string { - $themeName = $this->getVisualTheme(); - $fullPath = $themeName.DIRECTORY_SEPARATOR.$path; - try { - if ($this->filesystem->fileExists($fullPath)) { + if ($fullPath = $this->getFileLocation($path)) { $stream = $this->filesystem->readStream($fullPath); return stream_get_contents($stream); @@ -131,11 +150,8 @@ final class ThemeHelper public function getAssetBase64Encoded(string $path): string { - $visualTheme = $this->getVisualTheme(); - $fullPath = $visualTheme.DIRECTORY_SEPARATOR.$path; - try { - if ($this->filesystem->fileExists($fullPath)) { + if ($fullPath = $this->getFileLocation($path)) { $detector = new ExtensionMimeTypeDetector(); $mimeType = (string) $detector->detectMimeTypeFromFile($fullPath);