dryvPacks

@dryv/symfony-backendv0.1.0

Symfony controllers with typed DTOs and separated service boundaries.

Symfony BackendBackendUnitPublished 7 days ago2 downloads

dryv pack add @dryv/symfony-backend
2.5 KBRaw ↗
jinja
<?php

declare(strict_types=1);

namespace {{ pack.inputs.root_namespace }}\Services;

{%- for dependency in file.dependencies %}{% if dependency.symbol %}
use {{ pack.inputs.root_namespace }}\Models\{{ dependency.symbol }};
{%- endif %}{% endfor %}

{%- macro symbol_for(link) -%}
{%- for dependency in file.dependencies -%}
{%- if dependency.target.id == link.id and dependency.symbol -%}{{ dependency.symbol }}{%- endif -%}
{%- endfor -%}
{%- endmacro -%}

{%- macro php_type(value) -%}
{%- if value is none or value.is.unknown or value.is.dynamic or value.is.generic or value.is.composite or value.is.union or value.is.tuple or value.is.literal -%}mixed
{%- elif value.is.schema -%}{{ symbol_for(value.schema) or 'mixed' }}
{%- elif value.is.enum -%}{{ symbol_for(value.property.enum) or 'mixed' }}
{%- elif value.is.boolean -%}bool
{%- elif value.is.integer -%}int
{%- elif value.is.number -%}float
{%- elif value.is.decimal or value.is.money -%}string
{%- elif value.is.record -%}array
{%- elif value.is.string or value.is.temporal -%}string
{%- else -%}mixed
{%- endif -%}
{%- endmacro -%}

{%- macro field_type(field) -%}
{%- if field.array is not none -%}array{%- else -%}{{ php_type(field.value) }}{%- endif -%}
{%- if field.optional or field.nullable -%}
{%- if field.array is not none or php_type(field.value) != 'mixed' -%}|null{%- endif -%}
{%- endif -%}
{%- endmacro -%}

{%- macro method_name(name) -%}
{%- if name in ['class','function','const','public','protected','private','static','abstract','final','readonly','new','clone','if','else','elseif','endif','for','foreach','while','do','switch','match','case','default','break','continue','return','yield','try','catch','finally','throw','namespace','use','trait','interface','enum','extends','implements','instanceof','insteadof','as','fn','echo','print','include','require','global','isset','empty','unset','list','array','callable','iterable','void','never','mixed','true','false','null'] -%}{{ name }}Operation{%- else -%}{{ name }}{%- endif -%}
{%- endmacro -%}

interface {{ file.symbols.service }}
{
{%- for operation in operations %}
    public function {{ method_name(operation.name.camel) }}(
{%- if operation.input %}
{%- if operation.input.kind == 'schema' %}
        {{ php_type(operation.input.value) }} $input
{%- else %}
{%- for field in operation.input.fields %}
        {{ field_type(field) }} \${{ field.name.camel }}{% if not loop.last %},{% endif %}
{%- endfor %}
{%- endif %}
{%- endif %}
    ): {% if operation.output %}{{ php_type(operation.output.value) }}{% else %}void{% endif %};
{%- endfor %}
}