Checker.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\App\Requirements;
  3. /**
  4. * Description of Handler
  5. *
  6. * @author INBSX-37H
  7. */
  8. class Checker
  9. {
  10. /**
  11. * \ModulesGarden\Servers\ProxmoxVps\Core\FileReader\PathValidator
  12. * @var type null|\ModulesGarden\Servers\ProxmoxVps\Core\FileReader\|\ModulesGarden\Servers\ProxmoxVps\Core\FileReader\Directory
  13. */
  14. protected $directoryHandler = null;
  15. protected $requirementsList = [];
  16. protected $checkResaults = [];
  17. /**
  18. * Paths for Requirements class to be placed in
  19. * @var type array
  20. */
  21. const PATHS = [
  22. 'app' . DIRECTORY_SEPARATOR . 'Configuration' . DIRECTORY_SEPARATOR . 'Requirements',
  23. 'core' . DIRECTORY_SEPARATOR . 'Configuration' . DIRECTORY_SEPARATOR . 'Requirements'
  24. ];
  25. public function __construct()
  26. {
  27. $this->directoryHandler = new \ModulesGarden\Servers\ProxmoxVps\Core\FileReader\Directory();
  28. $this->loadRequirements();
  29. $this->checkRequirements();
  30. }
  31. protected function loadRequirements()
  32. {
  33. foreach (self::PATHS as $path)
  34. {
  35. $this->loadClassesByPath($path);
  36. }
  37. }
  38. protected function loadClassesByPath($path)
  39. {
  40. $fullPath = \ModulesGarden\Servers\ProxmoxVps\Core\ModuleConstants::getModuleRootDir() . DIRECTORY_SEPARATOR . $path;
  41. $files = $this->directoryHandler->getFilesList($fullPath, '.php', true);
  42. $classNamespace = $this->getClassNamespaceByPath($path);
  43. foreach ($files as $file)
  44. {
  45. $className = $classNamespace . $file;
  46. if (!class_exists($className) || !is_subclass_of($className, RequirementInterface::class))
  47. {
  48. continue;
  49. }
  50. $this->requirementsList[] = $className;
  51. }
  52. }
  53. protected function getClassNamespaceByPath($path)
  54. {
  55. $contextParts = explode('\\', self::class);
  56. $coreParts = explode(DIRECTORY_SEPARATOR, $path);
  57. $mergeArray = in_array($contextParts[1], ['Servers', 'Registrars'])
  58. ? [$contextParts[0], $contextParts[1], $contextParts[2]] : [$contextParts[0], $contextParts[1]];
  59. $allParts = array_merge($mergeArray, $coreParts);
  60. array_walk($allParts, function(&$item){
  61. $item = ucfirst($item);
  62. });
  63. return '\\' . implode('\\', $allParts) . '\\';
  64. }
  65. protected function checkRequirements()
  66. {
  67. foreach ($this->requirementsList as $requirement)
  68. {
  69. $instance = \ModulesGarden\Servers\ProxmoxVps\Core\DependencyInjection\DependencyInjection::call($requirement);
  70. $handler = $instance->getHandlerInstance();
  71. $this->checkResaults = array_merge($this->checkResaults, $handler->getUnfulfilledRequirements());
  72. }
  73. }
  74. public function getUnfulfilledRequirements()
  75. {
  76. return $this->checkResaults;
  77. }
  78. }