/** * @param string $path * @return \Raml\Resource * @throws ValidatorSchemaException */ private function getResource($path) { try { return $this->apiDefinition->getResourceByUri($path); } catch (ResourceNotFoundException $exception) { $message = sprintf('Schema for URI %s was not found in API definition', $path); throw new ValidatorSchemaException($message, 0, $exception); } }
/** * @param string $path * * @throw Symfony\Component\HttpKernel\Exception\NotFoundHttpException * * @return \Raml\Resource|false */ protected function getResourceFromPath($path) { try { $resource = $this->apiDefinition->getResourceByUri($path); } catch (ResourceNotFoundException $e) { //Try with a trailing slash to accept /resource/ and /ressource try { $resource = $this->apiDefinition->getResourceByUri($path . '/'); } catch (ResourceNotFoundException $e) { throw new NotFoundHttpException($e->getMessage()); } } return $resource; }
/** * Build namespace given ApiDefinition and base namespace * * @param ApiDefinition $definition Checks if definition has version * @param string $namespace Base * @return string */ protected function buildNamespace(ApiDefinition $definition, $namespace) { if ($this->config['version_in_namespace'] && $definition->getVersion()) { $namespace .= '\\' . preg_replace(array('/(^[0-9])/', '/[^a-zA-Z0-9]/'), array('Version\\1', '_'), $definition->getVersion()); } return $namespace; }
/** * Parse RAML data * * @param string $ramlData * @param string $rootDir * * @throws RamlParserException * * @return \Raml\ApiDefinition */ private function parseRamlData($ramlData, $rootDir) { if (!isset($ramlData['title'])) { throw new RamlParserException(); } $ramlData = $this->parseTraits($ramlData); $ramlData = $this->parseResourceTypes($ramlData); if ($this->configuration->isSchemaParsingEnabled()) { if (isset($ramlData['schemas'])) { $schemas = []; foreach ($ramlData['schemas'] as $schemaCollection) { foreach ($schemaCollection as $schemaName => $schema) { $schemas[$schemaName] = $schema; } } } foreach ($ramlData as $key => $value) { if (0 === strpos($key, '/')) { if (isset($schemas)) { $value = $this->replaceSchemas($value, $schemas); } if (is_array($value)) { $value = $this->recurseAndParseSchemas($value, $rootDir); } $ramlData[$key] = $value; } } } if (isset($ramlData['securitySchemes'])) { $ramlData['securitySchemes'] = $this->parseSecuritySettings($ramlData['securitySchemes']); } return ApiDefinition::createFromArray($ramlData['title'], $ramlData); }
/** * @param string $output file to generate */ public function generate($output) { $html = $this->twig->render('index.html.twig', array('title' => $this->specification->getTitle(), 'base_url' => $this->specification->getBaseUrl(), 'security' => $this->specification->getSecuritySchemes(), 'resources' => $this->specification->getResources(), 'documentation' => $this->specification->getDocumentationList(), 'base_dir_raml' => $this->base_dir_raml, 'version' => $this->specification->getVersion())); file_put_contents($output, $html); }
/** * @param string $ramlFile */ public function generateRest($ramlFile, Local $directoryOutput) { $parser = new Parser(); try { /* * file di routing symfony * @var array */ $routing = []; /* * Mappa delle proprieta dei controller da generare * @var array */ $mappaClassDef = []; $this->apiDef = $parser->parse($ramlFile); $this->logger->info('Title: ' . $this->apiDef->getTitle()); $baseUrl = $this->apiDef->getBaseUrl(); $parametriBaseUrl = $this->apiDef->getBaseUriParameters(); /** @var \Raml\BaseUriParameter $definition */ foreach ($parametriBaseUrl as $varName => $definition) { if (!array_key_exists($varName, $this->mapExternalInfo)) { $this->error('Missing: ' . $varName . ' -> ' . $definition->getDescription()); $this->mapExternalInfo[$varName] = 'undefined'; } $baseUrl = str_replace($varName, $this->mapExternalInfo[$varName], $baseUrl); } $this->info('BaseUrl ' . $baseUrl); //corrisponde a host: "{subdomain}.example.com" dentro routing.yml $enabledProtocols = $this->apiDef->getProtocols(); //serve per fare controlli su http/https -> schemes: [https] dentro routing.yml $infoSecuritySchema = $this->apiDef->getSecuredBy(); // descrive i vari security schema usati nelle varie risorse /* @var: \Raml\Resource[] */ $resources = $this->apiDef->getResources(); $namespace = $this->bundleName . '\\Controller'; /** @var: \Raml\Resource $resource */ foreach ($resources as $resource) { $displayName = $resource->getDisplayName(); $this->info('Controller per path: ' . $displayName); $names = explode('/', $displayName); preg_match_all("/(\\/{[a-zA-Z]+}(\\/)?)+/i", $displayName, $methodParam); array_walk($names, [$this, 'removeGraph']); $className = implode('', $names); $methods = $resource->getMethods(); if (count($methods) > 0) { /** @var \Raml\Method $method */ foreach ($methods as $method) { $controllerName = ucfirst($className); // Creo $appBundle / $workspace Controller . php $this->info('Genera: ' . $namespace . $controllerName . 'Controller'); $controllerProperties = []; $controllerProperties['name'] = $controllerName . 'Controller'; $controllerProperties['namespace'] = $namespace; $controllerProperties['extend'] = 'Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller'; $methodListParams = implode(',', $methodParam[0]); $type = strtolower($method->getType()); $methodCallName = $type . $controllerName; $actionName = $methodCallName . 'Action'; $this->info('Call Method: ' . $actionName . '(' . $methodListParams . ')'); $controllerProperties['methods'] = []; $controllerProperties['methods'][$actionName] = []; $controllerProperties['methods'][$actionName]['params'] = []; $description = $method->getDescription(); $this->info('Description: ' . $description); $entryName = strtolower($className) . '_' . $type; $routing[$entryName]['path'] = $displayName; $routing[$entryName]['defaults']['_controller'] = $this->bundleName . ':' . $controllerName . ':' . $methodCallName; $routing[$entryName]['host'] = $baseUrl; $routing[$entryName]['methods'] = []; $routing[$entryName]['methods'][] = strtoupper($type); $routing[$entryName]['schemas'] = $enabledProtocols; $routing[$entryName]['requirements'] = []; $routing[$entryName]['requirements'][] = 'FIXME'; $mappaClassDef[$controllerName . 'Controller'] = $controllerProperties; } //fine methods } /* @var \Raml\Resource $subResources */ $subResources = $resource->getResources(); foreach ($subResources as $subResource) { //$this->analyzeResource($subResource, $directoryOutput); //// SAMPLE: // /Workspace GET => Workspace/GetWorkspace.php // /Workspace POST => Workspace/POSTWorkspace.php // /Workspace/{id} GET => Workspace/GetWorkspaceById.php } } //fine reousrces $indent = 4; $inline = 2; $yaml = new Dumper($indent); $this->currentFile = $yaml->dump($routing, $inline, 0, 0); $this->_createFileOnDir($directoryOutput, $this->bundleName . '/Resources/config/routing.yml'); foreach ($mappaClassDef as $className => $controllerProp) { $this->info('Devo creare ' . $className); $gClassgen = new ClassGenerator($namespace, $className); $gClassgen->setLogger($this->logger); $config = new ClassConfig(); $typesReferenceArray = []; $typesDescArray = []; $gClassgen->generateClassType($controllerProp, $typesReferenceArray, $typesDescArray, $config); $gClassgen->createFileOnDir($directoryOutput); } $this->info('Scrittura su ' . $directoryOutput); } catch (InvalidJsonException $e) { $this->error('[' . $e->getErrorCode() . '] ' . $e->getMessage()); $this->error($e->getTraceAsString()); } catch (RamlParserException $e) { $this->error('[' . $e->getErrorCode() . '] ' . $e->getMessage()); } }