*/ namespace oat\tao\model\routing; use Doctrine\Common\Annotations\AnnotationReader; use oat\oatbox\service\ConfigurableService; use oat\tao\model\routing\AnnotationReader\requiredRights; use oat\tao\model\routing\AnnotationReader\security; use ReflectionClass; use ReflectionMethod; class AnnotationReaderService extends ConfigurableService { const SERVICE_ID = 'tao/AnnotationReaderService'; const KEY_PREFIX = 'routeAnnotation_'; const PROP_RIGHTS = 'required_rights'; const PROP_SECURITY = 'security'; /** * @var \common_cache_Cache */ private $cacheService; /** * @param $className * @param $methodName * @return array|mixed * @throws \common_cache_NotFoundException */ public function getAnnotations($className, $methodName) { $annotationKey = self::KEY_PREFIX . $className . $methodName; if ($this->getCacheService()->has($annotationKey)) { $annotation = json_decode($this->getCacheService()->get($annotationKey), true); } else { $annotation = $this->readAnnotations($className, $methodName); $this->getCacheService()->put(json_encode($annotation), $annotationKey); } return $annotation; } private function readAnnotations($className, $methodName) { $rules = [ self::PROP_RIGHTS => [], self::PROP_SECURITY => [], ]; try { // we need to define class // we need to change autoloader file without this, on each environment new requiredRights(); new security(); $annotationReader = new AnnotationReader(); if ($methodName) { $reflectionMethod = new ReflectionMethod($className, $methodName); $annotations = $annotationReader->getMethodAnnotations($reflectionMethod); } else { $reflectionClass = new ReflectionClass($className); $annotations = $annotationReader->getClassAnnotations($reflectionClass); } foreach ($annotations as $annotation) { switch (get_class($annotation)) { case requiredRights::class: $rules[self::PROP_RIGHTS][] = (array) $annotation; break; case security::class: $rules[self::PROP_SECURITY][] = $annotation->value; break; } } } catch (\Exception $e) { $this->logNotice('Undefined annotation: ' . $e->getMessage()); } return $rules; } /** * @return \common_cache_Cache */ private function getCacheService() { if (!$this->cacheService) { if ($this->hasOption('cacheService') && $this->getOption('cacheService') instanceof \common_cache_Cache) { $this->cacheService = $this->getOption('cacheService'); } else { $this->cacheService = $this->getServiceLocator()->get(\common_cache_Cache::SERVICE_ID); } } return $this->cacheService; } }