-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.php
145 lines (131 loc) · 4.13 KB
/
Controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Bread PHP Framework (http://github.com/saiv/Bread)
* Copyright 2010-2012, SAIV Development Team <[email protected]>
*
* Licensed under a Creative Commons Attribution 3.0 Unported License.
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010-2012, SAIV Development Team <[email protected]>
* @link http://github.com/saiv/Bread Bread PHP Framework
* @package Bread
* @since Bread PHP Framework
* @license http://creativecommons.org/licenses/by/3.0/
*/
namespace Bread;
use Bread\Configuration;
use Bread\Networking\HTTP\Request;
use Bread\Networking\HTTP\Response;
use Bread\Networking\HTTP\Client\Exceptions;
/**
* Application controller class for organization of business logic.
*/
abstract class Controller {
/**
* The HTTP request to control
*
* @var Request $request
*/
protected $request;
/**
* The HTTP response to generate
*
* @var Response $response
*/
protected $response;
protected $data;
public function __construct(Request $request, Response $response) {
$this->request = $request;
$this->response = $response;
switch ($this->request->method) {
case 'OPTIONS':
break;
case 'HEAD':
$this->response->on('headers', array($this->response, 'end'));
case 'GET':
break;
case 'POST':
case 'PUT':
$this->data = new Promise\Deferred();
$this->request->on('data', function ($data) use ($buffer) {
$this->data->progress($data);
})->on('end', function () {
$data = stream_get_contents($this->request->body->stream, $this->request->length, 0);
switch ($this->request->type) {
case 'application/json':
$json = json_decode($data);
$this->data->resolve($json);
break;
case 'application/x-www-form-urlencoded':
parse_str($data, $form);
$this->data->resolve($form);
break;
}
});
break;
case 'DELETE':
break;
case 'TRACE':
case 'CONNECT':
default:
throw new Exceptions\MethodNotAllowed($this->request->method);
}
}
public function browse() {
$search = (array) $this->request->query['search'];
$options = (array) $this->request->query['options'];
return Model::fetch($search, $options)->then(function ($models) {
$view = new View($this->request, $this->response);
return $this->response->end($view->table($models));
});
}
public function read($id) {
return Model::first(['id' => $id])->then(function ($model) {
$view = new View($this->request, $this->response);
return $this->response->end($view->read($model));
});
}
public function edit($id) {
return Model::first(['id' => $id])->then(function ($model) {
$view = new View($this->request, $this->response);
return $this->response->end($view->form($model));
});
}
public function add() {
switch ($this->request->method) {
case 'POST':
return $this->data->then(function ($attributes) {
$view = new View($this->request, $this->response);
$model = new Model($attributes);
$model->store();
$this->response->status(201);
return $this->response->end($view->form());
});
case 'GET':
$view = new View($this->request, $this->response);
return $this->response->end($view->form());
}
}
public function delete($id) {
return Model::first(['id' => $id])->then(function ($model) {
return $model->delete()->then(function () {
$this->response->message("Post successfully deleted.");
return $this->response->end();
});
});
}
public static function get($key = null) {
$class = get_called_class();
return Configuration\Manager::get($class, $key);
}
public static function Model($attributes) {
$class = get_called_class();
$model = str_replace('Controller', 'Model', $class);
return new $model($attributes);
}
public static function View(Request $request, Response $response) {
$class = get_called_class();
$view = str_replace('Controller', 'View', $class);
return new $view($request, $response);
}
}