Пример #1
0
 public function addEventMap(EventMap $eventMap)
 {
     $this->events[$eventMap->getName()] = $eventMap;
 }
 /**
  * Loads from annotations from a class.
  *
  * @param string $class A class name
  * @param string $type  The resource type
  *
  * @return EventsMap A event map
  *
  * @throws \InvalidArgumentException When annotations can't be parsed
  */
 public function load($class, $type = null)
 {
     if (!class_exists($class)) {
         throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
     }
     $class = new \ReflectionClass($class);
     if ($class->isAbstract()) {
         throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class));
     }
     $map = new EventsMap();
     $map->addResource(new FileResource($class->getFileName()));
     $eventMap = null;
     foreach ($this->reader->getClassAnnotations($class) as $annot) {
         if ($annot instanceof $this->annotationClassAction) {
             $eventMap = new EventMap($annot->getName(), $class->getName(), $this->annotationClassAction);
             continue;
         }
         if ($annot instanceof $this->annotationClassCause) {
             $eventMap = new EventMap($annot->getName(), $class->getName(), $this->annotationClassCause);
             continue;
         }
     }
     if ($eventMap) {
         $getters = array();
         $setters = array();
         foreach ($class->getMethods() as $method) {
             foreach ($this->reader->getMethodAnnotations($method) as $annot) {
                 if ($annot instanceof $this->annotationMethodGetter) {
                     $getter = array('type' => $annot->getType(), 'data' => $annot->isData());
                     if ($annot->getField()) {
                         $getter['field'] = $annot->getField();
                     } else {
                         $field = $method->getName();
                         if (strncmp($field, 'get', 3) === 0 && strlen($field) > 3) {
                             $field = lcfirst(substr($field, 3));
                         } elseif (strncmp($field, 'is', 2) === 0 && strlen($field) > 2) {
                             $field = lcfirst(substr($field, 2));
                         }
                         $getter['field'] = $field;
                     }
                     $getters[$method->getName()] = $getter;
                 }
                 if ($annot instanceof $this->annotationMethodSetter) {
                     $setter = array('required' => $annot->isRequired(), 'type' => $annot->getType());
                     if ($annot->getField()) {
                         $setter['field'] = $annot->getField();
                     } else {
                         $field = $method->getName();
                         if (strncmp($field, 'set', 3) === 0 && strlen($field) > 3) {
                             $field = lcfirst(substr($field, 3));
                         }
                         $setter['field'] = $field;
                     }
                     $setters[$method->getName()] = $setter;
                 }
             }
         }
         $eventMap->setGetters($getters);
         $eventMap->setSetters($setters);
         $map->addEventMap($eventMap);
     }
     return $map;
 }