123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Di\LazyLoader;
- use PhpParser\BuilderFactory;
- use PhpParser\Node;
- use PhpParser\Node\Const_;
- use PhpParser\Node\Scalar\String_;
- use PhpParser\Node\Stmt\ClassConst;
- use function Hyperf\Support\class_basename;
- abstract class AbstractLazyProxyBuilder
- {
- /**
- * The Builder instance.
- * @var mixed
- */
- public $builder;
- /**
- * The BuilderFactory.
- */
- public BuilderFactory $factory;
- /**
- * Class Namespace.
- */
- protected ?string $namespace = null;
- protected ?string $proxyClassName = null;
- protected ?string $originalClassName = null;
- public function __construct()
- {
- $this->factory = new BuilderFactory();
- $this->builder = $this->factory;
- }
- abstract public function addClassRelationship(): AbstractLazyProxyBuilder;
- public function addClassBoilerplate(string $proxyClassName, string $originalClassName): AbstractLazyProxyBuilder
- {
- $namespace = join('\\', array_slice(explode('\\', $proxyClassName), 0, -1));
- $this->namespace = $namespace;
- $this->proxyClassName = $proxyClassName;
- $this->originalClassName = $originalClassName;
- $this->builder = $this->factory->class(class_basename($proxyClassName))
- ->addStmt(new ClassConst([new Const_('PROXY_TARGET', new String_($originalClassName))]))
- ->addStmt($this->factory->useTrait('\Hyperf\Di\LazyLoader\LazyProxyTrait'))
- ->setDocComment("/**
- * Be careful: This is a lazy proxy, not the real {$originalClassName} from container.
- *
- * {@inheritdoc}
- */");
- return $this;
- }
- public function addNodes(array $nodes): AbstractLazyProxyBuilder
- {
- foreach ($nodes as $stmt) {
- $this->builder = $this->builder->addStmt($stmt);
- }
- return $this;
- }
- public function getNode(): Node
- {
- if ($this->namespace) {
- return $this->factory
- ->namespace($this->namespace)
- ->addStmt($this->builder)
- ->getNode();
- }
- return $this->builder->getNode();
- }
- public function getNamespace(): string
- {
- return $this->namespace;
- }
- public function getProxyClassName(): string
- {
- return $this->proxyClassName;
- }
- public function getOriginalClassName(): string
- {
- return $this->originalClassName;
- }
- }
|