12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace common\components;
- use common\models\Module;
- use common\modules\ModuleInfo;
- use plugins\Plugin;
- use yii\base\Component;
- use yii\helpers\FileHelper;
- class PackageManager extends Component
- {
- public $paths;
- public $type;
- public $namespace;
- public $infoClass;
- public $packages;
- public $packgeConfigName = "package-config";
- /**
- * Looks for packages in the specified directories and creates the objects
- */
- public function findAll($paths=[])
- {
- if(!empty($this->packages))
- {
- return $this->packages;
- }
- $this->packages = [];
- if(empty($paths))
- {
- $paths = $this->paths;
- }
- foreach ($paths as $path) {
- $path = \Yii::getAlias($path);
- $fileSystem = new \FilesystemIterator($path);
- foreach ($fileSystem as $item) {
- if ($item->isDir()) {
- $path = $item;
- if (($package = $this->findByPath($path)) == null) {
- continue;
- }
- $this->packages[$package->getPackage()] = $package;
- }
- }
- }
- return $this->packages;
- }
- public function findByPath($path)
- {
- $aliasFile = $path . '/alias.php';
- if (file_exists($aliasFile)) {
- $alias = include $aliasFile;
- \Yii::setAlias('@' . $alias, $path);
- $class = $alias . '\\' . $this->infoClass;
- } else {
- $path = new \SplFileInfo($path);
- $class = $this->namespace . $path->getBasename() . '\\' . $this->infoClass;
- }
- if (class_exists($class)) {
- $config = [
- 'class' => $class
- ];
- $package = \Yii::createObject($config);
- return $package;
- }
- return null;
- }
- public function deletePackage($package)
- {
- FileHelper::removeDirectory($package->getPath());
- }
- /**
- * @param $id
- * @return ModuleInfo|Plugin
- */
- public function findOne($id)
- {
- $packages = $this->findAll();
- if (isset($packages[$id])) {
- return $packages[$id];
- }
- return null;
- }
- }
|