add cs fixer and PHPStan and activate it (build 436)

This commit is contained in:
crazywhalecc
2022-03-15 18:05:33 +08:00
parent d01bd69aa5
commit 1706afbcd0
163 changed files with 4572 additions and 3588 deletions

View File

@@ -1,5 +1,6 @@
<?php
declare(strict_types=1);
namespace ZM\Module;
@@ -16,29 +17,35 @@ use ZM\Utils\ZMUtil;
/**
* 模块构造器
* Class ModulePacker
* @package ZM\Module
* @since 2.5
*/
class ModulePacker
{
const ZM_MODULE_PACKER_VERSION = '1.0';
/** @var array $module */
public const ZM_MODULE_PACKER_VERSION = '1.0';
/** @var array */
private $module = [];
/** @var bool $override */
/** @var bool */
private $override = false;
/** @var string $output_path */
/** @var string */
private $output_path = '';
/** @var string $filename */
/** @var string */
private $filename = '';
/** @var Phar $phar */
/** @var Phar */
private $phar;
/** @var null|array $module_config */
private $module_config = null;
/** @var null|array */
private $module_config;
/**
* @throws ModulePackException
*/
public function __construct(array $module) {
public function __construct(array $module)
{
if (ini_get('phar.readonly') == '1') {
throw new ModulePackException('请先在 php.ini 中设置 `phar.readonly = Off` 后再打包模块!');
}
@@ -52,16 +59,19 @@ class ModulePacker
* 设置输出文件夹
* @param $path
*/
public function setOutputPath($path) {
public function setOutputPath($path)
{
$this->output_path = $path;
if (!is_dir($path)) mkdir($path, 0755, true);
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
}
/**
* 设置是否覆盖
* @param bool $override
*/
public function setOverride(bool $override = true) {
public function setOverride(bool $override = true)
{
$this->override = $override;
}
@@ -69,15 +79,16 @@ class ModulePacker
* 获取模块名字
* @return mixed
*/
public function getName() {
public function getName()
{
return $this->module['name'];
}
/**
* 获取打包的文件名绝对路径
* @return string
*/
public function getFileName(): string {
public function getFileName(): string
{
return $this->filename;
}
@@ -85,7 +96,8 @@ class ModulePacker
* 打包模块
* @throws ZMException
*/
public function pack() {
public function pack()
{
$this->filename = $this->output_path . '/' . $this->module['name'];
if (isset($this->module['version'])) {
$this->filename .= '_' . $this->module['version'];
@@ -111,28 +123,32 @@ class ModulePacker
$this->phar->stopBuffering();
}
private function addFiles() {
private function addFiles()
{
$file_list = DataProvider::scanDirFiles($this->module['module-path'], true, false);
foreach ($file_list as $v) {
$this->phar->addFile($v, $this->getRelativePath($v));
}
}
private function getRelativePath($path) {
private function getRelativePath($path)
{
return str_replace(realpath(DataProvider::getSourceRootDir()) . '/', '', realpath($path));
}
private function generatePharAutoload(): array {
$pos = strpos($this->module['module-path'], DataProvider::getSourceRootDir().'/');
private function generatePharAutoload(): array
{
$pos = strpos($this->module['module-path'], DataProvider::getSourceRootDir() . '/');
if ($pos === 0) {
$path_value = substr($this->module['module-path'], strlen(DataProvider::getSourceRootDir().'/'));
$path_value = substr($this->module['module-path'], strlen(DataProvider::getSourceRootDir() . '/'));
} else {
throw new ModulePackException(zm_internal_errcode("E99999")); //未定义的错误
throw new ModulePackException(zm_internal_errcode('E99999')); //未定义的错误
}
return ZMUtil::getClassesPsr4($this->module['module-path'], $this->module['namespace'], null, $path_value);
}
private function getComposerAutoloadItems(): array {
private function getComposerAutoloadItems(): array
{
$composer = json_decode(file_get_contents(DataProvider::getSourceRootDir() . '/composer.json'), true);
$path = self::getRelativePath($this->module['module-path']);
$item = [];
@@ -153,21 +169,22 @@ class ModulePacker
* @throws ZMException
* @throws Exception
*/
private function addLightCacheStore() {
private function addLightCacheStore()
{
if (isset($this->module['light-cache-store'])) {
$store = [];
$r = ZMConfig::get('global', 'light_cache') ?? [
'size' => 512, //最多允许储存的条数需要2的倍数
'max_strlen' => 32768, //单行字符串最大长度需要2的倍数
'hash_conflict_proportion' => 0.6, //Hash冲突率越大越好但是需要的内存更多
'persistence_path' => DataProvider::getDataFolder() . '_cache.json',
'auto_save_interval' => 900
];
'size' => 512, //最多允许储存的条数需要2的倍数
'max_strlen' => 32768, //单行字符串最大长度需要2的倍数
'hash_conflict_proportion' => 0.6, //Hash冲突率越大越好但是需要的内存更多
'persistence_path' => DataProvider::getDataFolder() . '_cache.json',
'auto_save_interval' => 900,
];
LightCache::init($r);
foreach ($this->module['light-cache-store'] as $v) {
$r = LightCache::get($v);
if ($r === null) {
Console::warning(zm_internal_errcode("E00045") . 'LightCache 项:' . $v . ' 不存在或值为null无法为其保存。');
Console::warning(zm_internal_errcode('E00045') . 'LightCache 项:' . $v . ' 不存在或值为null无法为其保存。');
} else {
$store[$v] = $r;
Console::info('打包LightCache持久化项' . $v);
@@ -177,7 +194,8 @@ class ModulePacker
}
}
private function addModuleConfig() {
private function addModuleConfig()
{
$stub_values = [
'zm-module' => true,
'generated-id' => sha1(strval(microtime(true))),
@@ -187,18 +205,19 @@ class ModulePacker
'autoload-psr-4' => $this->generatePharAutoload(),
'unpack' => [
'composer-autoload-items' => $this->getComposerAutoloadItems(),
'global-config-override' => $this->module['global-config-override'] ?? false
'global-config-override' => $this->module['global-config-override'] ?? false,
],
'allow-hotload' => $this->module["allow-hotload"] ?? false,
'pack-time' => time()
'allow-hotload' => $this->module['allow-hotload'] ?? false,
'pack-time' => time(),
];
$this->phar->addFromString('zmplugin.json', json_encode($stub_values, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->module_config = $stub_values;
}
private function addEntry() {
private function addEntry()
{
$stub_replace = [
'generated_id' => $this->module_config['generated-id']
'generated_id' => $this->module_config['generated-id'],
];
$stub_template = str_replace(
@@ -216,24 +235,25 @@ class ModulePacker
/**
* @throws ModulePackException
*/
private function addZMDataFiles() {
private function addZMDataFiles()
{
$base_dir = realpath(DataProvider::getDataFolder());
if (is_array($this->module["zm-data-store"] ?? null)) {
foreach ($this->module["zm-data-store"] as $v) {
if (is_array($this->module['zm-data-store'] ?? null)) {
foreach ($this->module['zm-data-store'] as $v) {
if (is_dir($base_dir . '/' . $v)) {
$v = rtrim($v, '/');
Console::info("Adding external zm_data dir: " . $v);
Console::info('Adding external zm_data dir: ' . $v);
$files = DataProvider::scanDirFiles($base_dir . '/' . $v, true, true);
foreach ($files as $single) {
$this->phar->addFile($base_dir . '/' . $v . '/' . $single, 'zm_data/' . $v . '/' . $single);
}
} elseif (is_file($base_dir . '/' . $v)) {
Console::info("Add external zm_data file: " . $v);
Console::info('Add external zm_data file: ' . $v);
$this->phar->addFile($base_dir . '/' . $v, 'zm_data/' . $v);
} else {
throw new ModulePackException(zm_internal_errcode("E00066")."`zmdata-store` 指定的文件或目录不存在");
throw new ModulePackException(zm_internal_errcode('E00066') . '`zmdata-store` 指定的文件或目录不存在');
}
}
}
}
}
}