PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/request.class.php

https://github.com/rspieker/konsolidate_breed
PHP | 169 lines | 94 code | 17 blank | 58 comment | 13 complexity | ec35eb8c88b17964326b41f15f0fa31b MD5 | raw file
  1. <?php
  2. /**
  3. * Unified access to request variables
  4. * @name BreedRequest
  5. * @type class
  6. * @package Breed
  7. * @author Rogier Spieker <rogier@konfirm.net>
  8. */
  9. class BreedRequest extends Konsolidate
  10. {
  11. public function __construct(Konsolidate $parent)
  12. {
  13. parent::__construct($parent);
  14. $this->_collect();
  15. }
  16. /**
  17. * get a property value from a module using a path (including the GET/POST/PUT/etc type modules)
  18. * @name get
  19. * @type method
  20. * @access public
  21. * @param string path to the property to get
  22. * @param mixed default return value (optional, default null)
  23. * @return mixed
  24. */
  25. public function get()
  26. {
  27. $arg = func_get_args();
  28. $key = array_shift($arg);
  29. $default = (bool) count($arg) ? array_shift($arg) : null;
  30. $seperator = strrpos($key, $this->_objectSeparator);
  31. if ($seperator !== false && ($module = $this->getModule(substr($key, 0, $seperator))) !== false)
  32. {
  33. return $module->get(substr($key, $seperator + 1), $default);
  34. }
  35. else if ($this->{$_SERVER['REQUEST_METHOD']} && isset($this->{$_SERVER['REQUEST_METHOD']}->{$key}))
  36. {
  37. return $this->{$_SERVER['REQUEST_METHOD']}->{$key};
  38. }
  39. else if ($this->checkModuleAvailability($key))
  40. {
  41. return $this->register($key);
  42. }
  43. $return = $this->$key;
  44. return is_null($return) ? $default : $return; // can (and will be by default!) still be null
  45. }
  46. /**
  47. * Create a sub module of the current one, adding the supported request types as transparent instances of type
  48. * @name instance
  49. * @type method
  50. * @access public
  51. * @param string modulename
  52. * @param mixed param N
  53. * @return object
  54. */
  55. public function instance($module)
  56. {
  57. switch ($module)
  58. {
  59. case 'GET':
  60. case 'POST':
  61. case 'PUT':
  62. case 'DELETE':
  63. case 'PURGE':
  64. if (!array_key_exists($module, $this->_property))
  65. $this->_property[$module] = parent::instance('Type', $module);
  66. return $this->_property[$module];
  67. break;
  68. }
  69. $arg = func_get_args();
  70. return call_user_func_array(Array('parent', 'instance'), $arg);
  71. }
  72. /**
  73. * is the request method PUT
  74. * @name isPut
  75. * @type method
  76. * @access public
  77. * @returns bool
  78. * @syntax bool SiteRequest->isPut()
  79. */
  80. public function isPut()
  81. {
  82. return isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'PUT';
  83. }
  84. /**
  85. * is the request method DELETE
  86. * @name isDelete
  87. * @type method
  88. * @access public
  89. * @returns bool
  90. * @syntax bool SiteRequest->isDelete()
  91. */
  92. public function isDelete()
  93. {
  94. return isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'DELETE';
  95. }
  96. /**
  97. * is the request method PURGE
  98. * @name isPurge
  99. * @type method
  100. * @access public
  101. * @returns bool
  102. * @syntax bool SiteRequest->isPurge()
  103. */
  104. public function isPurge()
  105. {
  106. return isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'PURGE';
  107. }
  108. /**
  109. * Create <Tier>RequestType instances for all appropiate request type and overwrite the superglobal $_REQUEST
  110. * @name _collect
  111. * @type method
  112. * @access protected
  113. * @return void
  114. */
  115. protected function _collect()
  116. {
  117. $method = $_SERVER['REQUEST_METHOD'];
  118. switch ($method)
  119. {
  120. case 'POST':
  121. case 'PUT':
  122. case 'DELETE':
  123. $this->{$method} = parent::instance('Type', $method);
  124. // no break, all of these requests may also have GET variables
  125. case 'GET':
  126. case 'PURGE':
  127. $this->GET = parent::instance('Type', 'GET');
  128. break;
  129. default:
  130. $this->call('/Log/message', 'Request-type ' . $method . ' not supported', 3);
  131. break;
  132. }
  133. $GLOBALS['_REQUEST'] = $this;
  134. }
  135. /* ArrayAccess implementation */
  136. public function offsetGet($offset)
  137. {
  138. return $this->{$offset};
  139. }
  140. public function offsetSet($offset, $value)
  141. {
  142. return $this->{$offset} = $value;
  143. }
  144. public function offsetExists($offset)
  145. {
  146. return isset($this->{$offset});
  147. }
  148. public function offsetUnset($offset)
  149. {
  150. unset($this->{$offset});
  151. }
  152. }