agentleFS
gerard-labsgerard-labs/superpowers-api-platform/skills/api-platform-mutators/SKILL.md

api-platform-mutators

Skill2 starsChanged 6 months agoMITView on GitHub

Use API Platform 4.3's new `#[AsResourceMutator(resourceClass: …)]` and `#[AsOperationMutator(operationName: …)]` to mutate resource/operation metadata at **build time** — zero runtime cost. Implement `ResourceMutatorInterface::__invoke(ApiResource $resource): ApiResource` to e.g. prefix every operation's `routePrefix`, or `OperationMutatorInterface::__invoke(Operation $operation): Operation` to e.g. inject an extra serialization group. Includes the decision matrix between Mutator (build-time, cached, applies always) and Context Builder (runtime, per-request, role-/user-/header-dependent — see `api-platform-serialization`). Trigger on "apply the same change to every operation of a resource", "global API prefix injected from code", or "build-time metadata modification".

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Operation and ApiResource are immutable: Always use withOperations(), withNormalizationContext(), withRoutePrefix(), etc.

    line 28

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

API Platform 4.3 — Resource & Operation Mutators (NEW 4.3)

Use when

  • You want to apply a uniform metadata change to a resource (prefix routes, inject a group) without touching every operation by hand.
  • The change is decidable at build time (no runtime dependency on the request).
  • You're tempted to write a Context Builder for something that does not depend on the request — Mutator is cheaper.

Default workflow

  1. Decide: build-time (Mutator) or runtime (Context Builder)?
  2. For a resource-wide change, implement ResourceMutatorInterface and annotate with #[AsResourceMutator(resourceClass: Book::class)].
  3. For a single-operation change, implement OperationMutatorInterface and annotate with #[AsOperationMutator(operationName: '_api_Book_get_collection')].
  4. Return the modified ApiResource / Operation (these objects are immutable — use with*() mutators).

Guardrails

  • Mutator = build time, no access to the request / user / locale. If you need any of those, use a Context Builder.
  • Operation and ApiResource are immutable. Always use withOperations(), withNormalizationContext(), withRoutePrefix(), etc.
  • Operation names are stable (_api_<ShortName>_<operation> by convention). Pin them in a constant if you reference them in multiple mutators.

Progressive disclosure

  • SKILL.md covers posture and decision tree.
  • reference.md carries the full AsResourceMutator and AsOperationMutator recipes + the Context Builder vs Mutator comparison table.

Output contract

  • Mutators in src/ApiPlatform/Mutator/ with explicit #[AsResourceMutator] / #[AsOperationMutator] annotations.
  • Build-time metadata changes verified via php bin/console debug:router | grep api and php bin/console api:openapi:export --yaml.
  • A short comment in each mutator explaining what it changes and why.

References

  • reference.md

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.