198 lines
4.9 KiB
PHP
198 lines
4.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace Builder;
|
|
use Hyperf\Utils\Filesystem\Filesystem;
|
|
class Base
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private static string $version = '1.0.0';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
private string $appPath = '';
|
|
|
|
|
|
private string $addonPath = '';
|
|
|
|
/**
|
|
* 模块信息
|
|
* @var array
|
|
*/
|
|
private array $moduleInfo = [];
|
|
|
|
/**
|
|
* 插件信息
|
|
* @var array
|
|
*/
|
|
private array $AddonInfo = [];
|
|
|
|
|
|
/**
|
|
* @throws \Psr\Container\ContainerExceptionInterface
|
|
* @throws \Psr\Container\NotFoundExceptionInterface
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->setAppPath(BASE_PATH . '/app');
|
|
$this->setAddonPath(BASE_PATH . '/addon');
|
|
$this->scanModule();
|
|
$this->scanAddon();
|
|
}
|
|
|
|
/**
|
|
* @throws \Psr\Container\ContainerExceptionInterface
|
|
* @throws \Psr\Container\NotFoundExceptionInterface
|
|
*/
|
|
public function scanModule(): void
|
|
{
|
|
$modules = glob(self::getAppPath() . '*');
|
|
$fs = container()->get(Filesystem::class);
|
|
$infos = [];
|
|
foreach ($modules as &$mod) if (is_dir($mod)) {
|
|
$modInfo = $mod . DIRECTORY_SEPARATOR . 'info.json';
|
|
if (file_exists($modInfo)) {
|
|
$infos[basename($mod)] = json_decode($fs->sharedGet($modInfo), true);
|
|
}
|
|
}
|
|
$this->setModuleInfo($infos);
|
|
}
|
|
|
|
/**
|
|
* @throws \Psr\Container\ContainerExceptionInterface
|
|
* @throws \Psr\Container\NotFoundExceptionInterface
|
|
*/
|
|
public function scanAddon(): void
|
|
{
|
|
$modules = glob(self::getAddonPath() . '*');
|
|
$fs = container()->get(Filesystem::class);
|
|
$infos = [];
|
|
foreach ($modules as &$mod) if (is_dir($mod)) {
|
|
$modInfo = $mod . DIRECTORY_SEPARATOR . 'info.json';
|
|
if (file_exists($modInfo)) {
|
|
$infos[basename($mod)] = json_decode($fs->sharedGet($modInfo), true);
|
|
}
|
|
}
|
|
$this->setAddonInfo($infos);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public static function getVersion(): string
|
|
{
|
|
return self::$version;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getAppPath(): string
|
|
{
|
|
return $this->appPath . DIRECTORY_SEPARATOR;
|
|
}
|
|
|
|
/**
|
|
* @return mixed
|
|
*/
|
|
public function getAddonPath(): string
|
|
{
|
|
return $this->addonPath . DIRECTORY_SEPARATOR;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $appPath
|
|
*/
|
|
public function setAppPath(string $appPath): void
|
|
{
|
|
$this->appPath = $appPath;
|
|
}
|
|
|
|
/**
|
|
* @param base $appPath
|
|
*/
|
|
public function setAddonPath(string $path): void
|
|
{
|
|
$this->addonPath = $path;
|
|
}
|
|
|
|
/**
|
|
* 获取模块信息
|
|
* @param string|null $name
|
|
* @return base
|
|
*/
|
|
public function getModuleInfo(string $name = null): array
|
|
{
|
|
if (empty($name)) {
|
|
return $this->moduleInfo;
|
|
}
|
|
return $this->moduleInfo[$name] ?? [];
|
|
}
|
|
|
|
/**
|
|
* 获取插件信息
|
|
* @param string|null $name
|
|
* @return base
|
|
*/
|
|
public function getAddonInfo(string $name = null): array
|
|
{
|
|
if (empty($name)) {
|
|
return $this->AddonInfo;
|
|
}
|
|
return $this->AddonInfo[$name] ?? [];
|
|
}
|
|
|
|
/**
|
|
* @param mixed $moduleInfo
|
|
*/
|
|
public function setModuleInfo($moduleInfo): void
|
|
{
|
|
$this->moduleInfo = $moduleInfo;
|
|
}
|
|
|
|
/**
|
|
* @param base $AddonInfo
|
|
*/
|
|
public function setAddonInfo($AddonInfo): void
|
|
{
|
|
$this->AddonInfo = $AddonInfo;
|
|
}
|
|
/**
|
|
* @param String $key
|
|
* @param string $value
|
|
* @param false $save
|
|
* @return bool
|
|
* @throws \Psr\Container\ContainerExceptionInterface
|
|
* @throws \Psr\Container\NotFoundExceptionInterface
|
|
*/
|
|
public function setModuleConfigValue(String $key, string $value, bool $save = false): bool
|
|
{
|
|
if (strpos($key, '.') > 0) {
|
|
list($mod, $name) = explode('.', $key);
|
|
if (isset($this->moduleInfo[$mod]) && isset($this->moduleInfo[$mod][$name])) {
|
|
$this->moduleInfo[$mod][$name] = $value;
|
|
$save && $this->saveModuleConfig($mod);
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
/**
|
|
* @param string $mod
|
|
* @throws \Psr\Container\ContainerExceptionInterface
|
|
* @throws \Psr\Container\NotFoundExceptionInterface
|
|
*/
|
|
protected function saveModuleConfig(string $mod): void
|
|
{
|
|
if (!empty($mod)) {
|
|
$fs = container()->get(Filesystem::class);
|
|
$modJson = $this->getAppPath() . $mod . DIRECTORY_SEPARATOR . 'info.json';
|
|
if (! $fs->isWritable($modJson)) {
|
|
$fs->chmod($modJson, 666);
|
|
}
|
|
$fs->put($modJson, \json_encode($this->getModuleInfo($mod), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
|
|
}
|
|
}
|
|
} |