PackageManager.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace common\components;
  3. use common\models\Module;
  4. use common\modules\ModuleInfo;
  5. use plugins\Plugin;
  6. use yii\base\Component;
  7. use yii\helpers\FileHelper;
  8. class PackageManager extends Component
  9. {
  10. public $paths;
  11. public $type;
  12. public $namespace;
  13. public $infoClass;
  14. public $packages;
  15. public $packgeConfigName = "package-config";
  16. /**
  17. * Looks for packages in the specified directories and creates the objects
  18. */
  19. public function findAll($paths=[])
  20. {
  21. if(!empty($this->packages))
  22. {
  23. return $this->packages;
  24. }
  25. $this->packages = [];
  26. if(empty($paths))
  27. {
  28. $paths = $this->paths;
  29. }
  30. foreach ($paths as $path) {
  31. $path = \Yii::getAlias($path);
  32. $fileSystem = new \FilesystemIterator($path);
  33. foreach ($fileSystem as $item) {
  34. if ($item->isDir()) {
  35. $path = $item;
  36. if (($package = $this->findByPath($path)) == null) {
  37. continue;
  38. }
  39. $this->packages[$package->getPackage()] = $package;
  40. }
  41. }
  42. }
  43. return $this->packages;
  44. }
  45. public function findByPath($path)
  46. {
  47. $aliasFile = $path . '/alias.php';
  48. if (file_exists($aliasFile)) {
  49. $alias = include $aliasFile;
  50. \Yii::setAlias('@' . $alias, $path);
  51. $class = $alias . '\\' . $this->infoClass;
  52. } else {
  53. $path = new \SplFileInfo($path);
  54. $class = $this->namespace . $path->getBasename() . '\\' . $this->infoClass;
  55. }
  56. if (class_exists($class)) {
  57. $config = [
  58. 'class' => $class
  59. ];
  60. $package = \Yii::createObject($config);
  61. return $package;
  62. }
  63. return null;
  64. }
  65. public function deletePackage($package)
  66. {
  67. FileHelper::removeDirectory($package->getPath());
  68. }
  69. /**
  70. * @param $id
  71. * @return ModuleInfo|Plugin
  72. */
  73. public function findOne($id)
  74. {
  75. $packages = $this->findAll();
  76. if (isset($packages[$id])) {
  77. return $packages[$id];
  78. }
  79. return null;
  80. }
  81. }