- 移除ClosureGenerator中无用的方法注释 - 在CompilerBase中重命名参数并添加isInternalClass方法 - 从ConstantDef中移除无用的导入 - 完全删除FileSorter类,改用外部库MJS\TopSort进行文件排序 - 在Preprocessor中重构依赖收集逻辑,优化文件排序算法 - 过滤内置函数和类,避免参与依赖管理 - 重构Translator中的类属性初始化代码,优化对象创建流程 - 统一字符串拼接操作符为单引号格式pull/1/head
parent
bb52f8ab10
commit
f6a3e5b4b3
6 changed files with 67 additions and 160 deletions
@ -1,100 +0,0 @@ |
|||||||
<?php |
|
||||||
/** |
|
||||||
* This file is part of Swoole-Compiler(AOT). |
|
||||||
* |
|
||||||
* @link https://www.swoole.com/ |
|
||||||
* @contact service@swoole.com |
|
||||||
*/ |
|
||||||
|
|
||||||
namespace PhpAot\Php; |
|
||||||
|
|
||||||
class FileSorter |
|
||||||
{ |
|
||||||
private array $symbolDeclInFile; |
|
||||||
private array $symbolCallInFile; |
|
||||||
|
|
||||||
public function __construct(array $symbolDeclInFile, array $symbolCallInFile) |
|
||||||
{ |
|
||||||
$this->symbolDeclInFile = $symbolDeclInFile; |
|
||||||
$this->symbolCallInFile = $symbolCallInFile; |
|
||||||
} |
|
||||||
|
|
||||||
public function sort(): array |
|
||||||
{ |
|
||||||
$dependencies = $this->buildDependencies(); |
|
||||||
|
|
||||||
$allFiles = $this->getAllFiles(); |
|
||||||
|
|
||||||
$inDegree = array_fill_keys($allFiles, 0); |
|
||||||
foreach ($dependencies as $deps) { |
|
||||||
foreach ($deps as $dep) { |
|
||||||
$inDegree[$dep]++; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
$queue = []; |
|
||||||
foreach ($inDegree as $file => $degree) { |
|
||||||
if ($degree === 0) { |
|
||||||
$queue[] = $file; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
$sorted = []; |
|
||||||
while (!empty($queue)) { |
|
||||||
$current = array_shift($queue); |
|
||||||
$sorted[] = $current; |
|
||||||
|
|
||||||
if (isset($dependencies[$current])) { |
|
||||||
foreach ($dependencies[$current] as $dep) { |
|
||||||
$inDegree[$dep]--; |
|
||||||
if ($inDegree[$dep] === 0) { |
|
||||||
$queue[] = $dep; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if (count($sorted) !== count($allFiles)) { |
|
||||||
throw new \RuntimeException('Circular dependency of function call detected'); |
|
||||||
} |
|
||||||
|
|
||||||
return array_reverse($sorted); |
|
||||||
} |
|
||||||
|
|
||||||
private function buildDependencies(): array |
|
||||||
{ |
|
||||||
$dependencies = []; |
|
||||||
|
|
||||||
foreach ($this->symbolCallInFile as $call) { |
|
||||||
$callerFile = $call['file']; |
|
||||||
$symbolName = $call['name']; |
|
||||||
|
|
||||||
if (isset($this->symbolDeclInFile[$symbolName])) { |
|
||||||
$declFile = $this->symbolDeclInFile[$symbolName]; |
|
||||||
|
|
||||||
if ($callerFile !== $declFile) { |
|
||||||
if (!isset($dependencies[$callerFile])) { |
|
||||||
$dependencies[$callerFile] = []; |
|
||||||
} |
|
||||||
|
|
||||||
if (!in_array($declFile, $dependencies[$callerFile])) { |
|
||||||
$dependencies[$callerFile][] = $declFile; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return $dependencies; |
|
||||||
} |
|
||||||
|
|
||||||
private function getAllFiles(): array |
|
||||||
{ |
|
||||||
$files = array_values($this->symbolDeclInFile); |
|
||||||
|
|
||||||
foreach ($this->symbolCallInFile as $call) { |
|
||||||
$files[] = $call['file']; |
|
||||||
} |
|
||||||
|
|
||||||
return array_unique($files); |
|
||||||
} |
|
||||||
} |
|
||||||
Loading…
Reference in new issue