Installation:
composer require purwantoid/package-generator
Publish the default stubs (optional but recommended for customization):
php artisan vendor:publish --provider="Purwantoid\PackageGenerator\PackageGeneratorServiceProvider" --tag="stubs"
First Use Case: Generate a basic package structure:
php artisan make:package YourVendor YourPackage
This creates a folder YourPackage with:
src/ (main PHP classes)config/ (config files)tests/ (empty by default)composer.jsonREADME.mdWhere to Look First:
resources/stubs/ (or published location) for template customization.app/Console/Kernel.php for registered commands.Purwantoid\PackageGenerator\PackageGeneratorServiceProvider for core logic.Standard Package Generation:
php artisan make:package vendor/package-name --with-tests
src/ServiceProvider.phpsrc/PackageNameServiceProvider.phpconfig/package.phptests/Feature/ (if --with-tests)Custom Stub Integration:
resources/stubs/ (e.g., php/main-class.stub).{{PACKAGE_NAME_STUDLY}} for dynamic content.main-class.stub:
<?php
namespace {{VENDOR_NAME}}\{{PACKAGE_NAME_STUDLY}}\;
class {{PACKAGE_NAME_STUDLY}} extends \Illuminate\Support\ServiceProvider
{
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../../config/{{PACKAGE_NAME_KEBAB}}.php', 'package'
);
}
}
Conditional Logic:
{{#if}} and {{/if}} in stubs for optional files (e.g., tests):
{{#if with_tests}}
tests/Feature/ExampleTest.php
{{/if}}
Post-Generation Hooks:
Purwantoid\PackageGenerator\Commands\MakePackageCommand to add custom logic:
protected function postGenerate(string $packagePath): void
{
// Add custom files or run migrations
}
Template Validation:
php artisan package:validate-stubs to check for syntax errors in stubs.Stub Path Conflicts:
stubs_path config in config/package-generator.php.stubs/ exists in the published location before generation.Variable Name Mismatches:
{{VARIABLE_NAME}} (e.g., {{PACKAGE_NAME}} vs. {{package_name}}) will cause silent failures.php artisan package:validate-stubs to catch errors early.Composer Autoloading:
composer.json may not auto-discover namespaces if src/ is misconfigured.autoload.psr-4 includes:
"autoload": {
"psr-4": {
"{{VENDOR_NAME}}\\{{PACKAGE_NAME_STUDLY}}\\": "src/"
}
}
Test File Generation:
--with-tests flag only creates the tests/ directory structure; you must manually implement test classes.example-test.stub to include boilerplate test code.Namespace Collisions:
Illuminate) in package stubs.{{VENDOR_NAME}} as a prefix in all generated namespaces.Dry Run:
debug=true in config/package-generator.php to log generated file paths and content.Stub Overrides:
php artisan package:generate --stubs=/path/to/custom/stubs
Command Options:
--no-interaction for CI/CD pipelines:
php artisan make:package vendor/package --no-interaction --with-tests
Custom Commands:
PackageGeneratorServiceProvider:
$this->commands([
\Purwantoid\PackageGenerator\Commands\MakePackageCommand::class,
\App\Commands\CustomPackageCommand::class,
]);
Dynamic Stub Loading:
Purwantoid\PackageGenerator\StubLoader.Post-Processing:
post-update-cmd in the generated composer.json to run scripts after installation:
"scripts": {
"post-update-cmd": [
"@php artisan package:post-install"
]
}
Template Inheritance:
{{include 'partials/header.stub'}} for reusable components (e.g., license headers).resources/stubs/. Override with:
'stubs_path' => base_path('custom/stubs'),
{{PACKAGE_NAME}} ≠ {{package_name}}).How can I help you explore Laravel packages today?