89 lines
2.3 KiB
PHP
89 lines
2.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace Venom\Views;
|
|
|
|
|
|
use Venom\Config;
|
|
|
|
class Asset
|
|
{
|
|
static ?Asset $instance = null;
|
|
private ?VenomRenderer $renderer = null;
|
|
private array $jsFiles = [];
|
|
private array $cssFiles = [];
|
|
|
|
private function __construct()
|
|
{
|
|
}
|
|
|
|
public static function get(): Asset
|
|
{
|
|
if (self::$instance == null) {
|
|
self::$instance = new Asset();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function addJS(string $name, string $filepath, $pos = 99999)
|
|
{
|
|
$this->jsFiles[$name] = [
|
|
'file' => $filepath,
|
|
'pos' => $pos
|
|
];
|
|
}
|
|
|
|
public function addCSS(string $name, string $filepath, $pos = 99999)
|
|
{
|
|
$this->cssFiles[$name] = [
|
|
'file' => $filepath,
|
|
'pos' => $pos
|
|
];
|
|
}
|
|
|
|
public function getImagePath(string $filepath, bool $useAbsolute = false)
|
|
{
|
|
$preDir = '/content';
|
|
if ($useAbsolute) {
|
|
$preDir = Config::getInstance()->getBaseUrl() . $preDir;
|
|
}
|
|
}
|
|
|
|
public function setRenderer(VenomRenderer $renderer)
|
|
{
|
|
$this->renderer = $renderer;
|
|
}
|
|
|
|
//this will output all js files! sorted by position
|
|
public function renderJS()
|
|
{
|
|
usort($this->jsFiles, function ($a, $b) {
|
|
return $a['pos'] <=> $b['pos'];
|
|
});
|
|
$theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()['theme'] . '/js/');
|
|
foreach ($this->jsFiles as $key => $file) {
|
|
echo '<script src="' . $theme . $file['file'] . '" id="js-' . $key . '"></script>';
|
|
}
|
|
}
|
|
|
|
public function renderCSS()
|
|
{
|
|
usort($this->cssFiles, function ($a, $b) {
|
|
return $a['pos'] <=> $b['pos'];
|
|
});
|
|
$theme = $this->getPath('/theme/' . Config::getInstance()->getRenderer()['theme'] . '/css/');
|
|
foreach ($this->cssFiles as $key => $file) {
|
|
echo '<link rel="stylesheet" href="' . $theme . $file['file'] . '" id="css-' . $key . '">';
|
|
}
|
|
}
|
|
|
|
private function getPath($base)
|
|
{
|
|
$preDir = $base;
|
|
$baseUrl = Config::getInstance()->getBaseUrl();
|
|
if ($baseUrl != '') {
|
|
$preDir = Config::getInstance()->getBaseUrl() . $preDir;
|
|
}
|
|
return $preDir;
|
|
}
|
|
} |