fipamo/app/Services/Assets/AssetService.php

108 lines
3.8 KiB
PHP
Raw Normal View History

<?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->themeTestImagePath = '../public/theme/images/global/';
$this->themeTestCSSPath = '../public/theme/css/theme/';
$this->themeTestScriptsPath = '../public/theme/scripts/theme/';
$this->themeImagePath = '../public/assets/images/global/';
$this->themeCSSPath = '../public/assets/css/theme/';
$this->themeScriptsPath = '../public/assets/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);
if (is_dir($cssPath . $typePath[6])) {
delete_directory($cssPath . $typePath[6]);
}
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]);
}
}
}
}