Ejemplo n.º 1
0
 public function Service()
 {
     $className = get_class($this);
     parent::__construct($className);
     $this->route = $className;
     $this->serviceMethods = array();
     $this->subroutes = array();
     // Annotations
     if ($this->hasAnnotation('Route')) {
         $annotation = $this->getAnnotation('Route');
         if ($annotation->value) {
             $this->route = $annotation->value;
         }
     }
     // Service methods
     foreach ($this->getMethods() as $reflectionMethod) {
         $methodName = strtolower($reflectionMethod->name);
         $httpMethods = array();
         if ($methodName == 'get' || $methodName == 'post' || $methodName == 'put' || $methodName == 'delete' || $methodName == 'any') {
             $httpMethods[] = $methodName;
         }
         // Check for http method annotations
         if ($reflectionMethod->hasAnnotation('Get')) {
             $httpMethods[] = 'get';
         }
         if ($reflectionMethod->hasAnnotation('Post')) {
             $httpMethods[] = 'post';
         }
         if ($reflectionMethod->hasAnnotation('Put')) {
             $httpMethods[] = 'put';
         }
         if ($reflectionMethod->hasAnnotation('Delete')) {
             $httpMethods[] = 'delete';
         }
         if ($reflectionMethod->hasAnnotation('Any')) {
             $httpMethods[] = 'any';
         }
         $httpMethods = array_unique($httpMethods);
         $subroute = NULL;
         if ($reflectionMethod->hasAnnotation('Subroute')) {
             $subrouteAnnotation = $reflectionMethod->getAnnotation('Subroute');
             if ($subrouteAnnotation->value) {
                 $subroute = preg_replace('/{[^}]*}/', '{}', $subrouteAnnotation->value);
             }
         }
         if (count($httpMethods) > 0) {
             if ($subroute) {
                 if (!isset($this->subroutes[$subroute])) {
                     $this->subroutes[$subroute] = array();
                 }
                 $this->subroutes[$subroute][] = new ServiceMethod($this, $reflectionMethod, $httpMethods);
             } else {
                 $this->serviceMethods[] = new ServiceMethod($this, $reflectionMethod, $httpMethods);
             }
         }
     }
 }