Download the PHP package headio/phalcon-service-layer without Composer
On this page you can find all versions of the php package headio/phalcon-service-layer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download headio/phalcon-service-layer
More information about headio/phalcon-service-layer
Files in headio/phalcon-service-layer
Package phalcon-service-layer
Short Description A service layer implementation for Phalcon projects
License MIT
Informations about the package phalcon-service-layer
Phalcon service layer
A simple repository service implementation for Phalcon 5 projects
Introduction
This library provides a layered architecture promising easier unit and integration testing.
The service layer handles business logic, mediating between the application layer (controller or handler) and the domain, interacting with a single repository or multiple repositories. All repositories extend an abstract query repository, providing a collection-like interface, with well-defined query methods. Hence all queries are isolated in the repository layer.
Phalcon ORM implements the active record pattern, therefore the responsiblity of persistence remains with the active record, in contrast to the repository service pattern / data mapper (Doctrine), where repositories manage the entity lifecycle.
If you have been reading between the lines, you have probably gathered this is a hybrid solution offering: testability, reuseability and prevention of logic leaking into the application layer. The trade-off is you need to write, test and maintain some extra boiler-plate code.
Naturally, you can avoid this paradigm by integrating a data mapper (Doctrine, Atlas ORM etc.) with Phalcon. Nevertheless, for those enjoying the performance of Phalcon ORM, this library may be of interest.
Dependencies
- PHP >=8.0.0 <=8.0.99
- Phalcon 5.0.0+
Installation
Composer
Open a terminal window and run:
Usage
Assuming the following project structure, let's create the layers to handle removing a record from storage as a simple usage example.
Registering a service provider
Create a new Foo service dependency inside the service provider directory /src/Provider/.
Alternatively, create the dependency on a per-module basis.
Controller/Handler
Now the service is in place, the controller can interact with the service layer by injecting the service into the controller via the OnConstruct method.
Service layer
The service layer interacts with one repository (or multiple repositories) to process the business logic. In the example below, the service calls the delete method (implementation skipped for simplicity) to remove a model instance by primary key and return to the list view.
Repository
All repositories must extend the abstract query repository and implement one abstract method.
The Foo repository can implement additional interfaces, e.g. FooInterface, providing further concrete methods for the service layer.
The abstract query repository implements the following repository interface:
In addition, a relationship trait is provided to simplify handling model relationships.
Query caching
Query caching is handled utilizing Phalcon's event manager. To get started first include the CacheableTrait in your repository; the EventsAwareInterface is implemented inside the cacheable trait.
Then create a service provider for your service layer, or a repository if you want to omit the service layer and work with repositories directly. The example below utilizes Phalcon's service provider interface.
Cache event listener
The event listener provides two methods to handle caching, see below.
To trigger a cache event, see the following concrete examples from the cacheable trait.
Pagination
This library provides a cursor-based paginator adapter; see _stub directory inside the test directory for usage.
The model
All models must extend the abstract Model class, which implements the following model interface:
Validation
Validation can be implemented in the service layer or the model classes.
Testing
To see the tests, run:
License
Phalcon service layer is open-source and licensed under MIT License.
All versions of phalcon-service-layer with dependencies
ext-mbstring Version *
ext-phalcon Version >=5.1.4