Skip to content

Commit

Permalink
New Feat: Generate Views together with Tests
Browse files Browse the repository at this point in the history
- Added a view generator
- Added an optional test generator for the view
- BUG FIX: Added missing stubs in Commands
- BUG FIX: Look for stubs in Commands as well as a fallback for all generators
  • Loading branch information
coolsam726 committed Apr 8, 2024
1 parent 052332f commit c8c41ed
Show file tree
Hide file tree
Showing 61 changed files with 1,266 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use function Laravel\Prompts\text;

class ActivateModuleCommand extends Command
class ModuleActivateCommand extends Command
{
public $signature = 'modular:activate {name?}';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use function Laravel\Prompts\text;

class DeactivateModuleCommand extends Command
class ModuleDeactivateCommand extends Command
{
public $signature = 'modular:deactivate {name?}';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use function Laravel\Prompts\text;

class MakeModuleCommand extends Command
class ModuleMakeCommand extends Command
{
use CanManipulateFiles;

Expand Down
41 changes: 41 additions & 0 deletions src/Commands/ViewMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Savannabits\Modular\Commands;

use Illuminate\Support\Str;
use Savannabits\Modular\Facades\Modular;
use Savannabits\Modular\Support\Concerns\GeneratesModularFiles;

class ViewMakeCommand extends \Illuminate\Foundation\Console\ViewMakeCommand
{
use GeneratesModularFiles;
protected $name = 'modular:make-view';

protected $description = 'Create a new view file in a modular package';

protected function getPath($name)
{
return $this->viewPath(
$this->getNameInput().'.'.$this->option('extension'),
);
}

protected function getTestPath(): string
{
return $this->getModule()->path(
Str::of($this->testClassFullyQualifiedName())
->replace('\\', '/')
->replaceFirst('Tests/Feature', 'tests/Feature')
->append('Test.php')
->value()
);
}

protected function getTestStub(): string
{
$stubName = 'view.'.($this->usingPest() ? 'pest' : 'test').'.stub';
$stubName = '/stubs/'.$stubName;

return parent::resolveStubPath($stubName);
}
}
8 changes: 8 additions & 0 deletions src/Commands/stubs/api-routes.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
7 changes: 7 additions & 0 deletions src/Commands/stubs/broadcasting-routes.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
19 changes: 19 additions & 0 deletions src/Commands/stubs/cast.inbound.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;

class {{ class }} implements CastsInboundAttributes
{
/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
}
29 changes: 29 additions & 0 deletions src/Commands/stubs/cast.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace {{ namespace }};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class {{ class }} implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}

/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
{
return $value;
}
}
24 changes: 24 additions & 0 deletions src/Commands/stubs/channel.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace {{ namespace }};

use {{ namespacedUserModel }};

class {{ class }}
{
/**
* Create a new channel instance.
*/
public function __construct()
{
//
}

/**
* Authenticate the user's access to the channel.
*/
public function join({{ userModel }} $user): array|bool
{
//
}
}
22 changes: 22 additions & 0 deletions src/Commands/stubs/class.invokable.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace {{ namespace }};

class {{ class }}
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}

/**
* Invoke the class instance.
*/
public function __invoke(): void
{

}
}
14 changes: 14 additions & 0 deletions src/Commands/stubs/class.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace {{ namespace }};

class {{ class }}
{
/**
* Create a new class instance.
*/
public function __construct()
{
//
}
}
30 changes: 30 additions & 0 deletions src/Commands/stubs/console.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace {{ namespace }};

use Illuminate\Console\Command;

class {{ class }} extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = '{{ command }}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
//
}
}
7 changes: 7 additions & 0 deletions src/Commands/stubs/echo-bootstrap-js.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Echo exposes an expressive API for subscribing to channels and listening
* for events that are broadcast by Laravel. Echo and event broadcasting
* allow your team to quickly build robust real-time web applications.
*/

import './echo';
14 changes: 14 additions & 0 deletions src/Commands/stubs/echo-js.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Echo from 'laravel-echo';

import Pusher from 'pusher-js';
window.Pusher = Pusher;

window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 80,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 443,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
8 changes: 8 additions & 0 deletions src/Commands/stubs/enum.backed.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {{ namespace }};

enum {{ class }}: {{ type }}
{
//
}
8 changes: 8 additions & 0 deletions src/Commands/stubs/enum.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {{ namespace }};

enum {{ class }}
{
//
}
36 changes: 36 additions & 0 deletions src/Commands/stubs/event.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace {{ namespace }};

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class {{ class }}
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
26 changes: 26 additions & 0 deletions src/Commands/stubs/exception-render-report.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace {{ namespace }};

use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class {{ class }} extends Exception
{
/**
* Report the exception.
*/
public function report(): void
{
//
}

/**
* Render the exception as an HTTP response.
*/
public function render(Request $request): Response
{
//
}
}
18 changes: 18 additions & 0 deletions src/Commands/stubs/exception-render.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace {{ namespace }};

use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class {{ class }} extends Exception
{
/**
* Render the exception as an HTTP response.
*/
public function render(Request $request): Response
{
//
}
}
16 changes: 16 additions & 0 deletions src/Commands/stubs/exception-report.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace {{ namespace }};

use Exception;

class {{ class }} extends Exception
{
/**
* Report the exception.
*/
public function report(): void
{
//
}
}
10 changes: 10 additions & 0 deletions src/Commands/stubs/exception.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace {{ namespace }};

use Exception;

class {{ class }} extends Exception
{
//
}
8 changes: 8 additions & 0 deletions src/Commands/stubs/interface.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace {{ namespace }};

interface {{ class }}
{
//
}
Loading

0 comments on commit c8c41ed

Please sign in to comment.