-
Notifications
You must be signed in to change notification settings - Fork 0
feat: introducing resource views #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sitepark-becker
wants to merge
13
commits into
main
Choose a base branch
from
feature/resource-views
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4c31385
feat: introducing resource views
sitepark-becker 512f745
refactor: rename ResourceResolver to ResourceViewContributor
sitepark-becker 17b5c53
refactor(ResourceViewFactory): get should use tryGet, eliminating try…
sitepark-becker 208b3b6
refactor: renamed some methods
sitepark-becker 0aec704
refactor(ResourceViewBuilder): added custom class for feature bag
sitepark-becker cd06e54
refactor: removed AbstractResource & added SiteKitResource
sitepark-becker 4251f82
refactor(SiteKitResource): name, objectType, data should be declared …
sitepark-becker eeb1f2a
feat: added some basic resource features & contributors
sitepark-becker 04d53f6
feat: remove sitekit resource view contributors for now
sitepark-becker 745d41f
feat: modified respirce features
sitepark-becker 46e52ea
fix(SiteKitResource): dont redeclare properties, use @property-read t…
sitepark-becker bf376a2
feat(SiteKitLoader): set return type of load to SiteKitResource
sitepark-becker 1ed4e69
Merge branch 'main' into feature/resource-views
sitepark-veltrup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| /** | ||
| * Represents a resource with basic metadata (location, ID, name, language). | ||
| * | ||
| * This is the minimal domain object that contributors will use | ||
| * to decide which features to provide for a resource view. | ||
| */ | ||
| class AbstractResource | ||
| { | ||
| public function __construct( | ||
| public readonly string $location, | ||
| public readonly string $id, | ||
| public readonly string $name, | ||
| public readonly ResourceLanguage $lang, | ||
| ) {} | ||
|
|
||
| public function toLocation(): ResourceLocation | ||
| { | ||
| return ResourceLocation::of($this->location, $this->lang); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource\Exception; | ||
|
|
||
| use Atoolo\Resource\ResourceLocation; | ||
| use Atoolo\Resource\ResourceView; | ||
|
|
||
| /** | ||
| * This exception is used when a resource feature is requested that | ||
| * does not exist in a given resource view. | ||
| */ | ||
| class MissingFeatureException extends \LogicException | ||
| { | ||
| public function __construct( | ||
| public readonly string $feature, | ||
| public readonly ResourceView $resourceView, | ||
| int $code = 0, | ||
| ?\Throwable $previous = null, | ||
| ) { | ||
| parent::__construct( | ||
| "Feature $feature not available.", | ||
| $code, | ||
| $previous, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| /** | ||
| * Marker interface for features that can be attached to a ResourceView. | ||
| * | ||
| * Each feature encapsulates a specific aspect or capability of a resource. | ||
| */ | ||
| interface ResourceFeature {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| use Atoolo\Resource\Exception\MissingFeatureException; | ||
| use Atoolo\Resource\ResourceFeature; | ||
| use Psr\Log\LoggerAwareInterface; | ||
| use Psr\Log\LoggerAwareTrait; | ||
|
|
||
| /** | ||
| * A view of a resource, composed of features provided by contributors. | ||
| * | ||
| * Features are created lazily via factories and cached once resolved. | ||
| */ | ||
| final class ResourceView implements LoggerAwareInterface | ||
| { | ||
| use LoggerAwareTrait; | ||
|
|
||
| /** | ||
| * @var array<class-string<ResourceFeature>, ResourceFeature> | ||
| */ | ||
| private array $resolved = []; | ||
|
|
||
| /** | ||
| * @param array<class-string<ResourceFeature>, callable():ResourceFeature> $factories | ||
| */ | ||
| public function __construct( | ||
| private array $factories, | ||
| ) {} | ||
|
|
||
| /** | ||
| * Returns the feature of the given type, constructing it if necessary. | ||
| * | ||
| * @template T of ResourceFeature | ||
| * @param class-string<T> $feature Fully-qualified class name of the feature. | ||
| * @return T | ||
| * @throws MissingFeatureException if the feature is not registered. | ||
| * @throws \Throwable if feature construction fails. | ||
| */ | ||
| public function get(string $feature): ResourceFeature | ||
| { | ||
| if (!isset($this->resolved[$feature])) { | ||
| if (!isset($this->factories[$feature])) { | ||
| throw new MissingFeatureException($feature, $this); | ||
| } | ||
| $this->resolved[$feature] = ($this->factories[$feature])(); | ||
| } | ||
| /** @var T $resolved */ | ||
| $resolved = $this->resolved[$feature]; | ||
| return $resolved; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to return a feature. If unavailable, logs a warning and returns null. | ||
| * | ||
| * @template T of ResourceFeature | ||
| * @param class-string<T> $feature | ||
| * @return T|null | ||
| */ | ||
| public function tryGet(string $feature): ?ResourceFeature | ||
| { | ||
| try { | ||
| return $this->get($feature); | ||
| } catch (\Throwable $th) { | ||
| $this->logger?->warning('failed to get resource feature', [ | ||
| 'feature' => $feature, | ||
| 'error' => $th, | ||
| ]); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if the view contains a factory for the given feature type. | ||
| * | ||
| * @template T of ResourceFeature | ||
| * @param class-string<T> $feature | ||
| */ | ||
| public function has(string $feature): bool | ||
| { | ||
| return isset($this->factories[$feature]); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the view has at least one of the given features. | ||
| * | ||
| * @template T of ResourceFeature | ||
| * @param class-string<T>[] $features | ||
| */ | ||
| public function hasAny(array $features): bool | ||
| { | ||
| foreach ($features as $feature) { | ||
| if ($this->has($feature)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Instantiates and resolves all registered features eagerly. | ||
| * Useful for early failure detection or warm-up. | ||
| */ | ||
| public function preloadAll(): void | ||
| { | ||
| foreach ($this->factories as $feature => $callback) { | ||
| if (isset($this->resolved[$feature])) { | ||
| continue; | ||
| } | ||
| try { | ||
| $this->resolved[$feature] = $callback(); | ||
| } catch (\Throwable $th) { | ||
| $this->logger?->warning('failed to preload resource feature', [ | ||
| 'feature' => $feature, | ||
| 'error' => $th, | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| /** | ||
| * Contributes features to multiple ResourceViews at once. | ||
| * | ||
| * This allows batch contributors to optimize expensive operations | ||
| * (e.g., database or API lookups) by handling multiple resources together. | ||
| */ | ||
| interface ResourceViewBatchContributor | ||
| { | ||
| /** | ||
| * @param AbstractResource[] $resources | ||
| * @param array<string,ResourceViewBuilder> $builders keyed by resource ID. | ||
| */ | ||
| public function batchContribute(array $resources, array $builders): void; | ||
| } | ||
sitepark-becker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| use Atoolo\Resource\ResourceFeature; | ||
|
|
||
| final class ResourceViewBuilder | ||
| { | ||
| /** | ||
| * @var array<class-string<ResourceFeature>, array{priority:int, factory:(callable():ResourceFeature)}> | ||
| */ | ||
| private array $bag = []; | ||
sitepark-becker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @template T of ResourceFeature | ||
| * @param class-string<T> $feature | ||
| * @param ResourceFeature|(callable():T) $value | ||
| */ | ||
| public function add(string $feature, ResourceFeature|callable $value, int $priority = 0): void | ||
| { | ||
| $cur = $this->bag[$feature] ?? null; | ||
| $factory = $value instanceof ResourceFeature | ||
| ? fn() => $value | ||
| : $value; | ||
| if ($cur === null || $priority > $cur['priority']) { | ||
| $this->bag[$feature] = ['priority' => $priority, 'factory' => $factory]; | ||
| } | ||
| } | ||
|
|
||
| public function build(): ResourceView | ||
| { | ||
| $features = []; | ||
| foreach ($this->bag as $class => $entry) { | ||
| $features[$class] = $entry['factory']; | ||
| } | ||
| return new ResourceView($features); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| /** | ||
| * Contributes features to a ResourceView for supported resources. | ||
| */ | ||
| interface ResourceViewContributor | ||
| { | ||
| /** | ||
| * Checks whether this contributor supports a given resource. | ||
| */ | ||
| public function supports(AbstractResource $r): bool; | ||
|
|
||
| /** | ||
| * Adds features to the given ResourceViewBuilder for a supported resource. | ||
| */ | ||
| public function contribute(AbstractResource $r, ResourceViewBuilder $b): void; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Atoolo\Resource; | ||
|
|
||
| /** | ||
| * Factory for creating ResourceViews using registered contributors. | ||
| */ | ||
| final class ResourceViewFactory | ||
| { | ||
| /** | ||
| * @param iterable<ResourceViewContributor> $contributors | ||
| */ | ||
| public function __construct(private iterable $contributors) {} | ||
|
|
||
| /** | ||
| * Creates a ResourceView for a given resource. | ||
| */ | ||
| public function create(AbstractResource $resource): ResourceView | ||
| { | ||
| $builder = new ResourceViewBuilder(); | ||
| foreach ($this->contributors as $contributor) { | ||
| if ($contributor->supports($resource)) { | ||
| $contributor->contribute($resource, $builder); | ||
| } | ||
| } | ||
| return $builder->build(); | ||
| } | ||
|
|
||
| /** | ||
| * Builds ResourceViews for multiple resources in one pass. | ||
| * | ||
| * Contributors that implement ResourceViewBatchContributor | ||
| * can optimize batch creation | ||
| * | ||
| * @param AbstractResource[] $resources | ||
| * @return array<string,ResourceView> keyed by resource ID | ||
| */ | ||
| public function createBatch(array $resources): array | ||
sitepark-becker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $builders = []; | ||
| foreach ($resources as $r) { | ||
| $builders[$r->id] = new ResourceViewBuilder(); | ||
| } | ||
| foreach ($this->contributors as $contributor) { | ||
| $supportedResources = []; | ||
| $buildersForSupportedResources = []; | ||
| foreach ($resources as $r) { | ||
| if ($contributor->supports($r)) { | ||
| $supportedResources[] = $r; | ||
| $buildersForSupportedResources[$r->id] = $builders[$r->id]; | ||
| } | ||
| } | ||
| if (empty($supportedResources)) { | ||
| continue; | ||
| } | ||
| if ($contributor instanceof ResourceViewBatchContributor) { | ||
| $contributor->batchContribute($supportedResources, $buildersForSupportedResources); | ||
| } else { | ||
| foreach ($supportedResources as $r) { | ||
| $contributor->contribute($r, $buildersForSupportedResources[$r->id]); | ||
| } | ||
| } | ||
| } | ||
| return array_map(fn($b) => $b->build(), $builders); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.