Skip to content

Latest commit

 

History

History
277 lines (203 loc) · 7.35 KB

File metadata and controls

277 lines (203 loc) · 7.35 KB

Module System Refactoring: internachi/modular Integration

Overview

This document describes the refactoring of the module system to integrate the internachi/modular pattern, providing better composer-based autoloading, Laravel package discovery, and seamless integration with Filament 5 and custom themes.

Changes Summary

1. Package Integration

  • Added: internachi/modular ^3.0 to composer dependencies
  • Created: config/modular.php configuration file
  • Updated: composer.json to include Modules\ namespace in PSR-4 autoload

2. New Service Provider

File: app/Providers/ModularServiceProvider.php

This provider:

  • Registers module auto-discovery
  • Loads modules from both old (app/Modules) and new (app-modules) directories
  • Handles route, view, translation, and migration registration
  • Provides Filament 5 auto-discovery for resources, pages, and widgets
  • Manages theme support for modules

3. Module Creation Command

File: app/Console/Commands/MakeModuleCommand.php

A new Artisan command that creates modules following the internachi/modular pattern:

php artisan make:module YourModule

Creates:

  • Complete directory structure in app-modules/YourModule
  • composer.json with proper autoloading
  • Service provider with Laravel package discovery
  • Example controller, model, migration, view, and test
  • Configuration file
  • Filament resource directories

4. Enhanced Module Manager

File: app/Modules/ModuleManager.php

Extended to support:

  • Loading modules from both old and new directory structures
  • Creating wrapper classes for modular modules to work with existing ModuleInterface
  • Maintaining backward compatibility with existing modules

5. Updated Module Command

File: app/Console/Commands/ModuleCommand.php

Modified to delegate module creation to the new make:module command while maintaining all other functionality (list, enable, disable, install, uninstall, info).

6. Configuration

File: config/modular.php

Comprehensive configuration including:

  • Module and theme directories
  • Auto-discovery settings
  • Filament 5 integration options
  • Caching configuration
  • Testing settings

File: config/app.php

  • Registered ModularServiceProvider in the providers array

Module Structure

New Structure (Recommended)

app-modules/YourModule/
├── composer.json                  # Module as composer package
├── src/
│   ├── YourModuleModule.php      # Main module class
│   ├── Providers/
│   │   └── YourModuleServiceProvider.php
│   ├── Http/Controllers/
│   ├── Models/
│   ├── Services/
│   └── Filament/                 # Auto-discovered by Filament
│       ├── Resources/
│       ├── Pages/
│       └── Widgets/
├── routes/
│   ├── web.php
│   ├── api.php
│   └── admin.php
├── database/migrations/
├── resources/
│   ├── views/
│   ├── lang/
│   └── assets/
├── config/
└── tests/

Legacy Structure (Still Supported)

The old app/Modules/ structure continues to work for backward compatibility.

Key Features

1. Composer-Based Autoloading

Each module has its own composer.json with PSR-4 autoloading:

{
    "autoload": {
        "psr-4": {
            "Modules\\YourModule\\": "src/"
        }
    }
}

After creating modules, run:

composer dump-autoload

2. Laravel Package Discovery

Modules use Laravel's package auto-discovery:

{
    "extra": {
        "laravel": {
            "providers": [
                "Modules\\YourModule\\Providers\\YourModuleServiceProvider"
            ]
        }
    }
}

3. Filament 5 Integration

Place Filament components in standard directories:

  • src/Filament/Resources/ - Resource classes
  • src/Filament/Pages/ - Custom pages
  • src/Filament/Widgets/ - Dashboard widgets

Components are automatically discovered and registered.

4. Custom Theme Support

Modules can provide themes in resources/themes/ with:

  • Custom layouts
  • CSS stylesheets
  • JavaScript files
  • Theme metadata (theme.json)

Themes integrate with the existing theme system using helpers:

  • set_theme(), active_theme()
  • theme_asset(), theme_layout()
  • @themeCss, @themeJs Blade directives

5. Module Management

Commands remain the same:

# Create
php artisan make:module YourModule
php artisan module create YourModule

# Manage
php artisan module list
php artisan module enable YourModule
php artisan module disable YourModule
php artisan module install YourModule
php artisan module uninstall YourModule
php artisan module info YourModule

Benefits

For Developers

  1. Better IDE Support: Full autocomplete and code navigation
  2. Standard Structure: Follows Laravel package conventions
  3. Isolated Dependencies: Each module can have its own dependencies
  4. Easy Testing: Module-specific test suites
  5. Reusability: Modules can be extracted as standalone packages

For the Application

  1. Maintainability: Clear separation of concerns
  2. Flexibility: Easy to add, remove, or update modules
  3. Performance: Efficient autoloading and caching
  4. Scalability: Supports large applications with many modules
  5. Admin Panel: Seamless Filament 5 integration
  6. Theming: Module-specific themes and assets

Migration Guide

For New Modules

Use the new structure:

php artisan make:module YourModule
cd app-modules/YourModule
# Develop your module
composer dump-autoload

For Existing Modules

Existing modules in app/Modules/ continue to work. To migrate:

  1. Create new module structure:

    php artisan make:module YourModule
  2. Copy code from old module to new structure:

    • Controllers → src/Http/Controllers/
    • Models → src/Models/
    • Views → resources/views/
    • Routes → routes/
    • Migrations → database/migrations/
  3. Update namespaces from App\Modules\YourModule to Modules\YourModule

  4. Run composer dump-autoload

  5. Test thoroughly

  6. Remove old module directory when satisfied

Documentation

  • README.md: Updated with new module system overview
  • docs/MODULE_DEVELOPMENT.md: Comprehensive development guide
  • docs/MODULE_QUICK_START.md: Quick start guide (existing)
  • docs/EXTERNAL_MODULES.md: External module integration (existing)

Backward Compatibility

  • ✅ Old modules in app/Modules/ continue to work
  • ✅ Existing module commands work unchanged
  • ✅ Module database records remain compatible
  • ✅ Existing hooks and events still fire
  • ✅ Theme system integration preserved

Testing

All syntax validation passed:

  • ✅ ModularServiceProvider
  • ✅ MakeModuleCommand
  • ✅ ModuleManager
  • ✅ ModuleCommand updates

Security check:

  • ✅ No vulnerabilities detected by CodeQL

Next Steps

  1. Test module creation in running environment
  2. Create example module demonstrating all features
  3. Migrate BlogModule as reference implementation
  4. Add integration tests for new module system
  5. Create video/tutorial demonstrating usage

Conclusion

This refactoring provides a modern, standards-based module system that integrates seamlessly with Laravel's ecosystem, Filament 5, and the existing theme system. It maintains backward compatibility while providing a clear migration path for future development.