Skip to content

Commit 42a091b

Browse files
committed
Builder wip
1 parent eb4d201 commit 42a091b

File tree

7 files changed

+193
-15
lines changed

7 files changed

+193
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ Thumbs.db
7272
/app/Builder/*/*
7373
/config/previews/*
7474
/lang/*/previews/*
75+
/resources/lang/*/previews/*
7576
/packages/builder-pro

config/builder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,29 @@
1717

1818
return [
1919

20+
'presets' => [
21+
'light-item' => [
22+
'class' => \Moox\Builder\Presets\LightItemPreset::class,
23+
'generators' => ['model', 'migration', 'resource'],
24+
],
25+
'simple-item' => [
26+
'class' => \Moox\Builder\Presets\SimpleItemPreset::class,
27+
'generators' => ['model', 'migration', 'resource'],
28+
],
29+
'soft-delete-item' => [
30+
'class' => \Moox\Builder\Presets\SoftDeleteItemPreset::class,
31+
'generators' => ['model', 'migration', 'resource'],
32+
],
33+
'full-item' => [
34+
'class' => \Moox\Builder\Presets\FullItemPreset::class,
35+
'generators' => ['model', 'migration', 'resource'],
36+
],
37+
'static-language' => [
38+
'class' => \App\Builder\Presets\StaticLanguagePreset::class,
39+
'generators' => ['model', 'migration', 'resource'],
40+
],
41+
],
42+
2043
/*
2144
|--------------------------------------------------------------------------
2245
| Resources

packages/builder/config/builder.php

Lines changed: 94 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@
6666
*/
6767

6868
'contexts' => [
69-
'moox' => [
70-
'base_path' => app_path('Moox'),
71-
'base_namespace' => 'App\\Moox',
72-
],
73-
'app' => [
74-
'base_path' => app_path(),
69+
'custom' => [
70+
'base_path' => app_path('Custom'),
7571
'base_namespace' => 'App',
7672
'generators' => [
7773
'model' => [
@@ -115,6 +111,98 @@
115111
],
116112
],
117113
],
114+
'app' => [
115+
'base_path' => app_path(),
116+
'base_namespace' => 'App',
117+
'generators' => [
118+
'model' => [
119+
'path' => '%BasePath%\Models',
120+
'namespace' => '%BaseNamespace%\\Models',
121+
'template' => __DIR__.'/../src/Templates/Entity/model.php.stub',
122+
'generator' => \Moox\Builder\Generators\Entity\ModelGenerator::class,
123+
],
124+
'resource' => [
125+
'path' => '%BasePath%\Resources',
126+
'namespace' => '%BaseNamespace%\\Resources',
127+
'template' => __DIR__.'/../src/Templates/Entity/resource.php.stub',
128+
'page_templates' => [
129+
'List' => __DIR__.'/../src/Templates/Entity/pages/list.php.stub',
130+
'Create' => __DIR__.'/../src/Templates/Entity/pages/create.php.stub',
131+
'Edit' => __DIR__.'/../src/Templates/Entity/pages/edit.php.stub',
132+
'View' => __DIR__.'/../src/Templates/Entity/pages/view.php.stub',
133+
],
134+
'generator' => \Moox\Builder\Generators\Entity\ResourceGenerator::class,
135+
],
136+
'plugin' => [
137+
'path' => '%BasePath%\Filament\Plugins',
138+
'namespace' => '%BaseNamespace%\\Filament\\Plugins',
139+
'template' => __DIR__.'/../src/Templates/Entity/plugin.php.stub',
140+
'generator' => \Moox\Builder\Generators\Entity\PluginGenerator::class,
141+
],
142+
'migration' => [
143+
'path' => 'database\migrations',
144+
'template' => __DIR__.'/../src/Templates/Entity/migration.php.stub',
145+
'generator' => \Moox\Builder\Generators\Entity\MigrationGenerator::class,
146+
],
147+
'translation' => [
148+
'path' => 'lang\%locale%\entities',
149+
'template' => __DIR__.'/../src/Templates/Entity/translation.php.stub',
150+
'generator' => \Moox\Builder\Generators\Entity\TranslationGenerator::class,
151+
],
152+
'config' => [
153+
'path' => 'config\entities',
154+
'template' => __DIR__.'/../src/Templates/Entity/config.php.stub',
155+
'generator' => \Moox\Builder\Generators\Entity\ConfigGenerator::class,
156+
],
157+
],
158+
],
159+
/*
160+
'package' => [
161+
'base_path' => '$PackagePath',
162+
'base_namespace' => '$PackageNamespace',
163+
'generators' => [
164+
'model' => [
165+
'path' => '%BasePath%\src\Models',
166+
'namespace' => '%BaseNamespace%\\Models',
167+
'template' => __DIR__.'/../src/Templates/Entity/model.php.stub',
168+
'generator' => \Moox\Builder\Generators\Entity\ModelGenerator::class,
169+
],
170+
'resource' => [
171+
'path' => '%BasePath%\src\Resources',
172+
'namespace' => '%BaseNamespace%\\Resources',
173+
'template' => __DIR__.'/../src/Templates/Entity/resource.php.stub',
174+
'page_templates' => [
175+
'List' => __DIR__.'/../src/Templates/Entity/pages/list.php.stub',
176+
'Create' => __DIR__.'/../src/Templates/Entity/pages/create.php.stub',
177+
'Edit' => __DIR__.'/../src/Templates/Entity/pages/edit.php.stub',
178+
'View' => __DIR__.'/../src/Templates/Entity/pages/view.php.stub',
179+
],
180+
'generator' => \Moox\Builder\Generators\Entity\ResourceGenerator::class,
181+
],
182+
'migration_stub' => [
183+
'path' => '%BasePath%\database\migrations',
184+
'template' => __DIR__.'/../src/Templates/Entity/migration.php.stub',
185+
'generator' => \Moox\Builder\Generators\Entity\MigrationGenerator::class,
186+
],
187+
'plugin' => [
188+
'path' => '%BasePath%\src',
189+
'namespace' => '%BaseNamespace%',
190+
'template' => __DIR__.'/../src/Templates/Entity/plugin.php.stub',
191+
'generator' => \Moox\Builder\Generators\Entity\PluginGenerator::class,
192+
],
193+
'translation' => [
194+
'path' => '%BasePath%\resources\lang\entities',
195+
'template' => __DIR__.'/../src/Templates/Entity/translation.php.stub',
196+
'generator' => \Moox\Builder\Generators\Entity\TranslationGenerator::class,
197+
],
198+
'config' => [
199+
'path' => '%BasePath%\config\entities',
200+
'template' => __DIR__.'/../src/Templates/Entity/config.php.stub',
201+
'generator' => \Moox\Builder\Generators\Entity\ConfigGenerator::class,
202+
],
203+
],
204+
],
205+
*/
118206
'preview' => [
119207
'base_path' => app_path('Builder'),
120208
'base_namespace' => 'App\\Builder',

packages/builder/src/Commands/AbstractBuilderCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function createContext(string $entityName, ?string $package = null, bo
2020

2121
$config = [];
2222
if ($package !== null) {
23-
$config['package_namespace'] = $package;
23+
$config['package'] = ['name' => $package];
2424
}
2525

2626
return ContextFactory::create(
@@ -30,10 +30,11 @@ protected function createContext(string $entityName, ?string $package = null, bo
3030
);
3131
}
3232

33-
protected function getBuildContext(bool $preview, ?string $package = null): string
33+
protected function getBuildContext(bool $preview, ?bool $app = false, ?string $package = null): string
3434
{
3535
return match (true) {
3636
$preview => 'preview',
37+
$app => 'app',
3738
$package !== null => 'package',
3839
default => 'app'
3940
};

packages/builder/src/Commands/CreateEntityCommand.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ class CreateEntityCommand extends AbstractBuilderCommand
1717
{
1818
protected $signature = 'builder:create-entity
1919
{name : The name of the entity}
20+
{--context= : Context to use (app, moox, package, preview)}
2021
{--package= : Package namespace}
2122
{--preview : Generate in preview mode}
23+
{--app : Generate in app context}
2224
{--preset= : Preset to use}
2325
{--plural= : Plural form of the entity name}';
2426

@@ -109,13 +111,41 @@ public function handle(): int
109111
protected function createContext(
110112
string $entityName,
111113
?string $package = null,
112-
bool $preview = false
114+
bool $preview = false,
115+
bool $app = false
113116
): BuildContext {
114117
return ContextFactory::create(
115-
$this->getBuildContext($preview, $package),
118+
$this->getBuildContext($preview, $app, $package),
116119
$entityName,
117120
$package ? ['package' => ['name' => $package]] : [],
118121
$this->option('preset')
119122
);
120123
}
124+
125+
protected function getBuildContext(?bool $preview = false, ?bool $app = false, ?string $package = null): string
126+
{
127+
if ($context = $this->option('context')) {
128+
return $context;
129+
}
130+
131+
if ($app) {
132+
return 'app';
133+
}
134+
135+
if ($package) {
136+
return 'package';
137+
}
138+
139+
if ($preview) {
140+
return 'preview';
141+
}
142+
143+
$contexts = array_keys(config('builder.contexts', []));
144+
145+
if (count($contexts) === 1) {
146+
return reset($contexts);
147+
}
148+
149+
return $this->choice('Choose a context', $contexts);
150+
}
121151
}

packages/builder/src/Contexts/ContextFactory.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Moox\Builder\Contexts;
66

7+
use Illuminate\Support\Facades\Log;
78
use InvalidArgumentException;
89

910
class ContextFactory
@@ -16,15 +17,49 @@ public static function create(
1617
): BuildContext {
1718
$contexts = config('builder.contexts', []);
1819

20+
// Debug logging
21+
Log::info('ContextFactory creating context', [
22+
'contextType' => $contextType,
23+
'config' => $config,
24+
'available_contexts' => array_keys($contexts),
25+
'package_context_exists' => isset($contexts['package']),
26+
'package_context_config' => $contexts['package'] ?? null,
27+
]);
28+
29+
if ($contextType === 'package') {
30+
if (empty($config['package']['name'])) {
31+
throw new InvalidArgumentException('Package name is required for package context');
32+
}
33+
34+
$packagePath = str_replace('.', '/', $config['package']['name']);
35+
$packageNamespace = str_replace('.', '\\', $config['package']['name']);
36+
37+
$config['package']['namespace'] = $packageNamespace;
38+
39+
$contextConfig = array_merge(
40+
$contexts[$contextType] ?? [],
41+
[
42+
'base_path' => base_path("packages/{$packagePath}"),
43+
'base_namespace' => $packageNamespace,
44+
'package' => $config['package'],
45+
],
46+
$config
47+
);
48+
49+
Log::info('Package context configuration', [
50+
'final_context_config' => $contextConfig,
51+
]);
52+
53+
return new BuildContext($contextType, $contextConfig, [], $entityName, null, $preset);
54+
}
55+
1956
if (! isset($contexts[$contextType])) {
2057
throw new InvalidArgumentException("Invalid context type: {$contextType}");
2158
}
2259

23-
$contextConfig = array_merge($contexts[$contextType], $config);
24-
2560
return new BuildContext(
2661
$contextType,
27-
$contextConfig,
62+
array_merge($contexts[$contextType], $config),
2863
[],
2964
$entityName,
3065
null,

packages/builder/src/Services/ContextAwareService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ protected function validateContextConfig(): void
6666

6767
protected function validatePackageConfig(array $config): void
6868
{
69-
if (! isset($config['package']['name'], $config['package']['namespace'])) {
70-
throw new RuntimeException('Invalid package configuration');
69+
if (empty($config['package']['name'])) {
70+
throw new RuntimeException('Invalid package configuration: missing package name');
7171
}
7272
}
7373

0 commit comments

Comments
 (0)