Completed
Pull Request — master (#240)
by Alexander
05:40
created

GoAspectContainer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Test Coverage

Coverage 88.14%

Importance

Changes 8
Bugs 4 Features 0
Metric Value
c 8
b 4
f 0
dl 0
loc 199
wmc 13
lcom 2
cbo 13
ccs 52
cts 59
cp 0.8814
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 72 2
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
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\RawAnnotationReader;
20
use Go\Instrument\ClassLoading\CachePathManager;
21
use Doctrine\Common\Annotations\AnnotationReader;
22
23
/**
24
 * Aspect container contains list of all pointcuts and advisors
25
 */
26
class GoAspectContainer extends Container implements AspectContainer
27
{
28
    /**
29
     * List of resources for application
30
     *
31
     * @var array
32
     */
33
    protected $resources = [];
34
35
    /**
36
     * Cached timestamp for resources
37
     *
38
     * @var integer
39
     */
40
    protected $maxTimestamp = 0;
41
42
    /**
43
     * Constructor for container
44
     */
45 10
    public function __construct()
46
    {
47
        // Register all services in the container
48
        $this->share('aspect.loader', function(Container $container) {
49 2
            $aspectLoader = new AspectLoader(
50
                $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...
51 2
                $container->get('aspect.annotation.reader')
52
            );
53 2
            $lexer  = $container->get('aspect.pointcut.lexer');
54 2
            $parser = $container->get('aspect.pointcut.parser');
55
56
            // Register general aspect loader extension
57 2
            $aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser));
58 2
            $aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser));
59
60 2
            return $aspectLoader;
61 10
        });
62
63
        $this->share('aspect.cached.loader', function(Container $container) {
64
            $cachedAspectLoader = new CachedAspectLoader(
65
                $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...
66
                'aspect.loader',
67
                $container->get('kernel.options')
68
            );
69
70
            return $cachedAspectLoader;
71 10
        });
72
73
        $this->share('aspect.advisor.accessor', function(Container $container) {
74
            return new LazyAdvisorAccessor(
75
                $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...
76
                $container->get('aspect.cached.loader')
77
            );
78 10
        });
79
80
        $this->share('aspect.advice_matcher', function(Container $container) {
81 1
            return new AdviceMatcher(
82 1
                $container->get('aspect.loader'),
83 1
                $container->get('kernel.interceptFunctions')
84
            );
85 10
        });
86
87
        $this->share('aspect.annotation.reader', function(Container $container) {
88 4
            $options = $container->get('kernel.options');
89 4
            $reader  = new AnnotationReader();
90 4
            if (!empty($options['cacheDir'])) {
91
                $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...
92
                    $reader,
93
                    $options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR,
94
                    $options['debug']
95
                );
96
            }
97
98 4
            return $reader;
99 10
        });
100
        $this->share('aspect.cache.path.manager', function(Container $container) {
101
            return new CachePathManager($container->get('kernel'));
102 10
        });
103
104
        // Pointcut services
105
        $this->share('aspect.pointcut.lexer', function() {
106 3
            return new PointcutLexer();
107 10
        });
108 10
        $this->share('aspect.pointcut.parser', function(Container $container) {
109 3
            return new PointcutParser(
110 3
                new PointcutGrammar(
111
                    $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...
112 3
                    $container->get('aspect.annotation.reader')
113
                )
114
            );
115 10
        });
116 10
    }
117
118
    /**
119
     * Returns a pointcut by identifier
120
     *
121
     * @param string $id Pointcut identifier
122
     *
123
     * @return Aop\Pointcut
124
     */
125 1
    public function getPointcut($id)
126
    {
127 1
        return $this->get("pointcut.{$id}");
128
    }
129
130
    /**
131
     * Store the pointcut in the container
132
     *
133
     * @param Aop\Pointcut $pointcut Instance
134
     * @param string $id Key for pointcut
135
     */
136 1
    public function registerPointcut(Aop\Pointcut $pointcut, $id)
137
    {
138 1
        $this->set("pointcut.{$id}", $pointcut, array('pointcut'));
139 1
    }
140
141
    /**
142
     * Returns an advisor by identifier
143
     *
144
     * @param string $id Advisor identifier
145
     *
146
     * @return Aop\Advisor
147
     */
148
    public function getAdvisor($id)
149
    {
150
        return $this->get("advisor.{$id}");
151
    }
152
153
    /**
154
     * Store the advisor in the container
155
     *
156
     * @param Aop\Advisor $advisor Instance
157
     * @param string $id Key for advisor
158
     */
159 1
    public function registerAdvisor(Aop\Advisor $advisor, $id)
160
    {
161 1
        $this->set("advisor.{$id}", $advisor, array('advisor'));
162 1
    }
163
164
    /**
165
     * Returns an aspect by id or class name
166
     *
167
     * @param string $aspectName Aspect name
168
     *
169
     * @return Aop\Aspect
170
     */
171 1
    public function getAspect($aspectName)
172
    {
173 1
        return $this->get("aspect.{$aspectName}");
174
    }
175
176
    /**
177
     * Register an aspect in the container
178
     *
179
     * @param Aop\Aspect $aspect Instance of concrete aspect
180
     */
181 1
    public function registerAspect(Aop\Aspect $aspect)
182
    {
183 1
        $refAspect = new ReflectionClass($aspect);
184 1
        $this->set("aspect.{$refAspect->name}", $aspect, array('aspect'));
185 1
        $this->addResource($refAspect->getFileName());
186 1
    }
187
188
    /**
189
     * Add an AOP resource to the container
190
     *
191
     * Resources is used to check the freshness of AOP cache
192
     */
193 2
    public function addResource($resource)
194
    {
195 2
        $this->resources[]  = $resource;
196 2
        $this->maxTimestamp = 0;
197 2
    }
198
199
    /**
200
     * Returns list of AOP resources
201
     *
202
     * @return array
203
     */
204
    public function getResources()
205
    {
206
        return $this->resources;
207
    }
208
209
    /**
210
     * Checks the freshness of AOP cache
211
     *
212
     * @param integer $timestamp
213
     *
214
     * @return bool Whether or not concrete file is fresh
215
     */
216 1
    public function isFresh($timestamp)
217
    {
218 1
        if (!$this->maxTimestamp && !empty($this->resources)) {
219 1
            $this->maxTimestamp = max(array_map('filemtime', $this->resources));
220
        }
221
222 1
        return $this->maxTimestamp <= $timestamp;
223
    }
224
}
225