ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.arConverter.php
Go to the documentation of this file.
1<?php
2
27class arConverter
28{
29 public const REGEX = "/([a-z]*)\\((\\d*)\\)/u";
30 protected static array $field_map = [
31 'varchar' => arField::FIELD_TYPE_TEXT,
34 'tinyint' => arField::FIELD_TYPE_INTEGER,
35 'smallint' => arField::FIELD_TYPE_INTEGER,
36 'mediumint' => arField::FIELD_TYPE_INTEGER,
38 ];
39 protected static array $length_map = [
46 arField::FIELD_TYPE_INTEGER => [11 => 4, 4 => 1]
47 ];
48 protected string $table_name = '';
49 protected string $class_name = '';
50 protected array $structure = [];
51 protected array $ids = [];
52
53 public function __construct(string $table_name, string $class_name)
54 {
55 $this->setClassName($class_name);
56 $this->setTableName($table_name);
57 $this->readStructure();
58 }
59
60 public function readStructure(): void
61 {
62 $sql = 'DESCRIBE ' . $this->getTableName();
63 $ilDBStatement = self::getDB()->query($sql);
64 while ($data = self::getDB()->fetchObject($ilDBStatement)) {
65 $this->addStructure($data);
66 }
67 }
68
69 public function downloadClassFile(): void
70 {
71 $header = "<?php
72require_once('./components/ILIAS/ActiveRecord/class.ActiveRecord.php');
73
80class {CLASS_NAME} extends ActiveRecord {
81
86 static function returnDbTableName() {
87 return '{TABLE_NAME}';
88 }
89
93 public function getConnectorContainerName() {
94 return '{TABLE_NAME}';
95 }
96";
97 $txt = str_replace(
98 ['{CLASS_NAME}', '{TABLE_NAME}'],
99 [$this->getClassName(), $this->getTableName()],
100 $header
101 );
102 $all_members = '';
103 foreach ($this->getStructure() as $str) {
104 $member = "/**
105 * @var {DECLARATION}
106 *\n";
107 foreach ($this->returnAttributesForField($str) as $name => $value) {
108 $member .= ' * @con_' . $name . ' ' . $value . "\n";
109 }
110
111 $member .= "*/
112 protected \${FIELD_NAME};
113
114 ";
115
116 $member = str_replace(['{FIELD_NAME}', '{DECLARATION}'], [$str->Field, ' '], $member);
117
118 $all_members .= $member;
119 }
120 $txt = $txt . $all_members . '
121}
122
123?>';
124
125 // echo '<pre>' . print_r(, 1) . '</pre>';
126
127 header('Content-type: application/x-httpd-php');
128 header("Content-Disposition: attachment; filename=\"class." . $this->getClassName() . ".php\"");
129 echo $txt;
130 exit;
131 }
132
136 protected function returnAttributesForField(stdClass $field): array
137 {
138 $attributes = [];
139 $attributes[arFieldList::HAS_FIELD] = 'true';
140 $attributes[arFieldList::FIELDTYPE] = self::lookupFieldType($field->Type);
141 $attributes[arFieldList::LENGTH] = self::lookupFieldLength($field->Type);
142
143 if ($field->Null === 'NO') {
144 $attributes[arFieldList::IS_NOTNULL] = 'true';
145 }
146
147 if ($field->Key === 'PRI') {
148 $attributes[arFieldList::IS_PRIMARY] = 'true';
149 }
150
151 return $attributes;
152 }
153
154 protected static function lookupFieldType(string $field_name): string
155 {
156 preg_match(self::REGEX, $field_name, $matches);
157
158 return self::$field_map[$matches[1]];
159 }
160
164 protected static function lookupFieldLength(string $field_name)
165 {
166 $field_type = self::lookupFieldType($field_name);
167
168 preg_match(self::REGEX, $field_name, $matches);
169
170 if (self::$length_map[$field_type][$matches[2]]) {
171 return self::$length_map[$field_type][$matches[2]];
172 }
173
174 return $matches[2];
175 }
176
177 public static function getDB(): ilDBInterface
178 {
179 global $DIC;
180
181 return $DIC['ilDB'];
182 }
183
184 public function setTableName(string $table_name): void
185 {
186 $this->table_name = $table_name;
187 }
188
189 public function getTableName(): string
190 {
191 return $this->table_name;
192 }
193
197 public function setStructure(array $structure): void
198 {
199 $this->structure = $structure;
200 }
201
205 public function getStructure(): array
206 {
207 return $this->structure;
208 }
209
210 public function addStructure(stdClass $structure): void
211 {
212 if (!in_array($structure->Field, $this->ids)) {
213 $this->structure[] = $structure;
214 $this->ids[] = $structure->Field;
215 }
216 }
217
218 public function setClassName(string $class_name): void
219 {
220 $this->class_name = $class_name;
221 }
222
223 public function getClassName(): string
224 {
225 return $this->class_name;
226 }
227}
$structure
TOTAL STRUCTURE.
const FIELD_TYPE_TEXT
const FIELD_TYPE_FLOAT
const FIELD_TYPE_DATE
const FIELD_TYPE_TIME
const FIELD_TYPE_INTEGER
const FIELD_TYPE_TIMESTAMP
const FIELD_TYPE_CLOB
exit
$txt
Definition: error.php:31
Interface ilDBInterface.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26