ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilComponentDefinitionReader.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
26 protected array $processors;
27
28 public function __construct(
30 ) {
31 $this->processors = $processor;
32 }
33
38 public function purge(): void
39 {
40 foreach ($this->processors as $p) {
41 $p->purge();
42 }
43 }
44
49 public function readComponentDefinitions(): void
50 {
51 foreach ($this->getComponents() as [$type, $component, $path]) {
52 $file = $this->readFile($path);
53 foreach ($this->processors as $processor) {
54 $processor->beginComponent($component, $type);
55 }
56 $this->parseComponentXML($type, $component, $file);
57 foreach ($this->processors as $processor) {
58 $processor->endComponent($component, $type);
59 }
60 }
61 }
62
63 protected function readFile(string $path): string
64 {
65 if (!file_exists($path)) {
66 throw new \InvalidArgumentException(
67 "Cannot find file $path."
68 );
69 }
70 return file_get_contents($path);
71 }
72
73 protected function parseComponentXML(string $type, string $component, string $xml): void
74 {
75 $xml_parser = null;
76 try {
77 $xml_parser = xml_parser_create("UTF-8");
78 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
79 xml_set_element_handler($xml_parser, $this->beginTag(...), $this->endTag(...));
80 if (!xml_parse($xml_parser, $xml)) {
81 $code = xml_get_error_code($xml_parser);
82 $line = xml_get_current_line_number($xml_parser);
83 $col = xml_get_current_column_number($xml_parser);
84 $msg = xml_error_string($code);
85 throw new \InvalidArgumentException(
86 "Error $code component xml of $type/$component, on line $line in column $col: $msg"
87 );
88 }
89 } finally {
90 if ($xml_parser) {
91 xml_parser_free($xml_parser);
92 }
93 }
94 }
95
96 public function beginTag($_, string $name, array $attributes): void
97 {
98 foreach ($this->processors as $processor) {
99 $processor->beginTag($name, $attributes);
100 }
101 }
102
103 public function endTag($_, string $name): void
104 {
105 foreach ($this->processors as $processor) {
106 $processor->endTag($name);
107 }
108 }
109
113 protected function getComponents(): \Iterator
114 {
115 foreach ($this->getComponentInfo("components/ILIAS", "module.xml") as $i) {
116 yield $i;
117 }
118 foreach ($this->getComponentInfo("components/ILIAS", "service.xml") as $i) {
119 yield $i;
120 }
121 }
122
126 protected function getComponentInfo(string $type, string $name): \Iterator
127 {
128 $dir = __DIR__ . "/../../../../" . $type;
129 foreach ($this->getComponentPaths($dir, $name) as $c) {
130 yield [
131 $type,
132 $c,
133 realpath($dir . "/" . $c . "/" . $name)
134 ];
135 }
136 }
137
141 protected function getComponentPaths(string $root, string $name): \Iterator
142 {
143 $dir = opendir($root);
144 while ($sub = readdir($dir)) {
145 if ($sub === "." || $sub === "..") {
146 continue;
147 }
148 if (!is_dir($root . "/" . $sub)) {
149 continue;
150 }
151 if (!is_file($root . "/" . $sub . "/" . $name)) {
152 continue;
153 }
154 yield $sub;
155 }
156 }
157}
beginTag($_, string $name, array $attributes)
readComponentDefinitions()
This reads the component.xml of all components in the core and processes them with the provided proce...
__construct(ilComponentDefinitionProcessor ... $processor)
purge()
This methods is supposed to purge existing data in the registered processor.
parseComponentXML(string $type, string $component, string $xml)
$c
Definition: deliver.php:25
An ilComponentDefinitionProcessor processes some attributes from a component.xml (i....
$path
Definition: ltiservices.php:30