public function testLazyInitializerGenerator() { $enhancer = new Enhancer(new \ReflectionClass('CG\\Tests\\Proxy\\Fixture\\Entity'), array(), array($generator = new LazyInitializerGenerator())); $generator->setPrefix(''); $entity = $enhancer->createInstance(); $entity->setLazyInitializer($initializer = new Initializer()); $this->assertEquals('foo', $entity->getName()); $this->assertSame($entity, $initializer->getLastObject()); }
private function generateEntityManagerProxyClass(array $config, ContainerBuilder $container) { $cacheDir = $container->getParameterBag()->resolveValue($config['cache_dir']); if (!is_dir($cacheDir . '/doctrine')) { if (false === @mkdir($cacheDir . '/doctrine', 0777, true)) { throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir . '/doctrine')); } } $enhancer = new Enhancer($ref = new \ReflectionClass('Doctrine\\ORM\\EntityManager'), array(), array(new RepositoryInjectionGenerator())); $enhancer->writeClass($file = $cacheDir . '/doctrine/EntityManager.php'); $container->setParameter('jms_di_extra.doctrine_integration.entity_manager.file', $file); $container->setParameter('jms_di_extra.doctrine_integration.entity_manager.class', $enhancer->getClassName($ref)); }
private function generateEntityManagerProxyClass(array $config, ContainerBuilder $container) { $cacheDir = $container->getParameterBag()->resolveValue($config['cache_dir']); if (!is_dir($cacheDir . '/doctrine')) { if (false === @mkdir($cacheDir . '/doctrine', 0777, true)) { throw new \RuntimeException(sprintf('Could not create cache directory "%s".', $cacheDir . '/doctrine')); } } $enhancer = new Enhancer($ref = new \ReflectionClass('Doctrine\\ORM\\EntityManager'), array(), array(new RepositoryInjectionGenerator())); $uniqid = uniqid(); // We do have to use a non-static id to avoid problems with cache:clear. $enhancer->setNamingStrategy(new DefaultNamingStrategy('EntityManager' . $uniqid)); $enhancer->writeClass($file = $cacheDir . '/doctrine/EntityManager_' . $uniqid . '.php'); $container->setParameter('jms_di_extra.doctrine_integration.entity_manager.file', $file); $container->setParameter('jms_di_extra.doctrine_integration.entity_manager.class', $enhancer->getClassName($ref)); }
private function generateLookupMethods($def, $metadata) { $found = false; foreach ($metadata->classMetadata as $cMetadata) { if (!empty($cMetadata->lookupMethods)) { $found = true; break; } } if (!$found) { return; } $generator = new LookupMethodClassGenerator($metadata); $outerClass = $metadata->getOutsideClassMetadata()->reflection; if ($file = $def->getFile()) { $generator->setRequiredFile($file); } $enhancer = new Enhancer($outerClass, array(), array($generator)); $filename = $this->container->getParameter('jms_di_extra.cache_dir') . '/lookup_method_classes/' . str_replace('\\', '-', $outerClass->name) . '.php'; $enhancer->writeClass($filename); $def->setFile($filename); $def->setClass($enhancer->getClassName($outerClass)); $def->addMethodCall('__jmsDiExtra_setContainer', array(new Reference('service_container'))); }
/** * Constructor * * @param string $className */ public function __construct($className) { $this->className = $className; parent::__construct(new \ReflectionClass($className), ['\\ThinFrame\\Events\\DispatcherAwareInterface']); $this->setUp(); }
/** * @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'))); }
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') )); }