2017 © Pedro Peláez
 

laravel-package laravel-command

A simple Laravel package to provide artisan new commands

image

mckenziearts/laravel-command

A simple Laravel package to provide artisan new commands

  • Tuesday, May 29, 2018
  • by Mckenziearts
  • Repository
  • 2 Watchers
  • 18 Stars
  • 70 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 3 Versions
  • 204 % Grown

The README.md

Laravel-command

Latest Stable Version License Build Status Total Downloads, (*1)

Simple package to quickly generate Laravel templated Repository, Helpers and Observer files., (*2)

Install

Via Composer, (*3)

``` bash $ composer require mckenziearts/laravel-command --dev, (*4)


For Laravel 5.5 - you're done. For Laravel 5.4 or 5.3 you'll only want to use these commands for ```local``` development, 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: ```php public function register() { if ($this->app->environment() == 'local') { $this->app->register('Mckenziearts\LaravelCommand\LaravelCommandServiceProvider'); } }

Usage

By default Laravel does not allow to generate observers or even does not allow to take the notion of repository as Symfony. To generate its elements you can use the following commands, (*5)

Open the console and enter this command to generate a new repository :, (*6)

php artisan make:repository {Entity}

The generate file look like this :, (*7)

namespace App\Repositories;

use App\Models\Entity;

class EntityRepository
{
    /**
     * @var Entity
     */
    private $model;

    /**
     * EntityRepository constructor.
     * @param Entity $model
     */
    public function __construct(Entity $model)
    {
        $this->model = $model;
    }

    /**
     * Return a new instance of Entity Model
     *
     * @return Entity
     */
    public function newInstance()
    {
        return $this->model->newInstance();
    }
}

By default Repository load Model in the default application namespace App\Models If your models are in another namespace, it will be necessary to change the use in the repository to have no error like :, (*8)

use MODELS\NAMESPACE\Entity;

This is the same action to perform for the observers, it also loads the models in the namespace App\Models. To generate an observer, you must execute the command:, (*9)

php artisan make:observer {Entity}

The generate file look like this :, (*10)

namespace App\Observers;

use App\Models\Entity;

class EntityObserver
{
    /**
     * Trigger Before Create a Entity
     *
     * @param Entity $model
     */
    public function creating(Entity $model){}

    /**
     * Trigger after create a Entity
     *
     * @param Entity $model
     */
    public function created(Entity $model){}

    /**
     * Trigger before update a Entity
     *
     * @param Entity $model
     */
    public function updating(Entity $model){}

    ...
}

  • Helper files

``` bash $ php artisan make:helper {Entity}, (*11)


The generate file look like this : ```php namespace App\Helpers; class EntityHelper { }

If you need better distribut your code, you can create a helper to put a logic to lighten your controllers., (*12)

An example of a helper that I often use in my projects, (*13)

namespace App\Helpers;

use Intervention\Image\Facades\Image;

class MediaHelper
{
    /**
     * @protected
     *
     * @var string $dir, the file uploaded path
     */
    protected static $dir = 'uploads';

    /**
     * @return string
     */
    public static function getUploadsFolder()
    {
        return self::$dir;
    }

    /**
     * Return the size of an image
     *
     * @param string $file
     * @param string $folder
     * @return array $width and $height of the file give in parameter
     */
    public static function getFileSizes(string $file, string $folder = null)
    {
        if ($folder) {
            list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'. $folder .'/'.$file));
        }
        list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'.$file));

        return [
            'width'     => $width,
            'height'    => $height
        ];
    }

    /**
     * resize, To rezise and image
     *
     * @param string    $file      file to rezise
     * @param int       $width     width of the file
     * @param int       $height    height of the file
     * @param string    $filepath  path to save file
     */
    public static function resize($file, $width, $height, $filepath)
    {
        Image::make($file)->resize($width, $height)->save($filepath);
    }

    /**
     * getImageWeight
     *
     * @param $octets
     * @return string
     */
    public static function getImageWeight($octets) {
        $resultat = $octets;
        for ($i = 0; $i < 8 && $resultat >= 1024; $i++) {
            $resultat = $resultat / 1024;
        }
        if ($i > 0) {
            return preg_replace('/,00$/', '', number_format($resultat, 2, ',', ''))
                . ' ' . substr('KMGTPEZY', $i-1, 1) . 'o';
        } else {
            return $resultat . ' o';
        }
    }

}

Change log

Please see CHANGELOG for more information what has changed recently., (*14)

License

The MIT License (MIT)., (*15)

The Versions

29/05 2018

dev-master

9999999-dev https://github.com/Mckenziearts/laravel-command

A simple Laravel package to provide artisan new commands

  Sources   Download

MIT

The Requires

 

laravel repository generators helper observer artisan-cli

29/05 2018

dev-develop

dev-develop https://github.com/Mckenziearts/laravel-command

A simple Laravel package to provide artisan new commands

  Sources   Download

MIT

The Requires

 

laravel repository generators helper observer artisan-cli

29/05 2018

1.0.1

1.0.1.0 https://github.com/Mckenziearts/laravel-command

A simple Laravel package to provide artisan new commands

  Sources   Download

MIT

The Requires

 

laravel repository generators helper observer artisan-cli