/** * @param string * @param string * @return array */ public static function parseAnnotations($class, $method = NULL) { if (strpos($class, '::') !== FALSE && !$method) { list($class, $method) = explode('::', $class); } $ref = new Reflection\Method($class, $method); $cRef = new Reflection\ClassType($class); $anntations = (array)$ref->getAnnotation('allowed'); $role = isset($anntations['role']) ? $anntations['role'] : ($ref->hasAnnotation('role') ? $ref->getAnnotation('role') : NULL); $resource = isset($anntations['resource']) ? $anntations['resource'] : ($ref->hasAnnotation('resource') ? $ref->getAnnotation('resource') : ($cRef->hasAnnotation('resource') ? $cRef->getAnnotation('resource') : NULL)); $privilege = isset($anntations['privilege']) ? $anntations['privilege'] : ($ref->hasAnnotation('privilege') ? $ref->getAnnotation('privilege') : NULL); return array( static::ROLE => $role, static::RESOURCE => $resource, static::PRIVILEGE => $privilege, ); }
/** * @param array $dirs * @param array $annotations * @return array */ private function findByAnnotations(array $dirs, array $annotations) { $loader = $this->createLoader(); $loader->addDirectory($dirs); $loader->rebuild(); $loader->register(); $classes = []; foreach (array_keys($loader->getIndexedClasses()) as $class) { // Skip not existing class if (!class_exists($class, TRUE)) { continue; } // Detect by reflection $ct = new ClassType($class); // Skip abstract if ($ct->isAbstract()) { continue; } // Does class has one of the annotation? foreach ($annotations as $annotation) { if ($ct->hasAnnotation(trim($annotation, '@'))) { $classes[] = $ct->getName(); } } } return $classes; }