├── .editorconfig ├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Console │ ├── MakeClassCommand.php │ └── stubs │ │ ├── class.constructor.stub │ │ └── class.stub └── MakeClassServiceProvider.php └── tests └── MakeClassTest.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = false 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | .DS_Store 4 | Thumbs.db 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Hisman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel make:class 2 | 3 | Artisan command for generating a new custom class in Laravel. 4 | 5 | ## Installation 6 | 7 | Install the package via composer: 8 | ``` 9 | composer require --dev hisman/laravel-make-class 10 | ``` 11 | 12 | If you're using Laravel < 5.5, you'll need to add the service provider to `config/app.php` file: 13 | 14 | ```php 15 | 'providers' => [ 16 | ... 17 | 18 | Hisman\MakeClass\MakeClassServiceProvider::class, 19 | 20 | ... 21 | ] 22 | ``` 23 | 24 | ## Usage 25 | To create a new class, call the `make:class` command from Artisan. Class will be created under the `app` folder. 26 | ``` 27 | php artisan make:class ClassName 28 | ``` 29 | You can add the `-c` or `--constructor` option to generate new class with constructor. 30 | ``` 31 | php artisan make:class ClassName -c 32 | ``` 33 | ``` 34 | php artisan make:class ClassName --constructor 35 | ``` 36 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hisman/laravel-make-class", 3 | "description": "Artisan command for generating a new custom class in Laravel.", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Hisman", 8 | "homepage": "https://hisman.co", 9 | "role": "Developer" 10 | } 11 | ], 12 | "autoload": { 13 | "psr-4": { 14 | "Hisman\\MakeClass\\": "src/" 15 | } 16 | }, 17 | "autoload-dev": { 18 | "psr-4": { 19 | "Hisman\\MakeClass\\Tests\\": "tests/" 20 | } 21 | }, 22 | "require": {}, 23 | "require-dev": { 24 | "phpunit/phpunit": "^7.4", 25 | "orchestra/testbench": "^3.7", 26 | "mockery/mockery": "^1.2" 27 | }, 28 | "extra": { 29 | "laravel": { 30 | "providers": [ 31 | "Hisman\\MakeClass\\MakeClassServiceProvider" 32 | ] 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/Console/MakeClassCommand.php: -------------------------------------------------------------------------------- 1 | option('constructor')) { 41 | $stub = '/stubs/class.constructor.stub'; 42 | } 43 | 44 | return __DIR__.$stub; 45 | } 46 | 47 | /** 48 | * Get the default namespace for the class. 49 | * 50 | * @param string $rootNamespace 51 | * @return string 52 | */ 53 | protected function getDefaultNamespace($rootNamespace) 54 | { 55 | return $rootNamespace; 56 | } 57 | 58 | /** 59 | * Get the console command options. 60 | * 61 | * @return array 62 | */ 63 | protected function getOptions() 64 | { 65 | return [ 66 | ['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the class already exists'], 67 | ['constructor', 'c', InputOption::VALUE_NONE, 'Create a new class with constructor'], 68 | ]; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/Console/stubs/class.constructor.stub: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 18 | $this->commands([MakeClassCommand::class]); 19 | } 20 | } 21 | 22 | /** 23 | * Register bindings in the container. 24 | * 25 | * @return void 26 | */ 27 | public function register() 28 | { 29 | // 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/MakeClassTest.php: -------------------------------------------------------------------------------- 1 | files = new Filesystem(); 50 | $this->classname = 'TestClass'; 51 | $this->filepath = $this->app['path'].'/'.$this->classname.'.php'; 52 | } 53 | 54 | /** 55 | * Test for creating a new class from artisan command. 56 | * 57 | * @test 58 | */ 59 | public function testCreateClass() 60 | { 61 | $this->artisan('make:class', ['name' => $this->classname, '--force' => true]) 62 | ->expectsOutput('Class created successfully.'); 63 | 64 | $class_content = $this->files->get($this->filepath); 65 | 66 | $this->assertTrue($this->files->exists($this->filepath)); 67 | $this->assertContains('class '.$this->classname, $class_content); 68 | $this->assertNotContains('__construct', $class_content); 69 | } 70 | 71 | /** 72 | * Test for creating a new class with constructor from artisan command. 73 | * 74 | * @test 75 | */ 76 | public function testCreateClassWithConstructor() 77 | { 78 | $this->artisan('make:class', ['name' => $this->classname, '--force' => true, '--constructor' => true]) 79 | ->expectsOutput('Class created successfully.'); 80 | 81 | $class_content = $this->files->get($this->filepath); 82 | 83 | $this->assertTrue($this->files->exists($this->filepath)); 84 | $this->assertContains('class '.$this->classname, $class_content); 85 | $this->assertContains('__construct', $class_content); 86 | } 87 | 88 | /** 89 | * Test for creating a new class in subfolder from artisan command. 90 | * 91 | * @test 92 | */ 93 | public function testCreateClassInSubfolder() 94 | { 95 | $subfolder = 'Subfolder'; 96 | $filepath = $this->app['path'].'/'.$subfolder.'/'.$this->classname.'.php'; 97 | 98 | $this->artisan('make:class', ['name' => $subfolder.'\\'.$this->classname, '--force' => true]) 99 | ->expectsOutput('Class created successfully.'); 100 | 101 | $class_content = $this->files->get($filepath); 102 | 103 | $this->assertTrue($this->files->exists($filepath)); 104 | $this->assertContains('class '.$this->classname, $class_content); 105 | $this->assertContains('namespace App\\'.$subfolder, $class_content); 106 | } 107 | } 108 | --------------------------------------------------------------------------------