fipamo/app/Services/Assets/AssetService.php
ro 68469d958b
Fix for #115, theme css subdir deleted on publish
Patch for a bug that was deleteting the subdirectories of the theme css
folder holding additional theme assets  when being published and tested in the theme kit.

also removed an unneccessary helper script and organized directory
actions in their own helper file to keep it all tidy
2024-06-30 19:50:27 -06:00

114 lines
4 KiB
PHP

<?php
namespace App\Services\Assets;
use App\Services\Data\ThemeService;
class AssetService
{
protected $themeTestImagePath;
protected $themeTestCSSPath;
protected $themeTestScriptsPath;
protected $themes;
protected $currentTheme;
public function __construct(ThemeService $themeService)
{
$this->themeImagePath = '../public/assets/images/global/';
$this->themeTestImagePath = '../public/theme/images/global/';
$this->themeCSSPath = '../public/assets/css/theme/';
$this->themeTestCSSPath = '../public/theme/css/theme/';
$this->themeScriptsPath = '../public/assets/scripts/theme/';
$this->themeTestScriptsPath = '../public/theme/scripts/theme/';
$this->themes = $themeService;
$this->currentTheme = $this->themes->getCurrentTheme();
}
public function getCurrentTheme()
{
return $this->currentTheme;
}
public function moveToTheme($live = false)
{
$imagePath = '';
$cssPath = '';
$scriptPath = '';
($live) ? $imagePath = $this->themeImagePath : $imagePath = $this->themeTestImagePath;
($live) ? $cssPath = $this->themeCSSPath : $cssPath = $this->themeTestCSSPath;
($live) ? $scriptPath = $this->themeScriptsPath : $scriptPath = $this->themeTestScriptsPath;
//get current theme
foreach (
new \DirectoryIterator('../content/themes/' . $this->currentTheme . '/assets/images/global/') as $file
) {
if ($file->isDot()) {
continue;
}
//make theme directory if not present
if (!is_dir($imagePath)) {
mkdir($imagePath, 0755, true);
}
if (!is_file($imagePath . $file->getFileName())) {
copy(
'../content/themes/' .
$this->currentTheme .
'/assets/images/global/' .
$file->getFileName(),
$imagePath . $file->getFileName()
);
} else {
//image is already there, so chill
}
}
//clear test theme css and script directories
$styles = glob($cssPath . '*'); // get all file names
foreach ($styles as $file) { // iterate files
if (is_file($file)) {
unlink($file); // delete file
}
}
$scripts = glob($scriptPath . '*'); // get all file names
foreach ($scripts as $file) { // iterate files
if (is_file($file)) {
unlink($file); // delete file
}
}
//copy theme assets to public
$newcss = glob('../content/themes/' . $this->currentTheme . '/assets/css/*');
if (!is_dir($cssPath)) {
mkdir($cssPath, 0755, true);
}
foreach ($newcss as $file) { // iterate files
if (is_file($file)) {
$path = explode('/', $file);
copy($file, $cssPath . $path[6]);
} else {
// if there is a type/font folder, move it
$typePath = explode('/', $file);
$src = '../content/themes/' . $this->currentTheme . '/assets/css/' . $typePath[6];
$dst = $cssPath . $typePath[6];
if (is_dir($dst)) {
delete_directory($dst);
copy_directory($src, $dst);
} else {
copy_directory($src, $dst);
}
//rename($file, $cssPath . $typePath[6]);
}
}
$newjs = glob('../content/themes/' . $this->currentTheme . '/assets/scripts/*');
if (!is_dir($scriptPath)) {
mkdir($scriptPath, 0755, true);
}
foreach ($newjs as $file) { // iterate files
if (is_file($file)) {
$path = explode('/', $file);
copy($file, $scriptPath . $path[6]);
}
}
}
}