public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { foreach ($annotations as $annot) { if ($annot instanceof RequestParam) { $annotation->addParameter($annot->name, array('required' => $annot->strict && $annot->default === null, 'dataType' => $annot->requirements, 'description' => $annot->description, 'readonly' => false)); } elseif ($annot instanceof QueryParam) { if ($annot->strict && $annot->default === null) { $annotation->addRequirement($annot->name, array('requirement' => $annot->requirements, 'dataType' => '', 'description' => $annot->description)); } elseif ($annot->default !== null) { $annotation->addFilter($annot->name, array('requirement' => $annot->requirements, 'description' => $annot->description, 'default' => $annot->default)); } else { $annotation->addFilter($annot->name, array('requirement' => $annot->requirements, 'description' => $annot->description)); } } } }
/** * @inheritdoc */ public function handle(ApiDoc $annotation, array $annotations, Route $route, \ReflectionMethod $method) { foreach ($annotations as $annot) { if ($annot instanceof RequestParam) { $requirements = $this->handleRequirements($annot->requirements); $data = array('required' => $annot->strict && $annot->nullable === false && $annot->default === null, 'dataType' => $requirements . ($annot->array ? '[]' : ''), 'actualType' => $this->inferType($requirements), 'subType' => null, 'description' => $annot->description, 'readonly' => false); if ($annot->strict === false) { $data['default'] = $annot->default; } $annotation->addParameter($annot->name, $data); } elseif ($annot instanceof QueryParam) { if ($annot->strict && $annot->nullable === false && $annot->default === null) { $annotation->addRequirement($annot->name, array('requirement' => $this->handleRequirements($annot->requirements) . ($annot->array ? '[]' : ''), 'dataType' => '', 'description' => $annot->description)); } elseif ($annot->default !== null) { $annotation->addFilter($annot->name, array('requirement' => $this->handleRequirements($annot->requirements) . ($annot->array ? '[]' : ''), 'description' => $annot->description, 'default' => $annot->default)); } else { $annotation->addFilter($annot->name, array('requirement' => $this->handleRequirements($annot->requirements) . ($annot->array ? '[]' : ''), 'description' => $annot->description)); } } } }
/** * @param ApiDoc $annotation * @param ApiResource $resource */ protected function addPagination(ApiDoc $annotation, ApiResource $resource) { if ($resource->hasPagination()) { $annotation->addFilter('page', ['description' => 'Page']); } }
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)); } } } }
/** * Parses annotations for a given method, and adds new information to the given ApiDoc * annotation. Useful to extract information from the FOSRestBundle annotations. * * @param ApiDoc $annotation * @param Route $route * @param ReflectionMethod $method */ protected function parseAnnotations(ApiDoc $annotation, Route $route, \ReflectionMethod $method) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { if (is_subclass_of($annot, self::FOS_REST_PARAM_CLASS)) { if ($annot->strict) { $annotation->addRequirement($annot->name, array('requirement' => $annot->requirements, 'type' => '', 'description' => $annot->description)); } else { $annotation->addFilter($annot->name, array('requirement' => $annot->requirements, 'description' => $annot->description)); } } } }
/** * @param $resource * @param ApiDoc $annotation * @param Resource|Resource $dunglasResource * @param Route $route * @return ApiDoc */ private function addFilters($resource, ApiDoc $annotation, Resource $dunglasResource, Route $route) { $data = $annotation->toArray(); $tags = isset($data['tags']) ? $data['tags'] : []; //filter embed if ('DELETE' !== $annotation->getMethod()) { $availableIncludes = $this->transformerHelper->getAvailableIncludes($resource); $defaultIncludes = $this->transformerHelper->getDefaultIncludes($resource); if (false === array_key_exists('embed', $tags)) { $annotation->addFilter('embed', ['requirement' => '\\t', 'description' => 'Include resources within other resources.', 'available' => is_array($availableIncludes) ? implode(',', $availableIncludes) : $availableIncludes, 'default' => is_array($defaultIncludes) ? implode(',', $defaultIncludes) : $defaultIncludes]); } else { unset($data['requirements']['embed']); $data['tags']['embed'] = "#298A08"; $path = explode('/', $route->getPath()); $embed = array_pop($path); $singularize = Inflector::singularize($embed); if ($embed !== $singularize) { $data['tags']['collection'] = "#0040FF"; } foreach ($data['requirements'] as $key => $value) { $data['requirements'][$key] = array_merge(['name' => $key], $value); } $annotation = new ApiDoc($data); $routeClone = clone $route; $annotation->setRoute($routeClone); $tags = isset($annotation->toArray()['tags']) ? $annotation->toArray()['tags'] : []; } } if (false !== array_key_exists('collection', $tags)) { foreach ($dunglasResource->getFilters() as $filter) { foreach ($filter->getDescription($dunglasResource) as $key => $value) { $annotation->addFilter($key, ['type' => isset($value['type']) ? $value['type'] : 'string', 'requirement' => isset($value['requirement']) ? $value['requirement'] : '[a-zA-Z0-9-]+', 'description' => isset($value['description']) ? $value['description'] : $key . ' filter', 'default' => '']); } } //filter perpage $annotation->addFilter('perpage', ['requirement' => '\\d+', 'description' => 'How many object return per page.', 'default' => 10]); //filter perpage $annotation->addFilter('page', ['requirement' => '\\d+', 'description' => 'How many page start to return.', 'default' => 1]); } return $annotation; }
/** * @param ApiDoc $annotation * @param FilterCollection $filters */ protected function addFilters(ApiDoc $annotation, FilterCollection $filters) { foreach ($filters as $key => $filter) { if ($filter instanceof StandaloneFilter) { $options = ['description' => $filter->getDescription(), 'requirement' => $this->valueNormalizer->getRequirement($filter->getDataType(), [RequestType::REST, RequestType::JSON_API], $filter->isArrayAllowed())]; $default = $filter->getDefaultValueString(); if (!empty($default)) { $options['default'] = $default; } $annotation->addFilter($key, $options); } } }
/** * Parses annotations for a given method, and adds new information to the given ApiDoc * annotation. Useful to extract information from the FOSRestBundle annotations. * * @param ApiDoc $annotation * @param Route $route * @param ReflectionMethod $method */ protected function parseAnnotations(ApiDoc $annotation, Route $route, \ReflectionMethod $method) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { if (is_a($annot, self::FOS_REST_QUERY_PARAM_CLASS)) { if ($annot->strict) { $annotation->addRequirement($annot->name, array('requirement' => $annot->requirements, 'dataType' => '', 'description' => $annot->description)); } else { $annotation->addFilter($annot->name, array('requirement' => $annot->requirements, 'description' => $annot->description)); } } elseif (is_a($annot, self::FOS_REST_REQUEST_PARAM_CLASS)) { $annotation->addParameter($annot->name, array('required' => !$annot->nullable, 'dataType' => $annot->requirements, 'description' => $annot->description, 'readonly' => false)); } } }