项目作者: arkadiuszbachorski

项目描述 :
Extended make command for Laravel
高级语言: PHP
项目地址: git://github.com/arkadiuszbachorski/laravel-xmake.git
创建时间: 2020-01-28T13:11:13Z
项目社区:https://github.com/arkadiuszbachorski/laravel-xmake

开源协议:MIT License

下载


Additional Laravel Artisan xmake command for faster resource creating and scaffolding. Created to speed up developing process and stop typing same things in various places.

Table of contents

Getting started

Prerequisites

This package was developed for Laravel 5.8 and up. I haven’t tested earlier versions yet.

Installation

Require this package with composer for development only.

  1. composer require arkadiuszbachorski/laravel-xmake --dev

Publish config

  1. php artisan vendor:publish --tag=xmake-config

Change default resource paths if you would like to.

Publish resources

  1. php artisan vendor:publish --tag=xmake-resources

Features

  • Scaffold your app quicker
  • Create many related files with just one command: model, controller, request, resource, migration, seeder and factory
  • Provide fields in one place, rest will be prepared or even filled for you
  • Easily customize stubs for your needs

Usage

You’d rather see than read?

Everything Is AWESOME

Example:

  1. php artisan xmake -i --fields=title,foo,bar --modelName=Foobar --model --request --controller --api

Result

FoobarController.php

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Foobar;
  4. use App\Http\Requests\FoobarRequest;
  5. class FoobarController extends Controller
  6. {
  7. /**
  8. * Display a listing of the resource.
  9. *
  10. * @return Response
  11. */
  12. public function index()
  13. {
  14. $foobars = Foobar::all();
  15. return response()->json([]);
  16. }
  17. /**
  18. * Show the form for creating a new resource.
  19. *
  20. * @return Response
  21. */
  22. public function create()
  23. {
  24. return response()->json([]);
  25. }
  26. /**
  27. * Store a newly created resource in storage.
  28. *
  29. * @param FoobarRequest $request
  30. * @return Response
  31. */
  32. public function store(FoobarRequest $request)
  33. {
  34. Foobar::create($request->validated());
  35. return response()->json([]);
  36. }
  37. /**
  38. * Display the specified resource.
  39. *
  40. * @param Foobar $foobar
  41. * @return \Illuminate\Http\Response
  42. */
  43. public function show(Foobar $foobar)
  44. {
  45. return response()->json([]);
  46. }
  47. /**
  48. * Show the form for editing the specified resource.
  49. *
  50. * @param Foobar $foobar
  51. * @return \Illuminate\Http\Response
  52. */
  53. public function edit(Foobar $foobar)
  54. {
  55. return response()->json([]);
  56. }
  57. /**
  58. * Update the specified resource in storage.
  59. *
  60. * @param FoobarRequest $request
  61. * @param Foobar $foobar
  62. * @return \Illuminate\Http\Response
  63. */
  64. public function update(FoobarRequest $request, Foobar $foobar)
  65. {
  66. $foobar->update($request->validated());
  67. return response()->json([]);
  68. }
  69. /**
  70. * Remove the specified resource from storage.
  71. *
  72. * @param Foobar $foobar
  73. * @return \Illuminate\Http\Response
  74. * @throws \Exception
  75. */
  76. public function destroy(Foobar $foobar)
  77. {
  78. $foobar->delete();
  79. return response()->json([]);
  80. }
  81. }

Foobar.php

  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. class Foobar extends Model
  5. {
  6. protected $guarded = [];
  7. }

FoobarRequest.php

  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. class FoobarRequest extends FormRequest
  5. {
  6. /**
  7. * Determine if the user is authorized to make this request.
  8. *
  9. * @return bool
  10. */
  11. public function authorize()
  12. {
  13. return false;
  14. }
  15. /**
  16. * Get the validation rules that apply to the request.
  17. *
  18. * @return array
  19. */
  20. public function rules()
  21. {
  22. return [
  23. 'title' => '',
  24. 'foo' => '',
  25. 'bar' => '',
  26. ];
  27. }
  28. }

Pretty nice, huh? For more details and possibilities, see documentation.

To-do list

  • Guessing factory based on validation and migration field type
  • Guessing migration field type based on validation

Documentation

Config

config/xmake.php

  1. [
  2. 'paths' => [
  3. // Path where stubs are expected to exist. This path affects vendors publishing too.
  4. 'stubs' => '/resources/xmake/stubs',
  5. // Path where fields.php is expected to exist. This path affects vendors publishing too.
  6. 'fields' => '/resources/xmake',
  7. ],
  8. 'database' => [
  9. // Flag that indicates whether ->nullable() should be automatically added if provided in validation
  10. 'addNullableIfAppearsInValidation' => true,
  11. ],
  12. 'controller' => [
  13. // You can change PHPDoc methods captions there
  14. 'docs' => [
  15. 'index' => 'Display a listing of the resource.',
  16. 'create' => 'Show the form for creating a new resource.',
  17. 'store' => 'Store a newly created resource in storage.',
  18. 'show' => 'Display the specified resource.',
  19. 'edit' => 'Show the form for editing the specified resource.',
  20. 'update' => 'Update the specified resource in storage.',
  21. 'destroy' => 'Remove the specified resource from storage.',
  22. ],
  23. // You can change CRUD methods names there
  24. 'methods' => [
  25. 'index' => 'index',
  26. 'create' => 'create',
  27. 'store' => 'store',
  28. 'show' => 'show',
  29. 'edit' => 'edit',
  30. 'update' => 'update',
  31. 'destroy' => 'destroy',
  32. ],
  33. ],
  34. 'seeder' => [
  35. // Default amount used in seeders if not provided by --amount option
  36. 'defaultAmount' => 50,
  37. ],
  38. 'resource' => [
  39. // Flag that indicates whether resource fields should be parsed to camelCase
  40. 'camelizeFields' => true,
  41. ],
  42. 'validation' => [
  43. // It enables parsing pipe syntax (i.e. "string|nullable") to array
  44. 'parseArray' => true,
  45. // It enables guessing validation based on database field. I.e. string('foobar') parses to 'string' validation.
  46. 'guessBasedOnDatabase' => true,
  47. ],
  48. // You can change what will be created if you select "create everything"/"all" option
  49. 'createEverything' => [
  50. 'model' => true,
  51. 'migration' => true,
  52. 'factory' => true,
  53. 'seeder' => true,
  54. 'request' => true,
  55. 'resource' => true,
  56. 'controller' => true,
  57. ],
  58. ];

Stubs

Stubs can be found in /resources/xmake/stubs path by default. You will see a few text files there. All of them contain “Dummy…” content, that means it will be replaced by generated things. Everything what isn’t prefixed with “Dummy…” can be customised by you.

Fields

Fields file can be found in /resources/xmake/fields.php path by default. It’s a source of data about fields.

  1. [
  2. // key used in --fields option
  3. 'title20ch' => [
  4. /*
  5. Field name used in Eloquent, database migration.
  6. I.e $model->title
  7. */
  8. 'name' => 'title',
  9. /*
  10. Used in factory after $faker->
  11. I.e. 'title' => $faker->sentence(2)
  12. */
  13. 'factory' => 'sentence(2)',
  14. 'validation' => 'string|nullable',
  15. /*
  16. Migration, NAME will by replaced with actual name automatically
  17. I.e. string('title', 20)->default('test'),
  18. */
  19. 'database' => 'string(NAME, 20)->default("test")',
  20. ],
  21. ];

You don’t have to fill in all the data about each field. The only required one is the “name”. If you enter unknown field to —fields option, it will be treated as the key is the name and the other parameters are empty.

This means you can completely ignore this file if you would like just list every field in created files.

However Xmake has a few self-filling and data processing mechanisms as you could see in config. If you provide database field there is probability of getting at least some of repetitive things filled in validation and factory.

Commands

Every command has —help, so you can check available options faster than checking here.

In examples below I assume default config and file fields.php contains:

  1. [
  2. 'foo' => [
  3. 'name' => 'foo',
  4. 'factory' => 'sentence(2)',
  5. 'validation' => 'string|nullable',
  6. 'database' => 'string(NAME, 20)->default("test")',
  7. ],
  8. ];

xmake

It’s a command that will be used most often, because it gives powerful scaffolding ability. It can call every single command listed below.

Xmake by default runs interactive shell experience. If you would like to create resources even faster - use —instant flag. Results are almost the same, but you can’t name your files independently. Probably you wouldn’t do it anyway, because auto-naming works great.

Available options

—instant -i

Use this flag if you want don’t want to use interactive shell.

—modelName

This parameter is required, almost every command uses it.

—fields

Get fields keys array, use comma as separator

—all

Create everything based on your config. By default - it’s literally everything.

—api

Create API version of Controller and enable resource creating

—model

Create model with given modelName

—migration

Create migration with given fields prepared or filled

—factory

Create factory with given fields prepared of filled

—seeder

Create seeder that invokes factory

—request

Create request with given fields prepared or filled

—resource

Create resource with given fields prepared of filled

—controller

Create controller with various options - request, resource and api, based on your previous choices.

xmake:model

It simply creates model.


Example

shell php artisan xmake:model Foobar

Result:

Foobar.php
php <?php namespace App; use Illuminate\Database\Eloquent\Model; class Foobar extends Model { protected $guarded = []; }

xmake:controller

It creates controller with:

  • basic CRUD operations for your model
  • Blade views or REST API
  • Validation inside or injected

Available options

—model -m

It’s required. You must provide your models name.

—api

It’s a flag that changes responses from Blade views to REST API.

—fields

You can provide fields for validation there.

—resource -x

It injects given resource to index, store, show, edit and update returns. It does nothing when no —api flag provided.

—request -r

It injects given request to create and update methods. If the file doesn’t exist - it can be created with fields you provided.


Example

shell php artisan xmake:controller FoobarController --api --fields=foo,bar --model=Foobar

Result:

php <?php namespace App\Http\Controllers; use App\Foobar; use Illuminate\Http\Request; class FoobarController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $foobars = Foobar::all(); return response()->json([]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return response()->json([]); } /** * Store a newly created resource in storage. * * @param Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $data = $request->validate([ 'foo' => 'string|nullable', 'bar' => '', ]); Foobar::create($data); return response()->json([]); } /** * Display the specified resource. * * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function show(Foobar $foobar) { return response()->json([]); } /** * Show the form for editing the specified resource. * * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function edit(Foobar $foobar) { return response()->json([]); } /** * Update the specified resource in storage. * * @param Request $request * @param Foobar $foobar * @return \Illuminate\Http\Response */ public function update(Request $request, Foobar $foobar) { $data = $request->validate([ 'foo' => 'string|nullable', 'bar' => '', ]); $foobar->update($data); return response()->json([]); } /** * Remove the specified resource from storage. * * @param Foobar $foobar * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(Foobar $foobar) { $foobar->delete(); return response()->json([]); } }

xmake:resource

It creates resource with given fields filled. It makes fields name camelCase by default, however it can be changed in config.

Available options

—fields

Example

shell php artisan xmake:resource FoobarResource --fields=foo,bar,not_camel_case_field

Result:

php <?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; class FoobarResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function toArray($request) { return [ 'foo' => $this->foo, 'bar' => $this->bar, 'notCamelCaseField' => $this->not_camel_case_field, ]; } }

xmake:migration

It creates migration with given fields prepared or filled.

Available options

—create

Table name

—fields

Example

shell php artisan xmake:migration create_foobar_table --create=foobar --fields=foo,bar

Result:

php <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateFoobarTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('foobar', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('foo', 20)->default("test")->nullable(); $table->('bar'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('foobar'); } }

xmake:request

It creates request with given validation rules prepared or filled.


Example

shell php artisan xmake:request FoobarRequest --fields=foo,bar

Result:

php <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class FoobarRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'foo' => 'string|nullable', 'bar' => '', ]; } }

xmake:factory

It creates factory with given factory rules prepared or filled.


Example

shell php artisan xmake:factory FoobarFactory --fields=foo,bar

Result:

php <?php /* @var $factory \Illuminate\Database\Eloquent\Factory */ use App\Foobar; use Faker\Generator as Faker; $factory->define(Foobar::class, function (Faker $faker) { return [ 'foo' => $faker->sentence(2), 'bar' => $faker->, ]; });

xmake:seeder

It creates seeder with given model.

Available options

—model

Model name.

—amount

Number of models to be created in seeder. Default value can be changed in config.


Example

shell php artisan xmake:seeder FoobarSeeder --model=foo,bar --amount=33

Result:

php <?php use Illuminate\Database\Seeder; use App\Foobar; class FoobarSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { factory(Foobar::class, 33); } }

Summary

All the above files can be created with just one command:

  1. php artisan xmake -i --modelName=Foobar --all --api --fields=foo,bar