Пример #1
0
 public function testInterceptionGenerator()
 {
     $enhancer = new Enhancer(new \ReflectionClass('CG\\Tests\\Proxy\\Fixture\\Entity'), array(), array($generator = new InterceptionGenerator()));
     $enhancer->setNamingStrategy($this->getNamingStrategy('CG\\Tests\\Proxy\\Fixture\\Entity__CG__Traceable_' . sha1(microtime(true))));
     $generator->setPrefix('');
     $traceable = $enhancer->createInstance();
     $traceable->setLoader($this->getLoader(array($interceptor1 = new TraceInterceptor(), $interceptor2 = new TraceInterceptor())));
     $this->assertEquals('foo', $traceable->getName());
     $this->assertEquals('foo', $traceable->getName());
     $this->assertEquals(2, count($interceptor1->getLog()));
     $this->assertEquals(2, count($interceptor2->getLog()));
 }
 /**
  * @param array<PointcutInterface> $pointcuts
  * @param array<string,string> $interceptors
  */
 private function processDefinition(Definition $definition, $pointcuts, &$interceptors)
 {
     if ($definition->isSynthetic()) {
         return;
     }
     // Symfony 2.6 getFactory method
     // TODO: Use only getFactory when bumping require to Symfony >= 2.6
     if (method_exists($definition, 'getFactory') && $definition->getFactory()) {
         return;
     }
     if (!method_exists($definition, 'getFactory') && ($definition->getFactoryService() || $definition->getFactoryClass())) {
         return;
     }
     if ($originalFilename = $definition->getFile()) {
         require_once $originalFilename;
     }
     if (!class_exists($definition->getClass())) {
         return;
     }
     $class = new \ReflectionClass($definition->getClass());
     // check if class is matched
     $matchingPointcuts = array();
     foreach ($pointcuts as $interceptor => $pointcut) {
         if ($pointcut->matchesClass($class)) {
             $matchingPointcuts[$interceptor] = $pointcut;
         }
     }
     if (empty($matchingPointcuts)) {
         return;
     }
     $this->addResources($class, $this->container);
     if ($class->isFinal()) {
         return;
     }
     $classAdvices = array();
     foreach (ReflectionUtils::getOverrideableMethods($class) as $method) {
         if ('__construct' === $method->name) {
             continue;
         }
         $advices = array();
         foreach ($matchingPointcuts as $interceptor => $pointcut) {
             if ($pointcut->matchesMethod($method)) {
                 $advices[] = $interceptor;
             }
         }
         if (empty($advices)) {
             continue;
         }
         $classAdvices[$method->name] = $advices;
     }
     if (empty($classAdvices)) {
         return;
     }
     $interceptors[ClassUtils::getUserClass($class->name)] = $classAdvices;
     $proxyFilename = $this->cacheDir . '/' . str_replace('\\', '-', $class->name) . '.php';
     $generator = new InterceptionGenerator();
     $generator->setFilter(function (\ReflectionMethod $method) use($classAdvices) {
         return isset($classAdvices[$method->name]);
     });
     if ($originalFilename) {
         $relativeOriginalFilename = $this->relativizePath($proxyFilename, $originalFilename);
         if ($relativeOriginalFilename[0] === '.') {
             $generator->setRequiredFile(new RelativePath($relativeOriginalFilename));
         } else {
             $generator->setRequiredFile($relativeOriginalFilename);
         }
     }
     $enhancer = new Enhancer($class, array(), array($generator));
     $enhancer->setNamingStrategy(new DefaultNamingStrategy('EnhancedProxy' . substr(md5($this->container->getParameter('jms_aop.cache_dir')), 0, 8)));
     $enhancer->writeClass($proxyFilename);
     $definition->setFile($proxyFilename);
     $definition->setClass($enhancer->getClassName($class));
     $definition->addMethodCall('__CGInterception__setLoader', array(new Reference('jms_aop.interceptor_loader')));
 }
Пример #3
0
    private function processDefinition(Definition $definition, $pointcuts, &$interceptors)
    {
        if ($definition->isSynthetic()) {
            return;
        }

        if ($definition->getFactoryService() || $definition->getFactoryClass()) {
            return;
        }

        if ($file = $definition->getFile()) {
            require_once $file;
        }

        $class = new \ReflectionClass($definition->getClass());

        // check if class is matched
        $matchingPointcuts = array();
        foreach ($pointcuts as $interceptor => $pointcut) {
            if ($pointcut->matchesClass($class)) {
                $matchingPointcuts[$interceptor] = $pointcut;
            }
        }

        if (empty($matchingPointcuts)) {
            return;
        }

        $this->addResources($class, $this->container);

        if ($class->isFinal()) {
            return;
        }

        $classAdvices = array();
        foreach ($class->getMethods(\ReflectionMethod::IS_PROTECTED | \ReflectionMethod::IS_PUBLIC) as $method) {
            if ($method->isFinal()) {
                continue;
            }

            $advices = array();
            foreach ($matchingPointcuts as $interceptor => $pointcut) {
                if ($pointcut->matchesMethod($method)) {
                    $advices[] = $interceptor;
                }
            }

            if (empty($advices)) {
                continue;
            }

            $classAdvices[$method->name] = $advices;
        }

        if (empty($classAdvices)) {
            return;
        }

        $interceptors[ClassUtils::getUserClass($class->name)] = $classAdvices;

        $generator = new InterceptionGenerator();
        $generator->setFilter(function(\ReflectionMethod $method) use ($classAdvices) {
            return isset($classAdvices[$method->name]);
        });
        if ($file) {
            $generator->setRequiredFile($file);
        }
        $enhancer = new Enhancer($class, array(), array(
            $generator
        ));
        $enhancer->writeClass($filename = $this->cacheDir.'/'.str_replace('\\', '-', $class->name).'.php');
        $definition->setFile($filename);
        $definition->setClass($enhancer->getClassName($class));
        $definition->addMethodCall('__CGInterception__setLoader', array(
            new Reference('jms_aop.interceptor_loader')
        ));
    }