public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
 {
     // description
     if (null === $annotation->getDescription()) {
         $comments = explode("\n", $annotation->getDocumentation());
         // just set the first line
         $comment = trim($comments[0]);
         $comment = preg_replace("#\n+#", ' ', $comment);
         $comment = preg_replace('#\\s+#', ' ', $comment);
         $comment = preg_replace('#[_`*]+#', '', $comment);
         if ('@' !== substr($comment, 0, 1)) {
             $annotation->setDescription($comment);
         }
     }
     // requirements
     $requirements = $annotation->getRequirements();
     foreach ($route->getRequirements() as $name => $value) {
         if (!isset($requirements[$name]) && '_method' !== $name && '_scheme' !== $name) {
             $requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
         }
         if ('_scheme' === $name) {
             $https = 'https' == $value;
             $annotation->setHttps($https);
         }
     }
     if (method_exists($route, 'getSchemes')) {
         $annotation->setHttps(in_array('https', $route->getSchemes()));
     }
     $paramDocs = array();
     foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
         if (preg_match('{^@param (.+)}', trim($line), $matches)) {
             $paramDocs[] = $matches[1];
         }
         if (preg_match('{^@deprecated\\b(.*)}', trim($line), $matches)) {
             $annotation->setDeprecated(true);
         }
         if (preg_match('{^@link\\b(.*)}', trim($line), $matches)) {
             $annotation->setLink($matches[1]);
         }
     }
     $regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
     foreach ($route->compile()->getVariables() as $var) {
         $found = false;
         foreach ($paramDocs as $paramDoc) {
             if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
                 $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
                 $requirements[$var]['description'] = $matches[2];
                 if (!isset($requirements[$var]['requirement'])) {
                     $requirements[$var]['requirement'] = '';
                 }
                 $found = true;
                 break;
             }
         }
         if (!isset($requirements[$var]) && false === $found) {
             $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
         }
     }
     $annotation->setRequirements($requirements);
 }
예제 #2
0
 public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
 {
     // description
     if (null === $annotation->getDescription()) {
         $comments = explode("\n", $annotation->getDocumentation());
         // just set the first line
         $comment = trim($comments[0]);
         $comment = preg_replace("#\n+#", ' ', $comment);
         $comment = preg_replace('#\\s+#', ' ', $comment);
         $comment = preg_replace('#[_`*]+#', '', $comment);
         if ('@' !== substr($comment, 0, 1)) {
             $annotation->setDescription($comment);
         }
     }
     // schema
     if ($annotation->getSchema() !== null) {
         $path = 'file://' . realpath($this->schemaPath . $annotation->getSchema());
         $properties = (array) json_decode(file_get_contents($path))->properties;
         $tab = [];
         foreach ($properties as $objectName => $property) {
             if (isset($property->properties)) {
                 $tab = array_merge($tab, $annotation->schemaFormat($property->properties, $property->required, $objectName));
             } else {
                 $required = json_decode(file_get_contents($path))->required;
                 $tab = array_merge($tab, $annotation->schemaFormat($properties, $required));
             }
         }
         $annotation->setParameters($tab);
     }
     // requirements
     $requirements = $annotation->getRequirements();
     foreach ($route->getRequirements() as $name => $value) {
         if (!isset($requirements[$name]) && '_method' !== $name && '_scheme' !== $name) {
             $requirements[$name] = array('requirement' => $value, 'dataType' => '', 'description' => '');
         }
         if ('_scheme' === $name) {
             $https = 'https' == $value;
             $annotation->setHttps($https);
         }
     }
     if (method_exists($route, 'getSchemes')) {
         $annotation->setHttps(in_array('https', $route->getSchemes()));
     }
     $paramDocs = array();
     foreach (explode("\n", $this->commentExtractor->getDocComment($method)) as $line) {
         if (preg_match('{^@param (.+)}', trim($line), $matches)) {
             $paramDocs[] = $matches[1];
         }
         if (preg_match('{^@deprecated}', trim($line))) {
             $annotation->setDeprecated(true);
         }
         if (preg_match('{^@link (.+)}', trim($line), $matches)) {
             $annotation->setLink($matches[1]);
         }
     }
     $regexp = '{(\\w*) *\\$%s\\b *(.*)}i';
     foreach ($route->compile()->getVariables() as $var) {
         $found = false;
         foreach ($paramDocs as $paramDoc) {
             if (preg_match(sprintf($regexp, preg_quote($var)), $paramDoc, $matches)) {
                 $annotationRequirements = $annotation->getrequirements();
                 if (!isset($annotationRequirements[$var]['dataType'])) {
                     $requirements[$var]['dataType'] = isset($matches[1]) ? $matches[1] : '';
                 }
                 if (!isset($annotationRequirements[$var]['description'])) {
                     $requirements[$var]['description'] = $matches[2];
                 }
                 if (!isset($requirements[$var]['requirement']) && !isset($annotationRequirements[$var]['requirement'])) {
                     $requirements[$var]['requirement'] = '';
                 }
                 $found = true;
                 break;
             }
         }
         if (!isset($requirements[$var]) && false === $found) {
             $requirements[$var] = array('requirement' => '', 'dataType' => '', 'description' => '');
         }
     }
     $annotation->setRequirements($requirements);
 }
예제 #3
0
 public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method)
 {
     if (!($objectName = $route->getDefault('_jarves_object')) || !($object = $this->objects->getDefinition($objectName))) {
         return;
     }
     if ($entryPointPath = $route->getDefault('_jarves_entry_point')) {
         $adminUtils = new \Jarves\Admin\Utils($this->jarves);
         $entryPoint = $adminUtils->getEntryPoint($entryPointPath);
         $annotation->setSection(sprintf('%s %s %s', $entryPoint->isFrameworkWindow() ? 'Framework Window: ' : '', $entryPoint->getBundle() ? ($entryPoint->getBundle()->getLabel() ?: $entryPoint->getBundle()->getBundleName()) . ', ' : 'No Bundle, ', $entryPoint->getLabel() ?: $entryPoint->getPath()));
     } else {
         $objectKey = $route->getDefault('_jarves_object_section') ?: $route->getDefault('_jarves_object');
         $objectSection = $this->objects->getDefinition($objectKey);
         $annotation->setSection(sprintf('Object %s', $objectKey));
     }
     $filters = $annotation->getFilters();
     if (@$filters['fields']) {
         $fields = [];
         foreach ($object->getFields() as $field) {
             if ('object' === $field->getId()) {
                 $foreignObject = $this->objects->getDefinition($field->getObject());
                 foreach ($foreignObject->getFields() as $fField) {
                     $filters[] = $field->getId() . '.' . $fField->getId();
                 }
             } else {
                 $fields[] = $field->getId();
             }
         }
         $annotation->addFilter('fields', ['requirement' => '.*', 'description' => "Comma separated list of fields. Possible fields to select: \n" . implode(', ', $fields)]);
     }
     $annotation->setDescription(str_replace('%object%', $object->getBundle()->getBundleName() . ':' . lcfirst($object->getId()), $annotation->getDescription()));
     $isRelationRoute = $route->getDefault('_jarves_object_relation');
     $requirePk = $route->getDefault('_jarves_object_requirePk');
     $method = explode(':', $route->getDefault('_controller'))[1];
     //        maybe in version 1.1
     //        if ($isRelationRoute) {
     //            $objectKey = $route->getDefault('_jarves_object_section') ? : $route->getDefault('_jarves_object');
     //            $objectParent = $this->jarves->getObjects()->getDefinition($objectKey);
     //
     //            foreach ($objectParent->getFields() as $field) {
     //                if ($field->isPrimaryKey()) {
     //                    $annotation->addRequirement(
     //                        $field->getId(),
     //                        [
     //                            'requirement' => $field->getRequiredRegex(),
     //                            'dataType' => $field->getPhpDataType(),
     //                            'description' => '(' . $objectParent->getId() . ') ' . $field->getDescription()
     //                        ]
     //                    );
     //                }
     //            }
     //        }
     if ($requirePk) {
         foreach ($object->getFields() as $field) {
             if (!$field->hasFieldType()) {
                 continue;
             }
             if ($field->isPrimaryKey()) {
                 $annotation->addRequirement(($isRelationRoute ? lcfirst($object->getId()) . '_' : '') . $field->getId(), ['requirement' => $field->getRequiredRegex(), 'dataType' => $field->getPhpDataType(), 'description' => $isRelationRoute ? '(' . $object->getId() . ') ' : '']);
             }
         }
     }
     //add all fields to some actions
     if (in_array($method, ['addItemAction', 'patchItemAction', 'updateItemAction'])) {
         foreach ($object->getFields() as $field) {
             if (!$field->hasFieldType()) {
                 continue;
             }
             if ($field->isRequired() && !$field->getDefault()) {
                 $annotation->addRequirement($field->getId(), array('requirement' => $field->getRequiredRegex(), 'dataType' => $field->getPhpDataType(), 'description' => ($isRelationRoute ? '(' . $object->getId() . ') ' : '') . $field->getLabel()));
             } else {
                 $annotation->addParameter($field->getId(), array('format' => $field->getRequiredRegex(), 'dataType' => $field->getPhpDataType(), 'default' => $field->getDefault(), 'description' => $field->getLabel() . ($field->isAutoIncrement() ? ' (autoIncremented)' : ''), 'readonly' => false, 'required' => false));
             }
         }
     }
 }