Commit 11beadb6 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Ajout d'une extension Twig pour aider au formatage.

parent 490184bd
......@@ -28,7 +28,7 @@
</service>
<service id="irstea_ng_model_generator.metadata.resource_class_hierarchy"
class="Irstea\NgModelGeneratorBundle\Metadata\ResourceClassHierarchy" >
class="Irstea\NgModelGeneratorBundle\Metadata\ResourceClassHierarchy">
<argument id="irstea_ng_model_generator.resource_name_collection" type="service"/>
</service>
......@@ -74,6 +74,10 @@
<factory service="api_platform.metadata.resource.name_collection_factory" method="create"/>
</service>
<service class="Irstea\NgModelGeneratorBundle\Twig\GeneratorExtension" id="irstea_ng_model_generator.twig.generator_extension">
<tag name="twig.extension"/>
</service>
</services>
</container>
......@@ -44,8 +44,8 @@ export type Resource = {% for repo in repositories -%}
*/
export interface Metatypes {
{% for repo in repositories %}
{{ repo.resourceName }}: {
identifier: { key: '{{ repo.identifier.name }}', type: {{ repo.identifier.type.usage }} };
{{ repo.resourceName | objectKey }}: {
identifier: { key: {{ repo.identifier.name | quoteString }}, type: {{ repo.identifier.type.usage }} };
resource: {{ repo.resourceName }};
repository: typeof {{ repo.usage }};
};
......@@ -233,12 +233,12 @@ export class ResourceMetadata<T extends ResourceType, R extends Resource = Resou
*/
{% for repo in repositories %}
export const {{ repo.resourceName }}Metadata = new ResourceMetadata<'{{ repo.resourceName }}'>(
'{{ repo.resourceName }}',
'{{ repo.identifier.name }}',
export const {{ repo.resourceName }}Metadata = new ResourceMetadata<{{ repo.resourceName | quoteString }}>(
{{ repo.resourceName | quoteString }},
{{ repo.identifier.name | quoteString }},
[ {% for type in repo.atTypes -%}{{ type.usage }},{% endfor %} ],
{%- if repo.iri.parameters is empty -%}
new StaticIRIMetadata('@id', '{{ repo.iri.pattern }}'),
new StaticIRIMetadata('@id', {{ repo.iri.pattern | quoteString }}),
{% else %}
new ParametrizedIRIMetadata(
'@id',
......@@ -259,7 +259,7 @@ export const {{ repo.resourceName }}Metadata = new ResourceMetadata<'{{ repo.res
{%- if repo.resource.properties is defined -%}
{%- for name, property in repo.resource.properties %}
['{{ property.name }}', {{ (not property.nullable) | json_encode }}, {{ property.readonly | json_encode }}],
[{{ property.name | quoteString }}, {{ (not property.nullable) | json_encode }}, {{ property.readonly | json_encode }}],
{%- endfor -%}
{%- endif %}
......@@ -272,7 +272,7 @@ export const {{ repo.resourceName }}Metadata = new ResourceMetadata<'{{ repo.res
*/
const METADATA: { [T in ResourceType]: ResourceMetadata<T> } = {
{% for repo in repositories %}
{{ repo.resourceName }}: {{ repo.resourceName }}Metadata,
{{ repo.resourceName | objectKey }}: {{ repo.resourceName }}Metadata,
{% endfor %}
};
......
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 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\NgModelGeneratorBundle\Twig;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class GeneratorExtension.
*/
class GeneratorExtension extends \Twig_Extension
{
/** @noinspection PhpMissingParentCallCommonInspection */
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
new \Twig_Filter('quoteString', [TypescriptHelper::class, 'quoteString'], ['is_safe' => true]),
new \Twig_Filter('objectKey', [TypescriptHelper::class, 'objectLiteralKey'], ['is_safe' => true]),
];
}
/** @noinspection PhpMissingParentCallCommonInspection */
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
new \Twig_Function('propertyHaser', [TypescriptHelper::class, 'propertyTestor'], ['is_safe' => true]),
new \Twig_Function(
'propertyAccessor',
[TypescriptHelper::class, 'propertyAccessor'],
['is_safe' => true]
),
];
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment