AbstractController.php 3.20 KiB
<?php declare(strict_types=1);
/*
 * This file is part of "irstea/api-metadata".
 * Copyright (C) 2019 IRSTEA
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License and the GNU
 * Lesser General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
namespace Irstea\ApiMetadata\Bridge\Symfony\Bundle\Controller;
use Irstea\ApiMetadata\Exception\ResourceNotFoundException;
use Irstea\ApiMetadata\Factory\CachedContext;
use Irstea\ApiMetadata\Factory\Context;
use Irstea\ApiMetadata\Factory\ContextInterface;
use Irstea\ApiMetadata\Factory\Type\TypeFactoryInterface;
use Irstea\ApiMetadata\Service\ResourceMapInterface;
use JsonSerializable;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
/**
 * Class AbstractController.
abstract class AbstractController
    /**
     * @var ResourceMapInterface
    protected $resourceMap;
    /**
     * @var TypeFactoryInterface
    protected $typeFactory;
    /** @var SerializerInterface */
    protected $serializer;
    /**
     * ResourceListAction constructor.
     * @param ResourceMapInterface $resourceMap
     * @param TypeFactoryInterface $typeFactory
     * @param SerializerInterface  $serializer
    public function __construct(
        ResourceMapInterface $resourceMap,
        TypeFactoryInterface $typeFactory,
        SerializerInterface $serializer
    ) {
        $this->resourceMap = $resourceMap;
        $this->typeFactory = $typeFactory;
        $this->serializer = $serializer;
    /**
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108
* @param string $shortName * * @return array<string, Context> */ protected function setupContext(string $shortName): array { $className = $this->resourceMap->getClassName($shortName); if ($className === null) { throw new ResourceNotFoundException($shortName); } $baseContext = new Context($this->typeFactory); return [$className, CachedContext::create($baseContext)]; } /** * @param array|JsonSerializable $data * @param ContextInterface|null $context * * @return Response */ protected function createResponse($data, ContextInterface $context = null): Response { $json = $this->serializer->serialize($data, 'json', []); // // if ($context) { // $json = array_merge($context->jsonSerialize(), $json); // } // // if (isset($json['type'])) { // $json = array_merge(['$schema' => 'http://json-schema.org/schema#'], $json); // } return JsonResponse::create($json); } }