Skip to content

Commit d68db29

Browse files
committed
refactor(scopes): build registry from package configs
1 parent de264b6 commit d68db29

7 files changed

Lines changed: 119 additions & 31 deletions

File tree

packages/category/config/category.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@
9292

9393
'navigation_group' => 'trans//core::core.cms',
9494

95+
'scope_registry' => [
96+
'origins' => [
97+
'category' => \Moox\Category\Models\Category::class,
98+
],
99+
],
100+
95101
];

packages/core/config/core.php

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
<?php
22

3-
use Moox\Category\Models\Category;
4-
use Moox\Draft\Models\Draft;
5-
use Moox\Media\Models\Media;
6-
use Moox\Record\Models\Record;
7-
use Moox\Tag\Models\Tag;
8-
93
/*
104
|--------------------------------------------------------------------------
115
| Moox Configuration
@@ -99,6 +93,19 @@
9993
'package' => 'Moox Core',
10094
'models' => [],
10195
],
96+
'draft' => [
97+
'package' => 'Moox Draft',
98+
'models' => [
99+
'Draft' => [
100+
'api' => [
101+
'Index' => '',
102+
'Show' => '',
103+
'Update' => '',
104+
'Delete' => '',
105+
],
106+
],
107+
],
108+
],
102109
'expiry' => [
103110
'package' => 'Moox Expiry',
104111
'models' => [
@@ -162,6 +169,19 @@
162169
],
163170
],
164171
],
172+
'media' => [
173+
'package' => 'Moox Media',
174+
'models' => [
175+
'Media' => [
176+
'api' => [
177+
'Index' => '',
178+
'Show' => '',
179+
'Update' => '',
180+
'Delete' => '',
181+
],
182+
],
183+
],
184+
],
165185
'notifications' => [
166186
'package' => 'Moox Notifications',
167187
'models' => [
@@ -175,6 +195,19 @@
175195
],
176196
],
177197
],
198+
'record' => [
199+
'package' => 'Moox Record',
200+
'models' => [
201+
'Record' => [
202+
'api' => [
203+
'Index' => '',
204+
'Show' => '',
205+
'Update' => '',
206+
'Delete' => '',
207+
],
208+
],
209+
],
210+
],
178211
'page' => [
179212
'package' => 'Moox Page',
180213
'models' => [
@@ -489,26 +522,4 @@
489522
'prompt_when_any_published' => false,
490523
],
491524

492-
/*
493-
|--------------------------------------------------------------------------
494-
| Scopes
495-
|--------------------------------------------------------------------------
496-
|
497-
| Scopes stay plain strings in the database using the format
498-
| "origin:source:context:mode". The registry maps origin and target
499-
| segments to model classes inside the codebase.
500-
|
501-
*/
502-
'scopes' => [
503-
'origins' => [
504-
'media' => Media::class,
505-
'category' => Category::class,
506-
'tag' => Tag::class,
507-
'record' => Record::class,
508-
],
509-
'sources' => [
510-
'draft' => Draft::class,
511-
],
512-
],
513-
514525
];

packages/core/src/Services/ScopeRegistry.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,67 @@
44

55
class ScopeRegistry
66
{
7+
/**
8+
* Build the scope registry by merging all package contributions
9+
* (declared in each package config under `scope_registry`) and then
10+
* applying any application overrides from `core.scopes`.
11+
*
12+
* @return array{origins: array<string, class-string|null>, sources: array<string, class-string|null>}
13+
*/
14+
protected function registry(): array
15+
{
16+
/** @var array{origins?: array<string, class-string|null>, sources?: array<string, class-string|null>} $merged */
17+
$merged = [
18+
'origins' => [],
19+
'sources' => [],
20+
];
21+
22+
$packages = config('core.packages', []);
23+
24+
foreach (array_keys($packages) as $packageKey) {
25+
$contribution = config($packageKey.'.scope_registry', []);
26+
27+
if (! is_array($contribution)) {
28+
continue;
29+
}
30+
31+
/** @var array<string, class-string|null> $origins */
32+
$origins = is_array($contribution['origins'] ?? null) ? $contribution['origins'] : [];
33+
/** @var array<string, class-string|null> $sources */
34+
$sources = is_array($contribution['sources'] ?? null) ? $contribution['sources'] : [];
35+
36+
$merged['origins'] = array_replace($merged['origins'], $origins);
37+
$merged['sources'] = array_replace($merged['sources'], $sources);
38+
}
39+
40+
$overrides = config('core.scopes', []);
41+
if (is_array($overrides)) {
42+
/** @var array<string, class-string|null> $overrideOrigins */
43+
$overrideOrigins = is_array($overrides['origins'] ?? null) ? $overrides['origins'] : [];
44+
/** @var array<string, class-string|null> $overrideSources */
45+
$overrideSources = is_array($overrides['sources'] ?? null) ? $overrides['sources'] : [];
46+
47+
$merged['origins'] = array_replace($merged['origins'], $overrideOrigins);
48+
$merged['sources'] = array_replace($merged['sources'], $overrideSources);
49+
}
50+
51+
return $merged;
52+
}
53+
754
/**
855
* @return array<string, class-string|null>
956
*/
1057
public function getOrigins(): array
1158
{
12-
return config('core.scopes.origins', []);
59+
return $this->registry()['origins'];
1360
}
1461

1562
/**
1663
* @return array<string, class-string|null>
1764
*/
1865
public function getSources(): array
1966
{
20-
return config('core.scopes.sources', []);
67+
return $this->registry()['sources'];
2168
}
2269

2370
public function resolveOriginModel(string $origin): ?string

packages/draft/config/draft.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
'resource' => TagResource::class,
9494
],
9595
'category' => [
96-
'enabled' => false,
96+
'enabled' => true,
9797
'resource' => CategoryResource::class,
9898
'origin' => 'category',
9999
'boundary' => 'private',
@@ -161,4 +161,10 @@
161161
|
162162
*/
163163
'navigation_group' => 'DEV',
164+
165+
'scope_registry' => [
166+
'sources' => [
167+
'draft' => \Moox\Draft\Models\Draft::class,
168+
],
169+
],
164170
];

packages/media/config/media.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@
123123
'plural_model_label' => 'trans//media::media.collections',
124124
],
125125
],
126+
127+
'scope_registry' => [
128+
'origins' => [
129+
'media' => \Moox\Media\Models\Media::class,
130+
],
131+
],
126132
];

packages/record/config/record.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,10 @@
139139
|
140140
*/
141141
'navigation_group' => 'DEV',
142+
143+
'scope_registry' => [
144+
'origins' => [
145+
'record' => \Moox\Record\Models\Record::class,
146+
],
147+
],
142148
];

packages/tag/config/tag.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@
134134

135135
'allow_slug_change_after_saved' => env('ALLOW_SLUG_CHANGE_AFTER_SAVED', true),
136136
'allow_slug_change_after_publish' => env('ALLOW_SLUG_CHANGE_AFTER_PUBLISH', false),
137+
138+
'scope_registry' => [
139+
'origins' => [
140+
'tag' => \Moox\Tag\Models\Tag::class,
141+
],
142+
],
137143
];

0 commit comments

Comments
 (0)