ResourceController.php 2.42 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\Context;
use Irstea\ApiMetadata\Helper\PropertyInfoType;
use Irstea\ApiMetadata\Model\Reference;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
 * Class ResourceController.
class ResourceController extends AbstractController
    /**
     * @param string $shortName
     * @return Response
    public function item(Request $request, string $shortName): Response
        $className = $this->resourceMap->getClassName($shortName);
        if ($className === null) {
            throw new ResourceNotFoundException($shortName);
        $ctx = new Context($this->typeFactory);
        $type = PropertyInfoType::create($className);
        $metadata = $ctx->createType($type, $ctx);
        if ($metadata instanceof Reference) {
            $metadata = $metadata->getTarget();
        $json = $this->serializer->serialize($metadata, 'json', ['root' => $metadata]);
        return JsonResponse::fromJsonString($json);
    /**
     * @param Request $request
     * @return Response
    public function collection(Request $request): Response
        $resources = [];
        foreach ($this->resourceMap as $shortName) {
71727374757677787980
$resources[$shortName] = ['$ref' => $shortName]; } return JsonResponse::create([ '$id' => $request->getUri(), 'resources' => $resources, ]); } }