/** * {@inheritdoc} * @param ReflectionFileNamespace|string $ns */ public function matches($ns) { $isNamespaceIsObject = $ns === (object) $ns; if ($isNamespaceIsObject && !$ns instanceof ReflectionFileNamespace) { return false; } $nsName = $isNamespaceIsObject ? $ns->getName() : $ns; return $nsName === $this->nsName || (bool) preg_match("/^(?:{$this->regexp})\$/", $nsName); }
public function testHasFunction() { $hasFunction = $this->parsedRefFileNamespace->hasFunction('Unknown'); $this->assertFalse($hasFunction); $hasFunction = $this->parsedRefFileNamespace->hasFunction('testFunctionBar'); $this->assertTrue($hasFunction); }
public function testGetConstant() { $parsedRefClass = $this->parsedRefFileNamespace->getClass(ClassWithScalarConstants::class); $originalRefClass = new \ReflectionClass(ClassWithScalarConstants::class); $this->assertSame($originalRefClass->getConstant('D'), $parsedRefClass->getConstant('D')); $this->assertSame($originalRefClass->getConstant('E'), $parsedRefClass->getConstant('E')); }
/** * Save AOP proxy to the separate file anr returns the php source code for inclusion * * @param ReflectionClass $class Original class reflection * @param string|ClassProxy $child * * @return string */ private function saveProxyToCache($class, $child) { static $cacheDirSuffix = '/_proxies/'; $cacheDir = $this->cachePathManager->getCacheDir(); // Without cache we should rewrite original file if (!$cacheDir) { return $child; } $cacheDir = $cacheDir . $cacheDirSuffix; $fileName = str_replace($this->options['appDir'] . DIRECTORY_SEPARATOR, '', $class->getFileName()); $proxyFileName = $cacheDir . $fileName; $dirname = dirname($proxyFileName); if (!file_exists($dirname)) { mkdir($dirname, $this->options['cacheFileMode'], true); } $body = '<?php' . PHP_EOL; $namespace = $class->getNamespaceName(); if ($namespace) { $body .= "namespace {$namespace};" . PHP_EOL . PHP_EOL; } $refNamespace = new ReflectionFileNamespace($class->getFileName(), $namespace); foreach ($refNamespace->getNamespaceAliases() as $fqdn => $alias) { $body .= "use {$fqdn} as {$alias};" . PHP_EOL; } $body .= $child; file_put_contents($proxyFileName, $body, LOCK_EX); // For cache files we don't want executable bits by default chmod($proxyFileName, $this->options['cacheFileMode'] & ~0111); return 'include_once AOP_CACHE_DIR . ' . var_export($cacheDirSuffix . $fileName, true) . ';' . PHP_EOL; }
/** * Utility method to fetch reflection class instance by name * * Supports: * 'self' keyword * 'parent' keyword * not-FQN class names * * @param Node\Name $node Class name node * * @return bool|\ReflectionClass * * @throws ReflectionException */ private function fetchReflectionClass(Node\Name $node) { $className = $node->toString(); $isFQNClass = $node instanceof Node\Name\FullyQualified; if ($isFQNClass) { return new ReflectionClass($className); } if ('self' === $className) { if ($this->context instanceof \ReflectionClass) { return $this->context; } elseif (method_exists($this->context, 'getDeclaringClass')) { return $this->context->getDeclaringClass(); } } if ('parent' === $className) { if ($this->context instanceof \ReflectionClass) { return $this->context->getParentClass(); } elseif (method_exists($this->context, 'getDeclaringClass')) { return $this->context->getDeclaringClass()->getParentClass(); } } if (method_exists($this->context, 'getFileName')) { /** @var ReflectionFileNamespace|null $fileNamespace */ $fileName = $this->context->getFileName(); $namespaceName = $this->context->getNamespaceName(); $fileNamespace = new ReflectionFileNamespace($fileName, $namespaceName); return $fileNamespace->getClass($className); } throw new ReflectionException("Can not resolve class {$className}"); }
/** * Returns list of function advices for specific namespace * * @param ReflectionFileNamespace $namespace * @param Aop\PointcutAdvisor $advisor Advisor for class * @param string $advisorId Identifier of advisor * @param Aop\PointFilter $pointcut Filter for points * * @return array */ private function getFunctionAdvicesFromAdvisor(ReflectionFileNamespace $namespace, Aop\PointcutAdvisor $advisor, $advisorId, Aop\PointFilter $pointcut) { $functions = []; $advices = []; $listOfGlobalFunctions = get_defined_functions(); foreach ($listOfGlobalFunctions['internal'] as $functionName) { $functions[$functionName] = new NamespacedReflectionFunction($functionName, $namespace->getName()); } foreach ($functions as $functionName => $function) { if ($pointcut->matches($function)) { $advices[AspectContainer::FUNCTION_PREFIX][$functionName][$advisorId] = $advisor->getAdvice(); } } return $advices; }