Skip to content

Commit

Permalink
Merge pull request #8 from puleeno/ft/init-asset-manager
Browse files Browse the repository at this point in the history
Ft/init asset manager
  • Loading branch information
puleeno authored Sep 17, 2023
2 parents 02a040b + d57fdb0 commit 099fba2
Show file tree
Hide file tree
Showing 49 changed files with 1,480 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ jobs:
# if: matrix.analysis
# run: vendor/bin/phpstan

- name: Tests
run: vendor/bin/phpunit --coverage-clover clover.xml
# - name: Tests
# run: vendor/bin/phpunit --coverage-clover clover.xml

# - name: Upload coverage results to Coveralls
# if: matrix.analysis
Expand Down
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
90 changes: 90 additions & 0 deletions app/Constracts/AssetTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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 $INIT_SCRIPT;
protected static AssetTypeEnum $EXECUTE_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');

// 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;
}
}

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 INIT_SCRIPT(): AssetTypeEnum
{
return static::$INIT_SCRIPT;
}

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

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

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

namespace App\Constracts\Assets;

use App\Constracts\AssetTypeEnum;
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 isEnqueue(): bool;

public function enqueue();

public function printHtml();

public function getOptions(): ?AssetOptionsConstract;

public function getOption($name, $defaultValue = null, $classObject = null);
}
12 changes: 12 additions & 0 deletions app/Constracts/Assets/AssetExternalConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Constracts\Assets;

use App\Core\Assets\AssetUrl;

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

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

namespace App\Constracts\Assets;

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

namespace App\Constracts\Assets;

interface AssetIconConstract
{
}
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;
}
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
{
}
10 changes: 10 additions & 0 deletions app/Constracts/MiddlewareConstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Constracts;

use Psr\Http\Server\MiddlewareInterface;

interface MiddlewareConstract extends MiddlewareInterface
{
public function getPriority(): int;
}
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
92 changes: 92 additions & 0 deletions app/Core/Asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace App\Core;

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

abstract class Asset implements AssetConstract
{
use AssetBaseTrait;

protected $id;

protected AssetTypeEnum $assetType;
protected AssetOptionsConstract $options;

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

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(AssetOptionsConstract $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;
}

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

return $this;
}

public function isEnqueue(): bool
{
return $this->isEnqueue;
}
}
Loading

0 comments on commit 099fba2

Please sign in to comment.