From 312f4db6bb3d5bd20ebe2ae08a8496af9277d526 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Thu, 30 Jan 2014 17:09:40 +0100 Subject: [PATCH 01/17] convert stray tab character to spaces --- .../sfDoctrineRestGenerator/default/parts/showAction.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php index 935934f..036651b 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -22,7 +22,7 @@ public function executeShow(sfWebRequest $request) } catch (Exception $e) { - $this->getResponse()->setStatusCode(406); + $this->getResponse()->setStatusCode(406); $serializer = $this->getSerializer(); $this->getResponse()->setContentType($serializer->getContentType()); $error = $e->getMessage(); From 901862b721e4c1ada88cdfe88a469b2ff37a29be Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Thu, 30 Jan 2014 17:12:53 +0100 Subject: [PATCH 02/17] bring executeShow and executeIndex closer to each other --- .../sfDoctrineRestGenerator/default/parts/indexAction.php | 1 + 1 file changed, 1 insertion(+) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php index 4997beb..3fb447f 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 From defc61fc3d1741e2c5f481ec4729203c4b8f8bb4 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 09:47:32 +0100 Subject: [PATCH 03/17] Refactor indexAction and showAction to be more alike --- .../default/parts/indexAction.php | 10 ++++------ .../default/parts/showAction.php | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php index 3fb447f..da813e7 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php @@ -63,18 +63,16 @@ public function executeIndex(sfWebRequest $request) isManyToManyRelation($embed_relation)): ?> $this->embedManyToMany($params); -configuration->getValue('get.object_additional_fields'); ?> - 0): ?> +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'); ?> - - + +configuration->getValue('get.global_additional_fields') as $field): ?> $this->embedGlobalAdditional($params); diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php index 036651b..d70f13a 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -56,6 +56,7 @@ public function executeShow(sfWebRequest $request) $this->embedGlobalAdditional($params); + // configure the fields of the returned objects and eventually hide some $this->setFieldVisibility(); $this->configureFields(); From 85a20194983d3cdde706b37d90592d3c3e0b0fd7 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 10:04:03 +0100 Subject: [PATCH 04/17] Add base class sfDoctrineRestGeneratorActions (extends sfActions) --- .../default/template/actions/actions.class.php | 2 -- .../actions/sfDoctrineRestGeneratorActions.class.php | 7 ++++++- lib/generator/sfDoctrineRestGenerator.class.php | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) rename data/generator/sfDoctrineRestGenerator/default/parts/validate.php => lib/actions/sfDoctrineRestGeneratorActions.class.php (94%) diff --git a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 674e04c..1f82058 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -65,8 +65,6 @@ class getGeneratedModuleName() ?>Actions extends - - diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/validate.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php similarity index 94% rename from data/generator/sfDoctrineRestGenerator/default/parts/validate.php rename to lib/actions/sfDoctrineRestGeneratorActions.class.php index 0c92b20..7b05828 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/validate.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -1,3 +1,7 @@ +getMessage())); } } - } \ No newline at end of file + } +} 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'; } /** From e68c964f89a09662498f22a908a5b89b2d28bf40 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 10:07:33 +0100 Subject: [PATCH 05/17] factor validateCreate to sfDoctrineRestGeneratorActions --- .../default/parts/validateCreate.php | 15 --------------- .../default/template/actions/actions.class.php | 2 -- .../sfDoctrineRestGeneratorActions.class.php | 16 ++++++++++++++++ 3 files changed, 16 insertions(+), 17 deletions(-) delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/validateCreate.php 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/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 1f82058..b86bebd 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -65,8 +65,6 @@ class getGeneratedModuleName() ?>Actions extends - - diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index 7b05828..29992c8 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -51,4 +51,20 @@ public function validate($params, $validators, $prefix = '') } } } + + /** + * 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); + } } From 752a7a37b6f1b666cbeb9999e1116d2f5ae7fc78 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 10:10:56 +0100 Subject: [PATCH 06/17] Factor out common functions (with no generated code) factor out validateIndex factor out validateShow factor out validateUpdate factor out updateObjectFromRequest factor out queryFetchOne factor out postValidate factor out update(Post)Validators factor out doSave factor out CreateObject factor out createAction --- .../default/parts/createAction.php | 60 ------ .../default/parts/createObject.php | 4 - .../default/parts/doSave.php | 6 - .../default/parts/getUpdatePostValidators.php | 8 - .../default/parts/getUpdateValidators.php | 8 - .../default/parts/postValidate.php | 25 --- .../default/parts/queryFetchOne.php | 17 -- .../default/parts/updateObjectFromRequest.php | 4 - .../default/parts/validateIndex.php | 14 -- .../default/parts/validateShow.php | 14 -- .../default/parts/validateUpdate.php | 15 -- .../template/actions/actions.class.php | 22 --- .../sfDoctrineRestGeneratorActions.class.php | 186 ++++++++++++++++++ 13 files changed, 186 insertions(+), 197 deletions(-) delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/createAction.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/createObject.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/doSave.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/getUpdatePostValidators.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/getUpdateValidators.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/postValidate.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/queryFetchOne.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/updateObjectFromRequest.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/validateIndex.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/validateShow.php delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/validateUpdate.php 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/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/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/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/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/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 b86bebd..76ac4a7 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -18,14 +18,8 @@ class getGeneratedModuleName() ?>Actions extends - - - - - - @@ -38,36 +32,20 @@ class getGeneratedModuleName() ?>Actions extends - - - - - - - - - - - - - - - - } diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index 29992c8..8dc5347 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -2,6 +2,146 @@ class sfDoctrineRestGeneratorActions extends sfActions { + /** + * 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; + } + + protected function createObject() + { + return new $this->model(); + } + + protected function doSave() + { + $this->object->save(); + + return sfView::NONE; + } + + /** + * 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(); + } + + /** + * 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 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 * @@ -67,4 +207,50 @@ public function validateCreate($payload) $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); + } } From e9eb1eb0f2081473af6c812f4f44ec0d2fea11fd Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 11:12:30 +0100 Subject: [PATCH 07/17] factor out additional_params variable --- .../default/parts/cleanupParameters.php | 4 +--- .../default/parts/getIndexValidators.php | 11 +++++------ .../default/template/actions/actions.class.php | 1 + 3 files changed, 7 insertions(+), 9 deletions(-) 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/getIndexValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php index 8763637..a4bf133 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php @@ -28,12 +28,11 @@ public function getIndexValidators() $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)); - - + + foreach ($this->additional_params as $param) + { + $validators[$param] = new sfValidatorPass(array('required' => false)); + } return $validators; } diff --git a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 76ac4a7..97e7481 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -13,6 +13,7 @@ class getGeneratedModuleName() ?>Actions extends getModelClass() ?>'; + protected $additional_params = configuration->getValue('get.additional_params', array())); ?>; From 4e249e33dc9d86207c18d7e04cbee7c4ecf78a37 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 11:31:48 +0100 Subject: [PATCH 08/17] factor out Pagination validators --- .../default/parts/getIndexValidators.php | 18 +++------------- .../default/parts/getPaginationValidators.php | 21 +++++++++++++++++++ .../template/actions/actions.class.php | 7 +++++++ .../sfDoctrineRestGeneratorActions.class.php | 5 +++++ 4 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/getPaginationValidators.php diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php index a4bf133..2e9d08a 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php @@ -8,21 +8,9 @@ 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); - - + + $validators = array_merge($validators, $this->getPaginationValidators()); + configuration->getValue('get.sort_custom'); ?> $validators['sort_by'] = new sfValidatorChoice(array('choices' => table->getColumnNames()) ?>, 'required' => false)); 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/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 97e7481..b547bf0 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -31,6 +31,13 @@ class getGeneratedModuleName() ?>Actions extends +configuration->getValue('get.pagination_enabled')) +{ + include dirname(__FILE__).'/../../parts/getPaginationValidators.php'; +} +?> + diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index 8dc5347..01028a1 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -75,6 +75,11 @@ protected function doSave() return sfView::NONE; } + protected function getPaginationValidators() + { + return array(); + } + /** * Returns the list of validators for an update request. * @return array an array of validators From 49f1bc172769a8bf7094baa7935817ad5f074be3 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 11:52:53 +0100 Subject: [PATCH 09/17] factor out Sort validators --- .../default/parts/getIndexValidators.php | 6 +----- .../default/parts/getSortValidators.php | 15 +++++++++++++++ .../default/template/actions/actions.class.php | 7 +++++++ .../sfDoctrineRestGeneratorActions.class.php | 5 +++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php index 2e9d08a..215b591 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getIndexValidators.php @@ -11,11 +11,7 @@ public function getIndexValidators() $validators = array_merge($validators, $this->getPaginationValidators()); -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)); - + $validators = array_merge($validators, $this->getSortValidators()); foreach ($this->additional_params as $param) { diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php new file mode 100644 index 0000000..50b9320 --- /dev/null +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php @@ -0,0 +1,15 @@ +/** + * 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, + )); +} diff --git a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index b547bf0..517cd89 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -40,6 +40,13 @@ class getGeneratedModuleName() ?>Actions extends +configuration->getValue('get.sort_custom')) +{ + include dirname(__FILE__).'/../../parts/getSortValidators.php'; +} +?> + diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index 01028a1..a6d26fb 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -80,6 +80,11 @@ 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 From 934e46b9a34105bcaa2f0cb3c558f0432dc91ee3 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 12:22:27 +0100 Subject: [PATCH 10/17] factor out getContent --- .../default/parts/updateAction.php | 8 +---- .../sfDoctrineRestGeneratorActions.class.php | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php index 5c15302..a9622d0 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'); diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index a6d26fb..fc8f7af 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -10,17 +10,7 @@ class sfDoctrineRestGeneratorActions extends sfActions 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! - } + $content = $this->getContent(); $request->setRequestFormat('html'); @@ -75,6 +65,24 @@ protected function doSave() 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(); From dcfcd577dda754f70d7ebf0cec013f033128e4e0 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 12:29:08 +0100 Subject: [PATCH 11/17] Throw sfValidatorExceptions if validations fail --- lib/actions/sfDoctrineRestGeneratorActions.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index fc8f7af..090bc8b 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -175,7 +175,7 @@ public function validate($params, $validators, $prefix = '') { if (!isset($validators[$name])) { - throw new sfException(sprintf('Could not validate extra field "%s"', $prefix.$name)); + throw new sfValidatorError(sprintf('Could not validate extra field "%s"', $prefix.$name)); } else { @@ -203,9 +203,9 @@ public function validate($params, $validators, $prefix = '') $validators[$name]->clean(null); } } - catch (Exception $e) + catch (sfValidatorError $e) { - throw new sfException(sprintf('Could not validate field "%s": %s', $prefix.$name, $e->getMessage())); + throw new sfValidatorError(sprintf('Could not validate field "%s": %s', $prefix.$name, $e->getMessage())); } } } From 3c3d7f5bbde20b5836572b2822f597b3db74ad0e Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 12:38:17 +0100 Subject: [PATCH 12/17] factor out Exception handling --- .../default/parts/indexAction.php | 22 +------- .../default/parts/showAction.php | 22 +------- .../default/parts/updateAction.php | 23 +------- .../sfDoctrineRestGeneratorActions.class.php | 56 +++++++++++-------- 4 files changed, 36 insertions(+), 87 deletions(-) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php index da813e7..926c23f 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php @@ -23,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); diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php index d70f13a..a5f286f 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -23,27 +23,7 @@ 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; + return $this->handleException($e); } $this->queryFetchOne($params); diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php index a9622d0..ae366fa 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/updateAction.php @@ -17,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/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index 090bc8b..f082a99 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -20,29 +20,7 @@ public function executeCreate(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); } $this->object = $this->createObject(); @@ -111,6 +89,38 @@ 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; + } + /** * Applies a set of validators to an array of parameters * From 72475bbf44813f629c0420e2f6eefc96eac11e0e Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 13:31:03 +0100 Subject: [PATCH 13/17] Factor out queryExecute --- .../default/parts/queryExecute.php | 17 ----------------- .../default/template/actions/actions.class.php | 2 -- .../sfDoctrineRestGeneratorActions.class.php | 18 ++++++++++++++++++ 3 files changed, 18 insertions(+), 19 deletions(-) delete mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/queryExecute.php 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/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 517cd89..7ba0d55 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -56,8 +56,6 @@ class getGeneratedModuleName() ?>Actions extends - - diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index f082a99..bf2ba9e 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -147,6 +147,24 @@ public function postValidate($params, $validators, $prefix = '') } } + /** + * 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 From 39b3727a129629855756c7785232b6af81711ebf Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 14:26:02 +0100 Subject: [PATCH 14/17] factor out formatObjects --- .../default/parts/formatObjects.php | 25 +++++++++++++++++++ .../default/parts/indexAction.php | 16 +----------- .../default/parts/showAction.php | 11 +------- .../template/actions/actions.class.php | 2 ++ 4 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 data/generator/sfDoctrineRestGenerator/default/parts/formatObjects.php 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/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php index 926c23f..b37c5ad 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php @@ -43,22 +43,8 @@ public function executeIndex(sfWebRequest $request) isManyToManyRelation($embed_relation)): ?> $this->embedManyToMany($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(); + $this->formatObjects($params); $serializer = $this->getSerializer(); $this->getResponse()->setContentType($serializer->getContentType()); diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php index a5f286f..072ee4f 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -29,16 +29,7 @@ public function executeShow(sfWebRequest $request) $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); - - - // 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()); diff --git a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php index 7ba0d55..83fa704 100644 --- a/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php +++ b/data/generator/sfDoctrineRestGenerator/default/template/actions/actions.class.php @@ -21,6 +21,8 @@ class getGeneratedModuleName() ?>Actions extends + + From de9e3f712ad51f8008a0c9e5948a1245f49ae238 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Fri, 31 Jan 2014 14:46:10 +0100 Subject: [PATCH 15/17] Factor out outputObjects --- .../default/parts/indexAction.php | 4 +--- .../default/parts/showAction.php | 4 +--- .../sfDoctrineRestGeneratorActions.class.php | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php index b37c5ad..be5c9c0 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/indexAction.php @@ -46,8 +46,6 @@ public function executeIndex(sfWebRequest $request) $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/showAction.php b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php index 072ee4f..201e6d2 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/showAction.php @@ -31,8 +31,6 @@ public function executeShow(sfWebRequest $request) $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/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index bf2ba9e..8ebded2 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -121,6 +121,22 @@ public function handleException(Exception $e) 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 * From 6ecf30383238a3e9d906d94d7edf3e5362b85e9c Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Mon, 3 Feb 2014 15:10:19 +0100 Subject: [PATCH 16/17] return validators array for getSortValidators --- .../sfDoctrineRestGenerator/default/parts/getSortValidators.php | 1 + 1 file changed, 1 insertion(+) diff --git a/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php b/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php index 50b9320..3ad5b83 100644 --- a/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php +++ b/data/generator/sfDoctrineRestGenerator/default/parts/getSortValidators.php @@ -12,4 +12,5 @@ protected function getSortValidators() 'choices' => array('asc', 'desc'), 'required' => false, )); + return $validators; } From 926525dc9aa225b056fcbdc1335d58a33d5ff2a9 Mon Sep 17 00:00:00 2001 From: Dirk Nederveen Date: Mon, 3 Feb 2014 16:19:06 +0100 Subject: [PATCH 17/17] construct sfValidatorError objects correctly --- lib/actions/sfDoctrineRestGeneratorActions.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/actions/sfDoctrineRestGeneratorActions.class.php b/lib/actions/sfDoctrineRestGeneratorActions.class.php index 8ebded2..ceeebc3 100644 --- a/lib/actions/sfDoctrineRestGeneratorActions.class.php +++ b/lib/actions/sfDoctrineRestGeneratorActions.class.php @@ -219,7 +219,7 @@ public function validate($params, $validators, $prefix = '') { if (!isset($validators[$name])) { - throw new sfValidatorError(sprintf('Could not validate extra field "%s"', $prefix.$name)); + throw new sfValidatorError(new sfValidatorPass(), sprintf('Could not validate extra field "%s"', $prefix.$name)); } else { @@ -249,7 +249,7 @@ public function validate($params, $validators, $prefix = '') } catch (sfValidatorError $e) { - throw new sfValidatorError(sprintf('Could not validate field "%s": %s', $prefix.$name, $e->getMessage())); + throw new sfValidatorError($e->getValidator(), sprintf('Could not validate field "%s": %s', $prefix.$name, $e->getMessage())); } } }