internachi/modularize
Adds internachi/modular support to Laravel package commands via traits. Provides a --module option for console and generator commands, returning module config and generating files into the module directory with correct paths and namespaces.
composer require internachi/modularize
use Illuminate\Console\Command;
use InterNACHI\Modularize\Support\Modularize;
class MyCommand extends Command
{
use Modularize;
public function handle()
{
if ($module = $this->module()) {
// Module logic here
}
}
}
--module Flag
php artisan my:command --module=Blog
Make a Module-Aware Generator
use Illuminate\Console\GeneratorCommand;
use InterNACHI\Modularize\Support\ModularizeGeneratorCommand;
class MakeWidget extends GeneratorCommand
{
use ModularizeGeneratorCommand;
protected function getStub()
{
return __DIR__.'/stubs/widget.stub';
}
protected function getDefaultNamespace($rootNamespace = null)
{
return $this->module()
? $this->module()->getNamespace('Http/Controllers')
: parent::getDefaultNamespace($rootNamespace);
}
}
Run:
php artisan make:widget --module=Blog
Result: Generates modules/Blog/Http/Controllers/WidgetController.php.
Module Detection in Commands
if ($module = $this->module()) {
$moduleName = $module->name;
$modulePath = $module->path;
$moduleNamespace = $module->getNamespace('App');
}
Generator Command Integration
use ModularizeGeneratorCommand;
protected function getFileName()
{
return $this->module()
? $this->module()->name.'/'.$this->argument('name')
: parent::getFileName();
}
Fallback Logic for Non-Module Commands
protected function getDefaultNamespace($rootNamespace = null)
{
return $this->module()
? $this->module()->getNamespace('Http/Controllers')
: $rootNamespace.'\\Http\\Controllers';
}
--module is not specified.Leverage ModuleConfig Methods
$module->getNamespace('Path/To/Subdirectory'): Dynamically resolve namespaces.$module->path: Get the base path of the module.$module->name: Access the module name.Extend Generator Stubs
Modify stub files to include module-specific placeholders (e.g., {moduleNamespace}) and replace them in the generator:
protected function replaceNamespace($stub, $name)
{
$stub = parent::replaceNamespace($stub, $name);
return $this->module()
? str_replace('{moduleNamespace}', $this->module()->getNamespace('App'), $stub)
: $stub;
}
Customize Module Path Resolution
Override the module() method in your command to use a custom module resolver:
protected function module()
{
return app('module')->findByName($this->option('module'));
}
Batch Processing with Modules Use the module trait to process multiple modules in a single command:
$modules = app('module')->all();
foreach ($modules as $module) {
$this->line("Processing module: {$module->name}");
// Command logic per module
}
Missing internachi/modular Dependency
internachi/modular to be installed and configured. Without it, $this->module() will return null.internachi/modular is installed and the ModuleServiceProvider is registered in config/app.php.Namespace Collisions
App\Http\Controllers), generated files may overwrite each other.protected function getDefaultNamespace($rootNamespace = null)
{
return $this->module()
? $this->module()->getNamespace('Http/Controllers')
: $rootNamespace.'\\Http\\Controllers';
}
Path Resolution in Non-PSR-4 Modules
ModuleConfig class or override path resolution in your command:
$customPath = $module->path.'/Custom/Directory';
Laravel 13+ Console Component Changes
internachi/modularize releases and test commands after Laravel upgrades.Generator Command Caching
php artisan cache:clear
php artisan view:clear
Verify Module Configuration Dump the module object to debug its properties:
$module = $this->module();
$this->line('<comment>Module:</comment> '.print_r($module, true));
Check for Missing Dependencies
Ensure internachi/modular is installed and configured:
composer show internachi/modular
php artisan config:clear
Test Module Paths Validate module paths manually:
$this->line('<info>Module Path:</info> '.$module->path);
$this->line('<info>Namespace:</info> '.$module->getNamespace('App'));
Inspect Generator Output
Use --verbose to debug generator behavior:
php artisan make:widget --module=Blog --verbose
Custom Module Resolver
Override the module() method to use a custom resolver:
protected function module()
{
return app('custom.module.resolver')->resolve($this->option('module'));
}
Dynamic Stub Replacement Extend stub replacement logic to include module-specific placeholders:
protected function replaceModulePlaceholders($stub, $module)
{
return str_replace(
['{moduleName}', '{modulePath}'],
[$module->name, $module->path],
$stub
);
}
Pre- and Post-Command Hooks Add hooks to execute logic before/after module detection:
protected function beforeModuleDetection()
{
// Pre-module logic
}
protected function afterModuleDetection($module)
{
// Post-module logic
}
Module-Aware Argument Parsing Dynamically adjust argument parsing based on the active module:
protected function getArguments()
{
$arguments = parent::getArguments();
if ($this->module()) {
$arguments['module-specific-arg'] = Input::argument('module-specific-arg');
}
return $arguments;
}
Fallback to Root for Missing Modules Handle cases where a module doesn’t exist by falling back to the root namespace:
if (!$module = $this->module()) {
$this->error('Module not found. Falling back to root namespace.');
return parent::getDefaultNamespace($rootNamespace);
}
How can I help you explore Laravel packages today?