Skip to content

Commit

Permalink
Merge pull request #19 from webmasterskaya/update-to-base-lib-compatible
Browse files Browse the repository at this point in the history
Update to base lib v1.0.0 compatible
  • Loading branch information
kernusr authored Dec 12, 2024
2 parents d9aa237 + fe53c2c commit c70783b
Show file tree
Hide file tree
Showing 17 changed files with 434 additions and 155 deletions.
14 changes: 6 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
{
"name": "webmasterskaya/base-soap-lib-dev",
"description": "Набор инструментов для генерации кода и разработки PHP-SOAP приложения, совместимый с PHP 7.1",
"description": "Набор инструментов для генерации кода и разработки PHP-SOAP приложения",
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.1"
},
"require-dev": {
"phpro/soap-client": "~v1",
"laminas/laminas-code": "~3.1.0",
"webmasterskaya/base-soap-lib": "^0.0.2"
},
"php": "~8.2.0 || ~8.3.0 || ~8.4.0",
"phpro/soap-client": "~v3",
"laminas/laminas-code": "^4.14.0",
"webmasterskaya/base-soap-lib": "^1.0"
},
"autoload": {
"psr-4": {
"Webmasterskaya\\Soap\\Base\\Dev\\": "src/"
Expand Down
32 changes: 4 additions & 28 deletions src/CodeGenerator/Assembler/ArrayAccessAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ private function implementOffsetExists(ClassGenerator $class, Property $firstPro
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setParameters([['name' => 'offset']]);
$methodGenerator->setReturnType('bool');
$methodGenerator->setDocBlock(
DocBlockGenerator::fromArray([
'tags' => [
new Tag\GenericTag('inheritDoc')
]
])
);
$methodGenerator->setDocBlock(new DocBlockGenerator(tags: [new Tag\GenericTag('inheritDoc')]));

$methodGenerator->setBody(
sprintf('return is_array($this->%1$s) && isset($this->%1$s[$offset]);', $firstProperty->getName())
Expand All @@ -84,13 +78,7 @@ private function implementOffsetGet(ClassGenerator $class, Property $firstProper
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setParameters([['name' => 'offset']]);
$methodGenerator->setReturnType(null);
$methodGenerator->setDocBlock(
DocBlockGenerator::fromArray([
'tags' => [
new Tag\GenericTag('inheritDoc')
]
])
);
$methodGenerator->setDocBlock(new DocBlockGenerator(tags: [new Tag\GenericTag('inheritDoc')]));

$lines = [
sprintf('return (is_array($this->%1$s) && isset($this->%1$s[$offset]))', $firstProperty->getName()),
Expand All @@ -112,13 +100,7 @@ private function implementOffsetSet(ClassGenerator $class, Property $firstProper
$methodGenerator = new MethodGenerator($methodName);
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setParameters([['name' => 'offset'], ['name' => 'value']]);
$methodGenerator->setDocBlock(
DocBlockGenerator::fromArray([
'tags' => [
new Tag\GenericTag('inheritDoc')
]
])
);
$methodGenerator->setDocBlock(new DocBlockGenerator(tags: [new Tag\GenericTag('inheritDoc')]));

$lines = [
sprintf('if (!($value instanceof %s)) {', $firstProperty->getType()),
Expand Down Expand Up @@ -175,13 +157,7 @@ private function implementOffsetUnset(ClassGenerator $class, Property $firstProp
$methodGenerator = new MethodGenerator($methodName);
$methodGenerator->setVisibility(MethodGenerator::VISIBILITY_PUBLIC);
$methodGenerator->setParameters([['name' => 'offset']]);
$methodGenerator->setDocBlock(
DocBlockGenerator::fromArray([
'tags' => [
new Tag\GenericTag('inheritDoc')
]
])
);
$methodGenerator->setDocBlock(new DocBlockGenerator(tags: [new Tag\GenericTag('inheritDoc')]));

$lines = [
sprintf('if(is_array($this->%1$s)) {', $firstProperty->getName()),
Expand Down
49 changes: 0 additions & 49 deletions src/CodeGenerator/Assembler/ArrayPropertyAssembler.php

This file was deleted.

10 changes: 5 additions & 5 deletions src/CodeGenerator/Assembler/ArrayPropertyPatchAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
use Phpro\SoapClient\CodeGenerator\Model\Property;
use Phpro\SoapClient\CodeGenerator\Model\Type;
use Phpro\SoapClient\CodeGenerator\Util\Normalizer;
use Phpro\SoapClient\Exception\MetadataException;
use Phpro\SoapClient\Soap\Engine\Metadata\MetadataInterface;
use Soap\Engine\Exception\MetadataException;
use Soap\Engine\Metadata\Metadata;

class ArrayPropertyPatchAssembler implements Assembler\AssemblerInterface
{

/**
* @var MetadataInterface
* @var Metadata
*/
private $metadata;

public function __construct(MetadataInterface $metadata)
public function __construct(Metadata $metadata)
{
$this->metadata = $metadata;
}
Expand Down Expand Up @@ -115,7 +115,7 @@ private function getTypeProperty($context): ?Property
$property = $context->getProperty();

try {
$foundPropertyType = $this->metadata->getTypes()->fetchOneByName(
$foundPropertyType = $this->metadata->getTypes()->fetchFirstByName(
Normalizer::getClassNameFromFQN($property->getType())
);
} catch (MetadataException $e) {
Expand Down
21 changes: 5 additions & 16 deletions src/CodeGenerator/Assembler/ArrayTypePatchAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ private function applySetterPatch(Generator\ClassGenerator $class, Property $pro

if ($options && $options->useDocBlocks()) {
$docBlock = $method->getDocBlock();
$method->setDocBlock(
Generator\DocBlockGenerator::fromArray($this->replaceDocblockParam($docBlock, $property))
);
$method->setDocBlock(new Generator\DocBlockGenerator(...$this->replaceDocblockParam($docBlock, $property)));
}

$method->setParameter(['name' => $property->getName()]);
Expand Down Expand Up @@ -234,19 +232,10 @@ private function applyPropertyTypePatch(Generator\ClassGenerator $class, Propert
{
$class->removeProperty($property->getName());
$class->addPropertyFromGenerator(
Generator\PropertyGenerator::fromArray([
'name' => $property->getName(),
'visibility' => Generator\PropertyGenerator::VISIBILITY_PRIVATE,
'omitdefaultvalue' => true,
'docblock' => DocBlockGeneratorFactory::fromArray([
'tags' => [
[
'name' => 'var',
'description' => sprintf('%s[]', $property->getType()),
],
]
])
])
(new Generator\PropertyGenerator($property->getName()))
->setVisibility(Generator\PropertyGenerator::VISIBILITY_PRIVATE)
->setDocBlock(new Generator\DocBlockGenerator(tags: [new Generator\DocBlock\Tag\VarTag(description: sprintf('%s[]', $property->getType()))]))
->omitDefaultValue(true)
);
}

Expand Down
18 changes: 4 additions & 14 deletions src/CodeGenerator/Assembler/ClassMapAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ public function canAssemble(ContextInterface $context): bool
*/
public function assemble(ContextInterface $context)
{
$class = ClassGenerator::fromArray(
[
'name' => $context->getName(),
'implementedinterfaces' => [Normalizer::getClassNameFromFQN(ClientClassMapCollectionInterface::class)]
]
);
$class = new ClassGenerator($context->getName());
$class->setImplementedInterfaces([Normalizer::getClassNameFromFQN(ClientClassMapCollectionInterface::class)]);
$file = $context->getFile();
$file->setClass($class);
$file->setNamespace($context->getNamespace());
Expand All @@ -54,14 +50,8 @@ public function assemble(ContextInterface $context)
$classMap = $this->assembleClassMap($typeMap, $linefeed, $file->getIndentation());
$code = $this->assembleClassMapCollection($classMap, $linefeed).$linefeed;
$class->addMethodFromGenerator(
MethodGenerator::fromArray(
[
'name' => '__invoke',
'static' => false,
'body' => 'return '.$code,
'returntype' => ClassMapCollection::class,
]
)
(new MethodGenerator('__invoke', body: 'return ' . $code))
->setReturnType(ClassMapCollection::class)
);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
Expand Down
75 changes: 75 additions & 0 deletions src/CodeGenerator/Assembler/ClientConstructorAssembler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Webmasterskaya\Soap\Base\Dev\CodeGenerator\Assembler;

use Laminas\Code\Generator\AbstractMemberGenerator;
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\DocBlock\Tag\VarTag;
use Laminas\Code\Generator\DocBlockGenerator;
use Laminas\Code\Generator\MethodGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\PropertyGenerator;
use Laminas\Code\Generator\TypeGenerator;
use Phpro\SoapClient\CodeGenerator\Context\ClientContext;
use Phpro\SoapClient\CodeGenerator\Context\ContextInterface;
use Phpro\SoapClient\Exception\AssemblerException;
use Webmasterskaya\Soap\Base\Caller\CallerInterface;

use function Psl\Type\non_empty_string;

class ClientConstructorAssembler extends \Phpro\SoapClient\CodeGenerator\Assembler\ClientConstructorAssembler
{
public function assemble(ContextInterface $context)
{
if (!$context instanceof ClientContext) {
throw new AssemblerException(
__METHOD__ . ' expects an ' . ClientContext::class . ' as input ' . get_class($context) . ' given'
);
}

$class = $context->getClass();
try {
$caller = $this->generateClassNameAndAddImport(CallerInterface::class, $class);
$class->removeProperty('caller');
$class->addPropertyFromGenerator(
(new PropertyGenerator(
name: 'caller',
flags: AbstractMemberGenerator::FLAG_PRIVATE,
type: TypeGenerator::fromTypeString(CallerInterface::class)
))
->setDocBlock(new DocBlockGenerator(tags: [new VarTag(description: $caller)]))
->omitDefaultValue(true)
);
$class->removeMethod('__construct');
$class->addMethodFromGenerator(
(new MethodGenerator(
name: '__construct',
parameters: [
new ParameterGenerator('caller', CallerInterface::class)
],
body: '$this->caller = $caller;'
))
);
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}

return true;
}

/**
* @param non-empty-string $fqcn
*/
private function generateClassNameAndAddImport(string $fqcn, ClassGenerator $class): string
{
$fqcn = non_empty_string()->assert(ltrim($fqcn, '\\'));
$parts = explode('\\', $fqcn);
$className = array_pop($parts);

if (!\in_array($fqcn, $class->getUses(), true)) {
$class->addUse($fqcn);
}

return $className;
}
}
Loading

0 comments on commit c70783b

Please sign in to comment.