1 | <?php |
||
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() |
||
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() |
||
79 | |||
80 | /** |
||
81 | * 解析客户端的 include 参数. |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function parseIncludes() |
||
93 | } |
||
94 |