Repository navigation
Expand file tree
/
Copy pathProxyFactory.php
More file actions
83 lines (68 loc) · 2.29 KB
/
Copy pathProxyFactory.php
File metadata and controls
83 lines (68 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\RpcClient;
use Hyperf\Coroutine\Locker;
use Hyperf\Di\Annotation\ScanConfig;
use Hyperf\RpcClient\Proxy\Ast;
use Hyperf\RpcClient\Proxy\CodeLoader;
use Hyperf\Support\Filesystem\Filesystem;
use Hyperf\Support\Traits\Container;
class ProxyFactory
{
use Container;
protected Ast $ast;
protected CodeLoader $codeLoader;
protected Filesystem $filesystem;
public function __construct()
{
$this->ast = new Ast();
$this->codeLoader = new CodeLoader();
$this->filesystem = new Filesystem();
}
public function createProxy($serviceClass): string
{
if (self::has($serviceClass)) {
return (string) self::get($serviceClass);
}
$dir = BASE_PATH . '/runtime/container/proxy/';
if (! file_exists($dir)) {
mkdir($dir, 0755, true);
}
$proxyFileName = str_replace('\\', '_', $serviceClass);
$proxyClassName = $serviceClass . '_' . $this->codeLoader->getMd5ByClassName($serviceClass);
$path = $dir . $proxyFileName . '.rpc-client.proxy.php';
$key = md5($path);
// If the proxy file does not exist, then try to acquire the coroutine lock.
if ($this->isModified($serviceClass, $path) && Locker::lock($key)) {
$targetPath = $path . '.' . uniqid();
$code = $this->ast->proxy($serviceClass, $proxyClassName);
file_put_contents($targetPath, $code);
rename($targetPath, $path);
Locker::unlock($key);
}
include_once $path;
self::set($serviceClass, $proxyClassName);
return $proxyClassName;
}
protected function isModified(string $interface, string $path): bool
{
if (! $this->filesystem->exists($path)) {
return true;
}
if (ScanConfig::instance('')->isCacheable()) {
return false;
}
$time = $this->filesystem->lastModified(
$this->codeLoader->getPathByClassName($interface)
);
return $time >= $this->filesystem->lastModified($path);
}
}