@@ -96,7 +96,7 @@ |
||
96 | 96 | protected function traverse(array $nodes, VisitorInterface ...$visitors) |
97 | 97 | { |
98 | 98 | $traverser = new Traverser(); |
99 | - foreach ($visitors as $visitor) { |
|
99 | + foreach ($visitors as $visitor){ |
|
100 | 100 | $traverser->addVisitor($visitor); |
101 | 101 | } |
102 | 102 |
@@ -96,7 +96,8 @@ |
||
96 | 96 | protected function traverse(array $nodes, VisitorInterface ...$visitors) |
97 | 97 | { |
98 | 98 | $traverser = new Traverser(); |
99 | - foreach ($visitors as $visitor) { |
|
99 | + foreach ($visitors as $visitor) |
|
100 | + { |
|
100 | 101 | $traverser->addVisitor($visitor); |
101 | 102 | } |
102 | 103 |
@@ -36,7 +36,7 @@ |
||
36 | 36 | */ |
37 | 37 | public function enterNode($node, VisitorContext $ctx): void |
38 | 38 | { |
39 | - if ($node instanceof Block && $node->getContext()->getPath() === $this->path) { |
|
39 | + if ($node instanceof Block && $node->getContext()->getPath() === $this->path){ |
|
40 | 40 | $node->name = null; |
41 | 41 | } |
42 | 42 | } |
@@ -36,7 +36,8 @@ |
||
36 | 36 | */ |
37 | 37 | public function enterNode($node, VisitorContext $ctx): void |
38 | 38 | { |
39 | - if ($node instanceof Block && $node->getContext()->getPath() === $this->path) { |
|
39 | + if ($node instanceof Block && $node->getContext()->getPath() === $this->path) |
|
40 | + { |
|
40 | 41 | $node->name = null; |
41 | 42 | } |
42 | 43 | } |
@@ -44,9 +44,9 @@ |
||
44 | 44 | */ |
45 | 45 | public function leaveNode($node, VisitorContext $ctx) |
46 | 46 | { |
47 | - if ($node instanceof Raw && trim($node->content, $this->trim) === '') { |
|
48 | - foreach ($ctx->getScope() as $scope) { |
|
49 | - if ($scope instanceof Attr) { |
|
47 | + if ($node instanceof Raw && trim($node->content, $this->trim) === ''){ |
|
48 | + foreach ($ctx->getScope() as $scope){ |
|
49 | + if ($scope instanceof Attr){ |
|
50 | 50 | // do not trim attribute values |
51 | 51 | return null; |
52 | 52 | } |
@@ -44,9 +44,12 @@ |
||
44 | 44 | */ |
45 | 45 | public function leaveNode($node, VisitorContext $ctx) |
46 | 46 | { |
47 | - if ($node instanceof Raw && trim($node->content, $this->trim) === '') { |
|
48 | - foreach ($ctx->getScope() as $scope) { |
|
49 | - if ($scope instanceof Attr) { |
|
47 | + if ($node instanceof Raw && trim($node->content, $this->trim) === '') |
|
48 | + { |
|
49 | + foreach ($ctx->getScope() as $scope) |
|
50 | + { |
|
51 | + if ($scope instanceof Attr) |
|
52 | + { |
|
50 | 53 | // do not trim attribute values |
51 | 54 | return null; |
52 | 55 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | $this->path = $path; |
40 | 40 | $this->alias = $alias ?? $path; |
41 | 41 | |
42 | - if ($alias === null && strrpos($this->alias, '/') !== false) { |
|
42 | + if ($alias === null && strrpos($this->alias, '/') !== false){ |
|
43 | 43 | $this->alias = substr($this->alias, strrpos($this->alias, '/') + 1); |
44 | 44 | } |
45 | 45 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | */ |
68 | 68 | public function resolve(Builder $builder, string $name): ?Template |
69 | 69 | { |
70 | - if ($this->alias !== $name) { |
|
70 | + if ($this->alias !== $name){ |
|
71 | 71 | return null; |
72 | 72 | } |
73 | 73 |
@@ -39,7 +39,8 @@ discard block |
||
39 | 39 | $this->path = $path; |
40 | 40 | $this->alias = $alias ?? $path; |
41 | 41 | |
42 | - if ($alias === null && strrpos($this->alias, '/') !== false) { |
|
42 | + if ($alias === null && strrpos($this->alias, '/') !== false) |
|
43 | + { |
|
43 | 44 | $this->alias = substr($this->alias, strrpos($this->alias, '/') + 1); |
44 | 45 | } |
45 | 46 | |
@@ -67,7 +68,8 @@ discard block |
||
67 | 68 | */ |
68 | 69 | public function resolve(Builder $builder, string $name): ?Template |
69 | 70 | { |
70 | - if ($this->alias !== $name) { |
|
71 | + if ($this->alias !== $name) |
|
72 | + { |
|
71 | 73 | return null; |
72 | 74 | } |
73 | 75 |
@@ -46,7 +46,7 @@ |
||
46 | 46 | */ |
47 | 47 | public function resolve(Builder $builder, string $name): ?Template |
48 | 48 | { |
49 | - if ($name !== $this->name) { |
|
49 | + if ($name !== $this->name){ |
|
50 | 50 | return null; |
51 | 51 | } |
52 | 52 |
@@ -46,7 +46,8 @@ |
||
46 | 46 | */ |
47 | 47 | public function resolve(Builder $builder, string $name): ?Template |
48 | 48 | { |
49 | - if ($name !== $this->name) { |
|
49 | + if ($name !== $this->name) |
|
50 | + { |
|
50 | 51 | return null; |
51 | 52 | } |
52 | 53 |
@@ -49,6 +49,6 @@ |
||
49 | 49 | $path = substr($name, strlen($this->prefix) + 1); |
50 | 50 | $path = str_replace('.', DIRECTORY_SEPARATOR, $path); |
51 | 51 | |
52 | - return $builder->load($this->path . DIRECTORY_SEPARATOR . $path); |
|
52 | + return $builder->load($this->path.DIRECTORY_SEPARATOR.$path); |
|
53 | 53 | } |
54 | 54 | } |
@@ -50,19 +50,19 @@ |
||
50 | 50 | */ |
51 | 51 | public function resolve(Builder $builder, string $name): ?Template |
52 | 52 | { |
53 | - if ($this->template === null) { |
|
53 | + if ($this->template === null){ |
|
54 | 54 | $this->template = $builder->load($this->path); |
55 | 55 | } |
56 | 56 | |
57 | 57 | $path = $name; |
58 | - if ($this->prefix !== null) { |
|
58 | + if ($this->prefix !== null){ |
|
59 | 59 | $path = substr($path, strlen($this->prefix) + 1); |
60 | 60 | } |
61 | 61 | |
62 | 62 | /** @var ImportInterface $import */ |
63 | - foreach ($this->template->getAttribute(ImportContext::class, []) as $import) { |
|
63 | + foreach ($this->template->getAttribute(ImportContext::class, []) as $import){ |
|
64 | 64 | $tpl = $import->resolve($builder, $path); |
65 | - if ($tpl !== null) { |
|
65 | + if ($tpl !== null){ |
|
66 | 66 | return $tpl; |
67 | 67 | } |
68 | 68 | } |
@@ -50,19 +50,23 @@ |
||
50 | 50 | */ |
51 | 51 | public function resolve(Builder $builder, string $name): ?Template |
52 | 52 | { |
53 | - if ($this->template === null) { |
|
53 | + if ($this->template === null) |
|
54 | + { |
|
54 | 55 | $this->template = $builder->load($this->path); |
55 | 56 | } |
56 | 57 | |
57 | 58 | $path = $name; |
58 | - if ($this->prefix !== null) { |
|
59 | + if ($this->prefix !== null) |
|
60 | + { |
|
59 | 61 | $path = substr($path, strlen($this->prefix) + 1); |
60 | 62 | } |
61 | 63 | |
62 | 64 | /** @var ImportInterface $import */ |
63 | - foreach ($this->template->getAttribute(ImportContext::class, []) as $import) { |
|
65 | + foreach ($this->template->getAttribute(ImportContext::class, []) as $import) |
|
66 | + { |
|
64 | 67 | $tpl = $import->resolve($builder, $path); |
65 | - if ($tpl !== null) { |
|
68 | + if ($tpl !== null) |
|
69 | + { |
|
66 | 70 | return $tpl; |
67 | 71 | } |
68 | 72 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public function enterNode($node, VisitorContext $ctx) |
53 | 53 | { |
54 | - if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0) { |
|
54 | + if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0){ |
|
55 | 55 | return self::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
56 | 56 | } |
57 | 57 | |
@@ -63,9 +63,9 @@ discard block |
||
63 | 63 | */ |
64 | 64 | public function leaveNode($node, VisitorContext $ctx) |
65 | 65 | { |
66 | - if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0) { |
|
66 | + if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0){ |
|
67 | 67 | $parent = $ctx->getParentNode(); |
68 | - if (!$parent instanceof AttributedInterface) { |
|
68 | + if (!$parent instanceof AttributedInterface){ |
|
69 | 69 | throw new LogicException(sprintf( |
70 | 70 | 'Unable to extend non attributable node (%s)', |
71 | 71 | is_object($node) ? get_class($node) : gettype($node) |
@@ -78,20 +78,20 @@ discard block |
||
78 | 78 | } |
79 | 79 | |
80 | 80 | // extend current node |
81 | - if ($node instanceof AttributedInterface && $node->getAttribute(self::class) !== null) { |
|
81 | + if ($node instanceof AttributedInterface && $node->getAttribute(self::class) !== null){ |
|
82 | 82 | /** @var Tag $extends */ |
83 | 83 | $extends = $node->getAttribute(self::class); |
84 | 84 | |
85 | - foreach ($node->nodes as $child) { |
|
85 | + foreach ($node->nodes as $child){ |
|
86 | 86 | $extends->nodes[] = $child; |
87 | 87 | } |
88 | 88 | |
89 | 89 | $path = 'undefined'; |
90 | - try { |
|
90 | + try{ |
|
91 | 91 | $path = $this->getPath($extends); |
92 | 92 | |
93 | 93 | return $this->merger->merge($this->builder->load($path), $extends); |
94 | - } catch (\Throwable $e) { |
|
94 | + }catch (\Throwable $e){ |
|
95 | 95 | throw new ExtendsException( |
96 | 96 | "Unable to extend parent `{$path}`", |
97 | 97 | $extends->getContext(), |
@@ -109,14 +109,14 @@ discard block |
||
109 | 109 | */ |
110 | 110 | private function getPath(Tag $tag): string |
111 | 111 | { |
112 | - if (strpos($tag->name, $this->extendsKeyword . ':') === 0) { |
|
112 | + if (strpos($tag->name, $this->extendsKeyword.':') === 0){ |
|
113 | 113 | $name = substr($tag->name, strlen($this->extendsKeyword) + 1); |
114 | 114 | |
115 | 115 | return str_replace(['.'], DIRECTORY_SEPARATOR, $name); |
116 | 116 | } |
117 | 117 | |
118 | - foreach ($tag->attrs as $attr) { |
|
119 | - if ($attr->name === 'path' && is_string($attr->value)) { |
|
118 | + foreach ($tag->attrs as $attr){ |
|
119 | + if ($attr->name === 'path' && is_string($attr->value)){ |
|
120 | 120 | return trim($attr->value, '\'"'); |
121 | 121 | } |
122 | 122 | } |
@@ -51,7 +51,8 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public function enterNode($node, VisitorContext $ctx) |
53 | 53 | { |
54 | - if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0) { |
|
54 | + if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0) |
|
55 | + { |
|
55 | 56 | return self::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
56 | 57 | } |
57 | 58 | |
@@ -63,9 +64,11 @@ discard block |
||
63 | 64 | */ |
64 | 65 | public function leaveNode($node, VisitorContext $ctx) |
65 | 66 | { |
66 | - if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0) { |
|
67 | + if ($node instanceof Tag && strpos($node->name, $this->extendsKeyword) === 0) |
|
68 | + { |
|
67 | 69 | $parent = $ctx->getParentNode(); |
68 | - if (!$parent instanceof AttributedInterface) { |
|
70 | + if (!$parent instanceof AttributedInterface) |
|
71 | + { |
|
69 | 72 | throw new LogicException(sprintf( |
70 | 73 | 'Unable to extend non attributable node (%s)', |
71 | 74 | is_object($node) ? get_class($node) : gettype($node) |
@@ -78,20 +81,25 @@ discard block |
||
78 | 81 | } |
79 | 82 | |
80 | 83 | // extend current node |
81 | - if ($node instanceof AttributedInterface && $node->getAttribute(self::class) !== null) { |
|
84 | + if ($node instanceof AttributedInterface && $node->getAttribute(self::class) !== null) |
|
85 | + { |
|
82 | 86 | /** @var Tag $extends */ |
83 | 87 | $extends = $node->getAttribute(self::class); |
84 | 88 | |
85 | - foreach ($node->nodes as $child) { |
|
89 | + foreach ($node->nodes as $child) |
|
90 | + { |
|
86 | 91 | $extends->nodes[] = $child; |
87 | 92 | } |
88 | 93 | |
89 | 94 | $path = 'undefined'; |
90 | - try { |
|
95 | + try |
|
96 | + { |
|
91 | 97 | $path = $this->getPath($extends); |
92 | 98 | |
93 | 99 | return $this->merger->merge($this->builder->load($path), $extends); |
94 | - } catch (\Throwable $e) { |
|
100 | + } |
|
101 | + catch (\Throwable $e) |
|
102 | + { |
|
95 | 103 | throw new ExtendsException( |
96 | 104 | "Unable to extend parent `{$path}`", |
97 | 105 | $extends->getContext(), |
@@ -109,14 +117,17 @@ discard block |
||
109 | 117 | */ |
110 | 118 | private function getPath(Tag $tag): string |
111 | 119 | { |
112 | - if (strpos($tag->name, $this->extendsKeyword . ':') === 0) { |
|
120 | + if (strpos($tag->name, $this->extendsKeyword . ':') === 0) |
|
121 | + { |
|
113 | 122 | $name = substr($tag->name, strlen($this->extendsKeyword) + 1); |
114 | 123 | |
115 | 124 | return str_replace(['.'], DIRECTORY_SEPARATOR, $name); |
116 | 125 | } |
117 | 126 | |
118 | - foreach ($tag->attrs as $attr) { |
|
119 | - if ($attr->name === 'path' && is_string($attr->value)) { |
|
127 | + foreach ($tag->attrs as $attr) |
|
128 | + { |
|
129 | + if ($attr->name === 'path' && is_string($attr->value)) |
|
130 | + { |
|
120 | 131 | return trim($attr->value, '\'"'); |
121 | 132 | } |
122 | 133 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function enterNode($node, VisitorContext $ctx) |
55 | 55 | { |
56 | - if ($node instanceof Tag && strpos($node->name, $this->useKeyword) === 0) { |
|
56 | + if ($node instanceof Tag && strpos($node->name, $this->useKeyword) === 0){ |
|
57 | 57 | return self::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
58 | 58 | } |
59 | 59 | |
@@ -65,23 +65,23 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function leaveNode($node, VisitorContext $ctx) |
67 | 67 | { |
68 | - if (!$node instanceof Tag) { |
|
68 | + if (!$node instanceof Tag){ |
|
69 | 69 | return null; |
70 | 70 | } |
71 | 71 | |
72 | 72 | $importCtx = ImportContext::on($ctx); |
73 | 73 | |
74 | 74 | // import definition |
75 | - if (strpos($node->name, $this->useKeyword) === 0) { |
|
75 | + if (strpos($node->name, $this->useKeyword) === 0){ |
|
76 | 76 | $importCtx->add($this->makeImport($node)); |
77 | 77 | |
78 | 78 | return self::REMOVE_NODE; |
79 | 79 | } |
80 | 80 | |
81 | 81 | // imported tag |
82 | - try { |
|
82 | + try{ |
|
83 | 83 | $import = $importCtx->resolve($this->builder, $node->name); |
84 | - } catch (\Throwable $e) { |
|
84 | + }catch (\Throwable $e){ |
|
85 | 85 | throw new ImportException( |
86 | 86 | "Unable to resolve import `{$node->name}`", |
87 | 87 | $node->getContext(), |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | ); |
90 | 90 | } |
91 | 91 | |
92 | - if ($import !== null) { |
|
92 | + if ($import !== null){ |
|
93 | 93 | $node = $this->merger->merge($import, $node); |
94 | 94 | |
95 | 95 | return $this->merger->isolateNodes($node, $import->getContext()->getPath()); |
@@ -107,13 +107,13 @@ discard block |
||
107 | 107 | private function makeImport(Tag $tag): ImportInterface |
108 | 108 | { |
109 | 109 | $options = []; |
110 | - foreach ($tag->attrs as $attr) { |
|
111 | - if (is_string($attr->value)) { |
|
110 | + foreach ($tag->attrs as $attr){ |
|
111 | + if (is_string($attr->value)){ |
|
112 | 112 | $options[$attr->name] = trim($attr->value, '\'"'); |
113 | 113 | } |
114 | 114 | } |
115 | 115 | |
116 | - switch (strtolower($tag->name)) { |
|
116 | + switch (strtolower($tag->name)){ |
|
117 | 117 | case 'use': |
118 | 118 | case 'use:element': |
119 | 119 | $this->assertHasOption('path', $options, $tag); |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | */ |
165 | 165 | private function assertHasOption(string $option, array $options, Tag $tag): void |
166 | 166 | { |
167 | - if (!isset($options[$option])) { |
|
167 | + if (!isset($options[$option])){ |
|
168 | 168 | throw new ImportException("Missing `{$option}` option", $tag->getContext()); |
169 | 169 | } |
170 | 170 | } |
@@ -53,7 +53,8 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function enterNode($node, VisitorContext $ctx) |
55 | 55 | { |
56 | - if ($node instanceof Tag && strpos($node->name, $this->useKeyword) === 0) { |
|
56 | + if ($node instanceof Tag && strpos($node->name, $this->useKeyword) === 0) |
|
57 | + { |
|
57 | 58 | return self::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
58 | 59 | } |
59 | 60 | |
@@ -65,23 +66,28 @@ discard block |
||
65 | 66 | */ |
66 | 67 | public function leaveNode($node, VisitorContext $ctx) |
67 | 68 | { |
68 | - if (!$node instanceof Tag) { |
|
69 | + if (!$node instanceof Tag) |
|
70 | + { |
|
69 | 71 | return null; |
70 | 72 | } |
71 | 73 | |
72 | 74 | $importCtx = ImportContext::on($ctx); |
73 | 75 | |
74 | 76 | // import definition |
75 | - if (strpos($node->name, $this->useKeyword) === 0) { |
|
77 | + if (strpos($node->name, $this->useKeyword) === 0) |
|
78 | + { |
|
76 | 79 | $importCtx->add($this->makeImport($node)); |
77 | 80 | |
78 | 81 | return self::REMOVE_NODE; |
79 | 82 | } |
80 | 83 | |
81 | 84 | // imported tag |
82 | - try { |
|
85 | + try |
|
86 | + { |
|
83 | 87 | $import = $importCtx->resolve($this->builder, $node->name); |
84 | - } catch (\Throwable $e) { |
|
88 | + } |
|
89 | + catch (\Throwable $e) |
|
90 | + { |
|
85 | 91 | throw new ImportException( |
86 | 92 | "Unable to resolve import `{$node->name}`", |
87 | 93 | $node->getContext(), |
@@ -89,7 +95,8 @@ discard block |
||
89 | 95 | ); |
90 | 96 | } |
91 | 97 | |
92 | - if ($import !== null) { |
|
98 | + if ($import !== null) |
|
99 | + { |
|
93 | 100 | $node = $this->merger->merge($import, $node); |
94 | 101 | |
95 | 102 | return $this->merger->isolateNodes($node, $import->getContext()->getPath()); |
@@ -107,13 +114,16 @@ discard block |
||
107 | 114 | private function makeImport(Tag $tag): ImportInterface |
108 | 115 | { |
109 | 116 | $options = []; |
110 | - foreach ($tag->attrs as $attr) { |
|
111 | - if (is_string($attr->value)) { |
|
117 | + foreach ($tag->attrs as $attr) |
|
118 | + { |
|
119 | + if (is_string($attr->value)) |
|
120 | + { |
|
112 | 121 | $options[$attr->name] = trim($attr->value, '\'"'); |
113 | 122 | } |
114 | 123 | } |
115 | 124 | |
116 | - switch (strtolower($tag->name)) { |
|
125 | + switch (strtolower($tag->name)) |
|
126 | + { |
|
117 | 127 | case 'use': |
118 | 128 | case 'use:element': |
119 | 129 | $this->assertHasOption('path', $options, $tag); |
@@ -164,7 +174,8 @@ discard block |
||
164 | 174 | */ |
165 | 175 | private function assertHasOption(string $option, array $options, Tag $tag): void |
166 | 176 | { |
167 | - if (!isset($options[$option])) { |
|
177 | + if (!isset($options[$option])) |
|
178 | + { |
|
168 | 179 | throw new ImportException("Missing `{$option}` option", $tag->getContext()); |
169 | 180 | } |
170 | 181 | } |