uxf / gen
Requires
- php: ^8.4
- phpdocumentor/type-resolver: ^2.0
- phpstan/phpdoc-parser: ^2.0
- symfony/security-http: ^7.4 || ^8.0
Requires (Dev)
None
Suggests
- uxf/core: Required for bridge
Provides
None
Conflicts
- doctrine/dbal: <4.5
- doctrine/orm: <3.7
Replaces
None
- 3.x-dev
- 3.89.0
- 3.88.0
- 3.86.0
- 3.85.1
- 3.84.0
- 3.83.1
- 3.83.0
- 3.81.6
- 3.80.0
- 3.79.3
- 3.79.2
- 3.79.1
- 3.79.0
- 3.78.5
- 3.78.2
- 3.78.0
- 3.77.9
- 3.77.3
- 3.76.3
- 3.76.1
- 3.76.0
- 3.75.4
- 3.75.0
- 3.73.1
- 3.72.3
- 3.72.0
- 3.70.0
- 3.69.12
- 3.69.1
- 3.67.3
- 3.66.0
- 3.65.0
- 3.62.0
- 3.61.5
- 3.61.3
- 3.59.0
- 3.58.6
- 3.58.5
- 3.58.3
- 3.58.2
- 3.58.0
- 3.57.12
- 3.57.11
- 3.57.0
- 3.56.7
- 3.56.5
- 3.56.3
- 3.56.1
- 3.56.0
- 3.55.1
- 3.55.0
- 3.54.5
- 3.54.4
- 3.53.8
- 3.53.6
- 3.53.5
- 3.53.4
- 3.53.0
- 3.51.1
- 3.51.0
- 3.50.0
- 3.47.1
- 3.43.0
- 3.42.0
- 3.41.1
- 3.41.0
- 3.40.4
- 3.40.3
- 3.38.0
- 3.37.0
- 3.36.3
- 3.36.2
- 3.36.1
- 3.35.4
- 3.35.3
- 3.34.3
- 3.34.2
- 3.34.1
- 3.34.0
- 3.32.3
- 3.30.1
- 3.29.0
- 3.24.6
- 3.24.1
- 3.23.2
- 3.21.5
- 3.21.3
- 3.21.0
- 3.20.4
- 3.20.3
- 3.20.2
- 3.20.1
- 3.20.0
- 3.17.0
- 3.15.2
- 3.15.0
- 3.14.0
- 3.13.2
- 3.11.0
- 3.10.0
- 3.9.1
- 3.9.0
- 3.7.2
- 3.7.0
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.0
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.1
- 1.3.0
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.1
- v0.1.0
- dev-legacy
- dev-main
- dev-master
- dev-readonly
- dev-openapi-description
- dev-openapi-enums
This package is auto-updated.
Last update: 2026-11-15 20:25:33 UTC
README
Install
$ composer req uxf/gen
Run
$ bin/console uxf:gen
Config
// config/routes/uxf.php
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
return static function (RoutingConfigurator $routingConfigurator): void {
$routingConfigurator->import('@UXFGenBundle/config/routes.php');
};
// config/packages/uxf.php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->extension('uxf_gen', [
'config' => [
'prefix' => '/wow', // default '/api'
// default config -> values from UXF bridge
'disable_uxf_bridge' => false,
'route_body_attribute' => FromBody::class,
'route_query_attribute' => FromQuery::class,
'route_header_attribute' => FromHeader::class,
'route_entity_attribute' => Entity::class,
'route_form_attribute' => FromForm::class,
'ignored_types' => [NotSet::class],
'enum_as_union' => true, // default false
'typescript_types' => [
'DateTime' => '`${number}-${number}-${number}T${number}:${number}:${number}+${number}:${number}`',
'Date' => '`${number}-${number}-${number}`',
'Time' => '`${number}:${number}:${number}`',
],
],
'open_api' => [
'areas' => [
'app' => [
'path_pattern' => '/^\/api\/article/',
],
],
],
'hook' => [
'areas' => [
'admin' => [
'path_pattern' => '/^\/api\/article/',
'output' => 'swr', // default legacy (options: swr, swr-2.2, legacy, empty, keys, fetch)
'with_enum_options' => true, // default false
'destination' => '%kernel.project_dir%/tests/generated/hook.ts', // or array
'prepends' => [
'/* eslint-disable */',
'import { useAxiosRequest, axiosRequest, RequestConfig } from "@lib/api";'
],
],
],
],
'apollo' => [
'areas' => [
'app' => [
'path_pattern' => '/^\/api\/article/',
'destination' => '%kernel.project_dir%/tests/generated/apollo.ts', // or array
],
],
],
]);
};
Output swr
const { data } = useArticleAllQuery(
{ query, skip: !isOpen },
{
fallbackData: [], // data: FunArticleResponse[] (without fallbackData / suspense: FunArticleResponse[] | undefined)
onSuccess: (response) => {}, // response: FunArticleResponse[], no annotation needed
onError: (error) => {}, // error: ApiError
},
);
skip: truedisables the request (SWRnullkey).- A custom
keychanges only the cache key, the request is always built fromconfig(the fetcher ignores the key, so a SWR middleware changing the key does not change the request). E.g.key: [...pickArticleAllKey(config), offerIds]loads again whenofferIdschange. - Mutation hooks take any
key: it is the cache key revalidated after the mutation, e.g.useArticleUpdateMutation({ path, key: pickArticleAllKey({ query }) })reloads the article list. - Keep the query (e.g. datagrid filters) in the key, otherwise changing it does not load new data. To reload a list after a mutation for any query, revalidate all its keys with a matcher (SWR 2 passes the original key to it):
const { mutate } = useSWRConfig();
const listUrl = pickArticleAllKey({ query })[0];
const { trigger } = useArticleUpdateMutation(
{ path },
{ onSuccess: () => mutate((key) => Array.isArray(key) && key[0] === listUrl) },
);
Output fetch
Typed fetch functions without SWR (e.g. for a project using Apollo which sometimes calls REST).
It is the swr output without hooks and SWR imports, so it also contains pick*Key functions from the keys output.
// GET /api/article
export const pickArticleAllKey = (config: { query: FunArticleRequestQuery }) => [`/api/article`, config.query] as const;
export const getArticleAll = (config: { query: FunArticleRequestQuery }): Promise<Array<FunArticleResponse>> => _fetch(_url(pickArticleAllKey(config)), 'GET', undefined);
// POST /api/article
export const pickArticleCreateKey = () => [`/api/article`] as const;
export const articleCreate = (config: { body: FunArticleRequestBody }): Promise<FunArticleResponse> => _fetch(pickArticleCreateKey()[0], 'POST', config.body);
Errors are thrown as ApiError. The default implementation calls fetch(_baseUrl + url), where the exported _baseUrl
is process.env.API_URL, read once when the module is loaded (a bundler replacement via define works too). Without
process (e.g. Vite in the browser without define) or without API_URL it is "", so the relative URL (/api/...)
on the same origin is called. _baseUrl can also be used for absolute URLs, e.g.
_baseUrl + _url(pickExportKey(config)).
To replace the default implementation (e.g. to add an auth header), provide _customFetch(url, method, body) with the
same signature as _defaultFetch, preferably through prepends (same as in the swr output):
'prepends' => ['import { myFetch as _customFetch } from "@lib/api";'],
The generated file declares declare global { var _customFetch: any; }, so a global _customFetch works too and more
generated files (e.g. admin swr + public fetch) can live in one TypeScript project. Your own global declaration of
_customFetch must also be var _customFetch: any, another type fails with TS2403.
Example
InspectorPlugin
use UXF\Gen\Inspector\Schema\AppSchema;
use UXF\Gen\Inspector\Schema\RouteSchema;
use UXF\Gen\Inspector\Schema\TypeSchema;
use UXF\Gen\Inspector\Schema\TypeVariant;
use UXF\Gen\Inspector\TypeMap;
use UXF\Gen\Plugin\InspectorPluginInterface;
use UXF\GenTests\Project\FunZone\Entity\EntityNames;
class TestPlugin implements InspectorPluginInterface
{
public function pre(string $configName, TypeMap $typeMap): void
{
if ($configName === 'hook') {
$typeMap->add(new TypeSchema(EntityNames::class, TypeVariant::ENUM, EntityNames::class, onlyWhenUsed: false));
}
}
public function post(string $configName, AppSchema $appSchema): void
{
if ($configName === 'apollo') {
$appSchema->routes['plugin'] = new RouteSchema(
name: 'plugin',
path: '/plugin',
controller: 'FunZone\Ok',
requestBody: null,
requestQuery: null,
pathParams: [],
response: null,
description: '',
methods: ['get' => false],
);
}
}
}
Types used by routes only
Only types used by a route (body, query, headers, path params, response) and everything they reference are generated.
This is decided by TypeSchema::$onlyWhenUsed:
onlyWhenUsed: true(default): the type is generated only when a route uses it. This applies to types found by the inspector, built-in scalars,UXF\Core\Type\MoneyandUXF\Core\Type\Currency.onlyWhenUsed: false: the type is always generated, even when no route uses it, together with everything it references. Use it for types you add manually inInspectorPlugin::pre(), likeEntityNamesabove.
To always generate a built-in type, change it in your plugin (it runs after the built-in one):
public function pre(string $configName, TypeMap $typeMap): void
{
$typeMap->getStrict(Currency::class)->onlyWhenUsed = false;
}
The keys output follows the same rule, it just starts from the query and path params of routes only.
ConverterPlugin
use UXF\Core\Type\Date;
use UXF\Core\Type\DateTime;
use UXF\Core\Type\Time;
use UXF\Gen\Inspector\Schema\TypeSchema;
use UXF\Gen\Inspector\Schema\TypeVariant;
use UXF\Gen\Plugin\TypeConverterPlugin;
class UxfCoreTypeConverterPlugin implements TypeConverterPlugin
{
public function convertToTypescript(TypeSchema $typeSchema): ?string
{
if (
$typeSchema->variant === TypeVariant::SIMPLE &&
in_array($typeSchema->name, [Date::class, DateTime::class, Time::class], true)
) {
return 'string';
}
return null;
}
/**
* @inheritDoc
*/
public function convertToOpenApi(TypeSchema $typeSchema): ?array
{
if ($typeSchema->variant !== TypeVariant::SIMPLE) {
return null;
}
return match ($typeSchema->name) {
Date::class => [
'type' => 'string',
'format' => 'date',
],
DateTime::class => [
'type' => 'string',
'format' => 'date-time',
],
Time::class => [
'type' => 'string',
'pattern' => '^\d{2}:\d{2}:\d{2}$',
],
default => null,
};
}
}
Description support
class TestController
{
/**
* PRIVATE INTERNAL DESCRIPTION (MUST BE BEFORE @description)
*
* @description Public hello world description
* with multiline support bro!
*/
public function __invoke(): StatusResponse
{
return new StatusResponse();
}
}