Completed
Pull Request — master (#298)
by Alexander
04:10
created

GoAspectContainer::getPointcut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
    public function __construct()
45 11
    {
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
            $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
                $container->get('aspect.annotation.reader')
51 2
            );
52
            $lexer  = $container->get('aspect.pointcut.lexer');
53 2
            $parser = $container->get('aspect.pointcut.parser');
54 2
55
            // Register general aspect loader extension
56
            $aspectLoader->registerLoaderExtension(new GeneralAspectLoaderExtension($lexer, $parser));
57 2
            $aspectLoader->registerLoaderExtension(new IntroductionAspectExtension($lexer, $parser));
58 2
59
            return $aspectLoader;
60 2
        });
61 11
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
            $cachedAspectLoader = new CachedAspectLoader(
64
                $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...
65
                'aspect.loader',
66
                $container->get('kernel.options')
67
            );
68
69
            return $cachedAspectLoader;
70
        });
71 11
72
        $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...
73
            return new LazyAdvisorAccessor(
74
                $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...
75
                $container->get('aspect.cached.loader')
76
            );
77
        });
78 11
79
        $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...
80
            return new AdviceMatcher(
81 1
                $container->get('aspect.loader'),
82 1
                $container->get('kernel.interceptFunctions')
83 1
            );
84
        });
85 11
86
        $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...
87
            $options = $container->get('kernel.options');
88 3
            $reader  = new AnnotationReader();
89 3
            if (!empty($options['cacheDir'])) {
90 3
                $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...
91
                    $reader,
92
                    $options['cacheDir'] . DIRECTORY_SEPARATOR . '_annotations' . DIRECTORY_SEPARATOR,
93
                    $options['debug'],
94
                    0777 & (~$options['cacheFileMode'])
95
                );
96
            }
97
98
            return $reader;
99 3
        });
100 11
        $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...
101
            return new CachePathManager($container->get('kernel'));
102 4
        });
103 11
104
        // Pointcut services
105
        $this->share('aspect.pointcut.lexer', function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
106 11
            return new PointcutLexer();
107
        });
108
        $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...
109
            return new PointcutParser(
110 3
                new PointcutGrammar(
111 11
                    $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 11
                    $container->get('aspect.annotation.reader')
113 3
                )
114 3
            );
115
        });
116 3
    }
117
118
    /**
119 11
     * Returns a pointcut by identifier
120 11
     *
121
     * @param string $id Pointcut identifier
122
     *
123
     * @return Aop\Pointcut
124
     */
125
    public function getPointcut($id)
126
    {
127
        return $this->get("pointcut.{$id}");
128
    }
129 1
130
    /**
131 1
     * Store the pointcut in the container
132
     *
133
     * @param Aop\Pointcut $pointcut Instance
134
     * @param string $id Key for pointcut
135
     */
136
    public function registerPointcut(Aop\Pointcut $pointcut, $id)
137
    {
138
        $this->set("pointcut.{$id}", $pointcut, array('pointcut'));
139
    }
140 1
141
    /**
142 1
     * Returns an advisor by identifier
143 1
     *
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
    public function registerAdvisor(Aop\Advisor $advisor, $id)
160
    {
161
        $this->set("advisor.{$id}", $advisor, array('advisor'));
162
    }
163 1
164
    /**
165 1
     * Returns an aspect by id or class name
166 1
     *
167
     * @param string $aspectName Aspect name
168
     *
169
     * @return Aop\Aspect
170
     */
171
    public function getAspect($aspectName)
172
    {
173
        return $this->get("aspect.{$aspectName}");
174
    }
175 1
176
    /**
177 1
     * Register an aspect in the container
178
     *
179
     * @param Aop\Aspect $aspect Instance of concrete aspect
180
     */
181
    public function registerAspect(Aop\Aspect $aspect)
182
    {
183
        $refAspect = new ReflectionClass($aspect);
184
        $this->set("aspect.{$refAspect->name}", $aspect, array('aspect'));
185 1
        $this->addResource($refAspect->getFileName());
186
    }
187 1
188 1
    /**
189 1
     * Add an AOP resource to the container
190 1
     *
191
     * Resources is used to check the freshness of AOP cache
192
     */
193
    public function addResource($resource)
194
    {
195
        $this->resources[]  = $resource;
196
        $this->maxTimestamp = 0;
197 2
    }
198
199 2
    /**
200 2
     * Returns list of AOP resources
201 2
     *
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
    public function isFresh($timestamp)
217
    {
218
        if (!$this->maxTimestamp && !empty($this->resources)) {
219
            $this->maxTimestamp = max(array_map('filemtime', $this->resources));
220 1
        }
221
222 1
        return $this->maxTimestamp <= $timestamp;
223 1
    }
224
}
225