Skip to content

Commit

Permalink
feat: add ResourceChannel, ResourceLanguage, ResourceLocation, Cached…
Browse files Browse the repository at this point in the history
…ResourceLoader, ResourceHierarchyWalker and ResourceHierarchyFinder

* feat: add ResourceHierarchyLoader::findRecursive() and new CachedResourceLoader

* feat: work in process

* docs: typo

* refactor: create findRecursiveInternal for internal method signatur

* refactor: remove $parentPath for findResource() callable

* feat: lang support

* chore: normalize composer.json

* fix: add empty directories for tests

* fix: resource base location

* Update src/ResourceHierarchyLoader.php

Co-authored-by: Mario Schäper <95750382+sitepark-schaeper@users.noreply.github.com>

* feat: ResourceHierarchyWalker implementation

* refactor: findRecursive replaced

* docs: add ResourceHierarchyWalker class doc

* refactor: ResourceHierarchyLoader extends ResourceLoader

* feat: allow string for ResourceHierarchyWalker::walk

* Update src/Loader/SiteKitLoader.php

Co-authored-by: Mario Schäper <95750382+sitepark-schaeper@users.noreply.github.com>

* fix: phpstan error

* refactor: use shortened syntax

* refactor: make code easier to understand

* refactor: rename $resourceChannel to $resourceChannelFactory

* refactor: call $this->baseLocator->locate() in constructor

* refactor: simplify code

* docs: fix doc

* refactor: rename ResourceChannel::preview to ResourceChannel:isPreview

* style: update newlines

* refactor: SiteKitResourceChannelFactory::findContextPhpFile()

* refactor: remove ResourceBaseLocator

* refactor: ResourceLocation and ResourceLanguage

* fix: phpcs fixes

* test: refactor resource creation

* refactor: code review suggestion

---------

Co-authored-by: Mario Schäper <95750382+sitepark-schaeper@users.noreply.github.com>
  • Loading branch information
sitepark-veltrup and sitepark-schaeper authored Apr 18, 2024
1 parent 304c8cf commit c89df86
Show file tree
Hide file tree
Showing 79 changed files with 1,609 additions and 973 deletions.
8 changes: 8 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
# Create a regex for a conventional commit.
convetional_commit_regex="^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-z \-]+\))?!?: .+$"

# Accept Merge commits
merge_msg_regex="^Merge branch '.+'.+$"

# Get the commit message (the parameter we're given is just the path to the
# temporary file which holds the message).
commit_message=$(cat "$1")

if [[ "$commit_message" =~ $merge_msg_regex ]]; then
echo -e "\e[32mAccept Merge commits...\e[0m"
exit 0
fi

# Check the message, if we match, all good baby.
if [[ "$commit_message" =~ $convetional_commit_regex ]]; then
echo -e "\e[32mCommit message meets Conventional Commit standards...\e[0m"
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
}
],
"require": {
"php": ">=8.1 <8.4.0"
"php": ">=8.1 <8.4.0",
"ext-intl": "*"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
Expand Down
5 changes: 3 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/Exception/InvalidResourceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Atoolo\Resource\Exception;

use Atoolo\Resource\ResourceLocation;

/**
* This exception is used when a resource is invalid. This can have the
* following reasons:
Expand All @@ -14,19 +16,19 @@
class InvalidResourceException extends \RuntimeException
{
public function __construct(
private readonly string $location,
private readonly ResourceLocation $location,
string $message = "",
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct(
$location . ': ' . $message,
$location->__toString() . ': ' . $message,
$code,
$previous
);
}

public function getLocation(): string
public function getLocation(): ResourceLocation
{
return $this->location;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Exception/ResourceNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@

namespace Atoolo\Resource\Exception;

use Atoolo\Resource\ResourceLocation;

/**
* This exception is used if no resource could be found via the specified
* location.
*/
class ResourceNotFoundException extends \RuntimeException
{
public function __construct(
private readonly string $location,
private readonly ResourceLocation $location,
string $message = "",
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct(
$location . ': ' . $message,
$location->__toString() . ': ' . $message,
$code,
$previous
);
}

public function getLocation(): string
public function getLocation(): ResourceLocation
{
return $this->location;
}
Expand Down
20 changes: 10 additions & 10 deletions src/Loader/CachedResourceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Atoolo\Resource\Exception\ResourceNotFoundException;
use Atoolo\Resource\Resource;
use Atoolo\Resource\ResourceLoader;
use Atoolo\Resource\ResourceLocation;

/**
* The CachedResourceLoader class is used to load resources
* from a given location and cache them for future use.
* The cache is stored in memory and is not persistent.
*/
class CachedResourceLoader implements ResourceLoader
{
Expand All @@ -28,20 +30,18 @@ public function __construct(
* @throws InvalidResourceException
* @throws ResourceNotFoundException
*/
public function load(string $location): Resource
public function load(ResourceLocation $location): Resource
{
if (isset($this->cache[$location])) {
return $this->cache[$location];
}

$resource = $this->resourceLoader->load($location);
$this->cache[$location] = $resource;
return $resource;
$key = $location->__toString();
return $this->cache[$key] ??= $this->resourceLoader->load(
$location
);
}

public function exists(string $location): bool
public function exists(ResourceLocation $location): bool
{
if (isset($this->cache[$location])) {
$key = $location->__toString();
if (isset($this->cache[$key])) {
return true;
}
return $this->resourceLoader->exists($location);
Expand Down
31 changes: 0 additions & 31 deletions src/Loader/ServerVarResourceBaseLocator.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Loader/SiteKit/ResourceStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ResourceStub
*/
public function init(array $data): void
{
$this->data['init'] = $data;
$this->data = array_merge($this->data, $data);
}

/**
Expand Down
Loading

0 comments on commit c89df86

Please sign in to comment.