bgpgroup/api-builder

Api Builder

Installs: 80

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

pkg:composer/bgpgroup/api-builder

dev-main 2022-07-22 01:26 UTC

This package is auto-updated.

Last update: 2025-12-22 09:16:58 UTC


README

Latest Version on Packagist Software License Build Status Scrutinizer Coverage Quality Score Total Downloads

API Builder is a Laravel package for BGP system which allow to build Modules and Resources via commands.

Using this package you can build:

  • Modules
  • Resources
  • Collections
  • Requests
  • DTO's
  • Controllers
  • Models
  • Tests

You can also create it all at once using module and resource commands

Install

To install through Composer, by run the following command:

composer require bgpgroup/api-builder

The package will automatically register a service provider and alias.

Documentation

Modules

To build a new module, by run the following command:

php artisan bgp:make:module Sales

Where Sales will be the module name

This command will create:

  • src/Modules/Sales/Providers/AppServiceProvider.php
  • src/Modules/Sales/Providers/AuthServiceProvider.php
  • src/Modules/Sales/config/sales.php
  • src/Modules/Sales/routes/api.php

Collections

Run the following command to build a collection:

php artisan bgp:make:collection Order --module=Sales

Order will be the resource name and Sales will be the module name

This command will create:

  • src/Modules/Sales/Collections/OrderCollection.php

Controllers

For controllers, run the following command:

php artisan bgp:make:controller Order --module=Sales

Order will be the resource name and Sales will be the module name

This command will create:

  • src/Modules/Sales/Controllers/OrderController.php

Migrations

Before run the command, you must setup the table columns

Edit src/Modules/Sales/config/sales.php adding 'resources' key like:

<?php

return [

    'resources' => require_once('builder.php'),

    'roles' => [

    ],
];

And create a new php file src/Modules/Sales/config/builder.php with the following content as an example:

<?php

return [
    'Order' => [

        'migration' => [
            'table' => 'orders',
            'columns' => [
                'id' => true,
                'name' => [
                    'type' => 'string',
                    'size' => '100',
                ],
                'description' => [
                    'type' => 'text',
                    'nullable' => true,
                ],
                'amount' => [
                    'type' => 'double',
                    'size' => '100',
                    'default' => '0'
                ],
                'date' => [
                    'type' => 'dateTime',
                ],
                'client_id' => [
                    'type' => 'uuid',
                    'nullable' => true,
                ],
                'income' => [
                    'type' => 'enum',
                    'options' => "['on', 'off']",
                    'default' => 'on'
                ],
                'expenses' => [
                    'type' => 'enum',
                    'options' => "['on', 'off']",
                    'default' => 'on'
                ],
                'active' => true,
                'created_by' => true,
                'company_id' => true,
                'timestamps' => true,
                'soft_delete' => true
            ],
        ]
    ],
];

This config file allown you to generete the table columns in the migration file using the command:

php artisan bgp:make:migration Order --module=Sales

Order will be the resource name and Sales will be the module name

This command will create:

  • src/Modules/Sales/migrations/2022_06_20_212536_create_orders_table.php

With the previous configuration, this will be the generated code:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
			$table->uuid('id')->primary();
			$table->string('name', 100);
			$table->text('description')->nullable();
			$table->double('amount');
			$table->dateTime('date');
			$table->uuid('client_id')->nullable();
			$table->enum('income', ['on', 'off'])->default('on');
			$table->enum('expenses', ['on', 'off'])->default('on');
			$table->enum('active',['on', 'off'])->default('on');
			$table->foreignUuid('created_by')->nullable()->constrained('users');
			$table->foreignUuid('company_id');
			$table->timestamps();
			$table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
};

License

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