/**
  * Recursive method which converts raml resource into action and add it to controller
  *
  * @param  SymfonyController $controller Controller where actions will be added
  * @param  Resource $resource
  * @param  string   $chainName
  */
 protected function addActions(SymfonyController $controller, Resource $resource, $chainName = '')
 {
     $actions = array();
     $chainName = $chainName . '_' . strtolower($resource->getDisplayName());
     foreach ($resource->getMethods() as $method) {
         $actionName = strtolower($method->getType()) . str_replace(' ', '', ucwords(str_replace('_', ' ', $chainName))) . 'Action';
         $route = new SymfonyRoute($resource->getUri(), strtolower($method->getType() . $chainName));
         $action = new SymfonyAction($actionName, $route, $method->getType(), $method->getDescription());
         preg_match_all('/\\{[a-zA-Z]+\\}/', $resource->getUri(), $parameters);
         foreach ($parameters[0] as $parameter) {
             $action->addParameter(substr($parameter, 1, strlen($parameter) - 2));
         }
         if ($method->getResponses()) {
             foreach ($method->getResponses() as $code => $response) {
                 $headers = array();
                 foreach ($response->getHeaders() as $key => $value) {
                     if (isset($value['required']) && $value['required']) {
                         $headers[$key] = isset($value['example']) ? $value['example'] : '';
                     }
                 }
                 $_response = new SymfonyResponse($code, $headers);
                 foreach ($this->config['allowed_response_types'] as $allowedResponsetype) {
                     if (null !== ($example = $response->getExampleByType($allowedResponsetype))) {
                         $_response->addContent(new SymfonyResponseContent($allowedResponsetype, str_replace(array("\r\n", "\n", "\r", "\t", "  "), '', $example)));
                     }
                 }
                 $action->addResponse($_response);
             }
         }
         $controller->addAction($action);
     }
     foreach ($resource->getResources() as $subresource) {
         $this->addActions($controller, $subresource, $chainName);
     }
 }
Exemple #2
0
 /**
  *
  * @param ApiDefinition $apiDefinition
  * @param string $file
  */
 public function resource(Resource $resource)
 {
     $serializer = $this->getSerializer();
     $structure = [];
     $structure['displayName'] = $resource->getDisplayName();
     $structure['description'] = $resource->getDescription();
     $structure['uriParameters'] = $resource->getBaseUriParameters();
     foreach ($resource->getMethods() as $method) {
         $structure[$method->getType()] = $this->serializeMethod($method);
     }
     $resource->getResources();
     $resource->getSecuritySchemes();
     $resource->getUri();
     $structure['responses'] = array('200');
 }
 /**
  * Add an additional resource
  *
  * @param \Raml\Resource $resource
  */
 public function addResource(Resource $resource)
 {
     $this->resources[$resource->getUri()] = $resource;
 }
Exemple #4
0
 /**
  * @param Raml\Resource $resource
  * @param string        $httpMethod
  *
  * @throw Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
  *
  * @return Raml\Method
  */
 protected function getMethodFromResource(Resource $resource, $httpMethod)
 {
     try {
         $httpMethod = strtoupper($httpMethod);
         $method = $resource->getMethod($httpMethod);
     } catch (\Exception $e) {
         $methods = [];
         foreach ($resource->getMethods() as $method) {
             $methods[] = $method->getType();
         }
         if ($httpMethod !== 'OPTIONS') {
             throw new MethodNotAllowedHttpException($methods, $e->getMessage());
         }
     }
     return $method;
 }