Internal: Theme: Use chamilo theme assets as fallback for assets not found in current theme

pull/5836/head
Angel Fernando Quiroz Campos 2 months ago
parent 2dc3a78518
commit 73800871fe
No known key found for this signature in database
GPG Key ID: B284841AE3E562CD
  1. 9
      src/CoreBundle/Controller/ThemeController.php
  2. 36
      src/CoreBundle/ServiceHelper/ThemeHelper.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.');
}

@ -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);

Loading…
Cancel
Save