Skip to content

Commit

Permalink
create asset manager structure
Browse files Browse the repository at this point in the history
  • Loading branch information
puleeno committed Sep 9, 2023
1 parent 02a040b commit 0caeee8
Show file tree
Hide file tree
Showing 36 changed files with 801 additions and 91 deletions.
58 changes: 2 additions & 56 deletions app/Common/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Common;

use App\Core\Helper;
use App\Exceptions\ClassNotFoundException;
use ReflectionClass;

Expand Down Expand Up @@ -38,62 +39,7 @@ public function set($name, $value)
$this->$name = $value;
}

/**
* @param \ReflectionProperty[] $objectProperties
* @return array
*/
protected function extractPropertyNames($objectProperties): array
{
$properties = [];
foreach ($objectProperties as $objectProperty) {
array_push($properties, $objectProperty->getName());
}
return $properties;
}

protected function filterInvalidProperties($valueKeys, $propertyNames): array
{
$invalueProperties = [];
foreach ($valueKeys as $valueKey) {
if (!in_array($valueKey, $propertyNames)) {
array_push($invalueProperties, $valueKey);
}
}
return $invalueProperties;
}

protected function mapOptionValueToObject($rawValue, $className = null)
{
if (is_null($className) || !is_array($rawValue)) {
return $rawValue;
}
if (!class_exists($className, true)) {
throw new ClassNotFoundException($className);
}
$reflectClass = new ReflectionClass($className);
$object = $reflectClass->newInstance();
$properties = $this->extractPropertyNames($reflectClass->getProperties());
$validProperies = $this->filterInvalidProperties(array_keys($rawValue), $properties);

foreach ($rawValue as $key => $value) {
if (!in_array($key, $validProperies)) {
$property = $reflectClass->getProperty($key);
$property->setAccessible(true);
$propertyType = $property->getType();
if (is_array($value) && !empty($propertyType) && class_exists($propertyType->getName())) {
$property->setValue(
$object,
$this->mapOptionValueToObject($value, $propertyType->getName())
);
} else {
$property->setValue($object, $value);
}
$property->setAccessible(false);
}
}

return $object;
}

public function get($name, $defaultValue = null, $mapToObjectClass = null)
{
Expand All @@ -102,7 +48,7 @@ public function get($name, $defaultValue = null, $mapToObjectClass = null)
if (is_null($mapToObjectClass)) {
return $value;
}
return $this->mapOptionValueToObject($value, $mapToObjectClass);
return Helper::convertArrayValuesToObject($value, $mapToObjectClass);
}

public static function __callStatic($name, $arguments)
Expand Down
28 changes: 28 additions & 0 deletions app/Constracts/AssetConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Constracts;

use App\Core\Assets\AssetOptions;

interface AssetConstract
{
public function setDeps($deps): AssetConstract;

public function setOptions(AssetOptions $options): AssetConstract;

public function setVersion($version): AssetConstract;

public function setPriority(int $priority): AssetConstract;

public function setAssetType(AssetTypeEnum $assetType): AssetConstract;

public function isValid(): bool;

public function getId();

public function getAssetType(): AssetTypeEnum;

public function enqueue();

public function printHtml();
}
7 changes: 7 additions & 0 deletions app/Constracts/AssetHtmlConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Constracts;

interface AssetHtmlConstract
{
}
7 changes: 7 additions & 0 deletions app/Constracts/AssetIconConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Constracts;

interface AssetIconConstract
{
}
80 changes: 80 additions & 0 deletions app/Constracts/AssetTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Constracts;

final class AssetTypeEnum
{
private static $inited = false;

protected static AssetTypeEnum $CSS;
protected static AssetTypeEnum $JS;
protected static AssetTypeEnum $ICON;
protected static AssetTypeEnum $FONT;
protected static AssetTypeEnum $STYLE;
protected static AssetTypeEnum $SCRIPT;

protected $type;

protected function __construct($type)
{
$this->type = $type;
}

public static function init()
{
if (static::$inited === false) {
self::$CSS = new self('css');
self::$JS = new self('js');
self::$ICON = new self('icon');
self::$FONT = new self('font');
self::$STYLE = new self('style');
self::$SCRIPT = new self('script');

// Make inited flag is true
static::$inited = true;
}
}

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

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

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

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

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

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

/**
* @return string
*/
public function getType()
{
return $this->type;
}

public function __toString()
{
return $this->getType();
}
}
12 changes: 12 additions & 0 deletions app/Constracts/ExternalAssetConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Constracts;

use App\Core\Assets\AssetUrl;

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

public function getUrl();
}
7 changes: 7 additions & 0 deletions app/Constracts/IconTypeConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Constracts;

interface IconTypeConstract
{
}
1 change: 0 additions & 1 deletion app/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
use Slim\Middleware\BodyParsingMiddleware;
use Slim\Middleware\ErrorMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\MiddlewareDispatcher;
use Slim\Routing\RouteResolver;
use Slim\Routing\RouteRunner;

Expand Down
75 changes: 75 additions & 0 deletions app/Core/Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Core;

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

abstract class Asset implements AssetConstract
{
protected $id;

protected AssetTypeEnum $assetType;
protected AssetOptions $options;

protected $deps = [];
protected $version = null;
protected $priority = 10;

public function __construct($id)
{
$this->id = $id;
}

public function isValid(): bool
{
return !empty($this->id);
}

public function setAssetType(AssetTypeEnum $assetType): AssetConstract
{
$this->assetType = $assetType;

return $this;
}

public function getAssetType(): AssetTypeEnum
{
return $this->assetType;
}

public function setDeps($deps): AssetConstract
{
if (is_array($deps)) {
$this->deps = $deps;
}
return $this;
}

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

return $this;
}

public function setPriority(int $priority): AssetConstract
{
$this->priority = $priority;

return $this;
}

public function setVersion($version): AssetConstract
{
$this->version = $version;

return $this;
}

public function getId()
{
return $this->id;
}
}
Loading

0 comments on commit 0caeee8

Please sign in to comment.