Skip to content

Commit e40bbda

Browse files
committed
refactor: update code snippets in documentation for clarity and consistency
1 parent 12eaf6a commit e40bbda

File tree

21 files changed

+129
-154
lines changed

21 files changed

+129
-154
lines changed
32 KB
Binary file not shown.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<template>
2+
<div :class="ui.root({ class: [props.ui?.root], filename: !!filename })">
3+
<div v-if="filename && !hideHeader" :class="ui.header({ class: props.ui?.header })">
4+
<span
5+
v-if="language === 'php'"
6+
class="inline-flex items-center justify-center h-5 px-1.5 text-xs font-bold text-blue-700 dark:text-blue-200 bg-blue-100 dark:bg-blue-900/30 rounded border border-blue-300 dark:border-blue-700"
7+
>
8+
PHP
9+
</span>
10+
<UIcon v-else :name="icon || getFileTypeIcon(filename)" :class="ui.icon({ class: props.ui?.icon })" />
11+
12+
<span :class="ui.filename({ class: props.ui?.filename })">{{ filename }}</span>
13+
</div>
14+
15+
<UButton
16+
:icon="copied ? appConfig.ui.icons.copyCheck : appConfig.ui.icons.copy"
17+
color="neutral"
18+
variant="outline"
19+
size="sm"
20+
:aria-label="copied ? 'Copied!' : 'Copy'"
21+
:label="copied ? 'Copied' : 'Copy'"
22+
:class="ui.copy({ class: props.ui?.copy })"
23+
tabindex="-1"
24+
@click="copyCode"
25+
/>
26+
27+
<pre :class="ui.base({ class: [props.ui?.base, props.class] })" v-bind="$attrs"><slot /></pre>
28+
</div>
29+
</template>
30+
31+
<script setup>
32+
import theme from "#build/ui/prose/pre";
33+
import { computed } from "vue";
34+
import { useClipboard } from "@vueuse/core";
35+
import { useAppConfig } from "#imports";
36+
import { tv } from "tailwind-variants";
37+
38+
const props = defineProps({
39+
icon: { type: [String, Object], required: false },
40+
code: { type: String, required: false },
41+
language: { type: String, required: false },
42+
filename: { type: String, required: false },
43+
hideHeader: { type: Boolean, required: false },
44+
class: { type: null, required: false },
45+
ui: { type: null, required: false }
46+
});
47+
defineSlots();
48+
49+
const { copy, copied } = useClipboard();
50+
const appConfig = useAppConfig();
51+
const ui = computed(() => tv({ extend: tv(theme), ...appConfig.ui?.prose?.pre || {} })());
52+
53+
const getFileTypeIcon = (filename) => {
54+
if (!filename) return null;
55+
56+
const cleanFilename = filename.replace(/\s*\(.*\)\s*$/, "");
57+
const extension = cleanFilename.includes(".") && cleanFilename.split(".").pop();
58+
59+
return extension ? `i-vscode-icons-file-type-${extension}` : null;
60+
};
61+
62+
const copyCode = async () => {
63+
await copy(props.code || '');
64+
};
65+
</script>

docs-v3/content/docs/api/actions.md

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ Built in CRUD operations and filtering, Restify allows you to define extra actio
1111

1212
Let's say you have a list of posts and you have to publish them. Usually, for these kind of operations, you have to define a custom route like:
1313

14-
```php
14+
```php [PublishPostsController.php]
1515
$router->post('posts/publish', PublishPostsController::class);
1616

17-
// PublishPostsController.php
18-
1917
public function __invoke(RestifyRequest $request)
2018
{
2119
//...
@@ -100,9 +98,7 @@ The `$models` argument represents a collection of all the models for this query.
10098

10199
Then add the action instance to the repository `actions` method:
102100

103-
```php
104-
// PostRepository.php
105-
101+
```php [PostRepository.php]
106102
public function actions(RestifyRequest $request): array
107103
{
108104
return [
@@ -294,9 +290,7 @@ The show action definition is different, in a way it receives arguments for the
294290

295291
Restify automatically resolves Eloquent models defined in the route id and passes them to the action's handle method:
296292

297-
```php
298-
// PublishPostAction.php
299-
293+
```php [PublishPostAction.php]
300294
public function handle(ActionRequest $request, Post $post): JsonResponse
301295
{
302296

@@ -351,8 +345,7 @@ The index action definition is different in the way it receives arguments for th
351345

352346
Restify automatically resolves Eloquent models sent via the `repositories` key into the call payload. Then, it passes it to the action's handle method as a collection of items:
353347

354-
```php
355-
// PublishPostAction.php
348+
```php [PublishPostAction.php]
356349
use Illuminate\Support\Collection;
357350

358351
public function handle(ActionRequest $request, Collection $posts): JsonResponse
@@ -366,9 +359,7 @@ public function handle(ActionRequest $request, Collection $posts): JsonResponse
366359

367360
To register an index action, we have to use the `->onlyOnIndex()` accessor:
368361

369-
```php
370-
// PostRepository.php
371-
362+
```php [PostRepository.php]
372363
public function actions(RestifyRequest $request)
373364
{
374365
return [
@@ -409,9 +400,7 @@ Restify will get chunks of 200 and send them into the `Collection` argument for
409400

410401
You can customize the chunk number by customizing the `chunkCount` action property:
411402

412-
```php
413-
// PublishPostAction.php
414-
403+
```php [PublishPostAction.php]
415404
public static int $chunkCount = 500;
416405
```
417406

@@ -434,9 +423,7 @@ his/her account.
434423

435424
The index action definition is different, in a way it doesn't require the second argument for the `handle`.
436425

437-
```php
438-
// DisableProfileAction.php
439-
426+
```php [DisableProfileAction.php]
440427
public function handle(ActionRequest $request): JsonResponse
441428
{
442429
//
@@ -448,9 +435,7 @@ public function handle(ActionRequest $request): JsonResponse
448435

449436
There are two ways to register the standalone action:
450437

451-
```php
452-
// UserRepository
453-
438+
```php [UserRepository]
454439
public function actions(RestifyRequest $request)
455440
{
456441
return [
@@ -506,9 +491,7 @@ Thankfully, Restify makes it a breeze to add an action log to a model by attachi
506491

507492
By simply adding the `HasActionLogs` trait to your model, it will log all actions and CRUD operations into the database into the `action_logs` table:
508493

509-
```php
510-
// Post.php
511-
494+
```php [Post.php]
512495
class Post extends Model
513496
{
514497
use \Binaryk\LaravelRestify\Models\Concerns\HasActionLogs;
@@ -519,8 +502,7 @@ class Post extends Model
519502

520503
You can display them by attaching them to the related repository for example:
521504

522-
```php
523-
// PostRepository.php
505+
```php [PostRepository.php]
524506
use Binaryk\LaravelRestify\Fields\MorphToMany;
525507
use Binaryk\LaravelRestify\Repositories\ActionLogRepository;
526508

@@ -558,8 +540,7 @@ performed for posts:
558540

559541
You can definitely use your own `ActionLogRepository`. Just make sure you have it defined into the config:
560542

561-
```php
562-
// config/restify.php
543+
```php [config/restify.php]
563544
...
564545
'logs' => [
565546
'repository' => MyCustomLogsRepository::class,

docs-v3/content/docs/api/fields.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ using their static `new` or `make` method.
1818
The first argument is always the attribute name and usually matches the database `column`.
1919

2020
```php
21-
2221
use Illuminate\Support\Facades\Hash;
2322
use Binaryk\LaravelRestify\Fields\Field;
2423
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;

docs-v3/content/docs/api/getters.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ class StripeInformationGetter extends Getter
9393

9494
Then add the getter instance to the repository `getters` method:
9595

96-
```php
97-
// UserRepository.php
98-
96+
```php [UserRepository.php]
9997
public function getters(RestifyRequest $request): array
10098
{
10199
return [

docs-v3/content/docs/api/relations.md

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ Above we can see a few types of relationships declarations that Restify provides
7575

7676
This means that there is a relationship of the `hasMany` type declared in the Company model. The Eloquent relationship name is `users` (see the first argument of the HasMany field):
7777

78-
```php
79-
// app/Models/Company.php
78+
```php [app/Models/Company.php]
8079
public function users(): \Illuminate\Database\Eloquent\Relations\HasMany
8180
{
8281
return $this->hasMany(User::class);
@@ -270,9 +269,7 @@ public static function related(): array
270269

271270
The model should define the relationship `user`:
272271

273-
```php
274-
// Post.php
275-
272+
```php [Post.php]
276273
public function user()
277274
{
278275
return $this->belongsTo(User::class);
@@ -610,9 +607,7 @@ You have a few options to authorize the `attach` endpoint.
610607

611608
First, you can define the policy method `attachUsers`. The name should start with `attach` and suffix with the `CamelCase` name of the model's relationship name:
612609

613-
```php
614-
// CompanyPolicy.php
615-
610+
```php [CompanyPolicy.php]
616611
public function attachUsers(User $authenticatedUser, Company $company, User $userToBeAttached): bool
617612
{
618613
return $authenticatedUser->isAdmin();
@@ -691,9 +686,7 @@ $role->permissions()->sync($request->input('permissions'));
691686

692687
You can define a policy method `syncPermissions`. The name should start with `sync` and suffix with the plural `CamelCase` name of the model's relationship name:
693688

694-
```php
695-
// RolePolicy.php
696-
689+
```php [RolePolicy.php]
697690
public function syncPermissions(User $authenticatedUser, Company $company, Collection $keys): bool
698691
{
699692
// $keys are the primary keys of the related model (permissions in our case) Restify is trying to `sync`
@@ -722,9 +715,7 @@ You have a few options to authorize the `detach` endpoint.
722715

723716
Primarily, you can define the policy method `detachUsers`, as the name should start with `detach` and suffix with the `CamelCase` name of the model relationship name:
724717

725-
```php
726-
// CompanyPolicy.php
727-
718+
```php [CompanyPolicy.php]
728719
public function detachUsers(User $authenticatedUser, Company $company, User $userToBeDetached): bool
729720
{
730721
return $authenticatedUser->isAdmin();

docs-v3/content/docs/api/repositories-advanced.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ class PostRepository extends Repository
161161

162162
The default prefix of all Restify routes (except `login` and `register`) lives under the `restify->base` config:
163163

164-
```php
165-
// config/restify.php
164+
```php [config/restify.php]
166165
...
167166
'base' => '/api/restify',
168167
...
@@ -183,8 +182,7 @@ GET: /api/restify/users
183182

184183
However, you can prefix the repository with your own:
185184

186-
```php
187-
// UserRepository
185+
```php [UserRepository]
188186
public static $prefix = 'api/v1';
189187
```
190188

@@ -206,8 +204,7 @@ will be used for all the endpoints related to the user repository.
206204
Each repository has the middlewares from the config `restify.middleware` out of the box for the CRUD methods. However,
207205
you're free to add your own middlewares for a specific repository.
208206

209-
```php
210-
// PostRepository.php
207+
```php [PostRepository.php]
211208

212209
public static $middleware = [
213210
NeedsCompanyMiddleware::class,
@@ -629,8 +626,7 @@ class PostRepository extends Repository
629626

630627
**1. Update your global gate to allow null users:**
631628

632-
```php
633-
// app/Providers/RestifyApplicationServiceProvider.php
629+
```php [app/Providers/RestifyApplicationServiceProvider.php]
634630
protected function gate(): void
635631
{
636632
Gate::define('viewRestify', function ($user = null) {
@@ -645,8 +641,7 @@ protected function gate(): void
645641

646642
**2. Update your policies to allow null users:**
647643

648-
```php
649-
// app/Policies/PostPolicy.php
644+
```php [app/Policies/PostPolicy.php]
650645
public function allowRestify(User $user = null): bool
651646
{
652647
return true; // Allow all users (authenticated or not)
@@ -864,8 +859,7 @@ class PostRepository extends Repository
864859

865860
Each repository has several lifecycle methods. The most useful is `booted`, which is called as soon as the repository is loaded:
866861

867-
```php
868-
// PostRepository.php
862+
```php [PostRepository.php]
869863
protected static function booted()
870864
{
871865
// Initialization logic here

docs-v3/content/docs/api/repositories-basic.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ GET /api/restify/posts?page=2&perPage=10
288288

289289
Protect your repositories with Laravel policies:
290290

291-
```php
292-
// app/Policies/PostPolicy.php
291+
```php [app/Policies/PostPolicy.php]
293292
class PostPolicy
294293
{
295294
public function allowRestify(User $user = null): bool

0 commit comments

Comments
 (0)