├── .gitignore ├── CHANGELOG.md ├── README.md ├── composer.json └── src ├── Console ├── Commands │ ├── LaravelHelperCommand.php │ ├── LaravelObserverCommand.php │ └── LaravelRepositoryCommand.php └── Stubs │ ├── helper.stub │ ├── observer.stub │ └── repository.stub └── LaravelCommandServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.idea -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All Notable changes to `Laravel Command` will be documented in this file 3 | 4 | ## 1.0.1 - 2018-04-19 5 | 6 | ### Update 7 | - rename the package command, update command form name laravel: to make: 8 | 9 | ## 1.0.0 - 2018-03-06 10 | 11 | ### Added 12 | - Generators for files: laravel:helper, laravel:repository and laravel:observer -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-command 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/mckenziearts/laravel-command/version)](https://packagist.org/packages/mckenziearts/laravel-command) 4 | [![License](https://poser.pugx.org/mckenziearts/laravel-command/license)](https://packagist.org/packages/mckenziearts/laravel-command) 5 | [![Build Status](https://scrutinizer-ci.com/g/Mckenziearts/laravel-command/badges/build.png?b=master)](https://scrutinizer-ci.com/g/Mckenziearts/laravel-command/build-status) 6 | [![Total Downloads](https://poser.pugx.org/mckenziearts/laravel-command/downloads)](https://packagist.org/packages/mckenziearts/laravel-command) 7 | 8 | 9 | Simple package to quickly generate Laravel templated Repository, Helpers and Observer files. 10 | 11 | ## Install 12 | 13 | Via Composer 14 | 15 | ``` bash 16 | $ composer require mckenziearts/laravel-command --dev 17 | ``` 18 | 19 | For Laravel 5.5 - you're done. 20 | 21 | 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: 22 | 23 | ```php 24 | public function register() 25 | { 26 | if ($this->app->environment() == 'local') { 27 | $this->app->register('Mckenziearts\LaravelCommand\LaravelCommandServiceProvider'); 28 | } 29 | } 30 | ``` 31 | 32 | ## Usage 33 | 34 | 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 35 | 36 | Open the console and enter this command to generate a new repository : 37 | 38 | ```shell 39 | php artisan make:repository {Entity} 40 | ``` 41 | 42 | The generate file look like this : 43 | 44 | ```php 45 | namespace App\Repositories; 46 | 47 | use App\Models\Entity; 48 | 49 | class EntityRepository 50 | { 51 | /** 52 | * @var Entity 53 | */ 54 | private $model; 55 | 56 | /** 57 | * EntityRepository constructor. 58 | * @param Entity $model 59 | */ 60 | public function __construct(Entity $model) 61 | { 62 | $this->model = $model; 63 | } 64 | 65 | /** 66 | * Return a new instance of Entity Model 67 | * 68 | * @return Entity 69 | */ 70 | public function newInstance() 71 | { 72 | return $this->model->newInstance(); 73 | } 74 | } 75 | ``` 76 | 77 | 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 : 78 | 79 | ```php 80 | use MODELS\NAMESPACE\Entity; 81 | ``` 82 | 83 | 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: 84 | 85 | ```shell 86 | php artisan make:observer {Entity} 87 | ``` 88 | 89 | The generate file look like this : 90 | 91 | ```php 92 | namespace App\Observers; 93 | 94 | use App\Models\Entity; 95 | 96 | class EntityObserver 97 | { 98 | /** 99 | * Trigger Before Create a Entity 100 | * 101 | * @param Entity $model 102 | */ 103 | public function creating(Entity $model){} 104 | 105 | /** 106 | * Trigger after create a Entity 107 | * 108 | * @param Entity $model 109 | */ 110 | public function created(Entity $model){} 111 | 112 | /** 113 | * Trigger before update a Entity 114 | * 115 | * @param Entity $model 116 | */ 117 | public function updating(Entity $model){} 118 | 119 | ... 120 | } 121 | 122 | ``` 123 | 124 | - Helper files 125 | 126 | ``` bash 127 | $ php artisan make:helper {Entity} 128 | ``` 129 | 130 | The generate file look like this : 131 | 132 | ```php 133 | namespace App\Helpers; 134 | 135 | class EntityHelper 136 | { 137 | 138 | } 139 | ``` 140 | 141 | If you need better distribut your code, you can create a helper to put a logic to lighten your controllers. 142 | 143 | An example of a helper that I often use in my projects 144 | 145 | ```php 146 | namespace App\Helpers; 147 | 148 | use Intervention\Image\Facades\Image; 149 | 150 | class MediaHelper 151 | { 152 | /** 153 | * @protected 154 | * 155 | * @var string $dir, the file uploaded path 156 | */ 157 | protected static $dir = 'uploads'; 158 | 159 | /** 160 | * @return string 161 | */ 162 | public static function getUploadsFolder() 163 | { 164 | return self::$dir; 165 | } 166 | 167 | /** 168 | * Return the size of an image 169 | * 170 | * @param string $file 171 | * @param string $folder 172 | * @return array $width and $height of the file give in parameter 173 | */ 174 | public static function getFileSizes(string $file, string $folder = null) 175 | { 176 | if ($folder) { 177 | list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'. $folder .'/'.$file)); 178 | } 179 | list($width, $height, $type, $attr) = getimagesize(public_path(self::$dir.'/'.$file)); 180 | 181 | return [ 182 | 'width' => $width, 183 | 'height' => $height 184 | ]; 185 | } 186 | 187 | /** 188 | * resize, To rezise and image 189 | * 190 | * @param string $file file to rezise 191 | * @param int $width width of the file 192 | * @param int $height height of the file 193 | * @param string $filepath path to save file 194 | */ 195 | public static function resize($file, $width, $height, $filepath) 196 | { 197 | Image::make($file)->resize($width, $height)->save($filepath); 198 | } 199 | 200 | /** 201 | * getImageWeight 202 | * 203 | * @param $octets 204 | * @return string 205 | */ 206 | public static function getImageWeight($octets) { 207 | $resultat = $octets; 208 | for ($i = 0; $i < 8 && $resultat >= 1024; $i++) { 209 | $resultat = $resultat / 1024; 210 | } 211 | if ($i > 0) { 212 | return preg_replace('/,00$/', '', number_format($resultat, 2, ',', '')) 213 | . ' ' . substr('KMGTPEZY', $i-1, 1) . 'o'; 214 | } else { 215 | return $resultat . ' o'; 216 | } 217 | } 218 | 219 | } 220 | 221 | ``` 222 | 223 | ## Change log 224 | 225 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 226 | 227 | ## License 228 | 229 | The MIT License (MIT). -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mckenziearts/laravel-command", 3 | "version": "1.0.1", 4 | "description": "A simple Laravel package to provide artisan new commands", 5 | "type":"laravel-package", 6 | "keywords": [ 7 | "generators", 8 | "laravel", 9 | "artisan-cli", 10 | "observer", 11 | "helper", 12 | "repository" 13 | ], 14 | "homepage": "https://github.com/Mckenziearts/laravel-command", 15 | "license": "MIT", 16 | "authors": [ 17 | { 18 | "name": "Arthur Monney", 19 | "email": "monneylobe@gmail.com", 20 | "homepage": "http://www.twitter.com/mckenziearts", 21 | "role": "Developer" 22 | } 23 | ], 24 | "require": { 25 | "illuminate/support": ">=5.1", 26 | "illuminate/console": ">=5.1", 27 | "php" : ">=5.5.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Mckenziearts\\LaravelCommand\\": "src" 32 | } 33 | }, 34 | "minimum-stability": "dev", 35 | "prefer-stable": true, 36 | "extra": { 37 | "branch-alias": { 38 | "dev-master": "1.0.x-dev" 39 | }, 40 | "laravel": { 41 | "providers": [ 42 | "Mckenziearts\\LaravelCommand\\LaravelCommandServiceProvider" 43 | ] 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Console/Commands/LaravelHelperCommand.php: -------------------------------------------------------------------------------- 1 | laravel->getNamespace(), '', $name); 57 | return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Helper.php'; 58 | } 59 | 60 | /** 61 | * Get the default namespace for the class. 62 | * 63 | * @param string $rootNamespace 64 | * 65 | * @return string 66 | */ 67 | protected function getDefaultNamespace($rootNamespace) 68 | { 69 | return $rootNamespace.'\Helpers'; 70 | } 71 | 72 | /** 73 | * Replace the table name for the given stub. 74 | * 75 | * @param string $stub 76 | * @param string $name 77 | * 78 | * @return string 79 | */ 80 | protected function replaceNameStrings(&$stub, $name) 81 | { 82 | $table = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'); 83 | $stub = str_replace('DummyTable', $table, $stub); 84 | $stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub); 85 | return $this; 86 | } 87 | 88 | /** 89 | * Build the class with the given name. 90 | * 91 | * @param string $name 92 | * @return string 93 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 94 | */ 95 | protected function buildClass($name) 96 | { 97 | $stub = $this->files->get($this->getStub()); 98 | return $this->replaceNamespace($stub, $name)->replaceNameStrings($stub, $name)->replaceClass($stub, $name); 99 | } 100 | 101 | /** 102 | * Get the console command options. 103 | * 104 | * @return array 105 | */ 106 | protected function getOptions() 107 | { 108 | return [ 109 | 110 | ]; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/Console/Commands/LaravelObserverCommand.php: -------------------------------------------------------------------------------- 1 | laravel->getNamespace(), '', $name); 57 | return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Observer.php'; 58 | } 59 | 60 | /** 61 | * Get the default namespace for the class. 62 | * 63 | * @param string $rootNamespace 64 | * 65 | * @return string 66 | */ 67 | protected function getDefaultNamespace($rootNamespace) 68 | { 69 | return $rootNamespace.'\Observers'; 70 | } 71 | 72 | /** 73 | * Replace the table name for the given stub. 74 | * 75 | * @param string $stub 76 | * @param string $name 77 | * 78 | * @return string 79 | */ 80 | protected function replaceNameStrings(&$stub, $name) 81 | { 82 | $table = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'); 83 | $stub = str_replace('DummyTable', $table, $stub); 84 | $stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub); 85 | return $this; 86 | } 87 | 88 | /** 89 | * Build the class with the given name. 90 | * 91 | * @param string $name 92 | * @return string 93 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 94 | */ 95 | protected function buildClass($name) 96 | { 97 | $stub = $this->files->get($this->getStub()); 98 | return $this->replaceNamespace($stub, $name)->replaceNameStrings($stub, $name)->replaceClass($stub, $name); 99 | } 100 | 101 | /** 102 | * Get the console command options. 103 | * 104 | * @return array 105 | */ 106 | protected function getOptions() 107 | { 108 | return [ 109 | 110 | ]; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/Console/Commands/LaravelRepositoryCommand.php: -------------------------------------------------------------------------------- 1 | laravel->getNamespace(), '', $name); 57 | return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Repository.php'; 58 | } 59 | 60 | /** 61 | * Get the default namespace for the class. 62 | * 63 | * @param string $rootNamespace 64 | * 65 | * @return string 66 | */ 67 | protected function getDefaultNamespace($rootNamespace) 68 | { 69 | return $rootNamespace.'\Repositories'; 70 | } 71 | 72 | /** 73 | * Replace the table name for the given stub. 74 | * 75 | * @param string $stub 76 | * @param string $name 77 | * 78 | * @return string 79 | */ 80 | protected function replaceNameStrings(&$stub, $name) 81 | { 82 | $table = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'); 83 | $stub = str_replace('DummyTable', $table, $stub); 84 | $stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub); 85 | return $this; 86 | } 87 | 88 | /** 89 | * Build the class with the given name. 90 | * 91 | * @param string $name 92 | * @return string 93 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException 94 | */ 95 | protected function buildClass($name) 96 | { 97 | $stub = $this->files->get($this->getStub()); 98 | return $this->replaceNamespace($stub, $name)->replaceNameStrings($stub, $name)->replaceClass($stub, $name); 99 | } 100 | 101 | /** 102 | * Get the console command options. 103 | * 104 | * @return array 105 | */ 106 | protected function getOptions() 107 | { 108 | return [ 109 | 110 | ]; 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/Console/Stubs/helper.stub: -------------------------------------------------------------------------------- 1 | 5 | * @link https://github.com/Mckenziearts/laravel-command 6 | */ 7 | 8 | namespace DummyNamespace; 9 | 10 | class DummyClassHelper 11 | { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/Console/Stubs/observer.stub: -------------------------------------------------------------------------------- 1 | 5 | * @link https://github.com/Mckenziearts/laravel-command 6 | */ 7 | 8 | namespace DummyNamespace; 9 | 10 | use App\DummyClass; 11 | 12 | class DummyClassObserver 13 | { 14 | /** 15 | * Trigger Before Create a DummyClass 16 | * 17 | * @param DummyClass $model 18 | */ 19 | public function creating(DummyClass $model){} 20 | 21 | /** 22 | * Trigger after create a DummyClass 23 | * 24 | * @param DummyClass $model 25 | */ 26 | public function created(DummyClass $model){} 27 | 28 | /** 29 | * Trigger before update a DummyClass 30 | * 31 | * @param DummyClass $model 32 | */ 33 | public function updating(DummyClass $model){} 34 | 35 | /** 36 | * Trigger after update a DummyClass 37 | * 38 | * @param DummyClass $model 39 | */ 40 | public function updated(DummyClass $model){} 41 | 42 | /** 43 | * Trigger before delete a DummyClass 44 | * 45 | * @param DummyClass $model 46 | */ 47 | public function deleting(DummyClass $model){} 48 | 49 | /** 50 | * Trigger after delete a DummyClass 51 | * 52 | * @param DummyClass $model 53 | */ 54 | public function deleted(DummyClass $model){} 55 | } 56 | -------------------------------------------------------------------------------- /src/Console/Stubs/repository.stub: -------------------------------------------------------------------------------- 1 | 5 | * @link https://github.com/Mckenziearts/laravel-command 6 | */ 7 | 8 | namespace DummyNamespace; 9 | 10 | use App\Models\DummyClass; 11 | 12 | class DummyClassRepository 13 | { 14 | /** 15 | * @var DummyClass 16 | */ 17 | private $model; 18 | 19 | /** 20 | * DummyClassRepository constructor. 21 | * @param DummyClass $model 22 | */ 23 | public function __construct(DummyClass $model) 24 | { 25 | $this->model = $model; 26 | } 27 | 28 | /** 29 | * Return a new instance of DummyClass Model 30 | * 31 | * @return DummyClass 32 | */ 33 | public function newInstance() 34 | { 35 | return $this->model->newInstance(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/LaravelCommandServiceProvider.php: -------------------------------------------------------------------------------- 1 | commands($this->commands); 27 | } 28 | } 29 | --------------------------------------------------------------------------------