Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Openapi Cli Laravel Package

spatie/laravel-openapi-cli

Generate Laravel Artisan commands from an OpenAPI spec. Each API endpoint becomes its own command with typed options for params and request bodies, plus auth, base URL, caching, redirects, and output formatting—ideal for building API CLIs with Laravel Zero.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel-Native Integration: Leverages Laravel’s Artisan command system, service providers, and HTTP client (illuminate/http), ensuring tight integration with the framework’s ecosystem. The facade-based API (OpenApiCli) aligns with Laravel’s common patterns (e.g., Hash, Cache).
  • OpenAPI-Centric Design: Transparently maps OpenAPI specs to CLI commands, enforcing a spec-first workflow. This is ideal for teams already maintaining OpenAPI docs (e.g., for Swagger UI or API gateways) and reduces drift between docs and implementation.
  • Modular Configuration: Chaining methods like ->auth()->cache()->retryOn() mirrors Laravel’s fluent interfaces (e.g., query builders, mailables), improving readability and maintainability.
  • Laravel Zero Compatibility: Explicitly designed for Laravel Zero, enabling standalone CLI tools without a full Laravel app. This is a force multiplier for internal tools or scripts.

Integration Feasibility

  • Low Friction for Laravel Apps: Requires only:
    1. Composer install (spatie/laravel-openapi-cli).
    2. Service provider registration (handled by Laravel’s package discovery).
    3. OpenAPI spec registration (e.g., OpenApiCli::register() in a config file or bootstrapped service).
  • Non-Laravel PHP Projects: Possible but requires manual setup (e.g., mocking Laravel’s app() helper, HTTP client). Risk: ~3–5 hours of dev time for a custom wrapper.
  • OpenAPI Spec Requirements:
    • Must be valid OpenAPI 3.x (YAML/JSON).
    • Path/query/body parameters must be well-defined (e.g., no dynamic ** paths).
    • Auth schemes (OAuth, API keys) must be spec-compliant.
  • Dependency Conflicts: Minimal (only illuminate/http and spatie/array-to-object). Conflicts unlikely unless using highly customized Laravel versions.

Technical Risk

Risk Area Severity Mitigation
OpenAPI Spec Quality High Validate specs with tools like Swagger Editor pre-integration.
Auth Complexity Medium Test with OAuth2/API keys early. Use retryOn() for token refresh logic.
Parameter Parsing Low Fixed in v1.0.1; edge cases (e.g., query params with special chars) are handled.
Performance Low Caching (->cache(ttl: ...)) mitigates spec parsing overhead.
Laravel Zero Limitations Medium Ensure Laravel Zero’s handle() method is compatible with generated commands.
Output Formatting Low Customize with ->yamlOutput() or ->jsonOutput(); extend via facades.

Key Questions for the TPM

  1. Spec Maturity:
    • Are OpenAPI specs version-controlled and automatically validated in CI?
    • Do specs cover all APIs needed for CLI tools, or will gaps require manual commands?
  2. Auth Strategy:
    • Is authentication dynamic (e.g., per-command tokens) or static (e.g., API keys)?
    • Will retryOn() suffice for token refresh, or is a custom solution needed?
  3. Laravel Zero Adoption:
    • Are standalone CLI tools a priority, or is this primarily for Artisan commands?
    • Will commands need custom logic (e.g., post-processing responses) beyond HTTP calls?
  4. Error Handling:
    • Should errors trigger custom CLI feedback (e.g., ->onError()) or integrate with Laravel’s exception handling?
  5. Scaling:
    • How many APIs/specs will be registered? Performance testing may be needed for >10 specs.
  6. Documentation:
    • Will the auto-generated list command replace or supplement existing API docs?
    • Should usage examples be embedded in the CLI help (e.g., --help output)?

Integration Approach

Stack Fit

  • Laravel Core: Fully compatible with Laravel 8+ (tested up to v10.x). Uses:
    • Artisan: Command generation and registration.
    • HTTP Client: illuminate/http for requests (supports middleware, retries).
    • Service Container: Dependency injection for auth managers (e.g., OAuthTokenManager).
  • Laravel Zero: Native support for building standalone CLI apps (e.g., php artisan myapp:command).
  • Non-Laravel PHP: Possible but requires:
    • Mocking app() helper (e.g., via Symfony/DependencyInjection).
    • Replacing illuminate/http with Guzzle/HTTP client.
    • Estimated Effort: 1–2 days for a wrapper library.

Migration Path

  1. Assessment Phase (1–2 days):
    • Audit OpenAPI specs for completeness/validity.
    • Identify 1–2 high-priority APIs to pilot (e.g., a CRUD endpoint).
  2. Pilot Integration (3–5 days):
    • Register the first API spec in a Laravel service provider:
      OpenApiCli::register('https://api.example.com/openapi.yaml', 'example-api')
          ->baseUrl('https://api.example.com')
          ->auth(fn () => $this->authManager->token());
      
    • Test generated commands (e.g., php artisan example-api:get-users).
  3. Scaling Phase:
    • Batch Register APIs: Group related specs (e.g., auth-api, payment-api).
    • Customize Output: Extend facades for JSON/YAML formatting or logging.
    • Laravel Zero Apps: Move commands to a Zero app for standalone tools.
  4. Deprecation Plan:
    • Phase out ad-hoc scripts or custom CLI wrappers.
    • Document the new workflow (e.g., "Use api:command instead of curl").

Compatibility

Component Compatibility Notes
Laravel Versions 8.x–10.x (tested) Uses Laravel’s HTTP client and Artisan.
OpenAPI Versions 3.0.x–3.1.x YAML/JSON support; no OpenAPI 2.0.
Auth Schemes OAuth2, API keys, Bearer tokens, Basic Auth Custom auth via closures (e.g., ->auth(fn () => ...)).
HTTP Clients Guzzle (under the hood) Middleware/retries supported via Laravel’s HTTP client.
Laravel Zero Full support Commands work as Zero app routes.
Non-Laravel PHP Possible with wrappers Requires DI container and HTTP client mocking.

Sequencing

  1. Prerequisites:
    • Validate OpenAPI specs (use Swagger Editor).
    • Ensure Laravel 8+ or Laravel Zero is the target runtime.
  2. Core Integration:
    • Install package: composer require spatie/laravel-openapi-cli.
    • Register specs in a service provider (e.g., AppServiceProvider).
    • Test basic commands (e.g., php artisan api:list).
  3. Enhancements:
    • Add auth logic (e.g., ->auth()).
    • Customize error handling (e.g., ->onError()).
    • Extend output formats (e.g., ->yamlOutput()).
  4. Deployment:
    • Document commands in team wiki/CLI help.
    • Train users on php artisan api:command --help.
  5. Monitoring:
    • Log command usage (e.g., php artisan api:command calls).
    • Track spec changes to update CLI tools.

Operational Impact

Maintenance

  • Pros:
    • Spec-Driven: Commands auto-update when OpenAPI specs change. No manual sync needed.
    • Centralized Config: All settings (auth, caching, retries) are in one place (registration code).
    • Low Boilerplate: No need to maintain custom CLI parsers or HTTP clients.
  • Cons:
    • Spec Maintenance: CLI tooling now depends on OpenAPI spec quality. Mitigation: Add spec validation to CI.
    • Laravel Dependency: Tight coupling to Laravel’s HTTP client/Artisan. Mitigation: Abstract for non-Laravel use if needed.
    • Command Naming: Generated names (e.g., api:get-users) may not match team conventions. **
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai