Skip to content

Commit

Permalink
不再需要pcntl,支持windows
Browse files Browse the repository at this point in the history
  • Loading branch information
chengyao committed Sep 2, 2024
1 parent 296f64e commit 47abd97
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
7 changes: 3 additions & 4 deletions src/aop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# 环境要求

```
linux OS
php >=8.0 with ext-pcntl
php 8.2
开启passthru函数
```

# 安装
Expand All @@ -30,7 +30,6 @@ Aop::init(
);
```

* cache 是否缓存,true时下次启动不会重新生成代理类
* paths 注解扫描路径
* collectors 注解收集器
- \Next\Aop\Collector\AspectCollector::class 切面收集器,取消后不能使用切面
Expand Down Expand Up @@ -123,7 +122,7 @@ class Round implements AspectInterface
php start.php start
```

打开浏览器打开对应页面
打开浏览器打开~~对应页面~~

## 控制台输出内容为

Expand Down
9 changes: 4 additions & 5 deletions src/aop/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
],
"require": {
"php": "^8.2",
"ext-pcntl": "*",
"next/utils": "~0.1",
"next/di": "~0.1",
"nikic/php-parser": "^5.0.2",
"symfony/finder": "^7.0"
"next/utils": "^0.1",
"next/di": "^0.1",
"nikic/php-parser": "^5.1",
"symfony/finder": "^7.1"
},
"extra": {
"branch-alias": {
Expand Down
1 change: 0 additions & 1 deletion src/aop/publish/aop.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/

return [
'cache' => false,
'scanDirs' => [
__DIR__ . '/app',
],
Expand Down
68 changes: 39 additions & 29 deletions src/aop/src/Aop.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,49 @@

final class Aop
{
private AstManager $astManager;
private AstManager $astManager;

private string $proxyMapFile;
private string $proxyMapFile;

private static array $classMap = [];
private static array $classMap = [];

private Filesystem $filesystem;

private static bool $initialized = false;
private Filesystem $filesystem;

private function __construct(
private array $scanDirs = [],
private array $collectors = [],
private array $scanDirs = [],
private array $collectors = [],
private string $runtimeDir = '',
private bool $cache = false
) {
self::$initialized = true;
$this->filesystem = new Filesystem();
$this->astManager = new AstManager();
$this->runtimeDir = rtrim($runtimeDir, '/\\') . '/';
if (! $this->filesystem->isDirectory($this->runtimeDir)) {
)
{
$this->filesystem = new Filesystem();
$this->astManager = new AstManager();
$this->runtimeDir = rtrim($runtimeDir, '/\\') . '/';
if (!$this->filesystem->isDirectory($this->runtimeDir)) {
$this->filesystem->makeDirectory($this->runtimeDir, 0755, true);
}
$this->findClasses($this->scanDirs);
$this->proxyMapFile = $this->runtimeDir . 'proxy.php';
if (! $this->cache || ! $this->filesystem->exists($this->proxyMapFile)) {
$lockFile = $this->runtimeDir . 'lock';
if (file_exists($lockFile)) {
unlink($lockFile);
$this->getProxyMap();
} else {
$this->filesystem->exists($this->proxyMapFile) && $this->filesystem->delete($this->proxyMapFile);
}
if (!$this->filesystem->exists($this->proxyMapFile)) {
touch($lockFile);
$this->filesystem->exists($this->proxyMapFile) && $this->filesystem->delete($this->proxyMapFile);
if (($pid = pcntl_fork()) == -1) {
throw new \RuntimeException('Process fork failed.');
global $argv;
$cmd = PHP_BINARY . ' ' . implode(' ', $argv) . ' 2>&1';
$result = shell_exec($cmd);
if ($result) {
print($result . PHP_EOL);
exit(255);
}
pcntl_wait($pid);
// if (($pid = pcntl_fork()) == -1) {
// throw new \RuntimeException('Process fork failed.');
// }
// pcntl_wait($pid);
}
Composer::getClassLoader()->addClassMap($this->getProxyMap());
$this->collect();
Expand All @@ -65,14 +77,12 @@ private function __clone(): void
}

public static function init(
array $scanDirs = [],
array $collectors = [],
array $scanDirs = [],
array $collectors = [],
string $runtimeDir = '',
bool $cache = false
): void {
if (self::$initialized) {
throw new \RuntimeException('aop is already initialized, so don\'t call init again');
}
bool $cache = false
): void
{
new self($scanDirs, $collectors, $runtimeDir, $cache);
}

Expand All @@ -97,7 +107,7 @@ private function findClasses(array $dirs): void
*/
private function getProxyMap(): array
{
if (! $this->filesystem->exists($this->proxyMapFile)) {
if (!$this->filesystem->exists($this->proxyMapFile)) {
$proxyDir = $this->runtimeDir . 'proxy/';
$this->filesystem->exists($proxyDir) || $this->filesystem->makeDirectory($proxyDir, 0755, true, true);
$this->filesystem->cleanDirectory($proxyDir);
Expand All @@ -110,7 +120,7 @@ private function getProxyMap(): array
$proxies[$class] = $proxyPath;
}
$this->filesystem->put($this->proxyMapFile, sprintf("<?php \nreturn %s;", var_export($proxies, true)));
exit;
exit(0);
}
return include $this->proxyMapFile;
}
Expand Down Expand Up @@ -141,7 +151,7 @@ private function collect(): void
foreach ($reflectionClass->getAttributes() as $attribute) {
try {
$attributeInstance = $attribute->newInstance();
if (! $attributeInstance instanceof \Attribute) {
if (!$attributeInstance instanceof \Attribute) {
foreach ($this->collectors as $collector) {
$collector::collectClass($class, $attributeInstance);
}
Expand Down

0 comments on commit 47abd97

Please sign in to comment.