make JSON default format for API

This commit is contained in:
Kacper Donat 2020-11-23 00:13:50 +01:00
parent fb6cc37dd8
commit 106aa2149e
15 changed files with 42 additions and 244 deletions

View File

@ -1,5 +1,5 @@
web_profiler:
toolbar: true
toolbar: false
intercept_redirects: false
framework:

View File

@ -1,3 +1,4 @@
framework:
router:
strict_requirements: ~

View File

@ -0,0 +1,3 @@
_errors:
resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
prefix: /_error

View File

@ -1,3 +0,0 @@
_errors:
resource: '@TwigBundle/Resources/config/routing/errors.xml'
prefix: /_error

View File

@ -42,13 +42,6 @@ services:
resource: '../src/Handler'
tags: [ app.handler ]
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
#assets
assets.modified_time_version_strategy:
class: App\Asset\ModifiedTimeVersionStrategy
#eerialziser
jms_serializer.serialized_name_annotation_strategy:
class: JMS\Serializer\Naming\SerializedNameAnnotationStrategy

View File

@ -1,8 +1,8 @@
<?php
use App\Kernel;
use Symfony\Component\Debug\Debug;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;
require __DIR__.'/../vendor/autoload.php';

View File

@ -1,32 +0,0 @@
<?php
namespace App\Asset;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
class ModifiedTimeVersionStrategy implements VersionStrategyInterface
{
/**
* Returns the asset version for an asset.
*
* @param string $path A path
*
* @return string The version string
*/
public function getVersion($path)
{
return filemtime($path);
}
/**
* Applies version to the supplied path.
*
* @param string $path A path
*
* @return string The versionized path
*/
public function applyVersion($path)
{
return sprintf('%s?v=%s', $path, $this->getVersion($path));
}
}

View File

@ -3,8 +3,10 @@
namespace App\Controller\Api\v1;
use App\Controller\Controller;
use App\Exception\NonExistentServiceException;
use App\Service\Converter;
use App\Service\ProviderResolver;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use function Kadet\Functional\ref;
class ProviderController extends Controller
@ -22,8 +24,11 @@ class ProviderController extends Controller
public function one(ProviderResolver $resolver, Converter $converter, $id)
{
$provider = $resolver->resolve($id);
return $this->json($converter->convert($provider));
try {
$provider = $resolver->resolve($id);
return $this->json($converter->convert($provider));
} catch (NonExistentServiceException $exception) {
throw new NotFoundHttpException($exception->getMessage());
}
}
}

View File

@ -1,48 +0,0 @@
<?php
namespace App\Controller;
use App\Provider\Provider;
use App\Service\ProviderResolver;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends Controller
{
/**
* @Route("/", name="choose")
*/
public function choose(ProviderResolver $resolver)
{
return $this->render('choose.html.twig', ['providers' => $resolver->all()]);
}
/**
* @Route("/{provider}/manifest.json", name="provider_manifest")
* @Route("/manifest.json", name="main_manifest")
*/
public function manifest(?Provider $provider = null)
{
$response = $this->render('manifest.json.twig', ['provider' => $provider]);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* @Route("/{provider}", name="app")
*/
public function app(Provider $provider, Request $request)
{
$state = json_decode($request->query->get('state', '{}'), true) ?: [];
$state = array_merge(
[
'version' => 1,
'stops' => [],
],
$state
);
return $this->render('app.html.twig', compact('state', 'provider'));
}
}

View File

@ -1,18 +0,0 @@
<?php
namespace App\Service;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class VersionExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('version', function () {
return substr(`git rev-parse HEAD`, 0, 8) ?: '0.2';
})
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class JSONFormatSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => "onRequest",
];
}
public function onRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->has('_format')) {
$request->attributes->set('_format', 'json');
}
}
}

View File

@ -1,19 +0,0 @@
{% extends 'base.html.twig' %}
{% block title "#{parent()} - #{provider.name}" %}
{% block manifest path('provider_manifest', { provider: provider.identifier }) %}
{% block body %}
<main id="app" class="container not-ready">
</main>
{% endblock %}
{% block javascripts %}
<script>
window.data = {
provider: {{ provider.identifier|json_encode|raw }}
};
window.app = {};
window.app.state = {{ state|json_encode|raw }};
</script>
{% endblock %}

View File

@ -1,68 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="dist/main.css" />
<link rel="manifest" href="{% block manifest 'manifest.json' %}" />
<!-- icons -->
<link rel="icon" href="images/favicon.png" sizes="16x16" />
<link rel="icon" href="images/favicon-2x.png" sizes="32x32" />
<link rel="icon" href="images/favicon.ico" />
<!-- Apple shit -->
<link rel="apple-touch-icon" href="images/ios.png" sizes="512x512">
<link rel="apple-touch-icon" href="images/ios-80.png" sizes="80x80">
<link rel="apple-touch-icon" href="images/ios-192.png" sizes="192x192">
<meta name="apple-mobile-web-app-title" content="Co Jedzie?">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="theme-color" content="white"/>
<title>{% block title %}Co Jedzie?{% endblock %}</title>
{% if gtm_tracking %}
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','{{ gtm_tracking }}');</script>
<!-- End Google Tag Manager -->
{% endif %}
</head>
<body>
<noscript>
{% if gtm_tracking %}
<!-- Google Tag Manager (noscript) -->
<iframe src="https://www.googletagmanager.com/ns.html?id={{ gtm_tracking }}"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
<!-- End Google Tag Manager (noscript) -->
{% endif %}
<div class="container">
<div class="alert alert-danger">
Aplikacja wymaga do działania obsługi JavaScriptu.
</div>
</div>
</noscript>
{% block body '' %}
<footer class="container">
{% block footer %}
<span>
<img src="images/logo.png" alt="co jedzie logo"/>
v. {{ version() }} &bullet;
<a href="{{ url('app.swagger_ui') }}">API</a>
</span>
<span class="copyright flex flex-space-left justify-content-end">
<a href="https://kadet.net"><img src="images/kadet-net-logo.png" alt="kadet.net logo" class="mx-1"/></a>
&copy; {{ 'now'|date('Y') }}
</span>
{% endblock %}
</footer>
{% block javascripts %}{% endblock %}
<script src="dist/main.js"></script>
</body>
</html>

View File

@ -1,5 +0,0 @@
{% extends 'base.html.twig' %}
{% block body %}
<main class="d-flex" id="provider-picker"></main>
{% endblock %}

View File

@ -1,39 +0,0 @@
{
"name": "Co Jedzie?{% if provider %} - {{ provider.shortName }}{% endif %}",
"short_name": "Co Jedzie?{% if provider %} - {{ provider.shortName }}{% endif %}",
"orientation": "portrait",
"lang": "pl-PL",
"start_url": "{{ provider ? path('app', { provider: provider.identifier }) : path('choose') }}",
"display": "standalone",
"background_color": "white",
"theme_color": "white",
"description": "Odpowiedź na odwieczne pytanie ludzkości - czy tramwaje jeżdżą?",
"categories": ["navigation", "transport", "travel", "utilities"],
"icons": [{
"src": "{{ asset('images/icon-256.png') }}",
"sizes": "256x256"
},{
"src": "{{ asset('images/icon-512.png') }}",
"sizes": "512x512"
},{
"src": "{{ asset('images/icon-64.png') }}",
"sizes": "64x64"
},{
"src": "{{ asset('images/icon-128.png') }}",
"sizes": "128x128"
},{
"src": "{{ asset('images/icon-192.png') }}",
"sizes": "192x192"
},{
"src": "{{ asset('images/icon-96.png') }}",
"sizes": "96x96"
},{
"src": "{{ asset('images/icon-maskable.png') }}",
"sizes": "512x512",
"purpose": "any maskable"
},{
"src": "{{ asset('images/icon-monochrome.png') }}",
"sizes": "512x512",
"purpose": "monochrome"
}]
}