项目作者: mckenziearts

项目描述 :
A Laravel package to provide artisan new commands
高级语言: PHP
项目地址: git://github.com/mckenziearts/laravel-command.git
创建时间: 2018-03-06T05:53:13Z
项目社区:https://github.com/mckenziearts/laravel-command

开源协议:

下载


Laravel-command

Latest Stable Version
License
Build Status
Total Downloads

Simple package to quickly generate Laravel templated Repository, Helpers and Observer files.

Install

Via Composer

  1. $ composer require mckenziearts/laravel-command --dev

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:

  1. public function register()
  2. {
  3. if ($this->app->environment() == 'local') {
  4. $this->app->register('Mckenziearts\LaravelCommand\LaravelCommandServiceProvider');
  5. }
  6. }

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

Open the console and enter this command to generate a new repository :

  1. php artisan make:repository {Entity}

The generate file look like this :

  1. namespace App\Repositories;
  2. use App\Models\Entity;
  3. class EntityRepository
  4. {
  5. /**
  6. * @var Entity
  7. */
  8. private $model;
  9. /**
  10. * EntityRepository constructor.
  11. * @param Entity $model
  12. */
  13. public function __construct(Entity $model)
  14. {
  15. $this->model = $model;
  16. }
  17. /**
  18. * Return a new instance of Entity Model
  19. *
  20. * @return Entity
  21. */
  22. public function newInstance()
  23. {
  24. return $this->model->newInstance();
  25. }
  26. }

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 :

  1. 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:

  1. php artisan make:observer {Entity}

The generate file look like this :

  1. namespace App\Observers;
  2. use App\Models\Entity;
  3. class EntityObserver
  4. {
  5. /**
  6. * Trigger Before Create a Entity
  7. *
  8. * @param Entity $model
  9. */
  10. public function creating(Entity $model){}
  11. /**
  12. * Trigger after create a Entity
  13. *
  14. * @param Entity $model
  15. */
  16. public function created(Entity $model){}
  17. /**
  18. * Trigger before update a Entity
  19. *
  20. * @param Entity $model
  21. */
  22. public function updating(Entity $model){}
  23. ...
  24. }
  • Helper files
  1. $ php artisan make:helper {Entity}

The generate file look like this :

  1. namespace App\Helpers;
  2. class EntityHelper
  3. {
  4. }

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

An example of a helper that I often use in my projects

  1. namespace App\Helpers;
  2. use Intervention\Image\Facades\Image;
  3. class MediaHelper
  4. {
  5. /**
  6. * @protected
  7. *
  8. * @var string $dir, the file uploaded path
  9. */
  10. protected static $dir = 'uploads';
  11. /**
  12. * @return string
  13. */
  14. public static function getUploadsFolder()
  15. {
  16. return self::$dir;
  17. }
  18. /**
  19. * Return the size of an image
  20. *
  21. * @param string $file
  22. * @param string $folder
  23. * @return array $width and $height of the file give in parameter
  24. */
  25. public static function getFileSizes(string $file, string $folder = null)
  26. {
  27. if ($folder) {
  28. list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'. $folder .'/'.$file));
  29. }
  30. list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'.$file));
  31. return [
  32. 'width' => $width,
  33. 'height' => $height
  34. ];
  35. }
  36. /**
  37. * resize, To rezise and image
  38. *
  39. * @param string $file file to rezise
  40. * @param int $width width of the file
  41. * @param int $height height of the file
  42. * @param string $filepath path to save file
  43. */
  44. public static function resize($file, $width, $height, $filepath)
  45. {
  46. Image::make($file)->resize($width, $height)->save($filepath);
  47. }
  48. /**
  49. * getImageWeight
  50. *
  51. * @param $octets
  52. * @return string
  53. */
  54. public static function getImageWeight($octets) {
  55. $resultat = $octets;
  56. for ($i = 0; $i < 8 && $resultat >= 1024; $i++) {
  57. $resultat = $resultat / 1024;
  58. }
  59. if ($i > 0) {
  60. return preg_replace('/,00$/', '', number_format($resultat, 2, ',', ''))
  61. . ' ' . substr('KMGTPEZY', $i-1, 1) . 'o';
  62. } else {
  63. return $resultat . ' o';
  64. }
  65. }
  66. }

Change log

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT).