8 use Doctrine\Common\Collections\ArrayCollection;
9 use Epfremme\Swagger\Entity\Schemas\SchemaInterface;
10 use Epfremme\Swagger\Entity\Schemas\ObjectSchema;
11 use Epfremme\Swagger\Entity\Schemas\RefSchema;
12 use Epfremme\Swagger\Entity\Schemas\ArraySchema;
13 use Epfremme\Swagger\Factory\SwaggerFactory;
14 use Epfremme\Swagger\Entity\Swagger;
15 use Epfremme\Swagger\Entity\Operation;
16 use Epfremme\Swagger\Entity\Parameters\BodyParameter;
17 use Epfremme\Swagger\Entity\Parameters\AbstractTypedParameter;
19 use Mustache_Loader_FilesystemLoader;
58 private $mustache_engine;
65 private static $swagger;
100 private static $methodMapping = [
102 'DELETE' =>
'delete',
112 self::buildSwaggerSpec();
113 $this->initMustacheTemplates();
124 $paths = self::$swagger->getPaths();
126 foreach (
$paths->getIterator() as $pathKey =>
$path) {
128 foreach (
$operations->getIterator() as $operationKey => $operation) {
129 $this->renderOperation($operation, $pathKey, $operationKey);
133 $definitions = self::$swagger->getDefinitions();
134 foreach ($definitions->getIterator() as $defKey => $definition) {
135 $this->renderDefinition($defKey, $definition);
147 private function renderOperation($operation,
$path,
$method)
149 $operationArray = [];
150 $this->pathParams =
'';
153 $operationMethod = strtoupper(
$method);
154 $operationDataType = ucfirst($operation->getOperationId());
163 $params = $operation->getParameters();
164 if (!empty($params)) {
165 $this->parseParams($params,
$path);
170 if (!empty($this->pathParams)) {
174 $this->generateMetaDataFile(
189 private function renderDefinition($defKey, $definition)
191 $operationArray = [];
198 if ($definition instanceof ObjectSchema) {
203 foreach (
$properties->getIterator() as $propertyKey => $property) {
204 if ($property instanceof ArraySchema) {
205 $dataArray[] = $this->parseSchema($property, $propertyKey, 1, 1);
207 $dataField[] = $this->parseSchema($property, $propertyKey, 0, 1);
210 if (!empty($dataField)) {
213 if (!empty($dataArray)) {
214 foreach ($dataArray as $array) {
215 $operationArray[self::TEMPLATE_VAR_OP_ARRAY.
'1'][] = $array[self::TEMPLATE_VAR_OP_ARRAY.
'1'];
219 }
elseif ($definition instanceof ArraySchema) {
220 $operationArray = array_merge($operationArray, $this->parseSchema($definition, $defKey, 1, 1));
223 $this->generateMetaDataFile(
240 private function parseSchema(
$schema,
$name, $forArray, $depth)
244 if (
$schema instanceof RefSchema) {
246 preg_match(self::REF_REGEX, $ref, $matches);
247 if (count($matches) == 2) {
259 if (
$items instanceof ArrayCollection) {
260 foreach (
$items->getIterator() as $itemKey =>
$item) {
261 $values[] = $this->parseSchema(
$item, $itemKey, 1, $depth+1);
264 $data[self::TEMPLATE_VAR_OP_ARRAY.$depth] =
$data;
266 $data[self::TEMPLATE_VAR_OP_ARRAY.$depth] = array_merge(
267 $data[self::TEMPLATE_VAR_OP_ARRAY.$depth],
272 if (method_exists(
$schema,
'getType')) {
291 private function parseParams($params,
$path)
293 foreach ($params->getIterator() as $paramKey => $param) {
298 $paramIn = $param->getIn();
299 if ($paramIn ==
'body') {
300 $this->setBodyParams($param);
301 }
elseif ($paramIn ==
'path') {
302 $this->setPathParams($param,
$path);
303 }
elseif ($paramIn ==
'query') {
304 $this->setQueryParams($param);
315 private function setBodyParams($param)
320 $paramSchema = $param->getSchema();
321 $paramSchemaRequired = $paramSchema->getRequired();
322 if (!empty($paramSchemaRequired)) {
323 foreach ($paramSchemaRequired as
$i => $key) {
327 $paramSchemaProperties = $paramSchema->getProperties();
328 foreach ($paramSchemaProperties->getIterator() as $paramPropertyKey => $paramSchemaProperty) {
332 if ($field[self::TEMPLATE_VAR_FIELD_TYPE] ==
'ref') {
333 preg_match(self::REF_REGEX, $paramSchemaProperty->getRef(), $matches);
334 if (count($matches) == 2) {
338 if (in_array($paramPropertyKey,
$required)) {
343 $this->fields[] = $field;
354 private function setPathParams($param,
$path)
356 $pathParamStr =
'{' . $param->getName() .
'}';
357 if (strpos(
$path, $pathParamStr) ===
false) {
358 $this->pathParams .=
'/' . $pathParamStr;
368 private function setQueryParams($param)
382 private static function buildSwaggerSpec()
393 private function initMustacheTemplates()
395 $this->mustache_engine =
new Mustache_Engine(
396 [
'loader' =>
new Mustache_Loader_FilesystemLoader(
"views"),
397 'partials_loader' =>
new Mustache_Loader_FilesystemLoader(
398 "views" . DIRECTORY_SEPARATOR .
"partials" 414 $this->filepath = $relativeDir . DIRECTORY_SEPARATOR .
$fileName .
"-meta.xml";
416 $this->cleanAndCreateOutputDir();
428 private function cleanAndCreateOutputDir()
430 if (!file_exists(self::OUTPUT_DIR)) {
431 mkdir(self::OUTPUT_DIR);
434 if (!file_exists(self::OUTPUT_DIR2)) {
435 mkdir(self::OUTPUT_DIR2);
438 if (file_exists($this->filepath)) {
elseif(isset( $params[ 'redirect_parent']))
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
if(!isset($_GET['name'])) $name