This package focuses on reusability of code and provides support to laravel framework. The package has been developed with the aim to save developement time by providing developers very useful commands and helping in project structure setup.
To install this package you will need:
- PHP 7.2 +
- Laravel 5.8 +
Run the following from the terminal to install the package:
composer require devslane/generator
composer require devslane/generator --dev
Install via composer - edit your composer.json
to require the package.
"require": {
"devslane/generator": "dev-master"
}
Then run composer update in your terminal to pull it in.
- Add the service provider to the providers array in your app.php config as follows:
Devslane\Generator\Providers\GeneratorServiceProvider::class,
- Publish the configuration file with the following Artisan command:
php artisan vendor:publish --provider="Devslane\Generator\Providers\GeneratorServiceProvider"
php artisan generate:migration
php artisan generate:model
php artisan generate:contract
php artisan generate:request
php artisan generate:exception
php artisan generate:service
php artisan generate:transformer
php artisan generate:controller
php artisan generate:factory
php artisan generate:seeder
php artisan generate:all
Supports to create migration with columns of provided datatype, with constraints or column with foreign key:
php artisan generate:migration {table : Table name} {--columns= : Columns}
php artisan generate:migration users --columns=first_name:string,last_name:string,age:integer
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->integer('age');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Add unique and nullable constraints:
php artisan generate:migration table1 --columns=name:string/nullable
php artisan generate:migration table2 --columns=name:string/unique
Foreign Key constraint on a column can also be provided with the command as:
php artisan create:migration posts --columns=post:string,user_id:fk=users
The command generates the Eloquent Models for the tables provided with the command.
php artisan generate:model {tables*}
php artisan generate:model users
Generates: User.php
namespace App;
use Devslane\Generator\Models\BaseModel;
/**
* Class User
*
* @package App
*/
class User extends BaseModel
{
}
This command generates the required Contracts interfaces which are then implemented by the Request classes.
generate:contract {tables*}
php artisan generate:contract users
Generates the following:
- CreateUserContract
- ListUserContract
- UpdateUserContract
These Interfaces contain the most essential functions. The DeleteUserContract is not created. It can be manually added if requirement arises.
namespace App\Services\Contract;
interface ListUserContract
{
}
namespace App\Services\Contract;
interface CreateUserContract
{
public function getFirstName();
public function getLastName();
public function getAge();
public function getCreatedAt();
public function getUpdatedAt();
}
namespace App\Services\Contract;
interface UpdateUserContract
{
public function getFirstName();
public function hasFirstName();
public function getLastName();
public function hasLastName();
public function getAge();
public function hasAge();
public function getCreatedAt();
public function hasCreatedAt();
public function getUpdatedAt();
public function hasUpdatedAt();
}
This command generates all the CRUD operation's Request classes. These classes implement the corresponding Contracts generated by generate:contract {tables*}.
generate:request {tables*}
php artisan generate:request users
Running this for the very first time generates ListRequest Class which is then extended by all the ListRequest Classes.
- ListRequest
namespace App\Api\V1\Requests;
use Devslane\Generator\Requests\BaseRequest;
/**
* Class ListTesttableRequest
* @package App\Api\V1\Requests
*/
class ListRequest extends BaseRequest
{
const LIMIT = 'limit';
const ORDER = 'order';
const ORDER_BY = 'order_by';
const SEARCH_QUERY = 'search_query';
public function getLimit() {
return $this->input(self::LIMIT);
}
public function getOrder() {
return $this->input(self::ORDER);
}
public function getOrderBy() {
return $this->input(self::ORDER_BY);
}
public function getSearchQuery() {
return $this->input(self::SEARCH_QUERY);
}
}
Generates the following:
- CreateUserRequest
- ListUserRequest
- UpdateUserRequest
These classes implement the corresponding contracts generated by the previous command and also contain the implementation of the functions of the Contracts. The DeleteUserRequest is not created. It can be manually added if requirement arises.
namespace App\Api\V1\Requests;
use App\Services\Contract\ListUserContract as Contract;
/**
* Class ListUserRequest
* @package App\Api\V1\Requests
*/
class ListUserRequest extends ListRequest implements Contract
{
}
namespace App\Api\V1\Requests;
use App\Services\Contract\CreateUserContract as Contract;
/**
* Class CreateUserRequest
* @package App\Api\V1\Requests
*/
class CreateUserRequest extends ListRequest implements Contract
{
const FIRST_NAME = 'first_name';
const LAST_NAME = 'last_name';
const AGE = 'age';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
public function getFirstName() {
return $this->input(self::FIRST_NAME);
}
public function getLastName() {
return $this->input(self::LAST_NAME);
}
public function getAge() {
return $this->input(self::AGE);
}
public function getCreatedAt() {
return $this->input(self::CREATED_AT);
}
public function getUpdatedAt() {
return $this->input(self::UPDATED_AT);
}
}
namespace App\Api\V1\Requests;
use App\Services\Contract\UpdateUserContract as Contract;
/**
* Class UpdateUserRequest
* @package App\Api\V1\Requests
*/
class UpdateUserRequest extends ListRequest implements Contract
{
const FIRST_NAME = 'first_name';
const LAST_NAME = 'last_name';
const AGE = 'age';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
public function getFirstName() {
return $this->input(self::FIRST_NAME);
}
public function hasFirstName() {
return $this->has(self::FIRST_NAME);
}
public function getLastName() {
return $this->input(self::LAST_NAME);
}
public function hasLastName() {
return $this->has(self::LAST_NAME);
}
public function getAge() {
return $this->input(self::AGE);
}
public function hasAge() {
return $this->has(self::AGE);
}
public function getCreatedAt() {
return $this->input(self::CREATED_AT);
}
public function hasCreatedAt() {
return $this->has(self::CREATED_AT);
}
public function getUpdatedAt() {
return $this->input(self::UPDATED_AT);
}
public function hasUpdatedAt() {
return $this->has(self::UPDATED_AT);
}
}
This generates the NotFoundExceptions related to the tables provided with the command.
generate:exception {tables*}
php artisan generate:exception users
Generates: UserNotFoundException
namespace App\Api\V1\Exceptions;
/**
* Class UserNotFoundException
* @package App\Api\V1\Exceptions
*/
class UserNotFoundException extends \Symfony\Component\HttpKernel\Exception\HttpException
{
const ERROR_MESSAGE = 'User not Found';
const ERROR_CODE = 404;
public function __construct($statusCode = 422)
{
parent::__construct($statusCode, self::ERROR_MESSAGE, null, array(), self::ERROR_CODE);
}
}
This command generates Services for the given tables containing CRUD operations in the service. These services are used by the Controllers to execute CRUD operations on the Database.
generate:service {tables*}
php artisan generate:service users
Generates: UserService
namespace App\Services;
use App\Api\V1\Exceptions\UserNotFoundException;
use App\User;
use App\Services\Contract\CreateUserContract;
use App\Services\Contract\UpdateUserContract;
/**
* Class UserService
* @package App\Services
*/
class UserService
{
/**
* @param $userId
* @return User
*/
public static function find($userId) {
$user = User::find($userId);
if (!$user) {
throw new UserNotFoundException();
}
return $user;
}
/**
* @return User[]|\Illuminate\Database\Eloquent\Collection
*/
public function index() {
return User::all();
}
/**
* @param $userId
* @return User
*/
public function show($userId) {
return self::find($userId);
}
/**
* @param CreateUserContract $contract
* @return User
*/
public function store(CreateUserContract $contract) {
$user = new User();
$user->first_name = $contract->getFirstName();
$user->last_name = $contract->getLastName();
$user->age = $contract->getAge();
$user->save();
return $user;
}
/**
* @param $userId
* @param UpdateUserContract $contract
* @return User
*/
public function update($userId, UpdateUserContract $contract) {
$user = self::find($userId);
if ($contract->hasFirstName()) {
$user->first_name = $contract->getFirstName();
}
if ($contract->hasLastName()) {
$user->last_name = $contract->getLastName();
}
if ($contract->hasAge()) {
$user->age = $contract->getAge();
}
$user->save();
return $user;
}
/**
* @param $userId
*/
public function delete($userId) {
$user = $this->find($userId);
try {
$user->delete();
} catch (\Exception $e) {
}
}
}
Generates the Transformer for the response that will be generated after any CRUD operation, the Controllers exploit these Transformers to transform the response in the desired format.
By default the transformers contain all the columns of the table.
generate:transformer {tables*}
php artisan generate:transformer users
Generates: UserTransformer
namespace App\Transformers;
use App\User;
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract
{
public function transform(User $user){
return [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'age' => (int)$user->age,
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
];
}
}
Generates a controller having fundamental CRUD functions, which the routes point to. These controller classes use the generated Services for the database operations.
Generated transformers are used by the controller before sending the response.
generate:controller {tables*}
php artisan generate:controller users videos posts
Generates: UserController
namespace App\Api\V1\Controllers;
use App\Transformers\UserTransformer;
use App\Api\V1\Requests\CreateUserRequest;
use App\Api\V1\Requests\UpdateUserRequest;
use App\Services\UserService;
use Devslane\Generator\Controllers\BaseController;
/**
* Class UserController
* @package App\Api\V1\Controllers
*
* @property-read UserService $userService
*/
class UserController extends BaseController
{
protected $userService;
public function __construct() {
$this->userService = new UserService();
}
/**
* @param UserService $service
* @return \Dingo\Api\Http\Response
*/
public function index(UserService $service) {
$users = $service->index();
return $this->response->collection($users, new UserTransformer());
}
/**
* @param $id
* @param UserService $service
* @return \Dingo\Api\Http\Response
*/
public function show($id, UserService $service) {
$user = $service->show($id);
return $this->response->item($user, new UserTransformer());
}
/**
* @param CreateUserRequest $request
* @param UserService $service
* @return \Dingo\Api\Http\Response
*/
public function store(CreateUserRequest $request, UserService $service) {
$user = $service->store($request);
return $this->response->item($user, new UserTransformer());
}
/**
* @param UpdateUserRequest $request
* @param $id
* @param UserService $service
* @return \Dingo\Api\Http\Response
*/
public function update(UpdateUserRequest $request, $id, UserService $service) {
$user = $service->update($id, $request);
return $this->response->item($user, new UserTransformer());
}
/**
* @param $id
* @param UserService $service
*/
public function destroy($id, UserService $service) {
$service->delete($id);
}
}
Generates the Factory for the tables provided.
generate:factory {tables*}
php artisan generate:facotory users posts videos
Generates: UserFactory
use App\User;
use Faker\Generator as Faker;
$factory->define(User::class, function (Faker $faker) {
return [
'first_name'=>$faker->firstName,
'last_name'=>$faker->lastName,
'age'=>$faker->numberBetween(0,100),
'created_at'=>$faker->dateTime,
'updated_at'=>$faker->dateTime,
];
});
Generates the Database seeder for the tables. These seeders use the corresponding factories generated by generate:factory {tables*}. By default the number of rows are 10 for the seeders and can be modified by changing it in the mcs-helper.php in config.
return [
.
.
'seeder' => [
'row_count' => 10,
'path' => 'database/seeds',
'overwrite' => true,
'exclude_table' => [
'password_resets', 'migrations',
],
]
.
.
]
generate:seeder {tables*}
php artisan generate:seeder users videos posts
Generates: UserSeeder
use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Config;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
$size = (integer)Config::get('mcs-helper.seeder.row_count');
factory(User::class, $size)->create();
}
}
generate:all {tables*}
php artisan generate:all users videos posts