feat: introducing resource capabilities (WIP)#43
Closed
sitepark-becker wants to merge 1 commit intomainfrom
Closed
feat: introducing resource capabilities (WIP)#43sitepark-becker wants to merge 1 commit intomainfrom
sitepark-becker wants to merge 1 commit intomainfrom
Conversation
Contributor
Author
|
Closing this PR because of a new approach, see #45 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The goal of this PR is to create a more general and flexible interface for accessing the data of a
Resource, to allow for a decoupling of atoolo-bundles from the data structure of Sitekit/Infosite resources.This is done by introducing an configurable capability/provider model (strategy pattern). Capabilites are interfaces that define which data a resource is "capable" of exposing while the provider is the implementation of such interface.
See section "Introducing a capability/provider model" for a more in-depth explanation.
Problem
Currently, a
Atoolo\Resource\Resourceis pretty much just a SiteKit resource. It has anid,, alocation, aname, alang, anobjectTypeand somedatawhich is stored as a readonly associative array (DataBag).Since
Resourceobjects are assumed to be SiteKit resources, we explicitly expect theDataBagto have a certain data structure. There is no abstract interface through which we access the data, so we make direct calls to thedataproperty.This leads to a tight coupling between atoolo-bundles and the SiteKit specific data structure. E.g. in the
atoolo-graphql-search-bundlewe call$resource->data->getString('base.teaser.text')for accessing the teaser text of a resource.Approach
Decoupling from SiteKit by introducing an
AbstractResourceA
Resourcehas two properties that I think are rather SiteKit specific:string $objectTypeis simply a SiteKit specific conceptDataBag $datacan theoretically be used by all types of resources to store their data, but I think we shouldn't force that. Some resource might rather use an actual, fully typed model. Currently, it seems, that theDataBagis more of a "temporary" solution for SiteKit resources anyway.For these reasons, I created an
AbstractResource, that leaves out theobjectTypeand thedataproperty:The old
Resourceclass then extends thisAbstractResource.Optimally, I think we should rename
ResourcetoSiteKitResourceandAbstractResourcetoResource, but that would be a breaking change, of course. So keep in mind that for now,Resourcespecifically refers to a SiteKit resource.Introducing a capability/provider model
A "Capability" is an interface that defines which data a resource should be capable of exposing for a certain purpose. E.g. a (very simplified!)
MetadataCapabilitycould look like this:A "Provider" is an implementation of that capability. It usually takes a resource (subclass of an
AbstractResource) as a constructor property and then implements a capability interface by accessing the resources data.E.g. an implementation of the capability above for a SiteKit resource cold look like this:
Let's say that we want to access a certain capability of some
AbstractResource. This is done through aResourceCapabilityFactory. TheResourceCapabilityFactoryis a service that knows about the capabilities/providers of all resource types.For example: To get the
MetadataCapabilityof some randomAbstractResource $resource, we could do something like this:This way, we successfully accessed the metadata of a resource without knowing about the underlying data structure.
Configuring a resource type
To define which type of resource has which capability, I propose to create a custom symfony config that could look like this:
This config (or at least the
resource_definitionpart) is then passed to theResourceCapabilityFactoryduring the symfony compilation phase (see classAtooloResourceExtension).I also added a
ResourceCapabilityValidatorPassthat reads the config and checks whether all capability providers are compatible with the resource they're defined in. (For example, theSiteKitMetadataCapabilitycan only handle sitekit resource (Atoolo\Resource\Resource) and should therefore only be allowed there).This config is just a prototype btw. Feel free to add suggestions.
Conculsion
Pros
atoolo-events-calendar) don't have to know about the inner data structure of a resource. Instead, they can just define a capability interface (and maybe a default sitekit implementation).Cons
Todos, Next Steps, Ideas
Atoolo\Resource\ResourcetoAtoolo\Resource\SiteKitResourceAtoolo\Resource\AbstractResourcetoAtoolo\Resource\ResourceMetaDataCapabilityfor metadataBaiscContentCapabilityfor headline, intro etc. ??TeaserDataCapabilityfor teaser dataatoolo-sitekit-bundlethat contains all the sitekit specific implementations? Maybe move all SiteKit classes likeResource,SiteKitResourceHierarchyLoaderetc. there?