Advertisement
Guest User

Untitled

a guest
Oct 10th, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. localhost/user/viewall
  2.  
  3. $controller = new UserController();
  4. $controller->viewall();
  5.  
  6. public function setData($key, $value = null)
  7. {
  8. if (!is_string($key)) {
  9. throw new ExceptionsTypeException("The "key" parameter must be a string");
  10. }
  11.  
  12. $this->data[$key] = $value;
  13. }
  14.  
  15. public function setFile($file)
  16. {
  17. if (!is_string($file)) {
  18. throw new ExceptionsTypeException("The "file" parameter must be a string");
  19. }
  20.  
  21. $this->file = $file;
  22. }
  23.  
  24. public function render()
  25. {
  26. if (empty($this->file)) {
  27. return "";
  28. }
  29.  
  30. if (!empty($this->data)) {
  31. extract($this->data);
  32. }
  33.  
  34. // dirty-hack: use output buffering so we can easily check if a file can be included
  35. // if not, simply reject the output and throw an exception (let the caller handle it)
  36. // if the inclusion was successfull then return with the buffered content
  37. ob_start();
  38. if (!include($this->file)) {
  39. ob_end_clean();
  40. throw new ExceptionsFatalException("View file "{$this->file}" not found");
  41. }
  42.  
  43. return ob_get_clean();
  44. }
  45.  
  46. public function __call($name, $args)
  47. {
  48. // ...
  49.  
  50. $getMatches = StringHelper::match($name, "^get([a-zA-Z0-9]+)$");
  51. if (!empty($getMatches)) {
  52. $property = lcfirst($getMatches[0][1]);
  53.  
  54. if (!property_exists($this, $property)) {
  55. throw new ExceptionsFatalException("Cannot execute {$name}: property {$property} not found");
  56. }
  57.  
  58. $metadata = $this->inspector->getPropertyMeta($property);
  59. if (!isset($metadata["@readonly"]) && !isset($metadata["@readwrite"])) {
  60. throw new ExceptionsFatalException("Cannot execute getter on write-only property: {$property}");
  61. }
  62.  
  63. return $this->$property;
  64. }
  65.  
  66. // ...
  67. }
  68.  
  69. // input url: localhost/user/view/12
  70.  
  71. // =========================
  72. // index.php
  73. // =========================
  74.  
  75. // create services
  76. $services = new FrameworkServiceContainer();
  77. {
  78. // TEST SERVICES
  79. //$services->add("user", new UserServiceMock());
  80.  
  81. // REAL SERVICES
  82. $services->add("user", new UserServiceDB($db));
  83. }
  84.  
  85. // create routes
  86. $router = new FrameworkRouter($services);
  87. {
  88. $router->addRoute("", "HomeController", "home");
  89. }
  90.  
  91. // try to find a controller/action pair for this url and render the specified view
  92. try {
  93. $router->dispatchAndRender(new PageNotFoundView());
  94. } catch (Exception $ex) {
  95. die($ex->getMessage());
  96. }
  97.  
  98. // =========================
  99. // User.php
  100. // =========================
  101.  
  102. class User extends FrameworkModel
  103. {
  104. /**
  105. * @readwrite
  106. * @column
  107. * @primary
  108. * @type int
  109. */
  110. protected $id;
  111. /**
  112. * @readwrite
  113. * @column
  114. * @type text
  115. * @length 20
  116. */
  117. protected $name;
  118.  
  119. // ...
  120. }
  121.  
  122. // =========================
  123. // UserService.php
  124. // =========================
  125.  
  126. abstract class UserService
  127. {
  128. public abstract function findAll();
  129.  
  130. public abstract function findById($id);
  131.  
  132. // ...
  133. }
  134.  
  135. // =========================
  136. // UserController.php
  137. // =========================
  138.  
  139. class UserController extends FrameworkController
  140. {
  141. private $service;
  142. private $view;
  143.  
  144. public function __construct($services)
  145. {
  146. $this->service = $services->get("user");
  147.  
  148. $this->view = new UserView();
  149. $this->setView($this->view);
  150. }
  151.  
  152. public function view($id)
  153. {
  154. $user = null;
  155.  
  156. if (is_numeric($id)) {
  157. $user = $this->service->findById($id);
  158. }
  159.  
  160. $this->view->viewUser($user);
  161. }
  162. }
  163.  
  164. // =========================
  165. // MainLayoutView.php
  166. // =========================
  167.  
  168. class MainLayoutView extends FrameworkView
  169. {
  170. public function __construct()
  171. {
  172. $this->setFile("templates/MainLayout.php");
  173. }
  174.  
  175. public function assignContent($contentView)
  176. {
  177. $this->setData("content", $contentView->render());
  178. }
  179. }
  180.  
  181. // =========================
  182. // UserView.php
  183. // =========================
  184.  
  185. class UserView extends MainLayoutView
  186. {
  187. public function viewUser($user)
  188. {
  189. $contentView = new FrameworkView();
  190. $contentView->setFile("templates/User_View.php");
  191.  
  192. if ($user) {
  193. $userData = array();
  194.  
  195. $userData["id"] = $user->getId();
  196. $userData["name"] = $user->getName();
  197. // ... any other visible data
  198.  
  199. $contentView->setData("user", $userData);
  200. }
  201.  
  202. $this->assignContent($contentView);
  203. }
  204. }
  205.  
  206. // =========================
  207. // templates/User_View.php
  208. // =========================
  209.  
  210. if (isset($user)) {
  211. echo "id: {$user['id']}<br />";
  212. echo "name: {$user['name']}<br />";
  213. } else {
  214. echo "<b>User not found</b>";
  215. }
  216.  
  217. public function register()
  218. {
  219. $formSent = $this->view->readRegisterForm($username, $password);
  220.  
  221. if ($formSent) {
  222. $user = new User();
  223.  
  224. // validate input (using the User instance?)
  225. // TODO
  226. $valid = false;
  227.  
  228. if ($valid) {
  229. // hash password
  230. $password = password_hash($password, PASSWORD_DEFAULT);
  231.  
  232. $user->setName($username);
  233. $user->setPassword($password);
  234. $this->service->add($user);
  235.  
  236. $this->view->viewRegisterComplete($user);
  237.  
  238. return;
  239. }
  240. }
  241.  
  242. $this->view->viewRegisterForm();
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement