Skip to content

Commit d57959f

Browse files
committed
reimplement cdragon
1 parent fa605cc commit d57959f

352 files changed

Lines changed: 1343 additions & 1397 deletions

File tree

Some content is hidden

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

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"symfony/console": "^6.4||^7.0||^8.0",
3636
"symfony/filesystem": "^6.4||^7.0||^8.0",
3737
"symfony/finder": "^6.4||^7.0||^8.0",
38-
"symfony/http-client": "^6.4||^7.0||^8.0",
3938
"symfony/yaml": "^6.4||^7.0||^8.0"
4039
},
4140
"autoload": {

generator/Definitions/CDragonClientDefinition.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Phizz\Support\StaticClient;
1010

1111
/**
12-
* Generates CDragonClient — a thin wrapper that exposes $lol and $tft sub-clients.
12+
* Generates AssetClient — a thin wrapper that exposes $lol and $tft sub-clients.
1313
*
1414
* @extends Definition<ClassType>
1515
*/
@@ -19,34 +19,36 @@ class CDragonClientDefinition extends Definition implements Writable
1919

2020
public function namespace(): string
2121
{
22-
return $this->resolveNamespace('CDragon');
22+
return $this->resolveNamespace('Assets');
2323
}
2424

2525
public function directory(): string
2626
{
27-
return $this->resolvePath('CDragon');
27+
return $this->resolvePath('Assets');
2828
}
2929

3030
public function imports(): array
3131
{
3232
return [
3333
StaticApi::class,
3434
StaticClient::class,
35+
$this->resolveNamespace('Assets\\Lol').'\\LolClient',
36+
$this->resolveNamespace('Assets\\Tft').'\\TftClient',
3537
];
3638
}
3739

3840
public function definition(): ClassType
3941
{
40-
$class = (new ClassType('CDragonClient'))
42+
$class = (new ClassType('AssetClient'))
4143
->setExtends(StaticApi::class);
4244

4345
$class->addProperty('lol')
44-
->setType($this->resolveNamespace('CDragon').'\\LolClient')
46+
->setType($this->resolveNamespace('Assets\\Lol').'\\LolClient')
4547
->setPublic()
4648
->setReadOnly();
4749

4850
$class->addProperty('tft')
49-
->setType($this->resolveNamespace('CDragon').'\\TftClient')
51+
->setType($this->resolveNamespace('Assets\\Tft').'\\TftClient')
5052
->setPublic()
5153
->setReadOnly();
5254

generator/Definitions/StaticDataDefinition.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,31 @@ class StaticDataDefinition extends Definition implements Writable
3030
* @param string|null $fieldName When set, overrides class name derivation (used for nested definitions).
3131
* @param string $classPrefix Prepended to the short class name to avoid collisions in deep nesting.
3232
* @param bool $singularize When true, singularizes the field name for the class name (used for list fields).
33+
* @param string $game 'lol' or 'tft' — determines the Assets sub-namespace.
3334
*/
3435
public function __construct(
3536
private readonly CDragonEndpoint $endpoint,
3637
private readonly ?string $parentSlug = null,
3738
private readonly ?string $fieldName = null,
3839
private readonly string $classPrefix = '',
3940
private readonly bool $singularize = false,
41+
private readonly string $game = 'lol',
4042
) {}
4143

4244
public function namespace(): string
4345
{
4446
$slug = $this->parentSlug ?? $this->endpoint->slug;
47+
$stem = StaticGameClientDefinition::resolveSlugStem($slug, $this->game);
4548

46-
return $this->resolveNamespace('CDragon\\'.Str::studly($slug).'\\Objects');
49+
return $this->resolveNamespace('Assets\\'.ucfirst($this->game).'\\'.$stem.'\\Objects');
4750
}
4851

4952
public function directory(): string
5053
{
5154
$slug = $this->parentSlug ?? $this->endpoint->slug;
55+
$stem = StaticGameClientDefinition::resolveSlugStem($slug, $this->game);
5256

53-
return $this->resolvePath('CDragon/'.Str::studly($slug).'/Objects');
57+
return $this->resolvePath('Assets/'.ucfirst($this->game).'/'.$stem.'/Objects');
5458
}
5559

5660
public function imports(): array
@@ -159,7 +163,7 @@ public function nestedDefinitions(): array
159163
fields: $field->subFields,
160164
);
161165

162-
$def = new StaticDataDefinition($subEndpoint, $parentSlug, $field->name, $this->childClassPrefix(), $singularize);
166+
$def = new StaticDataDefinition($subEndpoint, $parentSlug, $field->name, $this->childClassPrefix(), $singularize, $this->game);
163167
$nested[] = $def;
164168

165169
foreach ($def->nestedDefinitions() as $deepNested) {
@@ -180,7 +184,9 @@ private function shortClassName(): string
180184
return $this->classPrefix.$base.'Data';
181185
}
182186

183-
return Str::studly(Str::singular($this->endpoint->slug)).'Data';
187+
$stem = StaticGameClientDefinition::resolveSlugStem($this->endpoint->slug, $this->game);
188+
189+
return Str::singular($stem).'Data';
184190
}
185191

186192
/**
@@ -227,7 +233,8 @@ private function shouldGenerateNested(CDragonField $field, bool $singularize): b
227233
private function nestedFqcn(string $fieldName, bool $singularize): string
228234
{
229235
$slug = $this->parentSlug ?? $this->endpoint->slug;
230-
$ns = $this->resolveNamespace('CDragon\\'.Str::studly($slug).'\\Objects');
236+
$stem = StaticGameClientDefinition::resolveSlugStem($slug, $this->game);
237+
$ns = $this->resolveNamespace('Assets\\'.ucfirst($this->game).'\\'.$stem.'\\Objects');
231238
$base = $singularize ? Str::studly(Str::singular($fieldName)) : Str::studly($fieldName);
232239
$cn = $this->childClassPrefix().$base.'Data';
233240

generator/Definitions/StaticGameClientDefinition.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Phizz\Generator\Definitions;
66

77
use Illuminate\Support\Collection;
8+
use Illuminate\Support\Str;
89
use Nette\PhpGenerator\ClassType;
910
use Nette\PhpGenerator\Helpers as GeneratorHelpers;
1011
use Phizz\Generator\Interfaces\Writable;
@@ -77,12 +78,12 @@ public function __construct(
7778

7879
public function namespace(): string
7980
{
80-
return $this->resolveNamespace('CDragon');
81+
return $this->resolveNamespace('Assets\\'.ucfirst($this->game));
8182
}
8283

8384
public function directory(): string
8485
{
85-
return $this->resolvePath('CDragon');
86+
return $this->resolvePath('Assets/'.ucfirst($this->game));
8687
}
8788

8889
public function imports(): array
@@ -131,21 +132,31 @@ public function definition(): ClassType
131132
}
132133

133134
private function resolveMethodName(string $slug): string
135+
{
136+
return Helpers::formatAttribute(self::resolveSlugStem($slug, $this->game), Helpers::CAMEL_CASE);
137+
}
138+
139+
/**
140+
* Applies alias mapping and strips the game prefix from a slug, returning
141+
* a StudlyCase stem suitable for directory/namespace generation.
142+
*/
143+
public static function resolveSlugStem(string $slug, string $game): string
134144
{
135145
$mapped = self::SLUG_ALIASES[strtolower($slug)] ?? $slug;
136146

137-
if (str_starts_with(strtolower($mapped), $this->game)) {
138-
$mapped = lcfirst(substr($mapped, strlen($this->game)));
147+
if (str_starts_with(strtolower($mapped), strtolower($game))) {
148+
$mapped = substr($mapped, strlen($game));
139149
}
140150

141-
return Helpers::formatAttribute($mapped, Helpers::CAMEL_CASE);
151+
return Str::studly($mapped);
142152
}
143153

144154
private function buildObjectMethod(ClassType $class, CDragonEndpoint $endpoint, string $dataShort, string $methodName, string $dataFqcn): void
145155
{
146156
$method = $class->addMethod($methodName)
147157
->setReturnType($dataFqcn)
148-
->addComment("@return $dataShort");
158+
->setReturnNullable()
159+
->addComment("@return $dataShort|null");
149160

150161
if ($endpoint->isDirectory) {
151162
$method->addParameter('id')->setType('int');
@@ -167,7 +178,8 @@ private function buildFlatWithIdMethod(ClassType $class, CDragonEndpoint $endpoi
167178
$idField = $endpoint->idField;
168179
$method = $class->addMethod($methodName)
169180
->setReturnType(Collection::class.'|'.$dataFqcn)
170-
->addComment("@return Collection<int, $dataShort>|$dataShort");
181+
->setReturnNullable()
182+
->addComment("@return Collection<int, $dataShort>|$dataShort|null");
171183
$method->addParameter('id')->setType('?int')->setDefaultValue(null);
172184
$method->setBody(
173185
'return $this->fetch('."\n".
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Phizz\Generator\Generators;
4+
5+
use Phizz\Generator\Definitions\CDragonClientDefinition;
6+
use Phizz\Generator\Definitions\Definition;
7+
use Phizz\Generator\Definitions\StaticDataDefinition;
8+
use Phizz\Generator\Definitions\StaticGameClientDefinition;
9+
10+
class AssetGenerator extends Generator
11+
{
12+
/**
13+
* @return Definition[]
14+
*/
15+
public function definitions(): array
16+
{
17+
$endpoints = $this->config->endpoints;
18+
19+
$dataDefinitions = [];
20+
$lolEndpoints = [];
21+
$lolData = [];
22+
$tftEndpoints = [];
23+
$tftData = [];
24+
25+
foreach ($endpoints as $endpoint) {
26+
$game = str_starts_with(strtolower($endpoint->slug), 'tft') ? 'tft' : 'lol';
27+
$data = new StaticDataDefinition($endpoint, game: $game);
28+
$dataDefinitions[] = $data;
29+
30+
foreach ($data->nestedDefinitions() as $nested) {
31+
$dataDefinitions[] = $nested;
32+
}
33+
34+
if ($game === 'tft') {
35+
$tftEndpoints[] = $endpoint;
36+
$tftData[] = $data;
37+
} else {
38+
$lolEndpoints[] = $endpoint;
39+
$lolData[] = $data;
40+
}
41+
}
42+
43+
$lolClientDef = new StaticGameClientDefinition('lol', $lolEndpoints, $lolData);
44+
$tftClientDef = new StaticGameClientDefinition('tft', $tftEndpoints, $tftData);
45+
46+
return [...$dataDefinitions, $lolClientDef, $tftClientDef, new CDragonClientDefinition];
47+
}
48+
}

0 commit comments

Comments
 (0)