2017 © Pedro Peláez
 

library generators

Laravel 5 generators.

image

3rd-sense/generators

Laravel 5 generators.

  • Monday, November 23, 2015
  • by mofodojodino
  • Repository
  • 2 Watchers
  • 0 Stars
  • 509 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

3rd Sense Australia Laravel 5 Generators

Based on the excellent Generators by Jeffery Way we have added a couple more that we use regularly here at 3rd Sense., (*1)

These include:, (*2)

  • make:repository
  • make:transformer

Replicated make commands from official repository Illuminate\Foundation for use in Lumen, (*3)

  • make:command
  • make:console
  • make:event
  • make:job
  • make:listener
  • make:model
  • make:provider
  • make:test

Usage

Step 1: Install using composer

composer require 3rd-sense/generators --dev

Step 2: Add the service provider

Following Jeffery's advice these should only be available in your local development environment, so you don't want to update the production providers array in config/app.php. Instead, add the provider in app/Providers/AppServiceProvider.php, like so:, (*4)

For Laravel config/app.php

public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('ThirdSense\Generators\LaravelServiceProvider');
    }
}

For Lumen bootstrap/app.php

if ($app->environment() === 'local') {
    $app->register(\ThirdSense\Generators\LumenServiceProvider::class);
}

Step 3: Run artisan generators!

You're all set. Run php artisan from the console, and you'll see the new commands in the make:* namespace section., (*5)

Examples

For all other commands refer to the official Laravel documentation, (*6)

Generate Repository Classes

php artisan make:repository UserRepository App/User

This will generate 2 files for you in the app/repositories directory, the repository:, (*7)

<?php

namespace App\Repositories;

use App\User;

/**
 * Class UserRepositoryRepository
 * @package App\Repositories
 */
class UserRepository implements UserRepositoryInterface
{
    /**
     * Retrieve all User.
     *
     * @return mixed
     */
    public function all()
    {
        return User::all();
    }

    /**
     * Retrieve a paginated list of User.
     *
     * @param $limit
     */
    public function paginated($limit)
    {
        return User::paginate($limit);
    }

    /**
     * Retrieve a single User by ID.
     *
     * @param $id
     *
     * @return mixed
     */
    public function find($id)
    {
        return User::find($id);
    }

    /**
     * Create and save a new User.
     *
     * @param $data
     *
     * @return bool
     */
    public function create($data)
    {
        $entity = new User;

        return $this->save($entity, $data);
    }

    /**
     * Update an existing User.
     *
     * @param $id
     * @param $data
     *
     * @return User
     */
    public function update($id, $data)
    {
        $entity = $this->findById($id);

        return $this->save($entity, $data);
    }

    /**
     * Remove/delete exiting User
     *
     * @param $id
     *
     * @return int
     */
    public function destroy($id)
    {
        return User::destroy($id);
    }

    /**
     * Save the User.
     *
     * @param $entity
     * @param $data
     *
     * @return boolean
     */
    protected function save($entity, $data)
    {
        // set model properties

        return $entity->save();
    }
}

and the repository interface:, (*8)

<?php

namespace App\Repositories;

use App\User;

/**
 * Interface UserRepositoryInterface
 * @package App\Repositories
 */
interface UserRepositoryInterface
{
    /**
     * Retrieve all User.
     *
     * @return mixed
     */
    public function all();

    /**
     * Retrieve a paginated list of User.
     *
     * @param $limit
     */
    public function paginated($limit);

    /**
     * Retrieve a single User by ID.
     *
     * @param $id
     *
     * @return mixed
     */
    public function find($id);

    /**
     * Create and save a new User.
     *
     * @param $data
     *
     * @return bool
     */
    public function create($data);

    /**
     * Update an existing User.
     *
     * @param $id
     * @param $data
     *
     * @return User
     *
     */
    public function update($id, $data);

    /**
     * Remove/delete exiting User
     *
     * @param $id
     *
     * @return int
     */
    public function destroy($id);
}

Now you just need to register these repositories with your service container. To do this just add the following snippet to your app/Providers/AppServiceProvider's register method:, (*9)

$this->app->bind(UserRepositoryInterface::class, UserRepository::class);

and you're good to go., (*10)

The other alternative is to create an App/Providers/RepositoriesServiceProvider class and place the above code in the register method. Remember to add this new service provider to your config/app.php providers array., (*11)

Generate Transformer Classes

This generator is to be used to create transformers for the Fractal package provided by The PHP League, (*12)

php artisan make:transformer UserTransformer App/User

This will generate the following tranformer for you in the app/Tranformers directory:, (*13)

<?php

namespace App\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    /**
     * List of resources to possibly include
     *
     * @var  array
     */
    protected $availableIncludes = [];

    /**
     * List of resources to automatically include
     *
     * @var  array
     */
    protected $defaultIncludes = [];

    /**
     * @param User $user
     *
     * @return array
     */
    public function transform(User $user)
    {
        return [
            'id'    =>  $user->id,

            // TODO: transform entity properties

            'links' => [
                [
                    'rel' => 'self',
                    'uri' => route('user.show', ['id' =>  $user->id]),
                ],
                [
                    'rel' => 'list',
                    'uri' => route('user.index'),
                ],
            ],
        ];
    }
}

The Versions

23/11 2015

dev-master

9999999-dev

Laravel 5 generators.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dean Tedesco

laravel generators

23/11 2015

dev-develop

dev-develop

Laravel 5 generators.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dean Tedesco

laravel generators

23/11 2015

0.0.2

0.0.2.0

Laravel 5 generators.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dean Tedesco

laravel generators

18/08 2015

0.0.1

0.0.1.0

Laravel 5 generators.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dean Tedesco

laravel generators