Skip to content

Laravel Package Builder is a powerful tool designed to streamline the process of creating and managing packages for Laravel.

License

Notifications You must be signed in to change notification settings

NyonCode/laravel-package-toolkit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📦 Laravel package toolkit

Laravel Package toolkit is a powerful tool designed to streamline the process of creating and managing packages for Laravel. It provides a set of intuitive abstractions and helper methods for common package development tasks, enabling developers to focus on building features rather than boilerplate code.

Features

  • Simple and expressive package configuration
  • Automatic handling of routes, migrations, translations, and views
  • Support for view components
  • Built-in exception handling for package-specific errors
  • Comprehensive language support

Table of Contents

Installation

You can install the package via composer:

composer require nyoncode/laravel-package-toolkit

Usage

Basic Configuration

To use Laravel Package Builder, create a ServiceProvider for your package that extends NyonCode\LaravelPackageToolkit\PackageServiceProvider:

use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\LaravelPackageToolkit\Packager;

class MyAwesomePackageServiceProvider extends PackageServiceProvider
{
    public function configure(Packager $packager): void
    {
        $packager
            ->name('My Awesome Package')
            ->hasConfig()
            ->hasRoutes()
            ->hasMigrations()
            ->hasTranslations()
            ->hasViews();
    }
}

Advanced Configuration

For more control over your package configuration, you can use additional methods and specify custom paths:

use NyonCode\LaravelPackageToolkit\PackageServiceProvider;
use NyonCode\LaravelPackageToolkit\Packager;

class AdvancedPackageServiceProvider extends PackageServiceProvider
{
    public function configure(Packager $packager): void
    {
        $packager
            ->name('Advanced package')
            ->hasShortName('adv-pkg')
            ->hasConfig('custom-config.php')
            ->hasRoutes(['api.php', 'web.php'])
            ->hasMigrations('custom-migrations')
            ->hasTranslations('lang')
            ->hasViews('custom-views')
            ->hasComponents([
                'data-table' => DataTable::class,
                'modal' => Modal::class,
            ]);
    }

    public function registeringPackage(): void
    {
        // Custom logic before package registration
    }

    public function bootingPackage(): void
    {
        // Custom logic before package boot
    }
}

Lifecycle Hooks

Here is a list of all available life cycle hooks:

Hook Method Description
registeringPackage() Called before register() is called
registeredPackage() Called after register() is called
bootingPackage() Called before boot() is called
bootedPackage() Called after boot() is called

Name

Define a name for the package:

$packager->name('Package name')

Short name

Define a custom short name for the package. The hasShortName method is used to modify the name defined by name() if you prefer not to use the short version from $packager->name('Package name'):

$packager->hasShortName('custom-short-name')

Routing

To enable routing in your package:

$packager->hasRoutes();

By default, this will load routes from the routes directory. For custom route files:

$packager->hasRoutes(['api.php', 'web.php']);

Or for specific file paths:

$packager->hasRoute([
        '../www/routes/web.php',
        '../api/routes/api.php'
    ])

To use an alternative directory for route files.

$package->hasRoute(['web.php'], 'webRouter')

Migrations

To enable migrations:

$packager->hasMigrations();

This loads migrations from the database/migrations directory. For a custom directory:

$packager->hasMigrations('custom-migrations');

Translations

To enable translations:

$packager->hasTranslations();

This loads translations from the lang directory and automatically supports JSON translations.

Views

To enable views:

$packager->hasViews();

This loads views from the resources/views directory. For a custom directory:

$packager->hasViews('custom-views');

View Components

To register view components:

$packager->hasComponents([
    'data-table' => DataTable::class,
    'modal' => Modal::class,
]);

You can then use these components in your Blade templates:

<x-data-table :data="$users"/>
<x-modal title="User Details">
    <!-- Modal content -->
</x-modal>

About Command

Laravel Package Builder provides methods to add package information to Laravel's php artisan about command.

hasAbout()

The hasAbout() method allows you to include your package's information in the Laravel About command. By default, it will include the package's version.

  $packager->hasAbout();

hasVersion()

The hasVersion() method lets you manually set the version of your package:

  $packager->hasVersion('1.0.0'); 

If no version is manually set, the package will automatically retrieve the version from your composer.json file.

Customizing About Command Data

You can extend the about command information by implementing the aboutData() method in your service provider:

  public function aboutData(): array
  {
      return [
          'Repository' => 'https://github.com/your/package',
          'Author' => 'Your Name',
      ];
  }

This method allows you to add custom key-value pairs to the About command output for your package. When you run php artisan about, your package's information will be displayed in a dedicated section.

This implementation allows for flexible and easy inclusion of package metadata in Laravel's system information command.

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

About

Laravel Package Builder is a powerful tool designed to streamline the process of creating and managing packages for Laravel.

Resources

License

Stars

Watchers

Forks

Languages