Introduce FrontController as the replacement for RootController.
[hoplite.git] / data / controller.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\data;
18 use \hoplite\http as http;
19
20 require_once HOPLITE_ROOT . '/http/response_code.php';
21 require_once HOPLITE_ROOT . '/http/rest_action.php';
22
23 /*!
24 A Controller is a RESTful http\Action that is used to bind a data\Model to a
25 web interface. Subclass this in order to perform business logic such as
26 validation and authentication.
27
28 This class is semi-abstract in that it cannot be used directly. At minimum,
29 _SelectModel() needs to be overridden to select the Model to use.
30 */
31 abstract class Controller extends http\RestAction
32 {
33 /*! @var hoplite\data\Model The object that will be operated on. */
34 protected $model = NULL;
35
36 /*! Selects the Model object. */
37 public function Invoke(http\Request $request, http\Response $response)
38 {
39 $this->model = $this->_SelectModel();
40 $this->model->SetFrom(array_merge(
41 $request->data, $request->data['_POST'], $request->data['_GET']));
42
43 parent::Invoke($request, $response);
44 }
45
46 /*! Returns a new instance of the Model that this object will control. */
47 abstract protected function _SelectModel();
48
49 /*! Gets the data from the model. */
50 public function DoGet(http\Request $request, http\Response $response)
51 {
52 try {
53 $response->data = $this->model->Fetch();
54 } catch (ModelException $e) {
55 $response->body = $e->GetMessage();
56 $response->response_code = http\ResponseCode::NOT_FOUND;
57 } catch (\PDOException $e) {
58 $response->body = $e->GetMessage();
59 $response->response_code = http\ResponseCode::INTERNAL_SERVER_ERROR;
60 }
61 }
62
63 /*! Updates an object in the store. */
64 public function DoPost(http\Request $request, http\Response $response)
65 {
66 try {
67 $this->model->Update();
68 $response->data = $this->model->Fetch();
69 } catch (ModelException $e) {
70 $response->body = $e->GetMessage();
71 $response->response_code = http\ResponseCode::NOT_FOUND;
72 } catch (\PDOException $e) {
73 $response->body = $e->GetMessage();
74 $response->response_code = http\ResponseCode::INTERNAL_SERVER_ERROR;
75 }
76 }
77
78 /*! Deletes the object from the store. */
79 public function DoDelete(http\Request $request, http\Response $response)
80 {
81 try {
82 $this->model->Delete();
83 } catch (ModelException $e) {
84 $response->body = $e->GetMessage();
85 $response->response_code = http\ResponseCode::BAD_REQUEST;
86 } catch (\PDOException $e) {
87 $response->body = $e->GetMessage();
88 $response->response_code = http\ResponseCode::INTERNAL_SERVER_ERROR;
89 }
90 }
91
92 /*! Updates an object in the store. */
93 public function DoPut(http\Request $request, http\Response $response)
94 {
95 try {
96 $this->model->Insert();
97 $response->data = $this->model->Fetch();
98 } catch (ModelException $e) {
99 $response->body = $e->GetMessage();
100 $response->response_code = http\ResponseCode::BAD_REQUEST;
101 } catch (\PDOException $e) {
102 $response->body = $e->GetMessage();
103 $response->response_code = http\ResponseCode::INTERNAL_SERVER_ERROR;
104 }
105 }
106 }
107
108 class ControllerException extends \Exception {}