Checker.php 2.9 KB

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