/** * @test */ public function parseAnnotation() { $doc = '/** @var string */'; $annotationData = $this->parser->parse($doc); $this->assertThat($annotationData[0]->name, $this->equalTo('var')); $doc = '/** @ORM\\Column(name="createdAt") */'; $annotationData = $this->parser->parse($doc); $this->assertThat($annotationData[0]->name, $this->equalTo('ORM\\Column')); $this->assertThat($annotationData[0]->values, $this->equalTo(['name' => 'createdAt'])); }
/** * @param ReflectionClass|String $clazz */ public static function getClassAnnotations($clazz) { $className = $clazz instanceof ReflectionClass ? $clazz->getName() : $clazz; if (!isset(self::$classCache[$className])) { $clazz = $clazz instanceof ReflectionClass ? $clazz : new ReflectionClass($clazz); self::$classCache[$className] = AnnotationsParser::parse($clazz->getDocComment()); } return self::$classCache[$className]; }
/** * Returns all annotations. * @param ReflectionClass|\ReflectionMethod|\ReflectionProperty * @param string annotation name * @return array */ public static function getAll(Reflector $r, $name = NULL) { $cache = AnnotationsParser::getAll($r); if ($name === NULL) { return $cache; } elseif (isset($cache[$name])) { return $cache[$name]; } else { return array(); } }
/** * Returns all annotations. * @param ReflectionClass|ReflectionMethod|ReflectionProperty * @param string annotation name * @return array */ public static function getAll(Reflector $r, $name = NULL) { trigger_error(__METHOD__ . '() is deprecated; use getReflection()->getAnnotations() instead.', E_USER_WARNING); $cache = AnnotationsParser::getAll($r); if ($name === NULL) { return $cache; } elseif (isset($cache[$name])) { return $cache[$name]; } else { return array(); } }
/** * Returns annotations. * @param ReflectionClass|ReflectionMethod|ReflectionProperty * @return array */ public static function getAll(Reflector $r) { if ($r instanceof ReflectionClass) { $type = $r->getName(); $member = ''; } elseif ($r instanceof ReflectionMethod) { $type = $r->getDeclaringClass()->getName(); $member = $r->getName(); } else { $type = $r->getDeclaringClass()->getName(); $member = '$' . $r->getName(); } if (!self::$useReflection) { // auto-expire cache $file = $r instanceof ReflectionClass ? $r->getFileName() : $r->getDeclaringClass()->getFileName(); // will be used later if ($file && isset(self::$timestamps[$file]) && self::$timestamps[$file] !== filemtime($file)) { unset(self::$cache[$type]); } unset(self::$timestamps[$file]); } if (isset(self::$cache[$type][$member])) { // is value cached? return self::$cache[$type][$member]; } if (self::$useReflection === NULL) { // detects whether is reflection available self::$useReflection = (bool) ClassReflection::from(__CLASS__)->getDocComment(); } if (self::$useReflection) { return self::$cache[$type][$member] = self::parseComment($r->getDocComment()); } else { if (self::$cache === NULL) { self::$cache = (array) self::getCache()->offsetGet('list'); self::$timestamps = isset(self::$cache['*']) ? self::$cache['*'] : array(); } if (!isset(self::$cache[$type]) && $file) { self::$cache['*'][$file] = filemtime($file); self::parseScript($file); self::getCache()->save('list', self::$cache); } if (isset(self::$cache[$type][$member])) { return self::$cache[$type][$member]; } else { return self::$cache[$type][$member] = array(); } } }
/** * Returns all annotations. * @return IAnnotation[][] */ public function getAnnotations() { return AnnotationsParser::getAll($this); }
/** * Iterates through all presenters and returns their actions with backlinks and arguments * @return array */ private function generate() { $links = array(); $depends = array(); $iterator = new RegexIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator(APP_DIR)), '/Presenter\\.(php|PHP)$/m', RecursiveRegexIterator::GET_MATCH); foreach ($iterator as $path => $match) { $fileinfo = pathinfo($path); $reflection = new ReflectionClass($this->getClassNameFromPath($path)); if ($reflection->isInstantiable()) { $depends[] = $path; $modules = $this->getModulesFromName($reflection->name); $link = ''; if ($modules !== FALSE) { $link .= implode(':', $modules); } $link .= ':'; preg_match('/(?:[A-z0-9]+?_)*([A-z0-9]+)Presenter/m', $reflection->getName(), $match); $presenter = $match[1]; $link .= $presenter; $persistent = array(); foreach ($reflection->getProperties() as $property) { foreach (AnnotationsParser::getAll($property) as $annotation => $value) { if ($annotation == 'persistent') { $persistent[] = $property; } } } $actions = array(); foreach ($reflection->getMethods() as $action) { if (preg_match('/^(action|render)(.*)$/m', $action->getName(), $name) && !in_array($name[2], $actions)) { $action_name = lcfirst($name[2]); $pattern = '/Method \\[.*? ' . $action . ' \\].*? (?:Parameters .*? \\{.*?Parameter #\\d+ \\[(.*?)\\].*?\\})? }/ms'; $set_required = FALSE; $set_optional = FALSE; foreach ($action->getParameters() as $arg) { if (!$arg->isOptional()) { $actions[$action_name]['arguments']['required'][$arg->getName()] = NULL; $set_required = TRUE; } else { $actions[$action_name]['arguments']['optional'][$arg->getName()] = $arg->getDefaultValue(); $set_optional = TRUE; } } if (!$set_required) { $actions[$action_name]['arguments']['required'] = array(); } if (!$set_optional) { $actions[$action_name]['arguments']['optional'] = array(); } $actions[$action_name]['arguments']['persistent'] = $persistent; } } if (count($actions) == 0) { $actions['Default']['arguments']['required'] = array(); $actions['Default']['arguments']['optional'] = array(); $actions['Default']['arguments']['persistent'] = array(); } foreach ($actions as $action => $info) { $label = ':' . $link . ':' . $action; if (Environment::getApplication()->getPresenter() instanceof Presenter) { $links[$label]['link'] = Environment::getApplication()->getPresenter()->link($label); } else { $links[$label]['link'] = 'false'; } $links[$label]['action'] = $action; $links[$label]['presenter'] = $presenter; $links[$label]['modules'] = $modules; $links[$label]['arguments'] = $info['arguments']; } } } return array('tree' => $this->categorize($links), 'depends' => $depends); }
/** * @return void */ public static function setCacheStorage(ICacheStorage $storage) { self::$cacheStorage = $storage; }
public function testAnnotationsParser() { $parser = new AnnotationsParser(); $block = "\n\t\t\t\t/**\n\t\t\t\t * @TestAnnotation(ratio = 2.5)\n\t\t\t\t * @AnotherAnnotation('Hello')\n\t\t\t\t **/"; $annotations = $parser->parse($block); $first = $annotations['TestAnnotation']; $second = $annotations['AnotherAnnotation']; $this->assertIsA($first, 'TestAnnotation'); $this->assertEqual($first->ratio, 2.5); $this->assertIsA($second, 'AnotherAnnotation'); $this->assertEqual($second->value, 'Hello'); }