/** * 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); } }
public function testAddActionAndGetActions() { $controller = new SymfonyController('my controller'); $route = $this->getMock('Creads\\Api2Symfony\\SymfonyRoute', array(), array('/route_path', 'route_name')); $action = $this->getMock('Creads\\Api2Symfony\\SymfonyAction', array(), array('myname', $route, 'get', 'my description')); $controller->addAction($action); $this->assertEquals(array($action), $controller->getActions()); }