Download the PHP package bear/resource without Composer
On this page you can find all versions of the php package bear/resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bear/resource
More information about bear/resource
Files in bear/resource
Package resource
Short Description Hypermedia framework for object as a service
License MIT
Informations about the package resource
BEAR.Resource
Hypermedia framework for object as a service
BEAR.Resource Is a Hypermedia framework that allows resources to behave as objects. It allows objects to have RESTful web service benefits such as client-server, uniform interface, statelessness, resource expression with mutual connectivity and layered components.
In order to introduce flexibility and longevity to your existing domain model or application data you can introduce an API as the driving force in your develpment by making your application REST-Centric in it's approach.
Resource Object
The resource object is an object that has resource behavior.
- 1 URI Resource is mapped to 1 class, it is retrieved by using a resource client.
- A request is made to a method with named parameters that responds to a uniform resource request.
- Through the request the method changes the resource state and return itself
$this
.
Instance retrieval
You can retrieve a client instance by using an injector that resolves dependencies.
By either method the resource client that resolves a URI such as app://self/user to the mapped Sandbox\Resource\App\User can be provisioned.
Resource request
Using the URI and a query the resource is requested.
- This request passes 1 to the onGet($id) method in the Sandbox\Resource\App\User class that conforms to PSR0.
- The retrieved resource has 3 properties code, headers and body.
There are two ways to request. One is eager request
and the other is lazy reuqust
.
Eager Request
An eager request is a way to execute a request immediately. It is the same as normal PHP method execution. There are other different ways of writing with some reasons.
Lazy Request
As known as Lazy Loading, Request to resource until the point at which it is needed. One common use case is that assign to template the resource request object
not the instance.
It is callable object, you can invoke with other parameters;
Hypermedia
A resource can contain hyperlinks to other related resources. Hyperlinks are shown by methods marked with #[Link] attribute. (PHP8.x)
or annotated with @Link
annotation. (PHP 7.x)
The relation name is set by rel and link URI's are set by href (hyper reference). The URI can assign the current resource value using the URI Template(rfc6570).
Within a link their are several types self, new, crawl which can be used to effectively create a resource graph.
linkSelf
linkSelf
retrieves the linked resource.
The result of the app://self/user resource request jumps over the the blog link and retrieves the app://self/blog resource. Just like clicking a link a the webpage it is replaced by the next resource.
linkNew
linkNew
adds the linked resource to the response.
In a web page this is like 'opening a page in a new window', while passing the current resource but also retreiving the next.
Crawl
A crawl passes over a list of resources (array) in order retrieving their links, with this you can construct a more complictated resource graph. Just as a crawler crawls a web page, the resource client crawls hyperlinks and creates a resource graph.
Let's think of author, post, meta, tag, tag/name and they are all connected together by a resource graph. Each resource has a hyperlink. In ths resource graph add the name post-tree, on each resource add the hyper-reference href in the @link annotation.
In the author resource there is a hyperlink to the post resource. This is a 1:n relationship.
In the post resource there is a hyperlink to meta and tag resources. This is also a 1:n relationship.
There is a hyperlink in the tag resource with only an ID for the tag/name resource that corresponds to that ID. It is a 1:1 relationship.
Set the crawl name and make the request.
The resource client looks for the crawl name annotated with @link using the rel name connects to the resource and creates a resource graph.
HATEOAS Hypermedia as the Engine of Application State
The resource client next then takes the next behavior as hyperlink and carrying on from that link changes the application state. For example in an order resource by using POST the order is created, from that order state to the payment resource using a PUT method a payment is made.
Order resource
Client code
The payment method is provided by the order resource with the hyperlink. There is no change in client code even though the relationship between the order and payment is changed, You can checkout more on HATEOAS at How to GET a Cup of Coffee.
Bind parameters You can bind method parameters to an “external value”. The external value might be a web context or any other resource state.
Bind parameters
You can bind method parameters to an "external value". The external value might be a web context or any other resource state.
Web context parameter
For instance, Instead you "pull" $_GET
or any global the web context values, You can bind PHP super global values to method parameters.
The above example is a case where a key name and the parameter name are the same.
You can specify key
and param
values when they don't match.
Full List
This bind parameter
is also very useful for testing.
Resource Parameter
We can bind the status of another resource to a parameter with the @ResourceParam
annotation.
In this example, the nickname
property of app://self//login
is bound to $name
.
Resource Representation
Each resource has a renderer for representation. This renderer is a dependency of the resource, so it is injected in using an injector.
Apart from JsonModule
you can also use the HalModule
which uses a HAL (Hyper Application Laungage) renderer.
When the resource is output as a string the injected resource renderer is used then displayed as the resource representation.
In this case $user
is the renderers internal ResourceObject
.
This is not a string so is treated as either an array or an object.
Lazy Loading
In a non eager
request()
not the resource request result but a request object is retrieved.
When this is assigned to the template engine at the timing of the output of a resource request {$user}
in the template the resource request
and resource rendering
is executed and is displayed as a string.
Embedding resources
@Embed
annotations makes easier to embed external resource to its own. Like <img src="image_url">
or <iframe src="content_url">
in HTML, Embedded resource is specified by src
field.
weather
resource ie embedded like as headline
or sports
in this News
resource.
HAL (Hypertext Application Language)
HAL Module
changes resource representation as HAL.
Embedded resource evaluate when it is present.
Result
A demo application code is available.
Representation
Cast ResourceObject
to string to get the resource view.
You can change the view format (media type) by injecting renderer. Following exmaple illustrates how simple json representation renderer "injected" in the constructor. Normally it is injected by dependency injection library.
Transfer
REST stands for representational state "transfer". transfer()
method in ResourceObject
output resource view to the client.
The above is simple http resoponse transfer example. This would be changed in different enviroment such as "console", "stream" or "socket serevr".
Performance boost
A resource client is serializable. It has huge performance boosts. Recommended in production use.
Annotation / Attribute
BEAR.Resource can be used either with doctrine/annotation in PHP 7/8 or with an Attributes in PHP8. See the annotation code examples in the older README(v1.4).
Installation
A Resource Oriented Framework
BEAR.Sunday is a Resource Oriented Framework. In BEAR.Sunday on top of the web behavior in BEAR.Resource also has the added Google guice style DI/AOP System Ray and is a web application framework.
Please check out BEAR.Sunday web site.
See Also
- BEAR.QueryRepository - Segregates reads and writes into two separate repository.
- Ray.WebParamModule - Binds the value(s) of a web context to method parameter.
Testing BEAR.Resource
Here's how to install BEAR.Resource from source and run the unit tests and demos.
All versions of resource with dependencies
ext-filter Version *
ext-curl Version *
justinrainbow/json-schema Version ^5.3 || ^6.0
koriym/attributes Version ^1.0
koriym/http-constants Version ^1.1
nocarrier/hal Version ^0.9.12
phpdocumentor/reflection-docblock Version ^5.2
psr/log Version ^1.1 || ^2.0 || ^3.0
ray/aop Version ^2.12.3
ray/di Version ^2.13
ray/web-param-module Version ^2.1.1
rize/uri-template Version ^0.3