Xpressengine\Interception\Proxy\ProxyGenerator::generate PHP Method

generate() public method

비지니스 로직에서는 타겟 클래스 대신 프록시 클래스의 인스턴스를 생성하여 사용한다.
public generate ( string $targetClass ) : string
$targetClass string 프록시 클래스를 생성할 타겟 클래스
return string
    public function generate($targetClass)
    {
        $config = new ProxyConfig($targetClass);
        // 파일 로더일 경우, 성능향상을 위하여 프록시 클래스 파일이 존재할 경우,
        // 생성 과정을 거치지 않고 바로 클래스명을 반환한다.
        if ($this->loader instanceof FileLoader) {
            if ($this->loader->existProxyFile($config) === true) {
                $proxyName = $config->getProxyName();
                $this->loadFile($this->loader->getProxyPath($proxyName));
                return $proxyName;
            }
        }
        $def = $this->generateProxyDefinition($config);
        $this->loader->load($def);
        return $def->getClassName();
    }

Usage Example

 /**
  * 타겟 클래스의 프록시 클래스를 생성하여 로드하고, 생성된 프록시 클래스 이름을 반환한다.
  * 만약 어떤 클래스에 interception을 적용하고 싶을 때 이 메소드를 사용하면 된다.
  *
  * ```
  * $targetClassName = 'My\Namespace\PostManager';
  * $proxyClass = XeInterception::proxy($targetClass, 'Post');
  *
  * $postManager = new $proxyClass();
  * ```
  * 두번째 파라메터를 사용하여 타겟클래스의 alias 이름을 등록할 수 있다. alias 이름을 지정하면,
  * 타겟 클래스에 interception을 등록할 때, alias 이름을 사용할 수 있다.
  *
  * ```
  * // Post alias를 사용
  * intercept('Post@insert', 'spam_filter', function(){...});
  * ```
  *
  * @param string      $targetClass 타겟 클래스
  * @param string|null $alias       타겟 클래스의 별칭
  *
  * @return string
  */
 public function proxy($targetClass, $alias = null)
 {
     $targetClass = trim($targetClass);
     if ($alias !== null) {
         $this->advisorCollection->setAlias($alias, $targetClass);
     }
     $proxyClass = $this->proxyGenerator->generate($targetClass);
     $this->proxyList[$targetClass] = $proxyClass;
     return $proxyClass;
 }