Exemplo n.º 1
0
 /**
  * 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);
     }
 }
Exemplo n.º 2
0
 public function testSetResponseAndGetResponse()
 {
     $route = $this->getMockedRoute();
     $action = new SymfonyAction('myname', $route, 'get', 'my description');
     $response = $this->getMockedResponse();
     $action->addResponse($response);
     $this->assertSame($response, $action->getResponses()[0]);
 }