2024-03-16 19:36:51 +01:00
|
|
|
<?php
|
|
|
|
|
2024-05-13 06:14:53 +02:00
|
|
|
namespace App\Services\Assets;
|
|
|
|
|
|
|
|
use App\Services\Data\ThemeService;
|
2024-03-16 19:36:51 +01:00
|
|
|
|
|
|
|
class AssetService
|
|
|
|
{
|
|
|
|
protected $themeTestImagePath;
|
|
|
|
protected $themeTestCSSPath;
|
|
|
|
protected $themeTestScriptsPath;
|
|
|
|
protected $themes;
|
|
|
|
protected $currentTheme;
|
|
|
|
|
|
|
|
public function __construct(ThemeService $themeService)
|
|
|
|
{
|
|
|
|
$this->themeTestImagePath = '../public/theme/images/global/';
|
|
|
|
$this->themeTestCSSPath = '../public/theme/css/theme/';
|
|
|
|
$this->themeTestScriptsPath = '../public/theme/scripts/theme/';
|
2024-04-25 21:17:24 +02:00
|
|
|
$this->themeImagePath = '../public/assets/images/global/';
|
|
|
|
$this->themeCSSPath = '../public/assets/css/theme/';
|
|
|
|
$this->themeScriptsPath = '../public/assets/scripts/theme/';
|
2024-03-16 19:36:51 +01:00
|
|
|
$this->themes = $themeService;
|
|
|
|
$this->currentTheme = $this->themes->getCurrentTheme();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCurrentTheme()
|
|
|
|
{
|
|
|
|
return $this->currentTheme;
|
|
|
|
}
|
|
|
|
|
2024-04-25 21:17:24 +02:00
|
|
|
public function moveToTheme($live = false)
|
2024-03-16 19:36:51 +01:00
|
|
|
{
|
2024-04-25 21:17:24 +02:00
|
|
|
$imagePath = '';
|
|
|
|
$cssPath = '';
|
|
|
|
$scriptPath = '';
|
|
|
|
($live) ? $imagePath = $this->themeImagePath : $imagePath = $this->themeTestImagePath;
|
|
|
|
($live) ? $cssPath = $this->themeCSSPath : $cssPath = $this->themeTestCSSPath;
|
|
|
|
($live) ? $scriptPath = $this->themeScriptsPath : $scriptPath = $this->themeTestScriptsPath;
|
2024-03-16 19:36:51 +01:00
|
|
|
//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
|
2024-04-25 21:17:24 +02:00
|
|
|
if (!is_dir($imagePath)) {
|
|
|
|
mkdir($imagePath, 0755, true);
|
2024-03-16 19:36:51 +01:00
|
|
|
}
|
|
|
|
|
2024-04-25 21:17:24 +02:00
|
|
|
if (!is_file($imagePath . $file->getFileName())) {
|
2024-03-16 19:36:51 +01:00
|
|
|
copy(
|
|
|
|
'../content/themes/' .
|
|
|
|
$this->currentTheme .
|
|
|
|
'/assets/images/global/' .
|
|
|
|
$file->getFileName(),
|
2024-04-25 21:17:24 +02:00
|
|
|
$imagePath . $file->getFileName()
|
2024-03-16 19:36:51 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
//image is already there, so chill
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//clear test theme css and script directories
|
2024-04-25 21:17:24 +02:00
|
|
|
$styles = glob($cssPath . '*'); // get all file names
|
2024-03-16 19:36:51 +01:00
|
|
|
foreach ($styles as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
|
|
|
unlink($file); // delete file
|
|
|
|
}
|
|
|
|
}
|
2024-04-25 21:17:24 +02:00
|
|
|
$scripts = glob($scriptPath . '*'); // get all file names
|
2024-03-16 19:36:51 +01:00
|
|
|
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/*');
|
2024-04-25 21:17:24 +02:00
|
|
|
if (!is_dir($cssPath)) {
|
|
|
|
mkdir($cssPath, 0755, true);
|
2024-03-16 19:36:51 +01:00
|
|
|
}
|
|
|
|
foreach ($newcss as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
|
|
|
$path = explode('/', $file);
|
2024-04-25 21:17:24 +02:00
|
|
|
copy($file, $cssPath . $path[6]);
|
2024-06-25 23:33:42 +02:00
|
|
|
} else {
|
|
|
|
// if there is a type/font folder, move it
|
|
|
|
$typePath = explode('/', $file);
|
|
|
|
if (is_dir($cssPath . $typePath[6])) {
|
|
|
|
delete_directory($cssPath . $typePath[6]);
|
|
|
|
}
|
|
|
|
rename($file, $cssPath . $typePath[6]);
|
2024-03-16 19:36:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$newjs = glob('../content/themes/' . $this->currentTheme . '/assets/scripts/*');
|
2024-04-25 21:17:24 +02:00
|
|
|
if (!is_dir($scriptPath)) {
|
|
|
|
mkdir($scriptPath, 0755, true);
|
2024-03-16 19:36:51 +01:00
|
|
|
}
|
|
|
|
foreach ($newjs as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
|
|
|
$path = explode('/', $file);
|
2024-04-25 21:17:24 +02:00
|
|
|
copy($file, $scriptPath . $path[6]);
|
2024-03-16 19:36:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|