1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Annotation; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use PhpBoot\Cache\CheckableCache; |
7
|
|
|
use PhpBoot\Cache\ClassModifiedChecker; |
8
|
|
|
use PhpBoot\Utils\Logger; |
9
|
|
|
|
10
|
|
|
abstract class ContainerBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* ContainerBuilder constructor. |
14
|
|
|
* @param array $annotations 需加载的注释和顺序 |
15
|
|
|
* @param Cache $cache |
16
|
|
|
* 语法 http://jmespath.org/tutorial.html |
17
|
|
|
* |
18
|
|
|
* [ |
19
|
|
|
* [PropertyAnnotationHandler::class, 'property'], |
20
|
|
|
* ... |
21
|
|
|
* ]; |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
87 |
|
public function __construct(array $annotations, Cache $cache) |
25
|
|
|
{ |
26
|
87 |
|
$this->annotations = $annotations; |
27
|
87 |
|
$this->cache = $cache; |
28
|
87 |
|
} |
29
|
|
|
|
30
|
1 |
|
public function setCache(Cache $cache) |
31
|
1 |
|
{ |
32
|
|
|
$this->cache = $cache; |
33
|
|
|
} |
34
|
|
|
/** |
35
|
|
|
* load from class with local cache |
36
|
|
|
* @param string $className |
37
|
|
|
* @return object |
38
|
|
|
*/ |
39
|
87 |
|
public function build($className) |
40
|
1 |
|
{ |
41
|
|
|
//TODO【重要】 使用全局的缓存版本号, 而不是针对每个文件判断缓存过期与否 |
42
|
87 |
|
$rfl = new \ReflectionClass($className) or \PhpBoot\abort("load class $className failed"); |
|
|
|
|
43
|
87 |
|
$fileName = $rfl->getFileName(); |
44
|
87 |
|
$key = str_replace('\\','.',get_class($this)).md5(serialize($this->annotations).$fileName.$className); |
45
|
87 |
|
$cache = new CheckableCache($this->cache); |
46
|
87 |
|
$res = $cache->get($key, $this); |
47
|
87 |
|
if($res === $this){ |
48
|
|
|
try{ |
49
|
20 |
|
$meta = $this->buildWithoutCache($className); |
50
|
20 |
|
$cache->set($key, $meta, 0, $fileName?new ClassModifiedChecker($className):null); |
51
|
20 |
|
return $meta; |
52
|
|
|
}catch (\Exception $e){ |
53
|
|
|
Logger::warning(__METHOD__.' failed with '.$e->getMessage()); |
54
|
|
|
$cache->set($key, $e->getMessage(), 0, $fileName?new ClassModifiedChecker($className):null); |
55
|
|
|
throw $e; |
56
|
|
|
} |
57
|
73 |
|
}elseif(is_string($res)){ |
58
|
|
|
\PhpBoot\abort($res); |
59
|
|
|
}else{ |
60
|
73 |
|
return $res; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $className |
66
|
|
|
* @return object |
67
|
|
|
*/ |
68
|
|
|
abstract protected function createContainer($className); |
69
|
|
|
|
70
|
20 |
|
protected function postCreateContainer($object) |
71
|
|
|
{ |
72
|
|
|
|
73
|
20 |
|
} |
74
|
15 |
|
protected function handleAnnotation($handlerName, $container, $ann){ |
75
|
15 |
|
$handler = new $handlerName(); |
76
|
15 |
|
return $handler($container, $ann); |
77
|
|
|
} |
78
|
|
|
/** |
79
|
|
|
* @param $className |
80
|
|
|
* @return object |
81
|
|
|
*/ |
82
|
20 |
|
public function buildWithoutCache($className) |
83
|
|
|
{ |
84
|
20 |
|
$container = $this->createContainer($className); |
85
|
20 |
|
$anns = AnnotationReader::read($className, $this->cache); |
86
|
20 |
|
foreach ($this->annotations as $i){ |
87
|
20 |
|
list($class, $target) = $i; |
88
|
|
|
|
89
|
20 |
|
$found = \JmesPath\search($target, $anns); |
90
|
20 |
|
if(is_array($found)){ |
91
|
20 |
|
foreach ($found as $f){ |
92
|
20 |
|
$this->handleAnnotation($class, $container,$f); //TODO 支持 |
93
|
20 |
|
} |
94
|
20 |
|
}else{ |
95
|
12 |
|
$this->handleAnnotation($class, $container, $found); |
96
|
|
|
} |
97
|
20 |
|
} |
98
|
20 |
|
$this->postCreateContainer($container); |
99
|
20 |
|
return $container; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
private $annotations=[]; |
106
|
|
|
/** |
107
|
|
|
* @var Cache |
108
|
|
|
*/ |
109
|
|
|
private $cache; |
110
|
|
|
} |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.