ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ImplementationOfInterfaceFinder.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Setup;
22 
29 {
30  protected string $root;
31 
35  protected array $ignore = [
36  '.*/src/',
37  '.*/libs/',
38  '.*/test/',
39  '.*/tests/',
40  '.*/setup/',
41  // Classes using removed Auth-class from PEAR
42  '.*ilSOAPAuth.*',
43  // Classes using unknown
44  '.*ilPDExternalFeedBlockGUI.*',
45  ];
46 
50  protected ?array $classmap = null;
51 
52  public function __construct()
53  {
54  $this->root = substr(__FILE__, 0, strpos(__FILE__, DIRECTORY_SEPARATOR . "src"));
55  $external_classmap = include "./libs/composer/vendor/composer/autoload_classmap.php";
56  $this->classmap = $external_classmap ?: null;
57  }
58 
70  public function getMatchingClassNames(
71  string $interface,
72  array $additional_ignore = [],
73  string $matching_path = null
74  ): \Iterator {
75  foreach ($this->getAllClassNames($additional_ignore, $matching_path) as $class_name) {
76  try {
77  $r = new \ReflectionClass($class_name);
78  if ($r->isInstantiable() && $r->implementsInterface($interface)) {
79  yield $class_name;
80  }
81  } catch (\Throwable $e) {
82  // noting to do here
83  }
84  }
85  }
86 
90  protected function getAllClassNames(array $additional_ignore, string $matching_path = null): \Iterator
91  {
92  $ignore = array_merge($this->ignore, $additional_ignore);
93 
94  if (!is_array($this->classmap)) {
95  throw new \LogicException("Composer ClassMap not loaded");
96  }
97 
98  $regexp = implode(
99  "|",
100  array_map(
101  // fix path-separators to respect windows' backspaces.
102  fn ($v): string => "(" . str_replace('/', '(/|\\\\)', $v) . ")",
103  $ignore
104  )
105  );
106  if ($matching_path) {
107  $matching_path = str_replace('/', '(/|\\\\)', $matching_path);
108  }
109 
110 
111  foreach ($this->classmap as $class_name => $file_path) {
112  $real_path = realpath($file_path);
113  if ($real_path === false) {
114  throw new \RuntimeException(
115  "Could not find file for class $class_name (path: $file_path). " .
116  "Please check the composer classmap, maybe it is outdated. " .
117  "You can regenerate it by executing 'composer du' or 'composer install' " .
118  "(which also ensures dependencies are correctly installed) in the ILIAS root directory."
119  );
120  }
121 
122  $path = str_replace($this->root, "", $real_path);
123  if ($matching_path && !preg_match("#^" . $matching_path . "$#", $path)) {
124  continue;
125  }
126  if (!preg_match("#^" . $regexp . "$#", $path)) {
127  yield $class_name;
128  }
129  }
130  }
131 }
$path
Definition: ltiservices.php:32
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getMatchingClassNames(string $interface, array $additional_ignore=[], string $matching_path=null)
The matcher finds the class names implementing the given interface, while ignoring paths in self::$ig...
getAllClassNames(array $additional_ignore, string $matching_path=null)