From 47abd97c7d4b4af117ecac181cbb2043ccfc9997 Mon Sep 17 00:00:00 2001 From: chengyao <987861463@qq.com> Date: Mon, 2 Sep 2024 13:52:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=86=8D=E9=9C=80=E8=A6=81pcntl?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81windows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/aop/README.md | 7 ++--- src/aop/composer.json | 9 +++--- src/aop/publish/aop.php | 1 - src/aop/src/Aop.php | 68 +++++++++++++++++++++++------------------ 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/aop/README.md b/src/aop/README.md index f1f9571d..b24cf7a9 100644 --- a/src/aop/README.md +++ b/src/aop/README.md @@ -5,8 +5,8 @@ # 环境要求 ``` -linux OS -php >=8.0 with ext-pcntl +php 8.2 +开启passthru函数 ``` # 安装 @@ -30,7 +30,6 @@ Aop::init( ); ``` -* cache 是否缓存,true时下次启动不会重新生成代理类 * paths 注解扫描路径 * collectors 注解收集器 - \Next\Aop\Collector\AspectCollector::class 切面收集器,取消后不能使用切面 @@ -123,7 +122,7 @@ class Round implements AspectInterface php start.php start ``` -打开浏览器打开对应页面 +打开浏览器打开~~对应页面~~ ## 控制台输出内容为 diff --git a/src/aop/composer.json b/src/aop/composer.json index 9cb60813..201cffea 100644 --- a/src/aop/composer.json +++ b/src/aop/composer.json @@ -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": { diff --git a/src/aop/publish/aop.php b/src/aop/publish/aop.php index 7eb11135..79e8abb8 100644 --- a/src/aop/publish/aop.php +++ b/src/aop/publish/aop.php @@ -10,7 +10,6 @@ */ return [ - 'cache' => false, 'scanDirs' => [ __DIR__ . '/app', ], diff --git a/src/aop/src/Aop.php b/src/aop/src/Aop.php index 34350d47..27e01357 100644 --- a/src/aop/src/Aop.php +++ b/src/aop/src/Aop.php @@ -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(); @@ -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); } @@ -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); @@ -110,7 +120,7 @@ private function getProxyMap(): array $proxies[$class] = $proxyPath; } $this->filesystem->put($this->proxyMapFile, sprintf("proxyMapFile; } @@ -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); }