AsynchronousSchemaRequestProcessor.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Controller\Rest;
  8. use Magento\Webapi\Model\Rest\Swagger\Generator;
  9. use Magento\Framework\Webapi\Rest\Response as RestResponse;
  10. use Magento\Framework\Webapi\Request;
  11. use Magento\Webapi\Controller\Rest\RequestProcessorInterface;
  12. class AsynchronousSchemaRequestProcessor implements RequestProcessorInterface
  13. {
  14. /**
  15. * Path for accessing Async Rest API schema
  16. */
  17. const PROCESSOR_PATH = 'async/schema';
  18. const BULK_PROCESSOR_PATH = 'async/bulk/schema';
  19. /**
  20. * @var \Magento\Webapi\Model\Rest\Swagger\Generator
  21. */
  22. private $swaggerGenerator;
  23. /**
  24. * @var \Magento\Framework\Webapi\Rest\Response
  25. */
  26. private $response;
  27. /**
  28. * @var string
  29. */
  30. private $processorPath;
  31. /**
  32. * Initial dependencies
  33. *
  34. * @param \Magento\Webapi\Model\Rest\Swagger\Generator $swaggerGenerator
  35. * @param \Magento\Framework\Webapi\Rest\Response $response
  36. * @param string $processorPath
  37. */
  38. public function __construct(
  39. Generator $swaggerGenerator,
  40. RestResponse $response,
  41. $processorPath = self::PROCESSOR_PATH
  42. ) {
  43. $this->swaggerGenerator = $swaggerGenerator;
  44. $this->response = $response;
  45. $this->processorPath = $processorPath;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function process(\Magento\Framework\Webapi\Rest\Request $request)
  51. {
  52. $requestedServices = $request->getRequestedServices('all');
  53. $requestedServices = $requestedServices == Request::ALL_SERVICES
  54. ? $this->swaggerGenerator->getListOfServices()
  55. : $requestedServices;
  56. $responseBody = $this->swaggerGenerator->generate(
  57. $requestedServices,
  58. $request->getScheme(),
  59. $request->getHttpHost(),
  60. $request->getRequestUri()
  61. );
  62. $this->response->setBody($responseBody)->setHeader('Content-Type', 'application/json');
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function canProcess(\Magento\Framework\Webapi\Rest\Request $request)
  68. {
  69. if (strpos(ltrim($request->getPathInfo(), '/'), $this->processorPath) === 0) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * @param \Magento\Framework\Webapi\Rest\Request $request
  76. * @return bool
  77. */
  78. public function isBulk(\Magento\Framework\Webapi\Rest\Request $request)
  79. {
  80. if (strpos(ltrim($request->getPathInfo(), '/'), self::BULK_PROCESSOR_PATH) === 0) {
  81. return true;
  82. }
  83. return false;
  84. }
  85. }