-
-
Notifications
You must be signed in to change notification settings - Fork 61
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 #5 from ijpatricio/dev-artisan-make
Add make: command
- Loading branch information
Showing
8 changed files
with
175 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -2,4 +2,4 @@ build | |
composer.lock | ||
docs | ||
vendor | ||
coverage | ||
coverage |
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,51 @@ | ||
<?php | ||
|
||
namespace Spatie\ViewModels\Console; | ||
|
||
use Illuminate\Support\Str; | ||
use Illuminate\Console\GeneratorCommand; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class ViewModelMakeCommand extends GeneratorCommand | ||
{ | ||
protected $name = 'make:view-model'; | ||
|
||
protected $description = 'Create a new ViewModel class'; | ||
|
||
protected $type = 'ViewModel'; | ||
|
||
public function handle() | ||
{ | ||
if (parent::handle() === false) { | ||
if (! $this->option('force')) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
protected function getStub() | ||
{ | ||
return __DIR__.'/../../stubs/DummyViewModel.stub'; | ||
} | ||
|
||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
if ($this->isCustomNamespace()) { | ||
return $rootNamespace; | ||
} | ||
|
||
return $rootNamespace.'\ViewModels'; | ||
} | ||
|
||
protected function getOptions(): array | ||
{ | ||
return [ | ||
['force', null, InputOption::VALUE_NONE, 'Create the class even if the view-model already exists'], | ||
]; | ||
} | ||
|
||
protected function isCustomNamespace(): bool | ||
{ | ||
return Str::contains($this->argument('name'), '/'); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Spatie\ViewModels\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use Spatie\ViewModels\Console\ViewModelMakeCommand; | ||
|
||
class ViewModelsServiceProvider extends ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
$this->commands([ | ||
ViewModelMakeCommand::class, | ||
]); | ||
} | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace DummyNamespace; | ||
|
||
use Spatie\ViewModels\ViewModel; | ||
|
||
class DummyClass extends ViewModel | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Spatie\ViewModels\Tests; | ||
|
||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Artisan; | ||
|
||
class ViewModelMakeCommandTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function creates_file_in_default_folder() | ||
{ | ||
$exitCode = Artisan::call('make:view-model', [ | ||
'name' => 'HomeViewModel', | ||
'--force' => true, | ||
]); | ||
|
||
$this->assertEquals(0, $exitCode); | ||
|
||
$this->assertContains('ViewModel created successfully.', Artisan::output()); | ||
|
||
$shouldOutputFilePath = $this->app['path'].'/ViewModels/HomeViewModel.php'; | ||
|
||
$this->assertTrue(File::exists($shouldOutputFilePath), 'File exists in default app/ViewModels folder'); | ||
|
||
$contents = File::get($shouldOutputFilePath); | ||
|
||
$this->assertContains('namespace App\ViewModels;', $contents); | ||
|
||
$this->assertContains('class HomeViewModel extends ViewModel', $contents); | ||
} | ||
|
||
/** @test */ | ||
public function creates_file_in_custom_folder() | ||
{ | ||
$exitCode = Artisan::call('make:view-model', [ | ||
'name' => 'Blog/PostsViewModel', | ||
'--force' => true, | ||
]); | ||
|
||
$this->assertEquals(0, $exitCode); | ||
|
||
$this->assertContains('ViewModel created successfully.', Artisan::output()); | ||
|
||
$shouldOutputFilePath = $this->app['path'].'/Blog/PostsViewModel.php'; | ||
|
||
$this->assertTrue(File::exists($shouldOutputFilePath), 'File exists in custom app/Blog folder'); | ||
|
||
$contents = File::get($shouldOutputFilePath); | ||
|
||
$this->assertContains('namespace App\Blog;', $contents); | ||
|
||
$this->assertContains('class PostsViewModel extends ViewModel', $contents); | ||
} | ||
} |