Skip to content

Commit

Permalink
Merge pull request #5 from ijpatricio/dev-artisan-make
Browse files Browse the repository at this point in the history
Add make: command
  • Loading branch information
freekmurze committed Sep 26, 2018
2 parents 90fee7f + 50f021a commit b987a23
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ build
composer.lock
docs
vendor
coverage
coverage
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ You can install the package via composer:
composer require spatie/laravel-view-models
```

For Laravel versions 5.5+, the ServiceProvider will be automatically discovered.
Otherwise, add to `config/app.php`, under `providers` key:

```php
'providers' => [
(...)
/*
* Package Service Providers...
*/
\Spatie\ViewModels\Providers\ViewModelsServiceProvider::class,
```

To generate a new ViewModel, into `App\ViewModels` namespace:

```bash
php artisan make:view-model HomepageViewModel
```

or into a custom namespace, say, `App\Blog`

```bash
php artisan make:view-model Blog/PostsViewModel
```

## Usage

A view model is a class where you can put some complex logic for your views. This will make your controllers a bit lighter. You can create a view model by extending the provided `Spatie\ViewModels\ViewModel`.
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
"Spatie\\ViewModels\\Tests\\": "tests"
}
},
"extra": {
"laravel": {
"providers": [
"Spatie\\ViewModels\\Providers\\ViewModelsServiceProvider"
]
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
Expand Down
51 changes: 51 additions & 0 deletions src/Console/ViewModelMakeCommand.php
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'), '/');
}
}
18 changes: 18 additions & 0 deletions src/Providers/ViewModelsServiceProvider.php
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,
]);
}
}
}
13 changes: 13 additions & 0 deletions stubs/DummyViewModel.stub
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()
{
//
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpFoundation\Response;
use Orchestra\Testbench\TestCase as OrchestraTestCase;
use Spatie\ViewModels\Providers\ViewModelsServiceProvider;

class TestCase extends OrchestraTestCase
{
Expand All @@ -31,4 +32,9 @@ protected function getResponseBody(Response $response): array
{
return json_decode($response->getContent(), true);
}

protected function getPackageProviders($app)
{
return [ViewModelsServiceProvider::class];
}
}
55 changes: 55 additions & 0 deletions tests/ViewModelMakeCommandTest.php
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);
}
}

0 comments on commit b987a23

Please sign in to comment.