Completed
Pull Request — master (#328)
by Nikola
03:49
created

GoAspectContainer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Test Coverage

Coverage 72%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 13
dl 0
loc 205
ccs 54
cts 75
cp 0.72
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPointcut() 0 4 1
A registerPointcut() 0 4 1
A getAdvisor() 0 4 1
A registerAdvisor() 0 4 1
A getAspect() 0 4 1
A registerAspect() 0 6 1
A addResource() 0 5 1
A getResources() 0 4 1
A isFresh() 0 8 3
B __construct() 0 78 3
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Core;
12
13
use Doctrine\Common\Annotations\FileCacheReader;
14
use ReflectionClass;
15
use Go\Aop;
16
use Go\Aop\Pointcut\PointcutLexer;
17
use Go\Aop\Pointcut\PointcutGrammar;
18
use Go\Aop\Pointcut\PointcutParser;
19
use Go\Instrument\ClassLoading\CachePathManager;
20
use Doctrine\Common\Annotations\AnnotationReader;
21
22
/**
23
 * Aspect container contains list of all pointcuts and advisors
24
 */
25
class GoAspectContainer extends Container implements AspectContainer
26
{
27
    /**
28
     * List of resources for application
29
     *
30
     * @var array
31
     */
32
    protected $resources = [];
33
34
    /**
35
     * Cached timestamp for resources
36
     *
37
     * @var integer
38
     */
39
    protected $maxTimestamp = 0;
40
41
    /**
42
     * Constructor for container
43
     */
44 10
    public function __construct()
45
    {
46
        // Register all services in the container
47
        $this->share('aspect.loader', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
48 2
            $aspectLoader = new AspectLoader(
49 2
                $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
50 2
                $container->get('aspect.annotation.reader')
51
            );
52 2
            $lexer  = $container->get('aspect.pointcut.lexer');
53 2
            $parser = $container->get('aspect.pointcut.parser');
54
55
            // Register general aspect loader extension
56 2
            $aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser));
57 2
            $aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser));
58
59 2
            return $aspectLoader;
60 10
        });
61
62
        $this->share('aspect.cached.loader', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
63
            $options = $container->get('kernel.options');
64
            if (!empty($options['cacheDir'])) {
65
                $loader = new CachedAspectLoader(
66
                    $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
                    'aspect.loader',
68
                    $container->get('kernel.options')
69
                );
70
            } else {
71
                $loader = $container->get('aspect.loader');
72
            }
73
74
            return $loader;
75 10
        });
76
77
        $this->share('aspect.advisor.accessor', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
78
            return new LazyAdvisorAccessor(
79
                $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
80
                $container->get('aspect.cached.loader')
81
            );
82 10
        });
83
84
        $this->share('aspect.advice_matcher', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
85 1
            return new AdviceMatcher(
86 1
                $container->get('aspect.loader'),
87 1
                $container->get('kernel.interceptFunctions')
88
            );
89 10
        });
90
91
        $this->share('aspect.annotation.reader', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
92 4
            $options = $container->get('kernel.options');
93 4
            $reader  = new AnnotationReader();
94 4
            if (!empty($options['cacheDir'])) {
95
                $reader = new FileCacheReader(
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Annotations\FileCacheReader has been deprecated with message: the FileCacheReader is deprecated and will be removed in version 2.0.0 of doctrine/annotations. Please use the {@see \Doctrine\Common\Annotations\CachedReader} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
96
                    $reader,
97
                    $options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR,
98
                    $options['debug'],
99
                    0777 & (~$options['cacheFileMode'])
100
                );
101
            }
102
103 4
            return $reader;
104 10
        });
105
        $this->share('aspect.cache.path.manager', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
106
            return new CachePathManager($container->get('kernel'));
107 10
        });
108
109
        // Pointcut services
110
        $this->share('aspect.pointcut.lexer', function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
111 3
            return new PointcutLexer();
112 10
        });
113 10
        $this->share('aspect.pointcut.parser', function(Container $container) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
114 3
            return new PointcutParser(
115 3
                new PointcutGrammar(
116 3
                    $container,
0 ignored issues
show
Documentation introduced by
$container is of type object<Go\Core\Container>, but the function expects a object<Go\Core\AspectContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117 3
                    $container->get('aspect.annotation.reader')
118
                )
119
            );
120 10
        });
121 10
    }
122
123
    /**
124
     * Returns a pointcut by identifier
125
     *
126
     * @param string $id Pointcut identifier
127
     *
128
     * @return Aop\Pointcut
129
     */
130 1
    public function getPointcut($id)
131
    {
132 1
        return $this->get("pointcut.{$id}");
133
    }
134
135
    /**
136
     * Store the pointcut in the container
137
     *
138
     * @param Aop\Pointcut $pointcut Instance
139
     * @param string $id Key for pointcut
140
     */
141 1
    public function registerPointcut(Aop\Pointcut $pointcut, $id)
142
    {
143 1
        $this->set("pointcut.{$id}", $pointcut, array('pointcut'));
144 1
    }
145
146
    /**
147
     * Returns an advisor by identifier
148
     *
149
     * @param string $id Advisor identifier
150
     *
151
     * @return Aop\Advisor
152
     */
153
    public function getAdvisor($id)
154
    {
155
        return $this->get("advisor.{$id}");
156
    }
157
158
    /**
159
     * Store the advisor in the container
160
     *
161
     * @param Aop\Advisor $advisor Instance
162
     * @param string $id Key for advisor
163
     */
164 1
    public function registerAdvisor(Aop\Advisor $advisor, $id)
165
    {
166 1
        $this->set("advisor.{$id}", $advisor, array('advisor'));
167 1
    }
168
169
    /**
170
     * Returns an aspect by id or class name
171
     *
172
     * @param string $aspectName Aspect name
173
     *
174
     * @return Aop\Aspect
175
     */
176 1
    public function getAspect($aspectName)
177
    {
178 1
        return $this->get("aspect.{$aspectName}");
179
    }
180
181
    /**
182
     * Register an aspect in the container
183
     *
184
     * @param Aop\Aspect $aspect Instance of concrete aspect
185
     */
186 1
    public function registerAspect(Aop\Aspect $aspect)
187
    {
188 1
        $refAspect = new ReflectionClass($aspect);
189 1
        $this->set("aspect.{$refAspect->name}", $aspect, array('aspect'));
190 1
        $this->addResource($refAspect->getFileName());
191 1
    }
192
193
    /**
194
     * Add an AOP resource to the container
195
     *
196
     * Resources is used to check the freshness of AOP cache
197
     */
198 2
    public function addResource($resource)
199
    {
200 2
        $this->resources[]  = $resource;
201 2
        $this->maxTimestamp = 0;
202 2
    }
203
204
    /**
205
     * Returns list of AOP resources
206
     *
207
     * @return array
208
     */
209
    public function getResources()
210
    {
211
        return $this->resources;
212
    }
213
214
    /**
215
     * Checks the freshness of AOP cache
216
     *
217
     * @param integer $timestamp
218
     *
219
     * @return bool Whether or not concrete file is fresh
220
     */
221 1
    public function isFresh($timestamp)
222
    {
223 1
        if (!$this->maxTimestamp && !empty($this->resources)) {
224 1
            $this->maxTimestamp = max(array_map('filemtime', $this->resources));
225
        }
226
227 1
        return $this->maxTimestamp <= $timestamp;
228
    }
229
}
230