コード例 #1
0
ファイル: ObjectBuilder.php プロジェクト: seytar/psx
 public function getObject($className, array $constructorArguments = array(), $instanceOf = null)
 {
     $class = new ReflectionClass($className);
     if ($class->getConstructor() === null) {
         $object = $class->newInstanceArgs([]);
     } else {
         $object = $class->newInstanceArgs($constructorArguments);
     }
     if ($instanceOf !== null && !$object instanceof $instanceOf) {
         throw new InvalidArgumentException('Class ' . $className . ' must be an instanceof ' . $instanceOf);
     }
     foreach ($class->getProperties() as $property) {
         if (strpos($property->getDocComment(), '@Inject') !== false) {
             $doc = Annotation::parse($property->getDocComment());
             if ($doc->hasAnnotation('Inject')) {
                 $name = $doc->getFirstAnnotation('Inject');
                 if (empty($name)) {
                     $name = $property->getName();
                 }
                 if ($this->container->has($name)) {
                     $property->setAccessible(true);
                     $property->setValue($object, $this->container->get($name));
                 } else {
                     throw new RuntimeException('Trying to inject an non existing service ' . $name);
                 }
             }
         }
     }
     return $object;
 }
コード例 #2
0
ファイル: Annotation.php プロジェクト: seytar/psx
 /**
  * @param \PSX\Loader\RoutingCollection $collection
  * @param \ReflectionClass $class
  */
 protected function parseClass(RoutingCollection $collection, ReflectionClass $class)
 {
     $methods = $class->getMethods();
     foreach ($methods as $method) {
         if ($method->isPublic()) {
             $doc = AnnotationParser::parse($method->getDocComment());
             $httpMethod = $doc->getFirstAnnotation('httpMethod');
             $path = $doc->getFirstAnnotation('path');
             if (!empty($httpMethod) && !empty($path)) {
                 $allowed = explode('|', $httpMethod);
                 $source = $class->getName() . '::' . $method->getName();
                 $collection->add($allowed, $path, $source);
             }
         }
     }
 }
コード例 #3
0
ファイル: ContainerCommand.php プロジェクト: seytar/psx
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $services = $this->container->getServiceIds();
     $container = new ReflectionClass($this->container);
     $rows = array();
     foreach ($services as $serviceId) {
         try {
             $method = $container->getMethod('get' . Container::normalizeName($serviceId));
             $doc = Annotation::parse($method->getDocComment());
             $return = $doc->getFirstAnnotation('return');
             if (!empty($return)) {
                 $definition = $return;
             } else {
                 $definition = 'void';
             }
             $rows[] = array($serviceId, $definition);
         } catch (ReflectionException $e) {
             // method does not exist
         }
     }
     $table = new Table($output);
     $table->setStyle('compact')->setRows($rows);
     $table->render();
 }
コード例 #4
0
ファイル: SchemaWriter.php プロジェクト: k42b3/psx-ws
 protected function buildEntity(ReflectionClass $class, $key = false, $idField = 'id')
 {
     $record = $class->newInstance();
     $fields = $record->getRecordInfo()->getFields();
     $methods = $class->getMethods();
     // add key if needed and available
     if ($key && array_key_exists($idField, $fields)) {
         $this->writer->startElement('Key');
         $this->writer->startElement('PropertyRef');
         $this->writer->writeAttribute('Name', ucfirst($idField));
         $this->writer->endElement();
         $this->writer->endElement();
     }
     foreach ($fields as $k => $v) {
         // convert to camelcase if underscore is in name
         if (strpos($k, '_') !== false) {
             $k = implode('', array_map('ucfirst', explode('_', $k)));
         }
         $methodName = 'set' . ucfirst($k);
         foreach ($methods as $method) {
             if ($method->getName() == $methodName) {
                 $doc = Annotation::parse($method->getDocComment());
                 $param = $doc->getFirstAnnotation('param');
                 if (!empty($param)) {
                     $parts = explode(' ', $param, 2);
                     $type = $parts[0];
                     $type = $this->getType($type);
                     if ($type !== null) {
                         $this->writer->startElement('Property');
                         $this->writer->writeAttribute('Name', ucfirst($k));
                         $this->writer->writeAttribute('Type', $type);
                         $this->writer->endElement();
                     }
                 }
             }
         }
     }
 }
コード例 #5
0
ファイル: Index.php プロジェクト: visapi/amun
 /**
  * This method returns all annotation wich are defined in this or any parent
  * class. If an annotation type is present it overwrites all other defined
  * types if not the annotations from the parent class will be used
  *
  * @param ReflectionClass $class
  * @param string $methodName
  * @return DocBlock
  */
 private function getAnnotations(ReflectionClass $class, $methodName)
 {
     // get hierarchy
     $parents[] = $class;
     while ($parent = $class->getParentClass()) {
         $parents[] = $parent;
         $class = $parent;
     }
     // parse doc comments
     $block = new DocBlock();
     foreach ($parents as $class) {
         try {
             $method = $class->getMethod($methodName);
             if ($method) {
                 $comment = $method->getDocComment();
                 if (!empty($comment)) {
                     $doc = Annotation::parse($comment);
                     $params = $doc->getAnnotations();
                     $text = $doc->getText();
                     foreach ($params as $k => $v) {
                         $block->setAnnotations($k, $v);
                     }
                     if (!empty($text)) {
                         $block->setText($text);
                     }
                 }
             }
         } catch (\Exception $e) {
             // method probably doesnt exist
         }
     }
     return $block;
 }
コード例 #6
0
ファイル: Record.php プロジェクト: visapi/amun
 public static function parseAnnotations($className)
 {
     $class = new ReflectionClass($className);
     $method = $class->getMethod('onLoad');
     $comment = $method->getDocComment();
     if (!empty($comment)) {
         $doc = Annotation::parse($comment);
         $params = $doc->getAnnotation('param');
         $result = array();
         foreach ($params as $param) {
             $parts = explode(' ', $param);
             $name = isset($parts[0]) ? $parts[0] : null;
             $type = isset($parts[1]) ? $parts[1] : null;
             $type = self::getDataType($type);
             if (ctype_alnum($name) && $type !== null) {
                 $result[$name] = $type;
             }
         }
         return $result;
     } else {
         throw new Exception('Empty doc comment');
     }
 }
コード例 #7
0
ファイル: Record.php プロジェクト: seytar/psx
 protected function getMethodValue(ReflectionMethod $method, $value)
 {
     $comment = $method->getDocComment();
     if (!empty($comment)) {
         $doc = Annotation::parse($comment);
         $param = $doc->getFirstAnnotation('param');
         if (!empty($param)) {
             $param = explode(' ', $param);
             $type = isset($param[0]) ? $param[0] : null;
             if (substr($type, 0, 6) == 'array<') {
                 $type = substr($type, 6, -1);
                 $values = (array) $value;
                 $value = array();
                 foreach ($values as $row) {
                     $value[] = $this->getMethodType($type, $row);
                 }
             } elseif (substr($type, -2) == '[]') {
                 $type = substr($type, 0, -2);
                 $values = (array) $value;
                 $value = array();
                 foreach ($values as $row) {
                     $value[] = $this->getMethodType($type, $row);
                 }
             } else {
                 $value = $this->getMethodType($type, $value);
             }
         }
     }
     return $value;
 }
コード例 #8
0
ファイル: Container.php プロジェクト: seytar/psx
 /**
  * Tries to determine the return type of an service. At first we try to
  * determine the type from the return annotation which is in most cases
  * more useful because it could specify an interface instead of an concrete
  * implementation. As fallback we get an instance of the service and return
  * the type
  *
  * @param string $name
  * @return string
  */
 public function getReturnType($name)
 {
     $container = new ReflectionClass($this);
     try {
         $method = $container->getMethod('get' . self::normalizeName($name));
         $doc = Annotation::parse($method->getDocComment());
         $return = $doc->getFirstAnnotation('return');
         if (!empty($return)) {
             return $return;
         }
     } catch (ReflectionException $e) {
         // method does not exist
     }
     // as fallback we get the service and return the used type
     $service = $this->get($name);
     if (is_object($service)) {
         return get_class($service);
     } else {
         return gettype($service);
     }
 }