assets class

This commit is contained in:
Kavalar 2025-01-09 17:13:22 +03:00
parent 32d1e93e73
commit 64dad0aaf9
3 changed files with 95 additions and 7 deletions

66
kernel/Assets.php Normal file
View File

@ -0,0 +1,66 @@
<?php
namespace kernel;
class Assets
{
protected array $jsHeader = [];
protected array $jsBody = [];
protected array $css = [];
protected string $resourceURI = "/resource";
public function __construct(string $resourceURI)
{
$this->setResourceURI($resourceURI);
$this->createCSS();
$this->createJS();
}
protected function createCSS(){}
protected function createJS(){}
public function setResourceURI(string $resourceURI): void
{
$this->resourceURI = $resourceURI;
}
public function registerJS(string $slug, string $resource, bool $body = true, bool $addResourceURI = true): void
{
$resource = $addResourceURI ? $this->resourceURI . $resource : $resource;
if ($body) {
$this->jsBody[$slug] = $resource;
} else {
$this->jsHeader[$slug] = $resource;
}
}
public function registerCSS(string $slug, string $resource, bool $addResourceURI = true): void
{
$resource = $addResourceURI ? $this->resourceURI . $resource : $resource;
$this->css[$slug] = $resource;
}
public function getJSAsStr(bool $body = true): void
{
if ($body) {
foreach ($this->jsBody as $key => $item){
echo "<script src='$item'></script>";
}
}
else {
foreach ($this->jsHeader as $key => $item){
echo "<script src='$item'></script>";
}
}
}
public function getCSSAsSTR(): void
{
foreach ($this->css as $key => $item){
echo "<link rel='stylesheet' href='$item'>";
}
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace kernel\admin_themes\default;
use kernel\Assets;
class DefaultAdminThemeAssets extends Assets
{
protected function createJS(): void
{
$this->registerJS(slug: "jquery", resource: "/js/jquery.min.js");
$this->registerJS(slug: "popper", resource: "/js/popper.js");
$this->registerJS(slug: "bootstrap", resource: "/js/bootstrap.min.js");
$this->registerJS(slug: "main", resource: "/js/main.js");
}
protected function createCSS()
{
$this->registerCSS(slug: "font-awesome", resource: "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css", addResourceURI: false);
$this->registerCSS(slug: "bootstrap", resource: "/css/bootstrap.min.css");
$this->registerCSS(slug: "style", resource: "/css/style.css");
}
}

View File

@ -6,6 +6,7 @@
* @var \kernel\CgView $view
*/
\Josantonius\Session\Facades\Session::start();
$assets = new \kernel\admin_themes\default\DefaultAdminThemeAssets($resources)
?>
<!doctype html>
<html lang="en">
@ -17,9 +18,8 @@
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700,800,900" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
<link rel="stylesheet" href="<?= $resources ?>/css/bootstrap.min.css">
<link rel="stylesheet" href="<?= $resources ?>/css/style.css">
<?php $assets->getCSSAsSTR(); ?>
<?php $assets->getJSAsStr(body: false); ?>
</head>
<body>
@ -92,9 +92,6 @@
</div>
</div>
<script src="<?= $resources ?>/js/jquery.min.js"></script>
<script src="<?= $resources ?>/js/popper.js"></script>
<script src="<?= $resources ?>/js/bootstrap.min.js"></script>
<script src="<?= $resources ?>/js/main.js"></script>
<?php $assets->getJSAsStr(); ?>
</body>
</html>