Fixed @Inject will be useless sometimes when using server:watch. (#2117)

This commit is contained in:
李铭昕 2020-07-15 10:59:36 +08:00 committed by GitHub
parent f1d666ff22
commit 75b9384996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 54 deletions

View File

@ -6,6 +6,10 @@
- [#2097](https://github.com/hyperf/hyperf/pull/2097) Added TencentCloud COS for `hyperf/filesystem`.
- [#2122](https://github.com/hyperf/hyperf/pull/2122) Added `\Hyperf\Snowflake\Concern\HasSnowflake` Trait to integrate `hyperf/snowflake` and database models.
## Fixed
- [#2117](https://github.com/hyperf/hyperf/pull/2117) Fixed `@Inject` will be useless sometimes when using `server:watch`.
## Optimized
- [#2110](https://github.com/hyperf/hyperf/pull/2110) Don't kill `SIGTERM` if the process not exists for `hyperf/watcher`.

View File

@ -22,5 +22,5 @@ require BASE_PATH . '/vendor/autoload.php';
use Hyperf\Watcher\Process;
$process = new Process($argv[1], $argv[2]);
$process = new Process($argv[1]);
$process();

View File

@ -33,10 +33,6 @@
"config": {
"sort-packages": true
},
"bin": [
"collector-reload.php",
"watcher.php"
],
"scripts": {
"test": "co-phpunit -c phpunit.xml --colors=always",
"analyse": "phpstan analyse --memory-limit 1024M -l 0 ./src",

View File

@ -33,11 +33,6 @@ class Process
*/
protected $file;
/**
* @var string
*/
protected $class;
/**
* @var BetterReflection
*/
@ -68,19 +63,23 @@ class Process
*/
protected $path = BASE_PATH . '/runtime/container/collectors.cache';
public function __construct(string $file, string $class)
public function __construct(string $file)
{
$this->file = $file;
$this->class = $class;
$this->ast = new Ast();
$this->reflection = new BetterReflection();
$this->reader = new AnnotationReader();
$this->config = ScanConfig::instance('/');
$this->filesystem = new Filesystem();
$this->ast = new Ast();
}
public function __invoke()
{
$meta = $this->getMetadata($this->file);
if ($meta === null) {
return;
}
$class = $meta->toClassName();
$collectors = $this->config->getCollectors();
$data = unserialize(file_get_contents($this->path));
foreach ($data as $collector => $deserialized) {
@ -93,9 +92,9 @@ class Process
require $this->file;
// Collect the annotations.
$ref = $this->reflection->classReflector()->reflect($this->class);
BetterReflectionManager::reflectClass($this->class, $ref);
$this->collect($this->class, $ref);
$ref = $this->reflection->classReflector()->reflect($class);
BetterReflectionManager::reflectClass($class, $ref);
$this->collect($class, $ref);
$collectors = $this->config->getCollectors();
$data = [];
@ -109,11 +108,11 @@ class Process
}
// Reload the proxy class.
$manager = new ProxyManager([], [$this->class => $this->file], BASE_PATH . '/runtime/container/proxy/');
$manager = new ProxyManager([], [$class => $this->file], BASE_PATH . '/runtime/container/proxy/');
$ref = new \ReflectionClass($manager);
$method = $ref->getMethod('generateProxyFiles');
$method->setAccessible(true);
$method->invokeArgs($manager, [$this->class => []]);
$method->invokeArgs($manager, [$class => []]);
}
public function collect($className, ReflectionClass $reflection)

View File

@ -13,16 +13,11 @@ namespace Hyperf\Watcher;
use Hyperf\Di\Annotation\AnnotationReader;
use Hyperf\Di\Annotation\ScanConfig;
use Hyperf\Di\Aop\Ast;
use Hyperf\Di\ClassLoader;
use Hyperf\Utils\Codec\Json;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Filesystem\Filesystem;
use Hyperf\Watcher\Ast\Metadata;
use Hyperf\Watcher\Ast\RewriteClassNameVisitor;
use Hyperf\Watcher\Driver\DriverInterface;
use PhpParser\Error;
use PhpParser\NodeTraverser;
use PhpParser\PrettyPrinter\Standard;
use Psr\Container\ContainerInterface;
use Roave\BetterReflection\BetterReflection;
@ -83,11 +78,6 @@ class Watcher
*/
protected $config;
/**
* @var Ast
*/
protected $ast;
/**
* @var Standard
*/
@ -115,7 +105,6 @@ class Watcher
$this->reflection = new BetterReflection();
$this->reader = new AnnotationReader();
$this->config = ScanConfig::instance('/');
$this->ast = new Ast();
$this->printer = new Standard();
$this->channel = new Channel(1);
$this->channel->push(true);
@ -123,6 +112,7 @@ class Watcher
public function run()
{
$this->dumpautoload();
$this->restart(true);
$channel = new Channel(999);
@ -139,18 +129,21 @@ class Watcher
$this->restart(false);
}
} else {
$meta = $this->getMetadata($file);
if ($meta) {
$ret = System::exec($this->option->getBin() . ' vendor/bin/collector-reload.php ' . $meta->path . ' ' . str_replace('\\', '\\\\', $meta->toClassName()));
if ($ret['code'] === 0) {
$this->output->writeln('Class reload success.');
}
$ret = System::exec($this->option->getBin() . ' vendor/hyperf/watcher/collector-reload.php ' . $file);
if ($ret['code'] === 0) {
$this->output->writeln('Class reload success.');
}
$result[] = $file;
}
}
}
public function dumpautoload()
{
$ret = System::exec('composer dump-autoload -o --no-scripts');
$this->output->writeln($ret['output'] ?? '');
}
public function restart($isStart = true)
{
$file = BASE_PATH . '/runtime/hyperf.pid';
@ -176,31 +169,13 @@ class Watcher
2 => STDERR,
];
proc_open($this->option->getBin() . ' vendor/bin/watcher.php start', $descriptorspec, $pipes);
proc_open($this->option->getBin() . ' vendor/hyperf/watcher/watcher.php start', $descriptorspec, $pipes);
$this->output->writeln('Stop server success.');
$this->channel->push(1);
});
}
protected function getMetadata(string $file): ?Metadata
{
try {
$stmts = $this->ast->parse($this->filesystem->get($file));
$meta = new Metadata();
$meta->path = $file;
$traverser = new NodeTraverser();
$traverser->addVisitor(new RewriteClassNameVisitor($meta));
$traverser->traverse($stmts);
if (! $meta->isClass()) {
$meta = null;
}
} catch (Error $error) {
$meta = null;
}
return $meta;
}
protected function getDriver()
{
$driver = $this->option->getDriver();