PackageManager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace ModulesGarden\Servers\KerioEmail\Core\App\Packages;
  3. use ModulesGarden\Servers\KerioEmail\Core\FileReader\Directory;
  4. use ModulesGarden\Servers\KerioEmail\Core\ModuleConstants;
  5. class PackageManager
  6. {
  7. const STATUS_ACTIVE = 'active';
  8. const STATUS_INACTIVE = 'inactive';
  9. const STATUS_REASON_INACTIVE = 'Inactive in the configuration';
  10. const STATUS_REASON_CONFIG_MISSING = 'Inactive because of lack of the configuration file';
  11. protected $packageList = [];
  12. public function __construct()
  13. {
  14. $this->loadPackages();
  15. }
  16. public function getPackageConfiguration($packageName)
  17. {
  18. if (isset($this->packageList[$packageName]))
  19. {
  20. return $this->packageList[$packageName];
  21. }
  22. return null;
  23. }
  24. public function getPackagesConfiguration()
  25. {
  26. return $this->packageList;
  27. }
  28. protected function loadPackages()
  29. {
  30. $packageList = $this->getPackageListByDirectory();
  31. foreach ($packageList as $className)
  32. {
  33. $config = new $className();
  34. $packageName = $config->getName();
  35. if ($this->isNameValid($packageName) && $this->isVersionValid($config->{PackageConfigurationConst::VERSION}) && $this->isStatusValid($config->{AppPackageConfiguration::PACKAGE_STATUS}))
  36. {
  37. $this->packageList[$packageName] = $config;
  38. }
  39. }
  40. }
  41. public function getPackageListByDirectory()
  42. {
  43. $directoryHelper = new Directory();
  44. $packagesDirectory = ModuleConstants::getModuleRootDir() . DIRECTORY_SEPARATOR . 'packages';
  45. $packagesList = $directoryHelper->getFilesList($packagesDirectory);
  46. $existing = [];
  47. foreach ($packagesList as $packageName)
  48. {
  49. $className = '\ModulesGarden\Servers\KerioEmail\Packages\\' . $packageName . '\Config\PackageConfiguration';
  50. if (class_exists($className) && is_subclass_of($className, BasePackageConfiguration::class))
  51. {
  52. $existing[] = $className;
  53. }
  54. }
  55. return $existing;
  56. }
  57. public function isNameValid($name = null)
  58. {
  59. if(trim($name) === '' || !is_string($name))
  60. {
  61. return false;
  62. }
  63. return true;
  64. }
  65. public function isVersionValid($version = null)
  66. {
  67. if(trim($version) === '' || !is_string($version))
  68. {
  69. return false;
  70. }
  71. return true;
  72. }
  73. public function isStatusValid($status = null)
  74. {
  75. if(!in_array($status, [AppPackageConfiguration::PACKAGE_STATUS_ACTIVE, AppPackageConfiguration::PACKAGE_STATUS_INACTIVE]))
  76. {
  77. return false;
  78. }
  79. return true;
  80. }
  81. }