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

Shortcode Laravel Package

thunderer/shortcode

Framework-agnostic PHP library for parsing and processing shortcodes/BBCodes. Extract shortcodes from text, handle replacements, and apply them via processors and events. Includes serializers for Text/XML/JSON/YAML. Supports PHP 5.3–8.x.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Framework Agnostic but Laravel-Friendly: The package is framework-agnostic but integrates seamlessly with Laravel due to its dependency-free nature and PHP 8.x compatibility. Laravel’s service container and facades can wrap the ShortcodeFacade for DI integration.
  • Modular Design: The separation of parsers, handlers, processors, and events aligns well with Laravel’s modular architecture (e.g., service providers, middleware, or even Blade directives).
  • Extensibility: Custom parsers, handlers, and event listeners can be injected into Laravel’s dependency injection system, making it adaptable to domain-specific needs (e.g., CMS content, dynamic UI components).

Integration Feasibility

  • Blade Integration: The package’s output can be directly embedded into Blade templates via custom directives (e.g., @shortcode) or helper functions.
  • Database/ORM Compatibility: Shortcodes can be stored in database fields (e.g., content columns) and processed on-the-fly during retrieval or rendering.
  • API/JSON Responses: Serializers (XML/JSON/YAML) enable structured shortcode handling in API responses or frontend frameworks (e.g., React/Vue).

Technical Risk

  • Performance Overhead: The RegularParser is slower than regex-based alternatives. Benchmark nested shortcodes in high-traffic Laravel routes (e.g., admin dashboards).
  • Syntax Conflicts: Default BBCode syntax ([tag]) may clash with existing HTML or Laravel-specific tags (e.g., [{{ }}] in Blade). Custom syntax (e.g., {{{shortcode}}}) may be needed.
  • Recursion Limits: Default recursion depth (null) could lead to stack overflows with deeply nested shortcodes. Configure withRecursionDepth() based on use cases.
  • Handler Isolation: Closure-based handlers may leak Laravel-specific dependencies (e.g., Auth, Cache). Use dependency injection via constructor injection or Laravel’s app() helper sparingly.

Key Questions

  1. Use Cases:
    • Will shortcodes replace Laravel’s existing templating (e.g., Blade @include or @component)? If so, assess overlap and redundancy.
    • Are shortcodes for dynamic content (e.g., user-generated posts) or static configurations (e.g., admin panels)?
  2. Performance:
    • What’s the expected volume of shortcodes per request? Test with 100+ nested shortcodes in a loop.
    • Can RegexParser suffice for 90% of cases to reduce overhead?
  3. Security:
    • How will user-uploaded shortcodes be sanitized? (e.g., prevent XSS via htmlspecialchars in handlers).
    • Are there sensitive parameters (e.g., [user id="123"]) that need validation?
  4. Maintenance:
    • Who will manage shortcode handlers (developers, content editors)? Document handler registration in Laravel’s service provider.
    • How will deprecated shortcodes be phased out? Use Laravel’s deprecated() helper or middleware.

Integration Approach

Stack Fit

  • Laravel Core:
    • Service Provider: Register the ShortcodeFacade as a singleton in AppServiceProvider::boot().
    • Facade: Publish the facade for easy access (e.g., Shortcode::process($content)).
    • Blade Directives: Create a custom directive (e.g., @shortcode) to process content in views.
  • Database:
    • Use Laravel’s Accessors/Mutators to auto-process shortcodes when retrieving model attributes (e.g., Post::getContentAttribute()).
    • Store serialized shortcodes (JSON/XML) in database fields for complex use cases.
  • API:
    • Process shortcodes in resource responses (e.g., PostResource::toArray()) or HTTP middleware.
    • Use serializers for structured API payloads (e.g., Shortcode::serialize($shortcode, 'json')).

Migration Path

  1. Pilot Phase:
    • Start with non-critical features (e.g., admin panels, static content).
    • Replace hardcoded HTML snippets with shortcodes (e.g., [alert type="success"]).
  2. Incremental Rollout:
    • Phase 1: Integrate into Blade templates via directives.
    • Phase 2: Add database processing for dynamic content (e.g., blog posts).
    • Phase 3: Extend to APIs and frontend frameworks.
  3. Deprecation:
    • Use Laravel’s deprecated() helper to warn about obsolete shortcodes.
    • Provide a migration path for handlers (e.g., Shortcode::removeHandler('old-tag')).

Compatibility

  • Laravel Versions: Tested on PHP 8.x; ensure compatibility with Laravel 9/10 (no breaking changes expected).
  • Existing Libraries:
    • Markdown: Use CommonMark + Shortcode for hybrid syntax (e.g., [note]Markdown here.[/note]).
    • Telescope/Horizon: Log shortcode processing for debugging (e.g., Shortcode::addEventHandler(Events::FILTER_SHORTCODES, ...)).
  • Frontend:
    • For SPAs, expose shortcode processing via Laravel API endpoints (e.g., /api/shortcodes/process).

Sequencing

  1. Setup:
    • Install via Composer: composer require thunderer/shortcode.
    • Publish the facade and config (if needed) using Laravel’s publishes in AppServiceProvider.
  2. Core Integration:
    • Register the facade and create Blade directives.
    • Implement 2–3 critical shortcodes (e.g., [button], [alert]).
  3. Advanced Features:
    • Add custom parsers (e.g., for double-bracket syntax {{{tag}}}).
    • Implement event listeners for logging/auditing.
  4. Testing:
    • Unit tests for handlers (use Laravel’s Mockery).
    • Integration tests for Blade/API processing.

Operational Impact

Maintenance

  • Handler Management:
    • Centralize handler registration in a dedicated service class (e.g., app/Services/ShortcodeService.php) for easier updates.
    • Use Laravel’s config caching (php artisan config:cache) to optimize handler lookups.
  • Documentation:
    • Document shortcode syntax and available tags in Laravel’s README.md or a dedicated wiki.
    • Provide a CLI command (e.g., php artisan shortcode:list) to dump registered shortcodes.
  • Updates:
    • Monitor thunderer/shortcode for breaking changes (e.g., PHP 8.2+ features).
    • Use Laravel’s package auto-updates or GitHub Actions to test new versions.

Support

  • Debugging:
    • Log unhandled shortcodes via Events::FILTER_SHORTCODES for analytics.
    • Create a debug Blade directive (e.g., @shortcodeDebug) to inspect parsed shortcodes.
  • Fallbacks:
    • Implement a default handler for unknown shortcodes (e.g., return raw text or log a warning).
    • Use Laravel’s exception handling to catch malformed shortcodes (e.g., ShortcodeException).
  • Editor Support:
    • Provide a Tiptap/ProseMirror plugin or CKEditor custom button for content editors to insert shortcodes.

Scaling

  • Performance:
    • Cache processed shortcodes in Laravel’s cache (e.g., Cache::remember()) for repeated content.
    • Use RegexParser for simple cases and RegularParser only when nesting is required.
  • Concurrency:
    • Shortcode processing is stateless; no locks needed for parallel requests.
    • For batch processing (e.g., bulk content updates), use Laravel’s queues to avoid timeouts.
  • Database:
    • Store serialized shortcodes (JSON) in database fields to avoid reprocessing on every request.
    • Index fields containing shortcodes if used in queries (e.g., WHERE content LIKE '%[tag]%').

Failure Modes

  • Syntax Errors:
    • Malformed shortcodes (e.g., [tag) may break rendering. Use try-catch in handlers or a global event listener.
    • Mitigation: Validate input with a regex or use RegularParser for strict parsing.
  • Recursion Limits:
    • Infinite loops with nested shortcodes (e.g., [a][b][a][/b][/a]). Set withRecursionDepth(5) as a default.
    • Mitigation: Log recursion depth violations and alert admins.
  • Handler Failures:
    • Exceptions in handlers (e.g., DivisionByZeroError) may crash the request.
    • Mitigation: Wrap handlers in try-catch or use Laravel’s app()->bound('shortcode.handler.fallback').
  • Memory Leaks:
    • Deeply nested shortcodes may consume memory. Monitor with Laravel Forge/New Relic.
    • Mitigation: Limit recursion depth and use withMaxIterations(3).

Ramp-Up

  • Developer Onboarding:
    • Add a Laravel-specific guide to the package’s README.md (or fork it
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky