Skip to content

Commit

Permalink
Render asset to HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
puleeno committed Sep 17, 2023
1 parent aacc47b commit 10480f3
Show file tree
Hide file tree
Showing 25 changed files with 636 additions and 48 deletions.
18 changes: 14 additions & 4 deletions app/Constracts/AssetTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ final class AssetTypeEnum
protected static AssetTypeEnum $ICON;
protected static AssetTypeEnum $FONT;
protected static AssetTypeEnum $STYLE;
protected static AssetTypeEnum $SCRIPT;
protected static AssetTypeEnum $INIT_SCRIPT;
protected static AssetTypeEnum $EXECUTE_SCRIPT;

protected $type;

Expand All @@ -28,7 +29,11 @@ public static function init()
self::$ICON = new self('icon');
self::$FONT = new self('font');
self::$STYLE = new self('style');
self::$SCRIPT = new self('script');

// phpcs:ignoreFile
self::$INIT_SCRIPT = new self('init_script');
// phpcs:ignoreFile
self::$EXECUTE_SCRIPT = new self('execute_script');

// Make inited flag is true
static::$inited = true;
Expand Down Expand Up @@ -60,9 +65,14 @@ public static function STYLE(): AssetTypeEnum
return static::$STYLE;
}

public static function SCRIPT(): AssetTypeEnum
public static function INIT_SCRIPT(): AssetTypeEnum
{
return static::$INIT_SCRIPT;
}

public static function EXECUTE_SCRIPT(): AssetTypeEnum
{
return static::$SCRIPT;
return static::$EXECUTE_SCRIPT;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace App\Constracts;
namespace App\Constracts\Assets;

use App\Constracts\AssetTypeEnum;
use App\Core\Assets\AssetOptions;

interface AssetConstract
Expand All @@ -22,7 +23,13 @@ public function getId();

public function getAssetType(): AssetTypeEnum;

public function isEnqueue(): bool;

public function enqueue();

public function printHtml();

public function getOptions(): ?AssetOptionsConstract;

public function getOption($name, $defaultValue = null, $classObject = null);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace App\Constracts;
namespace App\Constracts\Assets;

use App\Core\Assets\AssetUrl;

interface ExternalAssetConstract extends AssetConstract
interface AssetExternalConstract extends AssetConstract
{
public function setUrl(AssetUrl $url): self;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Constracts;
namespace App\Constracts\Assets;

interface AssetHtmlConstract
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Constracts;
namespace App\Constracts\Assets;

interface AssetIconConstract
{
Expand Down
8 changes: 8 additions & 0 deletions app/Constracts/Assets/AssetOptionsConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Constracts\Assets;

interface AssetOptionsConstract
{
public static function parseOptionFromArray($options): AssetOptionsConstract;
}
8 changes: 8 additions & 0 deletions app/Constracts/Assets/AssetScriptConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Constracts\Assets;

interface AssetScriptConstract
{
public function isFooterScript(): bool;
}
31 changes: 24 additions & 7 deletions app/Core/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace App\Core;

use App\Constracts\AssetConstract;
use App\Constracts\Assets\AssetConstract;
use App\Constracts\Assets\AssetOptionsConstract;
use App\Constracts\AssetTypeEnum;
use App\Core\Assets\AssetOptions;
use App\Traits\AssetBaseTrait;

abstract class Asset implements AssetConstract
{
use AssetBaseTrait;

protected $id;

protected AssetTypeEnum $assetType;
protected AssetOptions $options;
protected AssetOptionsConstract $options;

protected $deps = [];
protected $version = null;
protected $priority = 10;
protected $deps = [];
protected $version = null;
protected $priority = 10;
protected $isEnqueue = false;
protected $isRendered = false;

public function __construct($id)
{
Expand Down Expand Up @@ -47,7 +52,7 @@ public function setDeps($deps): AssetConstract
return $this;
}

public function setOptions(AssetOptions $assetOptions): AssetConstract
public function setOptions(AssetOptionsConstract $assetOptions): AssetConstract
{
$this->options = $assetOptions;

Expand All @@ -72,4 +77,16 @@ public function getId()
{
return $this->id;
}

public function enqueue(): self
{
$this->isEnqueue = true;

return $this;
}

public function isEnqueue(): bool
{
return $this->isEnqueue;
}
}
65 changes: 52 additions & 13 deletions app/Core/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Core;

use App\Constracts\AssetConstract;
use App\Constracts\Assets\AssetConstract;
use App\Constracts\AssetTypeEnum;
use App\Constracts\ExternalAssetConstract;
use App\Constracts\Assets\AssetExternalConstract;
use App\Core\Assets\AssetOptions;
use App\Core\Assets\AssetUrl;
use App\Core\Assets\Bucket;
Expand Down Expand Up @@ -41,7 +41,7 @@ public static function create(
$priority = 10
): AssetConstract {
$asset = Helper::createAssetByAssetType($id, $assetType);
if ($asset instanceof ExternalAssetConstract) {
if ($asset instanceof AssetExternalConstract) {
$asset->setUrl($url);
}
$asset->setDeps($deps);
Expand All @@ -60,13 +60,21 @@ public static function registerAsset(
$version = null,
AssetOptions $assetOptions = null,
$priority = 10
): self {
): AssetConstract {
$asset = static::create(
(string) $id,
$url,
$assetType,
$deps,
$version,
$assetOptions,
$priority
);
$instance = static::getInstance();
$instance->getFrontendBucket()
->addAsset(
static::create($id, $url, $assetType, $deps, $version, $assetOptions, $priority)
);
return $instance;
->addAsset($asset);

return $asset;
}

public static function registerBackendAsset(
Expand All @@ -77,13 +85,16 @@ public static function registerBackendAsset(
$version = null,
AssetOptions $assetOptions = null,
$priority = 10
): self {
): AssetConstract {
/**
* @var \App\Core\Assets\JavaScript
*/
$asset = static::create($id, $url, $assetType, $deps, $version, $assetOptions, $priority);
$instance = static::getInstance();
$instance->getBackendBucket()
->addAsset(
static::create($id, $url, $assetType, $deps, $version, $assetOptions, $priority)
);
return $instance;
->addAsset($asset);

return $asset;
}

public function getFrontendBucket(): Bucket
Expand All @@ -96,27 +107,55 @@ public function getBackendBucket(): Bucket
return $this->backendBucket;
}

protected function getActiveBucket(): Bucket
{
return !Helper::isDashboard()
? $this->getFrontendBucket()
: $this->getBackendBucket();
}

public function printInitHeadScripts()
{
foreach ($this->getActiveBucket()->getInitScripts(false) as $initScript) {
$initScript->printHtml();
}
}

public function printHeadAssets()
{
foreach ($this->getActiveBucket()->getStylesheets(true) as $css) {
$css->printHtml();
}
foreach ($this->getActiveBucket()->getJs(false, true) as $js) {
$js->printHtml();
}
}

public function printExecuteHeadScripts()
{
foreach ($this->getActiveBucket()->getExecuteScripts(false) as $executeScript) {
$executeScript->printHtml();
}
}

public function printFooterInitScripts()
{
foreach ($this->getActiveBucket()->getInitScripts(true) as $initScript) {
$initScript->printHtml();
}
}

public function printFooterAssets()
{
foreach ($this->getActiveBucket()->getJs(true, true) as $js) {
$js->printHtml();
}
}

public function executeFooterScripts()
{
foreach ($this->getActiveBucket()->getExecuteScripts(true) as $executeScript) {
$executeScript->printHtml();
}
}
}
22 changes: 19 additions & 3 deletions app/Core/Assets/AssetOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@

namespace App\Core\Assets;

use App\Constracts\Assets\AssetOptionsConstract;
use App\Core\Helper;

class AssetOptions
class AssetOptions implements AssetOptionsConstract
{
public static function parseOptionFromArray($options): AssetOptions
public static function parseOptionFromArray($options): AssetOptionsConstract
{
return Helper::convertArrayValuesToObject(
$options,
AssetOptions::class
static::class
);
}

public function __get($name)
{
if (property_exists($this, $name)) {
return $this->$name;
}
}

public function __call($name, $arguments)
{
if (property_exists($this, $name)) {
return $this->$name;
}
return array_get($arguments, 0, null);
}
}
8 changes: 8 additions & 0 deletions app/Core/Assets/AssetScriptOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Core\Assets;

class AssetScriptOptions extends AssetOptions
{
protected $isFooter = false;
}
7 changes: 7 additions & 0 deletions app/Core/Assets/AssetStylesheetOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Core\Assets;

class AssetStylesheetOptions extends AssetOptions
{
}
Loading

0 comments on commit 10480f3

Please sign in to comment.