mirror of
https://github.com/SuiteCRM/SuiteCRM-Core.git
synced 2025-09-04 10:14:13 +08:00
Move Services
Signed-off-by: Dillon-Brown <dillon.brown@salesagility.com>
This commit is contained in:
parent
0af7c9e41f
commit
931073be51
114 changed files with 209 additions and 191 deletions
|
@ -5,7 +5,7 @@ namespace App\Themes\DataProvider;
|
|||
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
|
||||
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
|
||||
use App\Themes\Entity\ThemeImages;
|
||||
use App\Service\ThemeImageService;
|
||||
use App\Themes\Service\ThemeImageService;
|
||||
|
||||
/**
|
||||
* Class ThemeImagesItemDataProvider
|
||||
|
|
48
core/backend/Themes/Service/ThemeImageFinder.php
Normal file
48
core/backend/Themes/Service/ThemeImageFinder.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
|
||||
namespace App\Themes\Service;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
|
||||
class ThemeImageFinder
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $themeImageSupportedTypes;
|
||||
|
||||
/**
|
||||
* ThemeImageFinder constructor.
|
||||
* @param array $themeImageSupportedTypes
|
||||
*/
|
||||
public function __construct(array $themeImageSupportedTypes)
|
||||
{
|
||||
$this->themeImageSupportedTypes = $themeImageSupportedTypes;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of existing images
|
||||
* @param $fullPath
|
||||
* @return SplFileInfo[]
|
||||
*/
|
||||
public function find($fullPath): iterable
|
||||
{
|
||||
if (!is_dir($fullPath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$finder = new Finder();
|
||||
$finder->files();
|
||||
|
||||
foreach ($this->themeImageSupportedTypes as $extension) {
|
||||
$finder->name("*.$extension");
|
||||
}
|
||||
|
||||
$finder->in($fullPath);
|
||||
|
||||
return $finder->getIterator();
|
||||
}
|
||||
}
|
152
core/backend/Themes/Service/ThemeImageService.php
Normal file
152
core/backend/Themes/Service/ThemeImageService.php
Normal file
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
namespace App\Themes\Service;
|
||||
|
||||
use App\Themes\Entity\ThemeImages;
|
||||
|
||||
class ThemeImageService
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $themeImagePaths;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $themeImageTypePriority;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $projectDir;
|
||||
/**
|
||||
* @var ThemeImageFinder
|
||||
*/
|
||||
private $themeImageFinder;
|
||||
|
||||
/**
|
||||
* ThemeImageService constructor.
|
||||
* @param string[] $themeImagePaths
|
||||
* @param array $themeImageSupportedTypes
|
||||
* @param string $projectDir
|
||||
* @param ThemeImageFinder $themeImageFinder
|
||||
*/
|
||||
public function __construct(
|
||||
array $themeImagePaths,
|
||||
array $themeImageSupportedTypes,
|
||||
string $projectDir,
|
||||
ThemeImageFinder $themeImageFinder
|
||||
) {
|
||||
$this->themeImagePaths = $themeImagePaths;
|
||||
$this->projectDir = $projectDir;
|
||||
$this->themeImageFinder = $themeImageFinder;
|
||||
|
||||
$this->themeImageTypePriority = $this->buildTypePriorityMap($themeImageSupportedTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the priority map
|
||||
* @param array $types
|
||||
* @return array
|
||||
*/
|
||||
protected function buildTypePriorityMap(array $types): array
|
||||
{
|
||||
return array_flip($types);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get images for given $theme
|
||||
* @param string $theme
|
||||
* @return ThemeImages
|
||||
*/
|
||||
public function get(string $theme): ThemeImages
|
||||
{
|
||||
$images = new ThemeImages();
|
||||
$images->setId($theme);
|
||||
$items = [];
|
||||
|
||||
foreach ($this->themeImagePaths as $themeImagePath) {
|
||||
$themeImages = $this->getImages($themeImagePath, $theme);
|
||||
foreach ($themeImages as $imageKey => $image) {
|
||||
$items[$imageKey] = $image;
|
||||
}
|
||||
}
|
||||
|
||||
$images->setItems($items);
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get images information for given path and theme
|
||||
* @param string $imagePath
|
||||
* @param string $theme
|
||||
* @return array
|
||||
*/
|
||||
protected function getImages(string $imagePath, string $theme): array
|
||||
{
|
||||
|
||||
$path = $this->buildPath($imagePath, $theme);
|
||||
$fullPath = $this->projectDir . '/public/' . $path;
|
||||
|
||||
$it = $this->themeImageFinder->find($fullPath);
|
||||
|
||||
$images = [];
|
||||
if (empty($it)) {
|
||||
return $images;
|
||||
}
|
||||
|
||||
foreach ($it as $file) {
|
||||
$filename = $file->getFilename();
|
||||
$name = pathinfo($filename, PATHINFO_FILENAME);
|
||||
|
||||
$filePath = "$path/{$file->getFilename()}";
|
||||
$extension = $file->getExtension();
|
||||
|
||||
if (!$this->hasPriority($images, $name, $extension)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$images[$name] = [
|
||||
'path' => $filePath,
|
||||
'name' => $name,
|
||||
'type' => $extension
|
||||
];
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build path
|
||||
* @param string $imagePath
|
||||
* @param string $theme
|
||||
* @return string
|
||||
*/
|
||||
protected function buildPath(string $imagePath, string $theme): string
|
||||
{
|
||||
return str_replace('<theme>', $theme, $imagePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current image has priority over already store image
|
||||
* @param array $images
|
||||
* @param string $name
|
||||
* @param string $currentType
|
||||
* @return bool
|
||||
*/
|
||||
protected function hasPriority(array $images, string $name, string $currentType): bool
|
||||
{
|
||||
if (empty($images[$name])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$existingType = $images[$name]['type'];
|
||||
|
||||
$existingPriority = $this->themeImageTypePriority[$existingType];
|
||||
$currentPriority = $this->themeImageTypePriority[$currentType];
|
||||
|
||||
return $existingPriority > $currentPriority;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue