Skip to content

Generate different code for async and sync resources refresh method #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions src/FileGenerators/AbstractExtendingClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
;
}
}
45 changes: 45 additions & 0 deletions src/FileGenerators/AsyncClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
}
56 changes: 56 additions & 0 deletions src/FileGenerators/SyncClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
;
}
}
2 changes: 1 addition & 1 deletion tests/expected-app/src/Resources/Async/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
}
2 changes: 1 addition & 1 deletion tests/expected-app/src/Resources/Async/Project/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
}
2 changes: 1 addition & 1 deletion tests/expected-app/src/Resources/Async/Project/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
}
3 changes: 2 additions & 1 deletion tests/expected-app/src/Resources/Sync/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
3 changes: 2 additions & 1 deletion tests/expected-app/src/Resources/Sync/Project/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}
3 changes: 2 additions & 1 deletion tests/expected-app/src/Resources/Sync/Project/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}