Skip to content

CLI Reference

Complete reference for the DTO Generator command-line interface.

Installation

After installing via Composer, the CLI is available at:

bash
vendor/bin/dto

Commands

generate (default)

Generate PHP DTOs from configuration files.

bash
vendor/bin/dto generate [options]

This is the default command, so vendor/bin/dto is equivalent to vendor/bin/dto generate.

typescript

Generate TypeScript interfaces from configuration files.

bash
vendor/bin/dto typescript [options]

jsonschema

Generate JSON Schema from configuration files.

bash
vendor/bin/dto jsonschema [options]

Common Options

Options available for all commands:

OptionDescription
--config-path=PATHPath to config directory (default: config/)
--format=FORMATConfig format: xml, yaml, neon, php (default: auto-detect)
--verbose, -vVerbose output with detailed information
--quiet, -qMinimal output, only errors
--help, -hShow help message

Generate Options

Options specific to PHP DTO generation:

OptionDescription
--src-path=PATHPath to src directory (default: src/)
--namespace=NSNamespace for generated DTOs (default: App)
--dry-runShow what would be generated without writing files
--forceRegenerate all DTOs, even if unchanged
--confirmValidate PHP syntax of generated files
--mapperGenerate Doctrine-compatible mapper classes

TypeScript Options

Options specific to TypeScript generation:

OptionDescription
--output=PATHPath for TypeScript output (default: types/)
--single-fileGenerate all types in one file (default)
--multi-fileGenerate each type in separate file
--readonlyMake all interface fields readonly
--strict-nullsUse | null instead of ? for nullable fields
--file-case=CASEFile naming: pascal, dashed, snake (default: pascal)

JSON Schema Options

Options specific to JSON Schema generation:

OptionDescription
--output=PATHPath for JSON Schema output (default: schemas/)
--single-fileGenerate all schemas in one file with $defs (default)
--multi-fileGenerate each schema in separate file
--no-refsInline nested DTOs instead of using $ref
--date-format=FMTDate format: date-time, date, string (default: date-time)

Examples

Basic Generation

bash
# Generate with defaults (config/ -> src/Dto/)
vendor/bin/dto generate

# Same as above (generate is default command)
vendor/bin/dto

Custom Paths

bash
# Custom config and output paths
vendor/bin/dto generate --config-path=dto/ --src-path=app/

# Custom namespace
vendor/bin/dto generate --namespace=MyApp\\Dto

# All custom
vendor/bin/dto generate \
  --config-path=definitions/ \
  --src-path=lib/ \
  --namespace=Acme\\Data

Preview and Debug

bash
# See what would be generated without writing
vendor/bin/dto generate --dry-run

# Verbose output for debugging
vendor/bin/dto generate --dry-run --verbose

# Validate generated PHP syntax
vendor/bin/dto generate --confirm

Force Regeneration

bash
# Regenerate all DTOs, ignoring timestamps
vendor/bin/dto generate --force

Doctrine Mapper Generation

bash
# Generate DTOs with Doctrine-compatible mapper classes
vendor/bin/dto generate --mapper

# Creates:
#   src/Dto/UserDto.php           - Standard DTO
#   src/Dto/Mapper/UserDtoMapper.php - Mapper with positional constructor

The mapper classes extend the DTOs with positional constructors for use with Doctrine's SELECT NEW syntax. See Framework Integration for usage details.

TypeScript Generation

bash
# Generate TypeScript interfaces (single file)
vendor/bin/dto typescript

# Custom output directory
vendor/bin/dto typescript --output=frontend/src/types/

# Separate files with dashed naming
vendor/bin/dto typescript --multi-file --file-case=dashed
# Creates: user-dto.ts, order-dto.ts, etc.

# Readonly interfaces with strict nulls
vendor/bin/dto typescript --readonly --strict-nulls

JSON Schema Generation

bash
# Generate JSON Schema (single file with $defs)
vendor/bin/dto jsonschema

# Custom output directory
vendor/bin/dto jsonschema --output=api/schemas/

# Separate files with external $ref
vendor/bin/dto jsonschema --multi-file
# Creates: UserDto.json, OrderDto.json, etc.

# Inline nested DTOs instead of using $ref
vendor/bin/dto jsonschema --no-refs

# Custom date format
vendor/bin/dto jsonschema --date-format=date

CI/CD Integration

bash
# In CI pipeline - fail if DTOs are outdated
vendor/bin/dto generate --dry-run
if [ $? -ne 0 ]; then
  echo "DTOs are not up to date. Run 'vendor/bin/dto generate' locally."
  exit 1
fi

# Or validate syntax after generation
vendor/bin/dto generate --confirm

Configuration File Discovery

The CLI auto-detects configuration files in the following order:

  1. Single file: {config-path}/dto.{ext} where {ext} is xml, yaml, neon, or php
  2. Multiple files: {config-path}/dto/*.{ext}

Format Detection

When --format is not specified:

  1. Checks file extension
  2. For directories, scans for first recognized format
  3. Falls back to XML

Multiple Configuration Files

You can split DTOs across multiple files:

config/
└── dto/
    ├── user.xml
    ├── order.xml
    └── product.xml

All files in the directory are merged during generation.

Exit Codes

CodeMeaning
0Success
1Error (invalid config, generation failure, etc.)

Environment Variables

The CLI respects standard environment variables:

VariableEffect
NO_COLORDisables colored output
TERM=dumbDisables colored output

Scripting

Composer Scripts

Add to composer.json:

json
{
  "scripts": {
    "dto:generate": "dto generate",
    "dto:generate-mapper": "dto generate --mapper",
    "dto:check": "dto generate --dry-run",
    "dto:typescript": "dto typescript --output=frontend/types/",
    "dto:schema": "dto jsonschema --output=api/schemas/"
  }
}

Then run:

bash
composer dto:generate
composer dto:check

Git Hooks

Pre-commit hook to ensure DTOs are current:

bash
#!/bin/sh
# .git/hooks/pre-commit

vendor/bin/dto generate --dry-run --quiet
if [ $? -ne 0 ]; then
  echo "Error: DTO configuration has changed."
  echo "Run 'vendor/bin/dto generate' and commit the generated files."
  exit 1
fi

Makefile

makefile
.PHONY: dto dto-check dto-typescript

dto:
	vendor/bin/dto generate

dto-check:
	vendor/bin/dto generate --dry-run

dto-typescript:
	vendor/bin/dto typescript --output=frontend/src/types/

Released under the MIT License.