1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* User: xuan |
6
|
|
|
* Date: 9/24/15 |
7
|
|
|
* Time: 4:06 PM. |
8
|
|
|
*/ |
9
|
|
|
namespace PHPHub\Transformers\IncludeManager; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Input; |
13
|
|
|
|
14
|
|
|
class IncludeManager |
15
|
|
|
{ |
16
|
|
|
private $available_includes = []; |
17
|
|
|
private $foreign_keys = []; |
18
|
|
|
private $includes = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* 添加一个可被引入的项. |
22
|
|
|
* |
23
|
|
|
* @param Includable $includable |
24
|
|
|
* |
25
|
|
|
* @throws Exception |
26
|
|
|
*/ |
27
|
|
|
public function add(Includable $includable) |
28
|
|
|
{ |
29
|
|
|
if ($includable->isNested()) { |
30
|
|
|
$parent_includable = $this->getIncludable($includable->getParentName()); |
31
|
|
|
if (null === $parent_includable) { |
32
|
|
|
throw new Exception('You must define includable '.$includable->getParentName()); |
33
|
|
|
} |
34
|
|
|
$parent_includable->addChildren($includable); |
35
|
|
|
} |
36
|
|
|
$this->available_includes[$includable->getName()] = $includable; |
37
|
|
|
$this->foreign_keys[$includable->getName()] = $includable->getForeignKey(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 按引入项的名称获取对象 |
42
|
|
|
* |
43
|
|
|
* @param $name |
44
|
|
|
* |
45
|
|
|
* @return Includable|null |
46
|
|
|
*/ |
47
|
|
|
public function getIncludable($name) |
48
|
|
|
{ |
49
|
|
|
return array_get($this->available_includes, $name, null); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* 获取所有的引入项名称. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getAvailableIncludableNames() |
58
|
|
|
{ |
59
|
|
|
return array_keys($this->available_includes); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 获取需要的引入项的外键. |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function getForeignKeys() |
68
|
|
|
{ |
69
|
|
|
return array_only($this->foreign_keys, $this->figureOutWhichIncludes()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 计算出需要那些引入项. |
74
|
|
|
*/ |
75
|
|
|
public function figureOutWhichIncludes() |
76
|
|
|
{ |
77
|
|
|
return array_intersect($this->parseIncludes(), $this->getAvailableIncludableNames()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 解析客户端的 include 参数. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function parseIncludes() |
86
|
|
|
{ |
87
|
|
|
if ($this->includes === null) { |
88
|
|
|
return $this->includes = explode(',', Input::get('include')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->includes; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|