Skip to content

Commit 35b74b4

Browse files
committed
Async and sync class generator have their own generator method and generate code better suited to their situation
1 parent fb92847 commit 35b74b4

File tree

9 files changed

+110
-54
lines changed

9 files changed

+110
-54
lines changed

src/FileGenerators/AbstractExtendingClassGenerator.php

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace ApiClients\Tools\ResourceGenerator\FileGenerators;
44

55
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
6-
use PhpParser\BuilderFactory;
76
use PhpParser\Node;
87

98
abstract class AbstractExtendingClassGenerator implements FileGeneratorInterface
@@ -35,51 +34,4 @@ public function getFilename(): string
3534
'.php'
3635
;
3736
}
38-
39-
/**
40-
* @return Node
41-
*/
42-
public function generate(): Node
43-
{
44-
$classChunks = explode('\\', $this->yaml['class']);
45-
$className = array_pop($classChunks);
46-
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE;
47-
if (count($classChunks) > 0) {
48-
$namespace .= '\\' . implode('\\', $classChunks);
49-
$namespace = str_replace('\\\\', '\\', $namespace);
50-
}
51-
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'];
52-
53-
$factory = new BuilderFactory;
54-
55-
$class = $factory->class($className)
56-
->extend('Base' . $className);
57-
58-
$class->addStmt($factory->method('refresh')
59-
->makePublic()
60-
->setReturnType($className)
61-
->addStmt(
62-
new Node\Stmt\Return_(
63-
new Node\Expr\MethodCall(
64-
new Node\Expr\Variable('this'),
65-
'wait',
66-
[
67-
new Node\Expr\MethodCall(
68-
new Node\Expr\Variable('this'),
69-
'callAsync',
70-
[
71-
new Node\Scalar\String_('refresh'),
72-
]
73-
),
74-
]
75-
)
76-
)
77-
));
78-
79-
return $factory->namespace($namespace)
80-
->addStmt($factory->use($baseClass)->as('Base' . $className))
81-
->addStmt($class)
82-
->getNode()
83-
;
84-
}
8537
}

src/FileGenerators/AsyncClassGenerator.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,54 @@
33
namespace ApiClients\Tools\ResourceGenerator\FileGenerators;
44

55
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
6+
use PhpParser\BuilderFactory;
67
use PhpParser\Node;
78

89
final class AsyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface
910
{
1011
const NAMESPACE = 'Async';
12+
13+
/**
14+
* @return Node
15+
*/
16+
public function generate(): Node
17+
{
18+
$classChunks = explode('\\', $this->yaml['class']);
19+
$className = array_pop($classChunks);
20+
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE;
21+
if (count($classChunks) > 0) {
22+
$namespace .= '\\' . implode('\\', $classChunks);
23+
$namespace = str_replace('\\\\', '\\', $namespace);
24+
}
25+
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'];
26+
27+
$factory = new BuilderFactory();
28+
29+
$class = $factory->class($className)
30+
->extend('Base' . $className);
31+
32+
$class->addStmt(
33+
$factory->method('refresh')
34+
->makePublic()
35+
->setReturnType($className)
36+
->addStmt(
37+
new Node\Stmt\Throw_(
38+
new Node\Expr\New_(
39+
new Node\Name('\Exception'),
40+
[
41+
new Node\Scalar\String_(
42+
'TODO: create refresh method!'
43+
)
44+
]
45+
)
46+
)
47+
)
48+
);
49+
50+
return $factory->namespace($namespace)
51+
->addStmt($factory->use($baseClass)->as('Base' . $className))
52+
->addStmt($class)
53+
->getNode()
54+
;
55+
}
1156
}

src/FileGenerators/SyncClassGenerator.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,66 @@
22

33
namespace ApiClients\Tools\ResourceGenerator\FileGenerators;
44

5+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
56
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
7+
use PhpParser\BuilderFactory;
68
use PhpParser\Node;
79

810
final class SyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface
911
{
1012
const NAMESPACE = 'Sync';
13+
14+
/**
15+
* @return Node
16+
*/
17+
public function generate(): Node
18+
{
19+
$classChunks = explode('\\', $this->yaml['class']);
20+
$className = array_pop($classChunks);
21+
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE;
22+
if (count($classChunks) > 0) {
23+
$namespace .= '\\' . implode('\\', $classChunks);
24+
$namespace = str_replace('\\\\', '\\', $namespace);
25+
}
26+
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'];
27+
28+
$factory = new BuilderFactory();
29+
30+
$class = $factory->class($className)
31+
->extend('Base' . $className);
32+
33+
$class->addStmt($factory->method('refresh')
34+
->makePublic()
35+
->setReturnType($className)
36+
->addStmt(
37+
new Node\Stmt\Return_(
38+
new Node\Expr\MethodCall(
39+
new Node\Expr\Variable('this'),
40+
'wait',
41+
[
42+
new Node\Expr\MethodCall(
43+
new Node\Expr\Variable('this'),
44+
'handleCommand',
45+
[
46+
new Node\Expr\New_(
47+
new Node\Name('BuildAsyncFromSyncCommand'),
48+
[
49+
new Node\Scalar\String_($this->yaml['class']),
50+
new Node\Expr\Variable('this'),
51+
]
52+
),
53+
]
54+
),
55+
]
56+
)
57+
)
58+
));
59+
60+
return $factory->namespace($namespace)
61+
->addStmt($factory->use(BuildAsyncFromSyncCommand::class))
62+
->addStmt($factory->use($baseClass)->as('Base' . $className))
63+
->addStmt($class)
64+
->getNode()
65+
;
66+
}
1167
}

tests/expected-app/src/Resources/Async/Project.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Project extends BaseProject
88
{
99
public function refresh() : Project
1010
{
11-
return $this->wait($this->callAsync('refresh'));
11+
throw new \Exception('TODO: create refresh method!');
1212
}
1313
}

tests/expected-app/src/Resources/Async/Project/Build.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Build extends BaseBuild
88
{
99
public function refresh() : Build
1010
{
11-
return $this->wait($this->callAsync('refresh'));
11+
throw new \Exception('TODO: create refresh method!');
1212
}
1313
}

tests/expected-app/src/Resources/Async/Project/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Config extends BaseConfig
88
{
99
public function refresh() : Config
1010
{
11-
return $this->wait($this->callAsync('refresh'));
11+
throw new \Exception('TODO: create refresh method!');
1212
}
1313
}

tests/expected-app/src/Resources/Sync/Project.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Example\Client\Resource\Sync;
44

5+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
56
use Example\Client\Resource\Project as BaseProject;
67

78
class Project extends BaseProject
89
{
910
public function refresh() : Project
1011
{
11-
return $this->wait($this->callAsync('refresh'));
12+
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project', $this)));
1213
}
1314
}

tests/expected-app/src/Resources/Sync/Project/Build.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Example\Client\Resource\Sync\Project;
44

5+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
56
use Example\Client\Resource\Project\Build as BaseBuild;
67

78
class Build extends BaseBuild
89
{
910
public function refresh() : Build
1011
{
11-
return $this->wait($this->callAsync('refresh'));
12+
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Build', $this)));
1213
}
1314
}

tests/expected-app/src/Resources/Sync/Project/Config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Example\Client\Resource\Sync\Project;
44

5+
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
56
use Example\Client\Resource\Project\Config as BaseConfig;
67

78
class Config extends BaseConfig
89
{
910
public function refresh() : Config
1011
{
11-
return $this->wait($this->callAsync('refresh'));
12+
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Config', $this)));
1213
}
1314
}

0 commit comments

Comments
 (0)