ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilXmlSchemaFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use SplFileObject;
24 use SplFileInfo;
26 
28 {
29  private const SCHEMA_DEFINITION_LOCATION = './xml/SchemaValidation';
30 
32 
33  private \ilLogger $logger;
34 
35  public function __construct()
36  {
37  global $DIC;
38 
39  $this->logger = $DIC->logger()->exp();
40  $this->collection = new ilXmlSchemaInfoCollection();
41  $this->readSchemaFiles();
42  }
43 
44  public function getLatest(string $type, string $sub_type = ''): ?SplFileInfo
45  {
46  $collection = $this->getByType($type, $sub_type);
47  if ($collection->count() === 0) {
48  return null;
49  }
50  $latest = $collection->offsetGet($collection->count() - 1);
51  return $latest->getFile();
52  }
53 
54  public function getByVersion(Version $version, string $type, string $sub_type = ''): ?SplFileInfo
55  {
56  $collection = $this->getByType($type, $sub_type);
57  foreach ($collection as $schema_info) {
58  if ($schema_info->getVersion()->equals($version)) {
59  return $schema_info->getFile();
60  }
61  }
62  return null;
63  }
64 
65  public function getByVersionOrLatest(Version $version, string $type, string $sub_type = ''): ?SplFileInfo
66  {
67  $collection = $this->getByType($type, $sub_type);
68  foreach ($collection as $schema_info) {
69  if ($schema_info->getVersion()->equals($version)) {
70  return $schema_info->getFile();
71  }
72  }
73  return $this->getLatest($type, $sub_type);
74  }
75 
76  protected function getByType(string $component, string $sub_type = ''): ilXmlSchemaInfoCollection
77  {
78  $collection = new ilXmlSchemaInfoCollection();
79  foreach ($this->collection as $schema_info) {
80  if ($schema_info->getComponent() === $component && $schema_info->getSubtype() === $sub_type) {
81  $collection[] = $schema_info;
82  $this->logger->info('Found version: ' . $schema_info->getFile()->getFilename());
83  }
84  }
85  return $this->sortByVersion($collection);
86  }
87 
89  {
90  $collection->uasort(function (ilXmlSchemaInfo $a, ilXmlSchemaInfo $b): int {
91  if ($a->getVersion()->equals($b->getVersion())) {
92  return 0;
93  }
94  if ($a->getVersion()->isGreaterThan($b->getVersion())) {
95  return 1;
96  }
97  return -1;
98  });
99  $sorted = new ilXmlSchemaInfoCollection();
100  foreach ($collection as $schema_info) {
101  $sorted[] = $schema_info;
102  }
103  return $sorted;
104  }
105 
106  private function readSchemaFiles(): void
107  {
108  foreach (new \DirectoryIterator(self::SCHEMA_DEFINITION_LOCATION) as $file) {
109  if ($file->isDot()) {
110  $this->logger->debug('Ignoring file (dot file): ' . $file->getFilename());
111  continue;
112  }
113  if ($file->getExtension() !== 'xsd') {
114  $this->logger->debug('Ignoring file (!xsd): ' . $file->getFilename());
115  continue;
116  }
117  $parts = explode('_', $file->getFilename());
118  if (!count($parts)) {
119  $this->logger->debug('Ignoring file (!_separated): ' . $file->getFilename());
120  continue;
121  }
122  if ($parts[0] !== 'ilias') {
123  $this->logger->debug('Ignoring file (!ilias): ' . $file->getFilename() . ' ' . $parts[0]);
124  continue;
125  }
126  $matches = [];
127  if (preg_match('/ilias_([a-zA-Z]+)(_([a-zA-Z]+))?_([3-9]|([1-9][0-9]+))_?([0-9]+)?.xsd/', $file->getFilename(), $matches) !== 1) {
128  $this->logger->debug('Ignoring file (match): ' . $file->getFilename());
129  $this->logger->dump($matches, \ilLogLevel::DEBUG);
130  continue;
131  }
132  $this->collection[] = new ilXmlSchemaInfo(
133  new SplFileInfo($file->getPathname()),
134  (string) $matches[1],
135  (string) $matches[3],
136  new Version((string) $matches[4] . (($matches[6] ?? '') ? '.' . $matches[6] : ''))
137  );
138  $this->logger->debug($file->getFilename() . ' matches');
139  }
140  }
141 }
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:64
getByVersionOrLatest(Version $version, string $type, string $sub_type='')
getByVersion(Version $version, string $type, string $sub_type='')
global $DIC
Definition: feed.php:28
sortByVersion(ilXmlSchemaInfoCollection $collection)
getByType(string $component, string $sub_type='')
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
A version number that consists of three numbers (major, minor, patch).
Definition: Version.php:26
getLatest(string $type, string $sub_type='')
$version
Definition: plugin.php:24