preface
What is AOP, here will not go into some theory, here is mainly to elaborate on the Aspect of AOP (Aspect), mainly with the actual development of an example to use AOP to illustrate more profound impression.
For example, controller beforeActionList in ThinkPHP, middleware, creating a base class to implement, and so on. These methods also have some AOP ideas in a sense. (AOP generally means extracting code common to several classes into a slice. AOP, like OOP, is just a programming paradigm. AOP does not specify how the code that implements the AOP protocol should be implemented. My understanding is that the programming idea of cutting code into the specified method and location of a class is faceted programming, putting things outside the code that have nothing to do with the main business, and not invading the original code.
AOP is probably something that many people have only learned about and never really used in a real project. I think it only makes sense to implement AOP in a real project (it’s best to try it in a personal project first).
ar414
practice
The environment
A service in the gateway uses HyperF 2.0
demand
The Laravel pager result metadata field is converted to the gateway paging specification data field for paged data
This requirement can be implemented in a number of ways. Here, I have used a unified approach to the aspects of the controller that provides paging data. This approach is non-invasive to the original code, which is the benefit of AOP
Laravel Pager default field
{
"total": 50."per_page": 15."current_page": 1."last_page": 4."first_page_url": "http://laravel.app? page=1"."last_page_url": "http://laravel.app? page=4"."next_page_url": "http://laravel.app? page=2"."prev_page_url": null."path": "http://laravel.app"."from": 1."to": 15."data":[
{
// Result Object
},
{
// Result Object}}]Copy the code
Gateway paging specification field
{
"total": 50."current_page": 1."last_page": 4."data":[
{
// Result Object
},
{
// Result Object}}]Copy the code
Concrete implementation method
Using Aspect aspects in HyperF 2.0 by configuring the classes to cut into, The specific methods in controllers for paged data such as TestController::getList and Test1Controller::getList provide paged data
Generally speaking, code snippets that cut into specified classes and methods are called aspects, and which classes and methods cut into are called pointcuts
class PageResultAspect extends AbstractAspect
{
public $classes = [
TestController::class . '... ' . 'getList',
Test1Controller::class . '... ' . 'getList',];public $annotations = [];
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
// After the section is cut, the corresponding method is responsible for executing it
// $proceedingJoinPoint is a join point that invokes the original method via the class's process() method and returns the result
$result = $proceedingJoinPoint->process();
// The result is processed uniformly
if(isset($result['first_page_url']) {unset($result['first_page_url']);
unset($result['last_page_url']);
unset($result['next_page_url']);
unset($result['path']);
unset($result['prev_page_url']);
unset($result['per_page']);
unset($result['from']);
unset($result['to']);
}
return $result; }}Copy the code