diff --git a/src/FileGenerators/AbstractExtendingClassGenerator.php b/src/FileGenerators/AbstractExtendingClassGenerator.php index c671eb7..d454261 100644 --- a/src/FileGenerators/AbstractExtendingClassGenerator.php +++ b/src/FileGenerators/AbstractExtendingClassGenerator.php @@ -3,7 +3,6 @@ namespace ApiClients\Tools\ResourceGenerator\FileGenerators; use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; -use PhpParser\BuilderFactory; use PhpParser\Node; abstract class AbstractExtendingClassGenerator implements FileGeneratorInterface @@ -35,51 +34,4 @@ public function getFilename(): string '.php' ; } - - /** - * @return Node - */ - public function generate(): Node - { - $classChunks = explode('\\', $this->yaml['class']); - $className = array_pop($classChunks); - $namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; - if (count($classChunks) > 0) { - $namespace .= '\\' . implode('\\', $classChunks); - $namespace = str_replace('\\\\', '\\', $namespace); - } - $baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; - - $factory = new BuilderFactory; - - $class = $factory->class($className) - ->extend('Base' . $className); - - $class->addStmt($factory->method('refresh') - ->makePublic() - ->setReturnType($className) - ->addStmt( - new Node\Stmt\Return_( - new Node\Expr\MethodCall( - new Node\Expr\Variable('this'), - 'wait', - [ - new Node\Expr\MethodCall( - new Node\Expr\Variable('this'), - 'callAsync', - [ - new Node\Scalar\String_('refresh'), - ] - ), - ] - ) - ) - )); - - return $factory->namespace($namespace) - ->addStmt($factory->use($baseClass)->as('Base' . $className)) - ->addStmt($class) - ->getNode() - ; - } } diff --git a/src/FileGenerators/AsyncClassGenerator.php b/src/FileGenerators/AsyncClassGenerator.php index 30b1c25..9808d7e 100644 --- a/src/FileGenerators/AsyncClassGenerator.php +++ b/src/FileGenerators/AsyncClassGenerator.php @@ -3,9 +3,54 @@ namespace ApiClients\Tools\ResourceGenerator\FileGenerators; use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; +use PhpParser\BuilderFactory; use PhpParser\Node; final class AsyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface { const NAMESPACE = 'Async'; + + /** + * @return Node + */ + public function generate(): Node + { + $classChunks = explode('\\', $this->yaml['class']); + $className = array_pop($classChunks); + $namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; + if (count($classChunks) > 0) { + $namespace .= '\\' . implode('\\', $classChunks); + $namespace = str_replace('\\\\', '\\', $namespace); + } + $baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; + + $factory = new BuilderFactory(); + + $class = $factory->class($className) + ->extend('Base' . $className); + + $class->addStmt( + $factory->method('refresh') + ->makePublic() + ->setReturnType($className) + ->addStmt( + new Node\Stmt\Throw_( + new Node\Expr\New_( + new Node\Name('\Exception'), + [ + new Node\Scalar\String_( + 'TODO: create refresh method!' + ) + ] + ) + ) + ) + ); + + return $factory->namespace($namespace) + ->addStmt($factory->use($baseClass)->as('Base' . $className)) + ->addStmt($class) + ->getNode() + ; + } } diff --git a/src/FileGenerators/SyncClassGenerator.php b/src/FileGenerators/SyncClassGenerator.php index ccb8a99..e833595 100644 --- a/src/FileGenerators/SyncClassGenerator.php +++ b/src/FileGenerators/SyncClassGenerator.php @@ -2,10 +2,66 @@ namespace ApiClients\Tools\ResourceGenerator\FileGenerators; +use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; +use PhpParser\BuilderFactory; use PhpParser\Node; final class SyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface { const NAMESPACE = 'Sync'; + + /** + * @return Node + */ + public function generate(): Node + { + $classChunks = explode('\\', $this->yaml['class']); + $className = array_pop($classChunks); + $namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; + if (count($classChunks) > 0) { + $namespace .= '\\' . implode('\\', $classChunks); + $namespace = str_replace('\\\\', '\\', $namespace); + } + $baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; + + $factory = new BuilderFactory(); + + $class = $factory->class($className) + ->extend('Base' . $className); + + $class->addStmt($factory->method('refresh') + ->makePublic() + ->setReturnType($className) + ->addStmt( + new Node\Stmt\Return_( + new Node\Expr\MethodCall( + new Node\Expr\Variable('this'), + 'wait', + [ + new Node\Expr\MethodCall( + new Node\Expr\Variable('this'), + 'handleCommand', + [ + new Node\Expr\New_( + new Node\Name('BuildAsyncFromSyncCommand'), + [ + new Node\Scalar\String_($this->yaml['class']), + new Node\Expr\Variable('this'), + ] + ), + ] + ), + ] + ) + ) + )); + + return $factory->namespace($namespace) + ->addStmt($factory->use(BuildAsyncFromSyncCommand::class)) + ->addStmt($factory->use($baseClass)->as('Base' . $className)) + ->addStmt($class) + ->getNode() + ; + } } diff --git a/tests/expected-app/src/Resources/Async/Project.php b/tests/expected-app/src/Resources/Async/Project.php index a784554..33b3e1e 100644 --- a/tests/expected-app/src/Resources/Async/Project.php +++ b/tests/expected-app/src/Resources/Async/Project.php @@ -8,6 +8,6 @@ class Project extends BaseProject { public function refresh() : Project { - return $this->wait($this->callAsync('refresh')); + throw new \Exception('TODO: create refresh method!'); } } diff --git a/tests/expected-app/src/Resources/Async/Project/Build.php b/tests/expected-app/src/Resources/Async/Project/Build.php index abcfdbc..6b72bec 100644 --- a/tests/expected-app/src/Resources/Async/Project/Build.php +++ b/tests/expected-app/src/Resources/Async/Project/Build.php @@ -8,6 +8,6 @@ class Build extends BaseBuild { public function refresh() : Build { - return $this->wait($this->callAsync('refresh')); + throw new \Exception('TODO: create refresh method!'); } } diff --git a/tests/expected-app/src/Resources/Async/Project/Config.php b/tests/expected-app/src/Resources/Async/Project/Config.php index 02462cb..d7330bd 100644 --- a/tests/expected-app/src/Resources/Async/Project/Config.php +++ b/tests/expected-app/src/Resources/Async/Project/Config.php @@ -8,6 +8,6 @@ class Config extends BaseConfig { public function refresh() : Config { - return $this->wait($this->callAsync('refresh')); + throw new \Exception('TODO: create refresh method!'); } } diff --git a/tests/expected-app/src/Resources/Sync/Project.php b/tests/expected-app/src/Resources/Sync/Project.php index a49b2ed..ac01078 100644 --- a/tests/expected-app/src/Resources/Sync/Project.php +++ b/tests/expected-app/src/Resources/Sync/Project.php @@ -2,12 +2,13 @@ namespace Example\Client\Resource\Sync; +use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use Example\Client\Resource\Project as BaseProject; class Project extends BaseProject { public function refresh() : Project { - return $this->wait($this->callAsync('refresh')); + return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project', $this))); } } diff --git a/tests/expected-app/src/Resources/Sync/Project/Build.php b/tests/expected-app/src/Resources/Sync/Project/Build.php index ef0e828..a393ef0 100644 --- a/tests/expected-app/src/Resources/Sync/Project/Build.php +++ b/tests/expected-app/src/Resources/Sync/Project/Build.php @@ -2,12 +2,13 @@ namespace Example\Client\Resource\Sync\Project; +use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use Example\Client\Resource\Project\Build as BaseBuild; class Build extends BaseBuild { public function refresh() : Build { - return $this->wait($this->callAsync('refresh')); + return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Build', $this))); } } diff --git a/tests/expected-app/src/Resources/Sync/Project/Config.php b/tests/expected-app/src/Resources/Sync/Project/Config.php index 7c16e29..36e909b 100644 --- a/tests/expected-app/src/Resources/Sync/Project/Config.php +++ b/tests/expected-app/src/Resources/Sync/Project/Config.php @@ -2,12 +2,13 @@ namespace Example\Client\Resource\Sync\Project; +use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; use Example\Client\Resource\Project\Config as BaseConfig; class Config extends BaseConfig { public function refresh() : Config { - return $this->wait($this->callAsync('refresh')); + return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Config', $this))); } }