backtheweb/laravel-package-maker
Laravel Package Maker helps scaffold Laravel packages quickly by generating a ready-to-publish structure, boilerplate classes, config, service provider, tests, and composer setup. Ideal for jumpstarting reusable components and standardizing package creation.
Installation:
composer require --dev backtheweb/laravel-package-maker
Ensure the package is registered in composer.json under require-dev.
Publish Assets (if needed):
php artisan vendor:publish --provider="Backtheweb\PackageMaker\PackageMakerServiceProvider"
(Optional: Customize default templates or configurations.)
First Use Case: Generate a basic Laravel package scaffold:
php artisan package:make MyVendor/MyPackage
This creates a new package directory (packages/MyVendor/MyPackage) with:
composer.json with Laravel autoloading.README.md and phpunit.xml.Where to Look First:
php artisan package:make --help for all available options (e.g., --no-interaction, --config-path).vendor/backtheweb/laravel-package-maker/src/Resources/stubs/. Override by publishing assets and modifying config/package-maker.php.Package Generation:
php artisan package:make Vendor/Package (interactive prompts for namespace, description, etc.).php artisan package:make Vendor/Package --namespace=App --author="John Doe" --no-interaction
php artisan package:make Vendor/Package --path=custom/packages/
Template Customization:
php artisan vendor:publish --tag=package-maker.stubs
resources/stubs/ (e.g., composer.json.stub, README.md.stub).stubs config array in config/package-maker.php.Integration with CI/CD:
# Example: Generate all packages in a single command
php artisan package:make Package1 --path=packages/
php artisan package:make Package2 --path=packages/
Post-Generation Hooks:
--post-generate option to run custom scripts after package creation:
php artisan package:make Vendor/Package --post-generate="php artisan vendor:publish --tag=package-config"
Dynamic Package Configuration:
php artisan package:make Vendor/Package --config="database.default=mysql"
{{ config('database.default') }}.Package Dependencies:
composer.json:
php artisan package:make Vendor/Package --dependency="laravel/framework:^10.0"
--dev-dependency.Modular Packages:
php artisan package:make Vendor/Package --path=packages/
cd packages/Vendor/Package && php artisan package:make SubPackage --path=src/SubPackages/
Testing Integration:
--with-tests flag:
php artisan package:make Vendor/Package --with-tests
Namespace Collisions:
Vendor\Package) doesn’t conflict with existing code.Company\Package instead of App\Package).Composer Autoloading Issues:
config/app.php under providers.composer dump-autoload after generation.Stub Overrides Not Applying:
stubs path in config/package-maker.php is correct.rm -rf resources/stubs && php artisan vendor:publish --tag=package-maker.stubs
Permission Errors:
packages/ may fail if permissions are restrictive.--path to point to a writable directory or adjust folder permissions:
chmod -R 775 packages/
Laravel Version Mismatches:
composer.json:
"require": {
"laravel/framework": "10.0.*"
}
Dry Run:
Use --dry-run to preview changes without generating files:
php artisan package:make Vendor/Package --dry-run
Log Output: Enable verbose mode for debugging:
php artisan package:make Vendor/Package --verbose
Check Config:
Validate config/package-maker.php for custom settings:
return [
'stubs' => resource_path('stubs'), // Override default stubs path
'default_namespace' => 'App',
];
Custom Commands:
Extend the package by creating a custom command that uses PackageMaker:
use Backtheweb\PackageMaker\PackageMaker;
class CustomPackageCommand extends Command {
protected function handle() {
$maker = new PackageMaker();
$maker->setStub('custom.stub')->make('Vendor/Package');
}
}
Dynamic Stub Loading:
Load stubs from a remote source (e.g., GitHub) by overriding the getStub() method in a custom PackageMaker class.
Post-Processing:
Use the postGenerate event to run additional logic after package creation:
// In a service provider
PackageMaker::postGenerate(function ($packagePath) {
// Run custom scripts, e.g., git init
});
Template Inheritance:
Extend existing stubs by copying them to your project’s resources/stubs/ and modifying only the necessary parts.
Template Versioning: Commit custom stubs to version control to maintain consistency across projects.
Package Metadata:
Use the --license, --description, and --keywords flags to auto-populate composer.json:
php artisan package:make Vendor/Package --license=MIT --description="A cool package" --keywords="laravel,api"
CI/CD Optimization: Cache generated packages in CI to avoid re-running the command on every build:
# .github/workflows/package.yml
steps:
- uses: actions/cache@v3
with:
path: packages/
key: ${{ runner.os }}-packages-${{ hashFiles('**/composer.json') }}
Monorepo Support: Generate packages in a monorepo by setting a base path:
php artisan package:make Vendor/Package --path=../monorepo/packages/
How can I help you explore Laravel packages today?