A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.2 KiB

  1. <?php
  2. namespace SparkLib\Application\Environment;
  3. use \SparkLib\Application\Environment;
  4. use \SparkLib\Application;
  5. use \Exception;
  6. /**
  7. * An application Environment for HTTP requests. Mangles standard
  8. * PHP request variables and such into objects that can be used
  9. * cleanly by the Application and Controller classes.
  10. *
  11. * Once upon a time, Application and Request between them handled
  12. * what little this class does. The advantage of decoupling
  13. * our Request object from things like PHP superglobals is that we
  14. * can then adopt the same application to other execution
  15. * models without worrying about the specifics of a PHP SAPI or
  16. * what-have-you. By mapping other forms of input onto the same
  17. * interfaces we've used to build a web application, we can more
  18. * easily reuse utility functions and expose a code surface for
  19. * unit testing.
  20. *
  21. * Besides the command-line client that prompted this, we might
  22. * consider possiblities like text-to-speech, an e-mail gateway,
  23. * or APIs as thin translation layers to the mainline application.
  24. *
  25. * -- bpb 2013/December/9
  26. */
  27. class HTTP extends Environment {
  28. public function __construct ()
  29. {
  30. if ($_GET && $_POST)
  31. throw new Exception("can't mingle GET variables with a POST if instantiating an Application");
  32. // See also: http://php.net/manual/en/reserved.variables.server.php
  33. $this->_path = $_SERVER['PATH_INFO'];
  34. switch ($this->method()) {
  35. case 'GET' : $this->_req = new \SparkLib\Application\Request\Get($_GET); break;
  36. case 'POST' : $this->_req = new \SparkLib\Application\Request\Post($_POST); break;
  37. case 'DELETE' : $this->_req = new \SparkLib\Application\Request\Delete($_REQUEST); break;
  38. case 'HEAD' : $this->_req = new \SparkLib\Application\Request\Head($_GET); break;
  39. default : $this->_req = new \SparkLib\Application\Request\Get(array()); // fake it for other methods
  40. }
  41. $this->discernType();
  42. }
  43. /**
  44. * Initialize and populate sessions.
  45. */
  46. public function startSession ()
  47. {
  48. if (! isset($_SESSION))
  49. session_start();
  50. }
  51. /**
  52. * End the current session.
  53. */
  54. public function endSession ()
  55. {
  56. $_SESSION = array();
  57. session_destroy();
  58. }
  59. public function method ()
  60. {
  61. return $_SERVER['REQUEST_METHOD'];
  62. }
  63. public function header ($header)
  64. {
  65. header($header);
  66. }
  67. /**
  68. * Grab the first thing in the Accepts: header, check it against
  69. * a whitelist, and (if it passes) set the expected MIME type of request
  70. * to it. This may later be overwritten by a type extension found
  71. * on the URL by Application's routing mechanism.
  72. *
  73. * For now this is defaulting to text/html if we get an unrecognized
  74. * type. More appropriate behavior MIGHT be to throw an exception,
  75. * but on the other hand it could be that we're going to get a lot of
  76. * weird input that we don't want to break on.
  77. *
  78. * Much to be investigated here.
  79. */
  80. protected function discernType ()
  81. {
  82. $accept = $_SERVER['HTTP_ACCEPT'];
  83. $types = explode(',', $accept);
  84. list($first_type) = explode(';', $types[0]);
  85. $first_type = trim($first_type);
  86. if (Application::$mimeToExtension[$first_type]) {
  87. $this->_req->setType($first_type);
  88. }
  89. }
  90. }