ARestfullController.php 719 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace Tools;
  3. /**
  4. * Abstract Controller
  5. **/
  6. abstract class RestfullController extends AController
  7. {
  8. private $encode_fnc;
  9. public function __construct($context, $params, $encode_fnc = null)
  10. {
  11. if (!\Entity\Config::getConfig(null, "restfullEnabled", false))
  12. throw new \Exception\Error404();
  13. parent::__construct($context, $params);
  14. if ($encode_fnc === null)
  15. $encode_fnc = array("method" => json_encode, "mime" => "application/json");
  16. $this->encode_fnc = $encode_fnc;
  17. }
  18. public function start()
  19. {
  20. //TODO call some hooks
  21. header("Content-Type: {$this->encode_fnc["mime"]}");
  22. echo $this->encode_fnc["method"]($this->run());
  23. //TODO call some hooks
  24. }
  25. abstract public function run();
  26. }