Skip to content

Commit a80cfd3

Browse files
committed
Add improved stubs
1 parent 9b5b3b2 commit a80cfd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1880
-16
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
# Laravel stubs
22

3-
Improved Laravel stubs with a command to keep them in sync
3+
Improved Laravel stubs.
44

5+
## Usage
6+
### Installation
7+
```bash
8+
composer require --dev recoded-dev/laravel-stubs
9+
```
10+
11+
### Publishing stubs
12+
```bash
13+
php artisan stubs:publish
14+
```
15+
That's it! This packages handles everything else.
16+
17+
## Contribution
18+
Development to this package requires tests, static analysis
19+
and conforming to the coding-standard.
20+
21+
To validate these locally run the following with dev dependencies installed:
22+
```bash
23+
vendor/bin/phpunit && vendor/bin/phpstan && vendor/bin/phpcs
24+
```

composer.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,12 @@
4242
"dealerdirect/phpcodesniffer-composer-installer": true
4343
},
4444
"sort-packages": true
45+
},
46+
"extra": {
47+
"laravel": {
48+
"providers": [
49+
"Recoded\\LaravelStubs\\Providers\\StubsServiceProvider"
50+
]
51+
}
4552
}
4653
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Recoded\LaravelStubs\Listeners;
4+
5+
use Illuminate\Foundation\Events\PublishingStubs;
6+
use Illuminate\Support\Collection;
7+
8+
final class OverwriteStubsListener
9+
{
10+
/**
11+
* Handle the event.
12+
*
13+
* @param \Illuminate\Foundation\Events\PublishingStubs $event
14+
* @return void
15+
*/
16+
public function handle(PublishingStubs $event): void
17+
{
18+
$event->stubs = Collection::make($event->stubs)->mapWithKeys(function (string $filename, string $path) {
19+
$localPath = realpath(sprintf('%s/../../stubs/%s', __DIR__, $filename));
20+
$path = $localPath !== false ? $localPath : $path;
21+
22+
return [$path => $filename];
23+
})->toArray();
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Recoded\LaravelStubs\Providers;
4+
5+
use Illuminate\Contracts\Events\Dispatcher;
6+
use Illuminate\Foundation\Events\PublishingStubs;
7+
use Illuminate\Support\ServiceProvider;
8+
use Recoded\LaravelStubs\Listeners\OverwriteStubsListener;
9+
10+
final class StubsServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Bootstrap the application events.
14+
*
15+
* @param \Illuminate\Contracts\Events\Dispatcher $dispatcher
16+
* @return void
17+
*/
18+
public function boot(Dispatcher $dispatcher): void
19+
{
20+
$dispatcher->listen(PublishingStubs::class, OverwriteStubsListener::class);
21+
}
22+
}

stubs/cast.inbound.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Contracts\Database\Eloquent\CastsInboundAttributes;
7+
8+
class {{ class }} implements CastsInboundAttributes
9+
{
10+
/**
11+
* Prepare the given value for storage.
12+
*
13+
* @param \Illuminate\Database\Eloquent\Model $model
14+
* @param string $key
15+
* @param mixed $value
16+
* @param array<string, mixed> $attributes
17+
* @return mixed
18+
*/
19+
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
20+
{
21+
return $value;
22+
}
23+
}

stubs/cast.stub

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
7+
8+
/**
9+
* @implements \Illuminate\Contracts\Database\Eloquent\CastsAttributes<x, y>
10+
*/
11+
class {{ class }} implements CastsAttributes
12+
{
13+
/**
14+
* Cast the given value.
15+
*
16+
* @param \Illuminate\Database\Eloquent\Model $model
17+
* @param string $key
18+
* @param mixed $value
19+
* @param array<string, mixed> $attributes
20+
* @return mixed
21+
*/
22+
public function get(Model $model, string $key, mixed $value, array $attributes): mixed
23+
{
24+
return $value;
25+
}
26+
27+
/**
28+
* Prepare the given value for storage.
29+
*
30+
* @param \Illuminate\Database\Eloquent\Model $model
31+
* @param string $key
32+
* @param mixed $value
33+
* @param array<string, mixed> $attributes
34+
* @return mixed
35+
*/
36+
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
37+
{
38+
return $value;
39+
}
40+
}

stubs/console.stub

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Console\Command;
6+
7+
class {{ class }} extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = '{{ command }}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Command description';
22+
23+
/**
24+
* Execute the console command.
25+
*
26+
* @return int
27+
*/
28+
public function handle(): int
29+
{
30+
//
31+
}
32+
}

stubs/controller.api.stub

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
8+
use Illuminate\Http\Resources\Json\JsonResource;
9+
use Illuminate\Http\Response;
10+
11+
class {{ class }}
12+
{
13+
use AuthorizesRequests;
14+
15+
/**
16+
* Display a listing of the resource.
17+
*
18+
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
19+
*/
20+
public function index(): AnonymousResourceCollection
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Store a newly created resource in storage.
27+
*
28+
* @param \Illuminate\Http\Request $request
29+
* @return \Illuminate\Http\Resources\Json\JsonResource
30+
*/
31+
public function store(Request $request): JsonResource
32+
{
33+
//
34+
}
35+
36+
/**
37+
* Display the specified resource.
38+
*
39+
* @param string $id
40+
* @return \Illuminate\Http\Resources\Json\JsonResource
41+
*/
42+
public function show(string $id): JsonResource
43+
{
44+
//
45+
}
46+
47+
/**
48+
* Update the specified resource in storage.
49+
*
50+
* @param \Illuminate\Http\Request $request
51+
* @param string $id
52+
* @return \Illuminate\Http\Resources\Json\JsonResource
53+
*/
54+
public function update(Request $request, string $id): JsonResource
55+
{
56+
//
57+
}
58+
59+
/**
60+
* Remove the specified resource from storage.
61+
*
62+
* @param string $id
63+
* @return \Illuminate\Http\Response
64+
*/
65+
public function destroy(string $id): Response
66+
{
67+
return response()->noContent();
68+
}
69+
}

stubs/controller.invokable.stub

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Response;
8+
9+
class {{ class }}
10+
{
11+
use AuthorizesRequests;
12+
13+
/**
14+
* Handle the incoming request.
15+
*
16+
* @param \Illuminate\Http\Request $request
17+
* @return \Illuminate\Http\Response
18+
*/
19+
public function __invoke(Request $request): Response
20+
{
21+
//
22+
}
23+
}

stubs/controller.model.api.stub

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use {{ namespacedModel }};
6+
use {{ namespacedRequests }}
7+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
8+
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
9+
use Illuminate\Http\Resources\Json\JsonResource;
10+
use Illuminate\Http\Response;
11+
12+
class {{ class }}
13+
{
14+
use AuthorizesRequests;
15+
16+
/**
17+
* Display a listing of the resource.
18+
*
19+
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
20+
*/
21+
public function index(): AnonymousResourceCollection
22+
{
23+
//
24+
}
25+
26+
/**
27+
* Store a newly created resource in storage.
28+
*
29+
* @param \{{ namespacedStoreRequest }} $request
30+
* @return \Illuminate\Http\Resources\Json\JsonResource
31+
*/
32+
public function store({{ storeRequest }} $request): JsonResource
33+
{
34+
//
35+
}
36+
37+
/**
38+
* Display the specified resource
39+
*
40+
* @param \{{ namespacedModel }} ${{ modelVariable }}
41+
* @return \Illuminate\Http\Resources\Json\JsonResource
42+
*/
43+
public function show({{ model }} ${{ modelVariable }}): JsonResource
44+
{
45+
//
46+
}
47+
48+
/**
49+
* Update the specified resource in storage.
50+
*
51+
* @param \{{ namespacedUpdateRequest }} $request
52+
* @param \{{ namespacedModel }} ${{ modelVariable }}
53+
* @return \Illuminate\Http\Resources\Json\JsonResource
54+
*/
55+
public function update({{ updateRequest }} $request, {{ model }} ${{ modelVariable }}): JsonResource
56+
{
57+
//
58+
}
59+
60+
/**
61+
* Remove the specified resource from storage.
62+
*
63+
* @param \{{ namespacedModel }} ${{ modelVariable }}
64+
* @return \Illuminate\Http\Response
65+
*/
66+
public function destroy({{ model }} ${{ modelVariable }}): Response
67+
{
68+
return response()->noContent();
69+
}
70+
}

0 commit comments

Comments
 (0)