You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+122-2Lines changed: 122 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,20 @@ GoAopBundle
8
8
9
9
The GoAopBundle adds support for Aspect-Oriented Programming via Go! AOP Framework for Symfony2 applications.
10
10
11
+
Overview
12
+
--------
13
+
14
+
Aspect-Oriented Paradigm allows to extend the standard Object-Oriented Paradigm with special instruments for effective solving of cross-cutting concerns in your application. This code is typically present everywhere in your application (for example, logging, caching, monitoring, etc) and there is no easy way to fix this without AOP.
15
+
16
+
AOP defines new instruments for developers, they are:
17
+
18
+
* Joinpoint - place in your code that can be used for interception, for example, execution of single public method or accessing of single object property.
19
+
* Pointcut is a list of joinpoints defined with a special regexp-like expression for your source code, for example, all public and protected methods in the concrete class or namespace.
20
+
* Advice is an additional callback that will be called before, after or around concrete joinpoint. For PHP each advice is represented as a `\Closure` instance, wrapped into the interceptor object.
21
+
* Aspect is a special class that combines pointcuts and advices together, each pointcut is defined as an annotation and each advice is a method inside this aspect.
22
+
23
+
You can read more about AOP in different sources, there are good articles for Java language and they can be applied for PHP too, because it's general paradigm.
Versions 2.x(master) are for Symfony >= 2.7 and 3.0
22
36
23
-
Documentation
37
+
Then enable the bundle in the kernel:
38
+
```php
39
+
// app/AppKernel.php
40
+
41
+
public function registerBundles()
42
+
{
43
+
$bundles = array(
44
+
new Go\Symfony\GoAopBundle\GoAopBundle(),
45
+
// ...
46
+
);
47
+
}
48
+
```
49
+
Make sure that bundle is the first item in this list. This is required for the AOP engine to work correctly.
50
+
51
+
Configuration
24
52
-------------
25
53
26
-
Documentation is available in the `Resources/doc` directory for the concrete version of bundle.
54
+
Configuration for bundle is required for additional tuning of AOP kernel and source code whitelistsing/blacklisting.
55
+
56
+
```yaml
57
+
# app/config/config.yml
58
+
go_aop:
59
+
# This setting enables or disables an automatic AOP cache warming in the application.
60
+
# By default, cache_warmer is enabled (true), disable it only if you have serious issues with
61
+
# cache warming process.
62
+
cache_warmer: true
63
+
64
+
# Additional settings for the Go! AOP kernel initialization
65
+
options:
66
+
# Debug mode for the AOP, enable it for debugging and switch off for production mode to have a
67
+
# better runtime performance for your application
68
+
debug: %kernel.debug%
69
+
70
+
# Application root directory, AOP will be applied ONLY to the files in this directory, by default it's
71
+
# src/ directory of your application.
72
+
app_dir: "%kernel.root_dir%/../src"
73
+
74
+
# AOP cache directory where all transformed files will be stored.
75
+
cache_dir: %kernel.cache_dir%/aspect
76
+
77
+
# Whitelist is array of directories where AOP should be enabled, leave it empty to process all files
78
+
include_paths: []
79
+
80
+
# Exclude list is array of directories where AOP should NOT be enabled, leave it empty to process all files
81
+
exclude_paths: []
82
+
83
+
# AOP container class name can be used for extending AOP engine or services adjustment
84
+
container_class: ~
85
+
86
+
# List of enabled features for AOP kernel, this allows to enable function interception, support for
87
+
# read-only file systems, etc. Each item should be a name of constant from the `Go\Aop\Features` class.
88
+
features: []
89
+
90
+
```
91
+
92
+
Defining new aspects
93
+
--------------------
94
+
95
+
Aspects are services in the Symfony2 apllications and loaded into the AOP container with the help of compiler pass that collects all services tagged with `goaop.aspect` tag. Here is an example how to implement a logging aspect that will log information about public method invocations in the src/ directory.
96
+
97
+
98
+
Definition of aspect class with pointuct and logging advice
99
+
```php
100
+
<?php
101
+
102
+
namespace App\Aspect;
103
+
104
+
use Go\Aop\Aspect;
105
+
use Go\Aop\Intercept\MethodInvocation;
106
+
use Go\Lang\Annotation\Before;
107
+
use Psr\Log\LoggerInterface;
108
+
109
+
/**
110
+
* Application logging aspect
111
+
*/
112
+
class LoggingAspect implements Aspect
113
+
{
114
+
/**
115
+
* @var LoggerInterface
116
+
*/
117
+
private $logger;
118
+
119
+
public function __construct(LoggerInterface $logger)
120
+
{
121
+
$this->logger = $logger;
122
+
}
123
+
124
+
/**
125
+
* Writes a log info before method execution
126
+
*
127
+
* @param MethodInvocation $invocation
128
+
* @Before("execution(public **->*(*))")
129
+
*/
130
+
public function beforeMethod(MethodInvocation $invocation)
0 commit comments