fipamo/app/Services/AssetService.php
ro efb2e4f0bd
created asset service class to handle file moving
theme controller was getting top heavy, so an asset service class was
plugged in to handle moving assets around for theme testing and
eventually to production when HTML rendering is set up
2024-03-16 12:36:51 -06:00

93 lines
3.1 KiB
PHP

<?php
namespace App\Services;
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/';
$this->themes = $themeService;
$this->currentTheme = $this->themes->getCurrentTheme();
}
public function getCurrentTheme()
{
return $this->currentTheme;
}
public function moveToTheme()
{
//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($this->themeTestImagePath)) {
mkdir($this->themeTestImagePath, 0755, true);
}
if (!is_file($this->themeTestImagePath . $file->getFileName())) {
copy(
'../content/themes/' .
$this->currentTheme .
'/assets/images/global/' .
$file->getFileName(),
$this->themeTestImagePath . $file->getFileName()
);
} else {
//image is already there, so chill
}
//print $file->getFilename() . "\n";
}
//clear test theme css and script directories
$styles = glob($this->themeTestCSSPath . '*'); // get all file names
foreach ($styles as $file) { // iterate files
if (is_file($file)) {
unlink($file); // delete file
}
}
$scripts = glob($this->themeTestScriptsPath . '*'); // 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($this->themeTestCSSPath)) {
mkdir($this->themeTestCSSPath, 0755, true);
}
foreach ($newcss as $file) { // iterate files
if (is_file($file)) {
$path = explode('/', $file);
copy($file, $this->themeTestCSSPath . $path[6]);
}
}
$newjs = glob('../content/themes/' . $this->currentTheme . '/assets/scripts/*');
if (!is_dir($this->themeTestScriptsPath)) {
mkdir($this->themeTestScriptsPath, 0755, true);
}
foreach ($newjs as $file) { // iterate files
if (is_file($file)) {
$path = explode('/', $file);
copy($file, $this->themeTestScriptsPath . $path[6]);
}
}
}
}