Skip to content

Commit e0a211d

Browse files
authored
Merge pull request #16 from WyriHaximus/feature-tested-objects-annotations
Nested resource annotation generator
2 parents 6cd26f8 + 47e5f3c commit e0a211d

File tree

11 files changed

+312
-19
lines changed

11 files changed

+312
-19
lines changed

composer.lock

Lines changed: 131 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceGenerator.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\CS\FileCacheManager;
2020
use Symfony\CS\Fixer;
2121
use Symfony\CS\FixerInterface;
22+
use WyriHaximus\ApiClient\Annotations\Nested;
2223
use WyriHaximus\ApiClient\Resource\ResourceInterface;
2324

2425
class ResourceGenerator
@@ -260,6 +261,16 @@ protected function createBaseClass(array $yaml): string
260261
$class = $factory->class($yaml['class'])
261262
->implement($yaml['class'] . 'Interface')
262263
->makeAbstract();
264+
265+
if (isset($yaml['nested'])) {
266+
$nestedResources = [];
267+
foreach ($yaml['nested'] as $key => $resource) {
268+
$nestedResources[] = $key . '="' . $resource . '"';
269+
}
270+
$docBlock = "/**\r\n * @Nested(" . implode(', ', $nestedResources) . ")\r\n */";
271+
$class->setDocComment($docBlock);
272+
}
273+
263274
$class->addStmt(
264275
new Node\Stmt\TraitUse([
265276
new Node\Name('TransportAwareTrait')
@@ -275,13 +286,19 @@ protected function createBaseClass(array $yaml): string
275286
$class->addStmt($this->createMethod($factory, $type, $name, $details));
276287
}
277288

278-
$node = $factory->namespace($yaml['namespace'])
289+
$stmt = $factory->namespace($yaml['namespace']);
290+
if (isset($yaml['nested'])) {
291+
$stmt = $stmt->addStmt(
292+
$factory->use(Nested::class)
293+
);
294+
}
295+
$stmt
279296
->addStmt($factory->use('WyriHaximus\ApiClient\Resource\TransportAwareTrait'))
280297
->addStmt($class)
281-
282-
->getNode()
283298
;
284299

300+
$node = $stmt->getNode();
301+
285302
$prettyPrinter = new PrettyPrinter\Standard();
286303
return $prettyPrinter->prettyPrintFile([
287304
$node

tests/ResourceGeneratorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public function testOutput()
4545
$getopt = Phake::mock(Context\Getopt::class);
4646
Phake::when($getopt)->get(1)->thenReturn($yamlPath . 'project.yaml');
4747
Phake::when($getopt)->get(2)->thenReturn($yamlPath . 'project-build.yaml');
48-
Phake::when($getopt)->get(3)->thenReturn($this->temporaryDirectory);
49-
Phake::when($getopt)->get(4)->thenReturn(null);
48+
Phake::when($getopt)->get(3)->thenReturn($yamlPath . 'project-config.yaml');
49+
Phake::when($getopt)->get(4)->thenReturn($this->temporaryDirectory);
50+
Phake::when($getopt)->get(5)->thenReturn(null);
5051
Phake::when($context)->getopt([])->thenReturn($getopt);
5152
(new ResourceGenerator($context, $stdio))->run();
5253
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($resourcesPath), RecursiveIteratorIterator::SELF_FIRST);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Example\Client\Resource\Async\Project;
4+
5+
use Example\Client\Resource\Project\Config as BaseConfig;
6+
7+
class Config extends BaseConfig
8+
{
9+
public function refresh() : Config
10+
{
11+
return $this->wait($this->callAsync('refresh'));
12+
}
13+
}

tests/resources/Project.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Example\Client\Resource;
44

5+
use WyriHaximus\ApiClient\Annotations\Nested;
56
use WyriHaximus\ApiClient\Resource\TransportAwareTrait;
67

8+
/**
9+
* @Nested(build="Project\Build", config="Project\Config")
10+
*/
711
abstract class Project implements ProjectInterface
812
{
913
use TransportAwareTrait;
@@ -24,9 +28,14 @@ abstract class Project implements ProjectInterface
2428
protected $description;
2529

2630
/**
27-
* @var array
31+
* @var Project\Build
2832
*/
29-
protected $config = array();
33+
protected $build;
34+
35+
/**
36+
* @var Project\Config
37+
*/
38+
protected $config;
3039

3140
/**
3241
* @var SplObjectStorage
@@ -68,9 +77,17 @@ public function description() : string
6877
}
6978

7079
/**
71-
* @return array
80+
* @return Project\Build
81+
*/
82+
public function build() : Project\Build
83+
{
84+
return $this->build;
85+
}
86+
87+
/**
88+
* @return Project\Config
7289
*/
73-
public function config() : array
90+
public function config() : Project\Config
7491
{
7592
return $this->config;
7693
}

tests/resources/Project/Config.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Example\Client\Resource\Project;
4+
5+
use WyriHaximus\ApiClient\Resource\TransportAwareTrait;
6+
7+
abstract class Config implements ConfigInterface
8+
{
9+
use TransportAwareTrait;
10+
11+
/**
12+
* @var string
13+
*/
14+
protected $a;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $b;
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $c;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $d = 'abcd';
30+
31+
/**
32+
* @return string
33+
*/
34+
public function a() : string
35+
{
36+
return $this->a;
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function b() : string
43+
{
44+
return $this->b;
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function c() : string
51+
{
52+
return $this->c;
53+
}
54+
55+
/**
56+
* @return string
57+
*/
58+
public function d() : string
59+
{
60+
return $this->d;
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Example\Client\Resource\Project;
4+
5+
use WyriHaximus\ApiClient\Resource\ResourceInterface;
6+
7+
interface ConfigInterface extends ResourceInterface
8+
{
9+
/**
10+
* @return string
11+
*/
12+
public function a() : string;
13+
14+
/**
15+
* @return string
16+
*/
17+
public function b() : string;
18+
19+
/**
20+
* @return string
21+
*/
22+
public function c() : string;
23+
24+
/**
25+
* @return string
26+
*/
27+
public function d() : string;
28+
}

0 commit comments

Comments
 (0)