コード例 #1
0
 public static function getAnnotations()
 {
     $controllersFilePaths = CommonFunction::getDirContents($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'Controllers');
     $annotations = [];
     $annotations['byType'] = [];
     $annotations['byController'] = [];
     foreach ($controllersFilePaths as $controllersFilePath) {
         //var_dump($controllersFilePath);
         //var_dump($annotations);
         if (preg_match('/Controllers\\' . DIRECTORY_SEPARATOR . '(.*?).php/', $controllersFilePath, $match)) {
             $className = $match[1];
             $fileName = $className . '.php';
             require_once 'Controllers' . DIRECTORY_SEPARATOR . $fileName;
             //echo "class:".$className;
             // Get  Class Annotations
             if (class_exists('SoftUni\\Controllers\\' . $className)) {
                 $classAnnotations = [];
                 $reflectionClass = new \ReflectionClass('SoftUni\\Controllers\\' . $className);
                 $doc = $reflectionClass->getDocComment();
                 if ($doc) {
                     $classAnnotations = self::extractAnnotations($doc, $classAnnotations);
                     //echo "<br/><br/>".json_encode($classAnnotations, JSON_PRETTY_PRINT)."<br/>";
                 }
                 // Get Method Annotations
                 $methods = $reflectionClass->getMethods();
                 foreach ($methods as $method) {
                     $methodName = $method->getName();
                     $methodDoc = $method->getDocComment();
                     if ($methodDoc != null) {
                         $methodAnnotations = self::extractAnnotations($methodDoc, $classAnnotations);
                         //echo $methodName.": ".json_encode($methodAnnotations, JSON_PRETTY_PRINT)."<br/>";
                     } else {
                         $methodAnnotations = [];
                         foreach ($classAnnotations as $annotationType => $value) {
                             if ($annotationType != 'Route') {
                                 $methodAnnotations[$annotationType] = $value;
                             }
                         }
                     }
                     // Add extracted annotaions to all Annotations
                     foreach ($methodAnnotations as $methodAnnotationType => $methodAnnotationProperty) {
                         $annotations['byType'][$methodAnnotationType][] = array("property" => $methodAnnotationProperty, "controller" => $className, "action" => $methodName);
                         $annotations['byController'][$className][$methodName][$methodAnnotationType] = $methodAnnotationProperty;
                     }
                     // Add GET annotation if no Get, Post, Put or Delete annotation is available
                     if (isset($annotations['byController'][$className][$methodName])) {
                         //var_dump($annotations['byController'][$className][$methodName]);
                         $httpRequestAnnotation = array_filter(array_keys($annotations['byController'][$className][$methodName]), function ($annotationType) {
                             //var_dump($annotationType);
                             if (preg_match('#(Get|Post|Delete|Put)#i', $annotationType)) {
                                 return true;
                             }
                             return false;
                         });
                     } else {
                         $httpRequestAnnotation = [];
                     }
                     if (count($httpRequestAnnotation) == 0) {
                         $getAnnotation = new GetAnnotation();
                         $getAnnotationProperty = $getAnnotation->onInitialize('GET');
                         $annotations['byController'][$className][$methodName]['GET'] = $getAnnotationProperty;
                     }
                 }
             }
         }
     }
     self::$allAnnotations = $annotations;
 }