api-platform/schema-generator
CLI tool from API Platform that generates PHP class models from vocabularies like Schema.org and ActivityStreams, or from OpenAPI specs. Quickly scaffold types and properties into a ready-to-use PHP codebase for APIs and domain models.
Installation:
composer require api-platform/schema-generator --dev
Or use the PHAR version:
wget https://github.com/api-platform/schema-generator/releases/latest/download/schema.phar
chmod +x schema.phar
First Generation:
Generate a basic Person class from Schema.org:
vendor/bin/schema generate https://schema.org/Person
Or via PHAR:
./schema.phar generate https://schema.org/Person
Default Output:
src/Entity/ (configurable).ApiResource attributes for API Platform integration.Generate a Product entity with API Platform attributes:
vendor/bin/schema generate https://schema.org/Product --config=config/schema.yaml
Example schema.yaml:
namespace: App\Entity
outputDir: src/Entity
classes:
Product:
properties:
name:
type: string
groups: ['default']
description:
type: string
groups: ['default']
image:
type: string
groups: ['default']
Define Schema:
Use schema.yaml to customize generation (e.g., add ApiResource operations):
classes:
Product:
operations:
get:
method: GET
uriTemplate: /products/{id}
normalizationContext:
groups: ['default']
Generate and Iterate:
vendor/bin/schema generate --config=config/schema.yaml
Regenerate after schema updates:
vendor/bin/schema generate --update
Integrate with API Platform:
Use generated ApiResource classes directly in your API:
// src/Entity/Product.php (auto-generated)
#[ApiResource]
class Product { ... }
Custom Attributes: Add PHP 8 attributes via config:
classes:
Product:
attributes:
- #[ORM\Table(name: 'products')]
Doctrine Relations:
Define ManyToOne/OneToMany via relations:
classes:
Product:
relations:
category:
type: manyToOne
target: Category
inversedBy: products
Serialization Groups:
Use groups to control serialization:
properties:
sku:
type: string
groups: ['admin']
OpenAPI Integration: Generate from OpenAPI specs:
vendor/bin/schema generate --openapi=api/openapi.yaml
Symfony Flex Projects:
Place schema.yaml in config/packages/schema.yaml for autoloading.
CI/CD:
Add to composer.json scripts:
"scripts": {
"generate:schema": "schema generate --config=config/schema.yaml --update"
}
Run in CI:
composer generate:schema
Version Control:
Exclude generated files from Git (add to .gitignore):
src/Entity/Generated/
Namespace Conflicts:
namespace in schema.yaml matches your project (e.g., App\Entity).prefix to avoid collisions:
prefix: App\Entity\Generated\
Self-Referencing Relations:
mappedBy/inversedBy:
relations:
parent:
type: manyToOne
target: self
mappedBy: children
Enum Generation:
OfferItemCondition) may require manual tweaks:
properties:
condition:
type: string
enum: [NEW, USED, REFURBISHED]
Update Mode:
--update may overwrite custom logic. Use sparingly:
vendor/bin/schema generate --update --dry-run # Test first
Dry Run: Preview changes without writing files:
vendor/bin/schema generate --dry-run
Verbose Output: Enable debug mode:
vendor/bin/schema generate -v
Custom Templates:
Override Twig templates in config/schema/templates/ to modify output.
Custom Logic: Extend generated classes via traits or post-generation scripts:
// src/Entity/Generated/Product.php (add after generation)
trait ProductExtensions {
public function getFormattedPrice(): string { ... }
}
Post-Generation Hooks: Use Symfony events to modify classes:
# config/services.yaml
App\EventSubscriber\SchemaGeneratedSubscriber:
tags: [kernel.event_subscriber]
OpenAPI Extensions: Add custom OpenAPI annotations:
classes:
Product:
openapi:
- $ref: '#/components/schemas/ProductExtension'
HTTPS URLs:
Always use https://schema.org/ to avoid mixed-content warnings.
Reserved Keywords:
Avoid PHP reserved words (e.g., class, new) as property names. Use columnPrefix:
columnPrefix: schema_
Doctrine Inheritance:
Use discriminatorMap for single-table inheritance:
classes:
CreativeWork:
inheritance:
type: single_table
discriminatorColumn: type
discriminatorMap:
Book: book
Movie: movie
Repeatable Attributes: Mark as repeatable in Schema.org:
properties:
offers:
type: Offer
repeatable: true
How can I help you explore Laravel packages today?