Skip to content

Add configurable namespaces for generated classes#1794

Open
Amoifr wants to merge 1 commit intosymfony:1.xfrom
Amoifr:feature/per-type-namespace-config
Open

Add configurable namespaces for generated classes#1794
Amoifr wants to merge 1 commit intosymfony:1.xfrom
Amoifr:feature/per-type-namespace-config

Conversation

@Amoifr
Copy link
Copy Markdown

@Amoifr Amoifr commented Apr 10, 2026

Q A
Bug fix? no
New feature? yes
BC breaks? no
Issues #1344
License MIT

This PR is a proof of concept to support the discussion in #1344 — it is not intended to be merged as-is, but to serve as a concrete base for the ongoing reflection.

What it does

Adds an optional namespaces configuration to override the default namespace for generated classes:

maker:
    namespaces:
        entity: Domain\Entity
        controller: Application\Controller
        repository: Infrastructure\Repository

The directory is derived automatically from PSR-4 autoloading rules (no dir option needed, as @GromNaN suggested).

Design choices

  • NamespaceType enum — replaces magic strings with a typed enum for all 5 configurable types (controller, command, entity, form, repository). Provides IDE autocompletion and prevents typos.
  • Defaults centralized in the enum — each case knows its default via defaultNamespace(). The Generator merges user config on top of these defaults.
  • Open map via scalarPrototype — the config accepts any key, not a fixed list. Users can override only what they need.
  • Validation — each namespace value is validated via Validator::validateClassName().
  • 10 makers updated — all makers using the 5 configurable types now go through $generator->getNamespace(NamespaceType::Entity) instead of hardcoded 'Entity\\'.
  • No constructor injection neededGenerator is already available in generate(). The MakeController logic was moved from interact() to generate() to avoid adding a constructor dependency.

What's NOT in scope (possible follow-ups)

  • CLI option (--namespace) for one-off overrides (mentioned by @GromNaN)
  • Additional types beyond the initial 5 (listener, message, voter, etc.)

Allow overriding the default namespace for controllers, commands,
entities, forms and repositories via the maker configuration:

    maker:
        namespaces:
            entity: Domain\Entity
            controller: Application\Controller

Defaults are centralized in a NamespaceType enum, and the directory
is derived automatically from PSR-4 autoloading rules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant