VirtualRESTService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Virtual HTTP service client
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Virtual HTTP service instance that can be mounted on to a VirtualRESTService
  24. *
  25. * Sub-classes manage the logic of either:
  26. * - a) Munging virtual HTTP request arrays to have qualified URLs and auth headers
  27. * - b) Emulating the execution of virtual HTTP requests (e.g. brokering)
  28. *
  29. * Authentication information can be cached in instances of the class for performance.
  30. * Such information should also be cached locally on the server and auth requests should
  31. * have reasonable timeouts.
  32. *
  33. * @since 1.23
  34. */
  35. abstract class VirtualRESTService {
  36. /** @var array Key/value map */
  37. protected $params = [];
  38. /**
  39. * @param array $params Key/value map
  40. */
  41. public function __construct( array $params ) {
  42. $this->params = $params;
  43. }
  44. /**
  45. * Return the name of this service, in a form suitable for error
  46. * reporting or debugging.
  47. *
  48. * @return string The name of the service behind this VRS object.
  49. */
  50. public function getName() {
  51. return $this->params['name'] ?? static::class;
  52. }
  53. /**
  54. * Prepare virtual HTTP(S) requests (for this service) for execution
  55. *
  56. * This method should mangle any of the $reqs entry fields as needed:
  57. * - url : munge the URL to have an absolute URL with a protocol
  58. * and encode path components as needed by the backend [required]
  59. * - query : include any authentication signatures/parameters [as needed]
  60. * - headers : include any authentication tokens/headers [as needed]
  61. *
  62. * The incoming URL parameter will be relative to the service mount point.
  63. *
  64. * This method can also remove some of the requests as well as add new ones
  65. * (using $idGenerator to set each of the entries' array keys). For any existing
  66. * or added request, the 'response' array can be filled in, which will prevent the
  67. * client from executing it. If an original request is removed, at some point it
  68. * must be added back (with the same key) in onRequests() or onResponses();
  69. * it's reponse may be filled in as with other requests.
  70. *
  71. * @param array $reqs Map of Virtual HTTP request arrays
  72. * @param Closure $idGeneratorFunc Method to generate unique keys for new requests
  73. * @return array Modified HTTP request array map
  74. */
  75. public function onRequests( array $reqs, Closure $idGeneratorFunc ) {
  76. $result = [];
  77. foreach ( $reqs as $key => $req ) {
  78. // The default encoding treats the URL as a REST style path that uses
  79. // forward slash as a hierarchical delimiter (and never otherwise).
  80. // Subclasses can override this, and should be documented in any case.
  81. $parts = array_map( 'rawurlencode', explode( '/', $req['url'] ) );
  82. $req['url'] = $this->params['baseUrl'] . '/' . implode( '/', $parts );
  83. $result[$key] = $req;
  84. }
  85. return $result;
  86. }
  87. /**
  88. * Mangle or replace virtual HTTP(S) requests which have been responded to
  89. *
  90. * This method may mangle any of the $reqs entry 'response' fields as needed:
  91. * - code : perform any code normalization [as needed]
  92. * - reason : perform any reason normalization [as needed]
  93. * - headers : perform any header normalization [as needed]
  94. *
  95. * This method can also remove some of the requests as well as add new ones
  96. * (using $idGenerator to set each of the entries' array keys). For any existing
  97. * or added request, the 'response' array can be filled in, which will prevent the
  98. * client from executing it. If an original request is removed, at some point it
  99. * must be added back (with the same key) in onRequests() or onResponses();
  100. * it's reponse may be filled in as with other requests. All requests added to $reqs
  101. * will be passed through onRequests() to handle any munging required as normal.
  102. *
  103. * The incoming URL parameter will be relative to the service mount point.
  104. *
  105. * @param array $reqs Map of Virtual HTTP request arrays with 'response' set
  106. * @param Closure $idGeneratorFunc Method to generate unique keys for new requests
  107. * @return array Modified HTTP request array map
  108. */
  109. public function onResponses( array $reqs, Closure $idGeneratorFunc ) {
  110. return $reqs;
  111. }
  112. }