diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/cleanupParameters.php b/data/generator/sfDoctrineRestGenerator/default/parts/cleanupParameters.php index 4f9e95d..e6cf93f 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/cleanupParameters.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/cleanupParameters.php @@ -10,11 +10,9 @@ protected function cleanupParameters($params) unset($params['module']); unset($params['action']); - $additional_params = configuration->getValue('get.additional_params', array())); ?>; - foreach ($params as $name => $value) { - if ((null === $value) || ('' === $value) || in_array($name, $additional_params)) + if ((null === $value) || ('' === $value) || in_array($name, $this->additional_params)) { unset($params[$name]); } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/createAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/createAction.php deleted file mode 100644 index 054822a..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/createAction.php +++ /dev/null @@ -1,60 +0,0 @@ - /** - * Creates a getModelClass() ?> object - * @param sfWebRequest $request a request object - * @return string - */ - public function executeCreate(sfWebRequest $request) - { - $this->forward404Unless($request->isMethod(sfRequest::POST)); - $content = $request->getContent(); - - // Restores backward compatibility. Content can be the HTTP request full body, or a form encoded "content" var. - if (strpos($content, 'content=') === 0) - { - $content = $request->getParameter('content'); - } - if ($content === false) - { - $content = $request->getPostParameter('content'); // Last chance to get the content! - } - - $request->setRequestFormat('html'); - - try - { - $this->validateCreate($content); - } - catch (Exception $e) - { - $this->getResponse()->setStatusCode(406); - $serializer = $this->getSerializer(); - $this->getResponse()->setContentType($serializer->getContentType()); - $error = $e->getMessage(); - - // event filter to enable customisation of the error message. - $result = $this->dispatcher->filter( - new sfEvent($this, 'sfDoctrineRestGenerator.filter_error_output'), - $error - )->getReturnValue(); - - if ($error === $result) - { - $error = array(array('message' => $error)); - $this->output = $serializer->serialize($error, 'error'); - } - else - { - $this->output = $serializer->serialize($result); - } - - $this->setTemplate('index'); - return sfView::SUCCESS; - } - - $this->object = $this->createObject(); - $this->updateObjectFromRequest($content); - $this->getResponse()->setStatusCode(201); - $this->doSave(); - $this->getResponse()->setHttpHeader('Location', $this->getUrlForAction('show', false)); - return sfView::NONE; - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/createObject.php b/data/generator/sfDoctrineRestGenerator/default/parts/createObject.php deleted file mode 100644 index 9fc9c24..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/createObject.php +++ /dev/null @@ -1,4 +0,0 @@ - protected function createObject() - { - return new $this->model(); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/doSave.php b/data/generator/sfDoctrineRestGenerator/default/parts/doSave.php deleted file mode 100644 index 965fe86..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/doSave.php +++ /dev/null @@ -1,6 +0,0 @@ - protected function doSave() - { - $this->object->save(); - - return sfView::NONE; - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/formatObjects.php b/data/generator/sfDoctrineRestGenerator/default/parts/formatObjects.php new file mode 100644 index 0000000..70f11a4 --- /dev/null +++ b/data/generator/sfDoctrineRestGenerator/default/parts/formatObjects.php @@ -0,0 +1,25 @@ + /** + * Format objects for output + * + * @param array $params The request parameters + * @return void + */ + protected function formatObjects(array $params) + { +configuration->getValue('get.object_additional_fields')) > 0): ?> + + foreach ($this->objects as $key => $object) + { +configuration->getValue('get.object_additional_fields') as $field): ?> + $this->embedAdditional($key, $params); + + } + +configuration->getValue('get.global_additional_fields') as $field): ?> + $this->embedGlobalAdditional($params); + + + // configure the fields of the returned objects and eventually hide some + $this->setFieldVisibility(); + $this->configureFields(); + } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php index 8763637..215b591 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php @@ -8,32 +8,15 @@ public function getIndexValidators() getColumns() as $column): ?> $validators['getFieldName() ?>'] = new getIndexValidatorClassForColumn($column) ?>(getIndexValidatorOptionsForColumn($column) ?>); -configuration->getValue('get.pagination_custom_page_size'); -$pagination_enabled = $this->configuration->getValue('get.pagination_enabled'); -$max_items = $this->configuration->getValue('get.max_items'); ?> - - $validators['page'] = new sfValidatorInteger(array('min' => 1, 'required' => false)); - 0)): ?> - $params = array( - 'min' => 1, - 'max' => , - 'required' => false - ); - $validators['page_size'] = new sfValidatorInteger($params); - - -configuration->getValue('get.sort_custom'); ?> - - $validators['sort_by'] = new sfValidatorChoice(array('choices' => table->getColumnNames()) ?>, 'required' => false)); - $validators['sort_order'] = new sfValidatorChoice(array('choices' => array('asc', 'desc'), 'required' => false)); - -configuration->getValue('get.additional_params'); ?> - - - $validators[''] = new sfValidatorPass(array('required' => false)); - - + + $validators = array_merge($validators, $this->getPaginationValidators()); + + $validators = array_merge($validators, $this->getSortValidators()); + + foreach ($this->additional_params as $param) + { + $validators[$param] = new sfValidatorPass(array('required' => false)); + } return $validators; } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getPaginationValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getPaginationValidators.php new file mode 100644 index 0000000..5398016 --- /dev/null +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getPaginationValidators.php @@ -0,0 +1,21 @@ +configuration->getValue('get.pagination_custom_page_size'); +$max_items = $this->configuration->getValue('get.max_items'); +?> + /** + * Returns the list of pagination validators + * @return array an array of validators + */ + protected function getPaginationValidators() + { + $validators = array(); + $validators['page'] = new sfValidatorInteger(array('min' => 1, 'required' => false)); + 0)): ?> + $validators['page_size'] = new sfValidatorInteger(array( + 'min' => 1, + 'max' => , + 'required' => false + )); + + return $validators; + } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php new file mode 100644 index 0000000..3ad5b83 --- /dev/null +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php @@ -0,0 +1,16 @@ +/** + * Returns the list of sort validators + * @return array an array of validators + */ +protected function getSortValidators() +{ + $validators['sort_by'] = new sfValidatorChoice(array( + 'choices' => table->getColumnNames()) ?>, + 'required' => false, + )); + $validators['sort_order'] = new sfValidatorChoice(array( + 'choices' => array('asc', 'desc'), + 'required' => false, + )); + return $validators; +} diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getUpdatePostValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getUpdatePostValidators.php deleted file mode 100644 index a502e84..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getUpdatePostValidators.php +++ /dev/null @@ -1,8 +0,0 @@ - /** - * Returns the list of validators for an update request. - * @return array an array of validators - */ - public function getUpdatePostValidators() - { - return $this->getCreatePostValidators() ; - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getUpdateValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getUpdateValidators.php deleted file mode 100644 index d6f90a1..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getUpdateValidators.php +++ /dev/null @@ -1,8 +0,0 @@ - /** - * Returns the list of validators for an update request. - * @return array an array of validators - */ - public function getUpdateValidators() - { - return $this->getCreateValidators(); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php index 4997beb..be5c9c0 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php @@ -12,6 +12,7 @@ public function executeIndex(sfWebRequest $request) $this->dispatcher->notify(new sfEvent($this, 'sfDoctrineRestGenerator.get.pre', array('params' => $params))); $request->setRequestFormat('html'); + $this->setTemplate('index'); $params = $this->cleanupParameters($params); try @@ -22,27 +23,7 @@ public function executeIndex(sfWebRequest $request) catch (Exception $e) { $this->getResponse()->setStatusCode(406); - $serializer = $this->getSerializer(); - $this->getResponse()->setContentType($serializer->getContentType()); - $error = $e->getMessage(); - - // event filter to enable customisation of the error message. - $result = $this->dispatcher->filter( - new sfEvent($this, 'sfDoctrineRestGenerator.filter_error_output'), - $error - )->getReturnValue(); - - if ($error === $result) - { - $error = array(array('message' => $error)); - $this->output = $serializer->serialize($error, 'error'); - } - else - { - $this->output = $serializer->serialize($result); - } - - return sfView::SUCCESS; + return $this->handleException($e); } $this->queryExecute($params); @@ -62,27 +43,9 @@ public function executeIndex(sfWebRequest $request) isManyToManyRelation($embed_relation)): ?> $this->embedManyToMany($params); -configuration->getValue('get.object_additional_fields'); ?> - 0): ?> - - foreach ($this->objects as $key => $object) - { - - $this->embedAdditional($key, $params); - - } -configuration->getValue('get.global_additional_fields'); ?> - - - $this->embedGlobalAdditional($params); - - // configure the fields of the returned objects and eventually hide some - $this->setFieldVisibility(); - $this->configureFields(); + $this->formatObjects($params); - $serializer = $this->getSerializer(); - $this->getResponse()->setContentType($serializer->getContentType()); - $this->output = $serializer->serialize($this->objects, $this->model); + $this->outputObjects(true); unset($this->objects); } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/postValidate.php b/data/generator/sfDoctrineRestGenerator/default/parts/postValidate.php deleted file mode 100644 index a1560b5..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/postValidate.php +++ /dev/null @@ -1,25 +0,0 @@ - /** - * Applies a set of validators to an array of parameters - * - * @param array $params An array of parameters - * @param array $validators An array of validators - * @throw sfException - */ - public function postValidate($params, $validators, $prefix = '') - { - foreach ($params as $name => $value) - { - if (isset($validators[$name])) - { - if (is_array($validators[$name])) - { - // validator for a related object - $this->validate($value, $validators[$name], $prefix.$name.'.'); - } - else - { - $validators[$name]->clean($value); - } - } - } - } \ No newline at end of file diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php b/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php deleted file mode 100644 index 2b72f16..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php +++ /dev/null @@ -1,17 +0,0 @@ - /** - * Execute the query for selecting a collection of objects, eventually - * along with related objects - * - * @param array $params an array of criterions for the selection - */ - public function queryExecute($params) - { - $this->objects = $this->dispatcher->filter( - new sfEvent( - $this, - 'sfDoctrineRestGenerator.filter_results', - array() - ), - $this->query($params)->execute(array(), Doctrine_Core::HYDRATE_ARRAY) - )->getReturnValue(); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php b/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php deleted file mode 100644 index db7c0fe..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php +++ /dev/null @@ -1,17 +0,0 @@ - /** - * Execute the query for selecting an object, eventually along with related - * objects - * - * @param array $params an array of criterions for the selection - */ - public function queryFetchOne($params) - { - $this->objects = array($this->dispatcher->filter( - new sfEvent( - $this, - 'sfDoctrineRestGenerator.filter_result', - array() - ), - $this->query($params)->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY) - )->getReturnValue()); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php index 935934f..201e6d2 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -22,45 +22,15 @@ public function executeShow(sfWebRequest $request) } catch (Exception $e) { - $this->getResponse()->setStatusCode(406); - $serializer = $this->getSerializer(); - $this->getResponse()->setContentType($serializer->getContentType()); - $error = $e->getMessage(); - - // event filter to enable customisation of the error message. - $result = $this->dispatcher->filter( - new sfEvent($this, 'sfDoctrineRestGenerator.filter_error_output'), - $error - )->getReturnValue(); - - if ($error === $result) - { - $error = array(array('message' => $error)); - $this->output = $serializer->serialize($error, 'error'); - } - else - { - $this->output = $serializer->serialize($result); - } - - return sfView::SUCCESS; + $this->getResponse()->setStatusCode(406); + return $this->handleException($e); } $this->queryFetchOne($params); $this->forward404Unless(is_array($this->objects[0])); -configuration->getValue('get.object_additional_fields') as $field): ?> - $this->embedAdditional(0, $params); - -configuration->getValue('get.global_additional_fields') as $field): ?> - $this->embedGlobalAdditional($params); - - - $this->setFieldVisibility(); - $this->configureFields(); + $this->formatObjects($params); - $serializer = $this->getSerializer(); - $this->getResponse()->setContentType($serializer->getContentType()); - $this->output = $serializer->serialize($this->objects[0], $this->model, false); + $this->outputObjects(false); unset($this->objects); } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php index 5c15302..ae366fa 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php @@ -6,13 +6,7 @@ public function executeUpdate(sfWebRequest $request) { $this->forward404Unless($request->isMethod(sfRequest::PUT)); - $content = $request->getContent(); - - // Restores backward compatibility. Content can be the HTTP request full body, or a form encoded "content" var. - if (strpos($content, 'content=') === 0 || $request->hasParameter('content')) - { - $content = $request->getParameter('content'); - } + $content = $this->getContent(); $request->setRequestFormat('html'); @@ -23,28 +17,7 @@ public function executeUpdate(sfWebRequest $request) catch (Exception $e) { $this->getResponse()->setStatusCode(406); - $serializer = $this->getSerializer(); - $this->getResponse()->setContentType($serializer->getContentType()); - $error = $e->getMessage(); - - // event filter to enable customisation of the error message. - $result = $this->dispatcher->filter( - new sfEvent($this, 'sfDoctrineRestGenerator.filter_error_output'), - $error - )->getReturnValue(); - - if ($error === $result) - { - $error = array(array('message' => $error)); - $this->output = $serializer->serialize($error, 'error'); - } - else - { - $this->output = $serializer->serialize($result); - } - - $this->setTemplate('index'); - return sfView::SUCCESS; + return $this->handleException($e); } // retrieve the object diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/updateObjectFromRequest.php b/data/generator/sfDoctrineRestGenerator/default/parts/updateObjectFromRequest.php deleted file mode 100644 index e60137b..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/updateObjectFromRequest.php +++ /dev/null @@ -1,4 +0,0 @@ - protected function updateObjectFromRequest($content) - { - $this->object->importFrom('array', $this->parsePayload($content)); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/validate.php b/data/generator/sfDoctrineRestGenerator/default/parts/validate.php deleted file mode 100644 index 0c92b20..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/validate.php +++ /dev/null @@ -1,49 +0,0 @@ - /** - * Applies a set of validators to an array of parameters - * - * @param array $params An array of parameters - * @param array $validators An array of validators - * @throw sfException - */ - public function validate($params, $validators, $prefix = '') - { - $unused = array_keys($validators); - - foreach ($params as $name => $value) - { - if (!isset($validators[$name])) - { - throw new sfException(sprintf('Could not validate extra field "%s"', $prefix.$name)); - } - else - { - if (is_array($validators[$name])) - { - // validator for a related object - $this->validate($value, $validators[$name], $prefix.$name.'.'); - } - else - { - $validators[$name]->clean($value); - } - - unset($unused[array_search($name, $unused, true)]); - } - } - - // are non given values required? - foreach ($unused as $name) - { - try - { - if (!is_array($validators[$name])) - { - $validators[$name]->clean(null); - } - } - catch (Exception $e) - { - throw new sfException(sprintf('Could not validate field "%s": %s', $prefix.$name, $e->getMessage())); - } - } - } \ No newline at end of file diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/validateCreate.php b/data/generator/sfDoctrineRestGenerator/default/parts/validateCreate.php deleted file mode 100644 index 14c39db..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/validateCreate.php +++ /dev/null @@ -1,15 +0,0 @@ - /** - * Applies the creation validators to the payload posted to the service - * - * @param string $payload A payload string - */ - public function validateCreate($payload) - { - $params = $this->parsePayload($payload); - - $validators = $this->getCreateValidators(); - $this->validate($params, $validators); - - $postvalidators = $this->getCreatePostValidators(); - $this->postValidate($params, $postvalidators); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/validateIndex.php b/data/generator/sfDoctrineRestGenerator/default/parts/validateIndex.php deleted file mode 100644 index 2dd85ec..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/validateIndex.php +++ /dev/null @@ -1,14 +0,0 @@ - /** - * Applies the get validators to the constraint parameters passed to the - * webservice - * - * @param array $params An array of criterions used for the selection - */ - public function validateIndex($params) - { - $validators = $this->getIndexValidators(); - $this->validate($params, $validators); - - $postvalidators = $this->getIndexPostValidators(); - $this->postValidate($params, $postvalidators); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/validateShow.php b/data/generator/sfDoctrineRestGenerator/default/parts/validateShow.php deleted file mode 100644 index 6efa9aa..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/validateShow.php +++ /dev/null @@ -1,14 +0,0 @@ - /** - * Applies the get validators to the constraint parameters passed to the - * webservice - * - * @param array $params An array of criterions used for the selection - */ - public function validateShow($params) - { - $validators = $this->getIndexValidators(); - $this->validate($params, $validators); - - $postvalidators = $this->getIndexPostValidators(); - $this->postValidate($params, $postvalidators); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/validateUpdate.php b/data/generator/sfDoctrineRestGenerator/default/parts/validateUpdate.php deleted file mode 100644 index 1f96557..0000000 --- a/data/generator/sfDoctrineRestGenerator/default/parts/validateUpdate.php +++ /dev/null @@ -1,15 +0,0 @@ - /** - * Applies the update validators to the payload posted to the service - * - * @param string $payload A payload string - */ - public function validateUpdate($payload) - { - $params = $this->parsePayload($payload); - - $validators = $this->getUpdateValidators(); - $this->validate($params, $validators); - - $postvalidators = $this->getUpdatePostValidators(); - $this->postValidate($params, $postvalidators); - } diff --git a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 674e04c..83fa704 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -13,18 +13,15 @@ class getGeneratedModuleName() ?>Actions extends getModelClass() ?>'; + protected $additional_params = configuration->getValue('get.additional_params', array())); ?>; - - - - - + @@ -36,11 +33,21 @@ class getGeneratedModuleName() ?>Actions extends - +configuration->getValue('get.pagination_enabled')) +{ + include dirname(__FILE__).'/../../parts/getPaginationValidators.php'; +} +?> - + - +configuration->getValue('get.sort_custom')) +{ + include dirname(__FILE__).'/../../parts/getSortValidators.php'; +} +?> @@ -48,30 +55,12 @@ class getGeneratedModuleName() ?>Actions extends - - - - - - - - - - - - - - - - - - } diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php new file mode 100644 index 0000000..ceeebc3 --- /dev/null +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -0,0 +1,318 @@ +getModelClass() ?> object + * @param sfWebRequest $request a request object + * @return string + */ + public function executeCreate(sfWebRequest $request) + { + $this->forward404Unless($request->isMethod(sfRequest::POST)); + $content = $this->getContent(); + + $request->setRequestFormat('html'); + + try + { + $this->validateCreate($content); + } + catch (Exception $e) + { + return $this->handleException($e); + } + + $this->object = $this->createObject(); + $this->updateObjectFromRequest($content); + $this->getResponse()->setStatusCode(201); + $this->doSave(); + $this->getResponse()->setHttpHeader('Location', $this->getUrlForAction('show', false)); + return sfView::NONE; + } + + protected function createObject() + { + return new $this->model(); + } + + protected function doSave() + { + $this->object->save(); + + return sfView::NONE; + } + + protected function getContent() + { + $request = $this->getRequest(); + $content = $request->getContent(); + + // Restores backward compatibility. Content can be the HTTP request full body, or a form encoded "content" var. + if (strpos($content, 'content=') === 0) + { + $content = $request->getParameter('content'); + } + if ($content === false) + { + $content = $request->getPostParameter('content'); // Last chance to get the content! + } + + return $content; + } + + protected function getPaginationValidators() + { + return array(); + } + + protected function getSortValidators() + { + return array(); + } + + /** + * Returns the list of validators for an update request. + * @return array an array of validators + */ + public function getUpdatePostValidators() + { + return $this->getCreatePostValidators() ; + } + + /** + * Returns the list of validators for an update request. + * @return array an array of validators + */ + public function getUpdateValidators() + { + return $this->getCreateValidators(); + } + + /** + * Handle an exception + * @param Exception exception + * @return sfView::SUCCESS; + */ + public function handleException(Exception $e) + { + $this->getResponse()->setStatusCode(406); + $serializer = $this->getSerializer(); + $this->getResponse()->setContentType($serializer->getContentType()); + $error = $e->getMessage(); + + // event filter to enable customisation of the error message. + $result = $this->dispatcher->filter( + new sfEvent($this, 'sfDoctrineRestGenerator.filter_error_output'), + $error + )->getReturnValue(); + + if ($error === $result) + { + $error = array(array('message' => $error)); + $this->output = $serializer->serialize($error, 'error'); + } + else + { + $this->output = $serializer->serialize($result); + } + + $this->setTemplate('index'); + return sfView::SUCCESS; + } + + /** + * Output the objects + * + * @return void + */ + protected function outputObjects($multiple = true) + { + $serializer = $this->getSerializer(); + $this->getResponse()->setContentType($serializer->getContentType()); + if ($multiple) { + $this->output = $serializer->serialize($this->objects, $this->model); + } else { + $this->output = $serializer->serialize($this->objects[0], $this->model, false); + } + } + + /** + * Applies a set of validators to an array of parameters + * + * @param array $params An array of parameters + * @param array $validators An array of validators + * @throw sfException + */ + public function postValidate($params, $validators, $prefix = '') + { + foreach ($params as $name => $value) + { + if (isset($validators[$name])) + { + if (is_array($validators[$name])) + { + // validator for a related object + $this->validate($value, $validators[$name], $prefix.$name.'.'); + } + else + { + $validators[$name]->clean($value); + } + } + } + } + + /** + * Execute the query for selecting a collection of objects, eventually + * along with related objects + * + * @param array $params an array of criterions for the selection + */ + public function queryExecute($params) + { + $this->objects = $this->dispatcher->filter( + new sfEvent( + $this, + 'sfDoctrineRestGenerator.filter_results', + array() + ), + $this->query($params)->execute(array(), Doctrine_Core::HYDRATE_ARRAY) + )->getReturnValue(); + } + + /** + * Execute the query for selecting an object, eventually along with related + * objects + * + * @param array $params an array of criterions for the selection + */ + public function queryFetchOne($params) + { + $this->objects = array($this->dispatcher->filter( + new sfEvent( + $this, + 'sfDoctrineRestGenerator.filter_result', + array() + ), + $this->query($params)->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY) + )->getReturnValue()); + } + + protected function updateObjectFromRequest($content) + { + $this->object->importFrom('array', $this->parsePayload($content)); + } + + /** + * Applies a set of validators to an array of parameters + * + * @param array $params An array of parameters + * @param array $validators An array of validators + * @throw sfException + */ + public function validate($params, $validators, $prefix = '') + { + $unused = array_keys($validators); + + foreach ($params as $name => $value) + { + if (!isset($validators[$name])) + { + throw new sfValidatorError(new sfValidatorPass(), sprintf('Could not validate extra field "%s"', $prefix.$name)); + } + else + { + if (is_array($validators[$name])) + { + // validator for a related object + $this->validate($value, $validators[$name], $prefix.$name.'.'); + } + else + { + $validators[$name]->clean($value); + } + + unset($unused[array_search($name, $unused, true)]); + } + } + + // are non given values required? + foreach ($unused as $name) + { + try + { + if (!is_array($validators[$name])) + { + $validators[$name]->clean(null); + } + } + catch (sfValidatorError $e) + { + throw new sfValidatorError($e->getValidator(), sprintf('Could not validate field "%s": %s', $prefix.$name, $e->getMessage())); + } + } + } + + /** + * Applies the creation validators to the payload posted to the service + * + * @param string $payload A payload string + */ + public function validateCreate($payload) + { + $params = $this->parsePayload($payload); + + $validators = $this->getCreateValidators(); + $this->validate($params, $validators); + + $postvalidators = $this->getCreatePostValidators(); + $this->postValidate($params, $postvalidators); + } + + /** + * Applies the get validators to the constraint parameters passed to the + * webservice + * + * @param array $params An array of criterions used for the selection + */ + public function validateIndex($params) + { + $validators = $this->getIndexValidators(); + $this->validate($params, $validators); + + $postvalidators = $this->getIndexPostValidators(); + $this->postValidate($params, $postvalidators); + } + + /** + * Applies the get validators to the constraint parameters passed to the + * webservice + * + * @param array $params An array of criterions used for the selection + */ + public function validateShow($params) + { + $validators = $this->getIndexValidators(); + $this->validate($params, $validators); + + $postvalidators = $this->getIndexPostValidators(); + $this->postValidate($params, $postvalidators); + } + + /** + * Applies the update validators to the payload posted to the service + * + * @param string $payload A payload string + */ + public function validateUpdate($payload) + { + $params = $this->parsePayload($payload); + + $validators = $this->getUpdateValidators(); + $this->validate($params, $validators); + + $postvalidators = $this->getUpdatePostValidators(); + $this->postValidate($params, $postvalidators); + } +} diff --git a/lib/generator/sfDoctrineRestGenerator.class.php b/lib/generator/sfDoctrineRestGenerator.class.php index f6c6fc2..4ae6512 100644 --- a/lib/generator/sfDoctrineRestGenerator.class.php +++ b/lib/generator/sfDoctrineRestGenerator.class.php @@ -114,7 +114,7 @@ public function generate($params = array()) */ public function getActionsBaseClass() { - return isset($this->params['actions_base_class']) ? $this->params['actions_base_class'] : 'sfActions'; + return isset($this->params['actions_base_class']) ? $this->params['actions_base_class'] : 'sfDoctrineRestGeneratorActions'; } /**