1<?php 2 3/* 4 * vim:set softtabstop=4 shiftwidth=4 expandtab: 5 * 6 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later) 7 * Copyright 2001 - 2020 Ampache.org 8 * 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU Affero General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Affero General Public License for more details. 18 * 19 * You should have received a copy of the GNU Affero General Public License 20 * along with this program. If not, see <https://www.gnu.org/licenses/>. 21 * 22 */ 23 24declare(strict_types=1); 25 26namespace Ampache\Module\Api; 27 28use Ampache\Config\ConfigContainerInterface; 29use Ampache\Module\Api\Output\ApiOutputFactoryInterface; 30use Narrowspark\HttpEmitter\AbstractSapiEmitter; 31use Nyholm\Psr7Server\ServerRequestCreatorInterface; 32use Psr\Http\Message\ResponseFactoryInterface; 33 34final class JsonApiApplication implements ApiApplicationInterface 35{ 36 private ApiOutputFactoryInterface $apiOutputFactory; 37 38 private ApiHandlerInterface $apiHandler; 39 40 private ConfigContainerInterface $configContainer; 41 42 private ResponseFactoryInterface $responseFactory; 43 44 private AbstractSapiEmitter $sapiEmitter; 45 46 private ServerRequestCreatorInterface $serverRequestCreator; 47 48 public function __construct( 49 ApiOutputFactoryInterface $apiOutputFactory, 50 ApiHandlerInterface $apiHandler, 51 ConfigContainerInterface $configContainer, 52 ResponseFactoryInterface $responseFactory, 53 AbstractSapiEmitter $sapiEmitter, 54 ServerRequestCreatorInterface $serverRequestCreator 55 ) { 56 $this->apiOutputFactory = $apiOutputFactory; 57 $this->apiHandler = $apiHandler; 58 $this->configContainer = $configContainer; 59 $this->responseFactory = $responseFactory; 60 $this->sapiEmitter = $sapiEmitter; 61 $this->serverRequestCreator = $serverRequestCreator; 62 } 63 64 public function run(): void 65 { 66 $response = $this->responseFactory->createResponse(); 67 68 // @todo add headers to response after all api methods have been modernized 69 /* Set the correct headers */ 70 header(sprintf('Content-type: application/json; charset=%s', $this->configContainer->get('site_charset'))); 71 72 $request = $this->serverRequestCreator->fromGlobals(); 73 $request = $request->withQueryParams( 74 array_merge( 75 ['api_format' => 'json'], 76 $request->getQueryParams() 77 ) 78 ); 79 80 $response = $this->apiHandler->handle( 81 $request, 82 $response, 83 $this->apiOutputFactory->createJsonOutput() 84 ); 85 86 // @todo remove condition after all api methods have been modernized 87 if ($response !== null) { 88 $this->sapiEmitter->emit($response); 89 } 90 } 91} 92