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

Jetpack Autoloader Laravel Package

automattic/jetpack-autoloader

A Composer-compatible autoloader for Jetpack and other Automattic PHP packages. It helps load classes across multiple plugins/projects without conflicts, supporting shared dependencies and smoother upgrades in WordPress environments.

Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer in your Laravel project:

    composer require automattic/jetpack-autoloader
    

    Register the autoloader in composer.json under autoload:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Jetpack\\": "vendor/automattic/jetpack-autoloader/src/"
        }
    }
    

    Run composer dump-autoload.

  2. First Use Case Use Jetpack’s core classes directly in Laravel:

    use Jetpack\Modules\Module;
    use Jetpack\Modules\ModuleLoader;
    
    // Load a Jetpack module (e.g., "stats")
    $moduleLoader = new ModuleLoader();
    $moduleLoader->load('stats');
    

Implementation Patterns

1. Module Integration

  • Lazy Loading: Load Jetpack modules on-demand to avoid performance overhead:

    if (ModuleLoader::isModuleActive('stats')) {
        $stats = new \Jetpack\Modules\Stats\Module();
        $stats->init();
    }
    
  • Dependency Injection: Bind Jetpack services to Laravel’s container:

    $this->app->singleton(\Jetpack\Modules\ModuleLoader::class, function ($app) {
        return new ModuleLoader($app['config']);
    });
    

2. Configuration

  • Override Jetpack defaults via Laravel’s config/jetpack.php:
    return [
        'modules' => [
            'stats' => [
                'enabled' => env('JETPACK_STATS_ENABLED', false),
            ],
        ],
    ];
    

3. Event Listeners

  • Hook into Jetpack events (e.g., module activation/deactivation):
    use Jetpack\Modules\ModuleEvent;
    
    event(new ModuleEvent('stats', 'activated'));
    

4. API Integration

  • Use Jetpack’s REST API endpoints in Laravel routes:
    Route::get('/jetpack/stats', function () {
        return \Jetpack\Modules\Stats\API::getStats();
    });
    

Gotchas and Tips

Pitfalls

  • Namespace Conflicts: Jetpack uses Jetpack\ namespace; ensure no collisions with your app.
  • Module Dependencies: Some Jetpack modules require WordPress core. Test in a WordPress environment first.
  • Caching: Jetpack’s autoloader may bypass Laravel’s OPcache. Use composer dump-autoload --optimize for production.

Debugging

  • Class Not Found: Verify composer dump-autoload was run and PSR-4 paths are correct.
  • Module Errors: Check Jetpack’s logs (/wp-content/jetpack/logs/ if WordPress is installed).

Extension Points

  • Custom Modules: Extend Jetpack’s module system by creating a subclass of Jetpack\Modules\Module.
  • Hooks: Use Jetpack’s filter/action system (e.g., jetpack_module_loaded) for custom logic.

Performance

  • Disable Unused Modules: Set enabled: false in config/jetpack.php for unused modules.
  • Autoload Excludes: Exclude Jetpack classes from PSR-4 if unused:
    "autoload-dev": {
        "psr-4": {
            "Jetpack\\": "vendor/automattic/jetpack-autoloader/src/"
        }
    }
    
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation
uri-template/tests