Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
codetree.php
1<?php
3
5{
6 protected $statements = [];
7 protected $tree = [];
8
12 public function __construct(array $statements)
13 {
14 $this->statements = [];
15 foreach ($statements as $i => $statement)
16 {
17 $this->statements[$i] = clone $statement;
18 }
19 $this->tree = [];
20 }
21
29 public function getCode($level)
30 {
31 $tree = $this->getCodeTree();
32 return $this->formatCodeTree($tree, $level);
33 }
34
41 protected function formatCodeTree($result, $level=0)
42 {
43 $code = '';
44 foreach ($result as $stmt)
45 {
46 if (is_array($stmt) && isset($stmt['if']))
47 {
48 $code .= str_repeat("\t", $level) . 'if (' . implode(' && ', $stmt['if']) . ")\n";
49 $code .= str_repeat("\t", $level) . "{\n";
50 $code .= $this->formatCodeTree($stmt['body'], $level + 1);
51 $code .= str_repeat("\t", $level) . "}\n";
52 }
53 else
54 {
55 $stmt = trim($stmt, "\n\t");
56 $stmt = preg_replace("/\\n\\t+/", "\n", $stmt);
57 $code .= str_repeat("\t", $level) . str_replace("\n\$", "\n" . str_repeat("\t", $level) . '$', $stmt) . "\n";
58 }
59 }
60 return $code;
61 }
62
66 public function getCodeTree()
67 {
68 if (!$this->tree)
69 {
70 $this->makeCodeTree($this->statements, $this->tree);
71 }
72 return $this->tree;
73 }
74
83 protected function makeCodeTree(array $updaterSteps, &$result)
84 {
85 foreach ($updaterSteps as $i => $statement)
86 {
87 if (empty($statement->conditions))
88 {
89 $result[] = $statement->formatBodyLines(0);
90 unset($updaterSteps[$i]);
91 }
92 }
93
94 while ($updaterSteps)
95 {
96 $byPredicates = [];
97 foreach ($updaterSteps as $i => $statement)
98 {
102 foreach ($statement->conditions as $condition)
103 {
104 $predicate = $condition->getPredicate();
105 if (!isset($byPredicates[$predicate]))
106 {
107 $byPredicates[$predicate] = [
108 'predicate' => $predicate,
109 'dep' => $statement->dependOn,
110 'sort' => $this->getPredicateSort($predicate),
111 'count' => 1,
112 ];
113 }
114 else
115 {
116 $byPredicates[$predicate]['count']++;
117 }
118 }
119 }
120
121 if ($byPredicates)
122 {
123 sortByColumn($byPredicates, [
124 'dep' => SORT_ASC,
125 'count' => SORT_DESC,
126 'sort' => SORT_ASC,
127 ]);
128 $mostPopular = key($byPredicates);
129 $subSteps = [];
130 $ifStatement = [
131 'if' => [$mostPopular],
132 'body' => [],
133 ];
134 foreach ($updaterSteps as $i => $statement)
135 {
136 foreach ($statement->conditions as $j => $condition)
137 {
138 if ($condition->getPredicate() == $mostPopular)
139 {
140 unset($statement->conditions[$j]);
141 $subSteps[] = $statement;
142 unset($updaterSteps[$i]);
143 }
144 }
145 }
146 $this->makeCodeTree($subSteps, $ifStatement['body']);
147 if (
148 is_array($ifStatement['body'])
149 && count($ifStatement['body']) == 1
150 && is_array($ifStatement['body'][0])
151 && isset($ifStatement['body'][0]['if'])
152 && isset($ifStatement['body'][0]['body'])
153 && mb_strlen(implode(' && ', array_merge($ifStatement['if'], $ifStatement['body'][0]['if']))) < 100
154 )
155 {
156 $ifStatement['if'] = array_merge($ifStatement['if'], $ifStatement['body'][0]['if']);
157 $ifStatement['body'] = $ifStatement['body'][0]['body'];
158 }
159 $result[] = $ifStatement;
160 }
161 }
162 }
163
169 protected function getPredicateSort($predicate)
170 {
171 if (mb_strpos($predicate, 'CanUpdateDatabase'))
172 {
173 return 10;
174 }
175 elseif (mb_strpos($predicate, '->type'))
176 {
177 return 20;
178 }
179 elseif (mb_strpos($predicate, 'TableExists'))
180 {
181 return 30;
182 }
183 elseif (mb_strpos($predicate, 'IndexExists'))
184 {
185 return 60;
186 }
187 else
188 {
189 return 50;
190 }
191 }
192}
formatCodeTree($result, $level=0)
Definition codetree.php:41
__construct(array $statements)
Definition codetree.php:12