Skip to content

Commit

Permalink
Add docblocks question to Config Wizard command (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
simivar authored Apr 30, 2021
1 parent a3faf36 commit b0c9708
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 22 deletions.
76 changes: 56 additions & 20 deletions src/Phpro/SoapClient/CodeGenerator/ConfigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,7 @@ class ConfigGenerator implements GeneratorInterface
BODY;

const RULESET_DEFAULT = <<<RULESET
->addRule(new Rules\AssembleRule(new Assembler\GetterAssembler(new Assembler\GetterAssemblerOptions())))
->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler()))
RULESET;


const RULESET_REQUEST_RESPONSE = <<<RULESET
->addRule(
new Rules\IsRequestRule(
\$engine->getMetadata(),
new Rules\MultiRule([
new Rules\AssembleRule(new Assembler\RequestAssembler()),
new Rules\AssembleRule(new Assembler\ConstructorAssembler(new Assembler\ConstructorAssemblerOptions())),
])
)
)
const RULESET_RESPONSE = <<<RULESET
->addRule(
new Rules\IsResultRule(
\$engine->getMetadata(),
Expand All @@ -56,8 +41,6 @@ class ConfigGenerator implements GeneratorInterface
))
EOENGINE;



/**
* @param string $name
* @param string $value
Expand Down Expand Up @@ -103,11 +86,64 @@ public function generate(FileGenerator $file, $context): string
$body .= $this->generateSetter($name, $value, $file);
}

$body .= $this->parseIndentedRuleSet($file, self::RULESET_DEFAULT);
$body .= $this->parseIndentedRuleSet($file, self::RULESET_REQUEST_RESPONSE);
$body .= $this->parseIndentedRuleSet($file, $this->generateGetterSetterRuleSet($context));
$body .= $this->parseIndentedRuleSet($file, $this->generateRequestRuleSet($context));
$body .= $this->parseIndentedRuleSet($file, self::RULESET_RESPONSE);

$file->setBody($body.';'.PHP_EOL);

return $file->generate();
}

private function generateGetterSetterRuleSet(ConfigContext $context): string
{
if ($context->isGenerateDocblocks()) {
return <<<RULESET
->addRule(new Rules\AssembleRule(new Assembler\GetterAssembler(new Assembler\GetterAssemblerOptions())))
->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler(
new Assembler\ImmutableSetterAssemblerOptions()
)))
RULESET;
}

return <<<RULESET
->addRule(new Rules\AssembleRule(new Assembler\GetterAssembler(
(new Assembler\GetterAssemblerOptions())->withDocBlocks(false)
)))
->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler(
(new Assembler\ImmutableSetterAssemblerOptions())->withDocBlocks(false)
)))
RULESET;
}

private function generateRequestRuleSet(ConfigContext $context): string
{
if ($context->isGenerateDocblocks()) {
return <<<REQUEST
->addRule(
new Rules\IsRequestRule(
\$engine->getMetadata(),
new Rules\MultiRule([
new Rules\AssembleRule(new Assembler\RequestAssembler()),
new Rules\AssembleRule(new Assembler\ConstructorAssembler(new Assembler\ConstructorAssemblerOptions())),
])
)
)
REQUEST;
}

return <<<REQUEST
->addRule(
new Rules\IsRequestRule(
\$engine->getMetadata(),
new Rules\MultiRule([
new Rules\AssembleRule(new Assembler\RequestAssembler()),
new Rules\AssembleRule(new Assembler\ConstructorAssembler(
(new Assembler\ConstructorAssemblerOptions())->withDocBlocks(false)
)),
])
)
)
REQUEST;
}
}
14 changes: 14 additions & 0 deletions src/Phpro/SoapClient/CodeGenerator/Context/ConfigContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class ConfigContext implements ContextInterface
*/
private $wsdl;

private bool $generateDocblocks = true;

public function addSetter(string $name, string $value): self
{
$this->setters[$name] = $value;
Expand Down Expand Up @@ -44,4 +46,16 @@ public function setWsdl(string $wsdl): self

return $this;
}

public function setGenerateDocblocks(bool $generateDocblocks): self
{
$this->generateDocblocks = $generateDocblocks;

return $this;
}

public function isGenerateDocblocks(): bool
{
return $this->generateDocblocks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$context->setWsdl($io->ask('Wsdl location (URL or path to file)', null, $required));
$context->setGenerateDocblocks($io->confirm('Should methods be generated with docblocks?', true));
$name = $io->ask(
'Generic name used to name this client (Results in <name>Client <name>Classmap etc.)',
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ConfigGeneratorTest extends TestCase
{
public function testGenerate()
public function testGenerate(): void
{
$expected = <<<CONTENT
<?php
Expand All @@ -34,7 +34,9 @@ public function testGenerate()
->setClassmapName('Classmap')
->setClassmapNamespace('App\\\\Classmap')
->addRule(new Rules\AssembleRule(new Assembler\GetterAssembler(new Assembler\GetterAssemblerOptions())))
->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler()))
->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler(
new Assembler\ImmutableSetterAssemblerOptions()
)))
->addRule(
new Rules\IsRequestRule(
\$engine->getMetadata(),
Expand Down Expand Up @@ -71,4 +73,58 @@ public function testGenerate()
$generated = $generator->generate(new FileGenerator(), $context);
self::assertEquals($expected, $generated);
}

public function testGenerateWithoutDocblocks(): void
{
$expected = <<<CONTENT
<?php
use Phpro\SoapClient\CodeGenerator\Assembler;
use Phpro\SoapClient\CodeGenerator\Rules;
use Phpro\SoapClient\CodeGenerator\Config\Config;
use Phpro\SoapClient\Soap\Driver\ExtSoap\ExtSoapOptions;
use Phpro\SoapClient\Soap\Driver\ExtSoap\ExtSoapEngineFactory;
return Config::create()
->setEngine(\$engine = ExtSoapEngineFactory::fromOptions(
ExtSoapOptions::defaults('wsdl.xml', [])
->disableWsdlCache()
))
->addRule(new Rules\AssembleRule(new Assembler\GetterAssembler(
(new Assembler\GetterAssemblerOptions())->withDocBlocks(false)
)))
->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler(
(new Assembler\ImmutableSetterAssemblerOptions())->withDocBlocks(false)
)))
->addRule(
new Rules\IsRequestRule(
\$engine->getMetadata(),
new Rules\MultiRule([
new Rules\AssembleRule(new Assembler\RequestAssembler()),
new Rules\AssembleRule(new Assembler\ConstructorAssembler(
(new Assembler\ConstructorAssemblerOptions())->withDocBlocks(false)
)),
])
)
)
->addRule(
new Rules\IsResultRule(
\$engine->getMetadata(),
new Rules\MultiRule([
new Rules\AssembleRule(new Assembler\ResultAssembler()),
])
)
)
;
CONTENT;
$context = new ConfigContext();
$context
->setWsdl('wsdl.xml')
->setGenerateDocblocks(false);

$generator = new ConfigGenerator();
$generated = $generator->generate(new FileGenerator(), $context);
self::assertEquals($expected, $generated);
}
}

0 comments on commit b0c9708

Please sign in to comment.