-
-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1802 from solomon-ochepa/module-make-class-command
Create module:make-class command
- Loading branch information
Showing
11 changed files
with
274 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ coverage | |
.phpunit.result.cache | ||
.idea | ||
.php-cs-fixer.cache | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
namespace Nwidart\Modules\Commands\Make; | ||
|
||
use Illuminate\Support\Str; | ||
use Nwidart\Modules\Support\Config\GenerateConfigReader; | ||
use Nwidart\Modules\Support\Stub; | ||
use Nwidart\Modules\Traits\ModuleCommandTrait; | ||
|
||
class ClassMakeCommand extends GeneratorCommand | ||
{ | ||
use ModuleCommandTrait; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
*/ | ||
protected $signature = 'module:make-class | ||
{--t|type=class : The type of class, e.g. class, service, repository, contract, etc.} | ||
{--s|suffix : Create the class without the type suffix} | ||
{--i|invokable : Generate a single method, invokable class} | ||
{--f|force : Create the class even if the class already exists} | ||
{name : The name of the class} | ||
{module : The targeted module}'; | ||
|
||
/** | ||
* The console command description. | ||
*/ | ||
protected $description = 'Create a new class'; | ||
|
||
protected $argumentName = 'name'; | ||
|
||
public function getTemplateContents(): string | ||
{ | ||
return (new Stub($this->stub(), [ | ||
'NAMESPACE' => $this->getClassNamespace($this->module()), | ||
'CLASS' => $this->typeClass(), | ||
]))->render(); | ||
} | ||
|
||
public function stub(): string | ||
{ | ||
return $this->option('invokable') ? '/class-invoke.stub' : '/class.stub'; | ||
} | ||
|
||
public function getDestinationFilePath(): string | ||
{ | ||
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); | ||
|
||
$filePath = GenerateConfigReader::read('class')->getPath() ?? config('modules.paths.app_folder') . 'Classes'; | ||
|
||
return $this->typePath($path . $filePath . '/' . $this->getFileName() . '.php'); | ||
} | ||
|
||
protected function getFileName(): string | ||
{ | ||
$file = Str::studly($this->argument('name')); | ||
|
||
if ($this->option('suffix') === true) { | ||
$names = [Str::plural($this->type()), Str::singular($this->type())]; | ||
$file = Str::of($file)->remove($names, false); | ||
$file .= Str::of($this->type())->studly(); | ||
} | ||
|
||
return $file; | ||
} | ||
|
||
/** | ||
* Get the type of class e.g. class, service, repository, etc. | ||
*/ | ||
protected function type(): string | ||
{ | ||
return Str::of($this->option('type'))->remove('=')->singular(); | ||
} | ||
|
||
protected function typePath(string $path): string | ||
{ | ||
return ($this->type() === 'class') ? $path : Str::of($path)->replaceLast('Classes', Str::of($this->type())->plural()->studly()); | ||
} | ||
|
||
public function typeClass(): string | ||
{ | ||
return Str::of($this->getFileName())->basename()->studly(); | ||
} | ||
|
||
public function getDefaultNamespace(): string | ||
{ | ||
$type = $this->type(); | ||
|
||
return config("modules.paths.generator.{$type}.namespace", 'Classes'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace $NAMESPACE$; | ||
|
||
class $CLASS$ | ||
{ | ||
public function __invoke() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace $NAMESPACE$; | ||
|
||
class $CLASS$ | ||
{ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace Nwidart\Modules\Tests\Commands; | ||
|
||
use Nwidart\Modules\Contracts\RepositoryInterface; | ||
use Nwidart\Modules\Tests\BaseTestCase; | ||
use Spatie\Snapshots\MatchesSnapshots; | ||
|
||
class ClassMakeCommandTest extends BaseTestCase | ||
{ | ||
use MatchesSnapshots; | ||
|
||
/** | ||
* @var \Illuminate\Filesystem\Filesystem | ||
*/ | ||
private $finder; | ||
/** | ||
* @var string | ||
*/ | ||
private $modulePath; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->finder = $this->app['files']; | ||
$this->createModule(); | ||
$this->modulePath = $this->getModuleAppPath(); | ||
|
||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->app[RepositoryInterface::class]->delete('Blog'); | ||
parent::tearDown(); | ||
} | ||
|
||
public function test_it_generates_a_new_class() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog']); | ||
|
||
$this->assertTrue(is_file($this->modulePath . '/Classes/Demo.php')); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_generates_a_new_class_can_override_with_force_option() | ||
{ | ||
$this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog']); | ||
$code = $this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog', '--force' => true]); | ||
|
||
$this->assertTrue(is_file($this->modulePath . '/Classes/Demo.php')); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_generates_a_new_class_can_use_invoke_option() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog', '--invokable' => true]); | ||
|
||
$this->assertTrue(is_file($this->modulePath . '/Classes/Demo.php')); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_generates_a_new_class_can_use_suffix_option() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog', '--suffix' => true]); | ||
|
||
$this->assertTrue(is_file($this->modulePath . '/Classes/DemoClass.php')); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_generates_a_new_class_use_type_option() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog', '--type' => 'contract']); | ||
|
||
$this->assertTrue(is_file($this->modulePath . '/Contracts/Demo.php')); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_generated_correct_file_with_content() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Demo', 'module' => 'Blog']); | ||
|
||
$file = $this->finder->get($this->modulePath . '/Classes/Demo.php'); | ||
|
||
$this->assertMatchesSnapshot($file); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_can_generate_a_class_in_sub_namespace_in_correct_folder() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Api\\Demo', 'module' => 'Blog']); | ||
|
||
$this->assertTrue(is_file($this->modulePath . '/Classes/Api/Demo.php')); | ||
$this->assertSame(0, $code); | ||
} | ||
|
||
public function test_it_can_generate_a_class_in_sub_namespace_with_correct_generated_file() | ||
{ | ||
$code = $this->artisan('module:make-class', ['name' => 'Api\\Demo', 'module' => 'Blog']); | ||
|
||
$file = $this->finder->get($this->modulePath . '/Classes/Api/Demo.php'); | ||
|
||
$this->assertMatchesSnapshot($file); | ||
$this->assertSame(0, $code); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...andTest__test_it_can_generate_a_class_in_sub_namespace_with_correct_generated_file__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Modules\Blog\Classes\Api; | ||
|
||
class Api\Demo | ||
{ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ds/__snapshots__/ClassMakeCommandTest__test_it_generated_correct_file_with_content__1.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Modules\Blog\Classes; | ||
|
||
class Demo | ||
{ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
} |