Skip to content

Commit

Permalink
https://github.com/laminas/laminas-code/pull/145
Browse files Browse the repository at this point in the history
  • Loading branch information
jakublabno committed Aug 14, 2022
1 parent f0c043f commit e606216
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 170 deletions.
64 changes: 46 additions & 18 deletions src/Generator/AttributeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,78 @@
namespace Laminas\Code\Generator;

use Laminas\Code\Generator\AttributeGenerator\AttributeAssembler;
use Laminas\Code\Generator\AttributeGenerator\AttributeAssemblerBag;
use Laminas\Code\Generator\AttributeGenerator\AttributeAssemblerFactory;
use Laminas\Code\Generator\AttributeGenerator\AttributeBuilder;
use Laminas\Code\Generator\AttributeGenerator\AttributePrototype;
use Laminas\Code\Generator\AttributeGenerator\AttributeWithArgumentsAssembler;
use Laminas\Code\Generator\AttributeGenerator\SimpleAttributeAssembler;
use ReflectionAttribute;
use ReflectionClass;

class AttributeGenerator extends AbstractGenerator
final class AttributeGenerator implements GeneratorInterface
{
private function __construct(private AttributeAssemblerBag $attributeAssemblerBag)
private array $assemblers;

private function __construct(AttributeAssembler ...$assembler)
{
$this->assemblers = $assembler;
}

public function generate(): string
{
$generatedAttributes = array_map(fn(AttributeAssembler $attributeAssembler) => $attributeAssembler->__toString(),
$this->attributeAssemblerBag->toArray(),
$generatedAttributes = array_map(fn(AttributeAssembler $attributeAssembler) => $attributeAssembler->assemble(),
$this->assemblers,
);

return implode(self::LINE_FEED, $generatedAttributes);
return implode(AbstractGenerator::LINE_FEED, $generatedAttributes);
}

public static function fromReflection(ReflectionClass $reflectionClass): self
public static function fromPrototype(AttributePrototype ...$attributePrototype): self
{
return new self(AttributeAssemblerFactory::createForClassByReflection($reflectionClass));
$assemblers = [];

foreach ($attributePrototype as $prototype) {
$assemblers[] = self::negotiateAssembler($prototype);
}

return new self(...$assemblers);
}

public static function fromBuilder(AttributeBuilder $attributeBuilder): self
public static function fromReflection(ReflectionClass $reflectionClass): self
{
return new self(AttributeAssemblerFactory::createForClassFromBuilder($attributeBuilder));
$attributes = $reflectionClass->getAttributes();
$assemblers = [];

foreach ($attributes as $attribute) {
$assembler = self::negotiateAssembler($attribute);

$assemblers[] = $assembler;
}

return new self(...$assemblers);
}

public static function fromArray(array $definitions): self
{
$builder = new AttributeBuilder();
$assemblers = [];

foreach ($definitions as $definition) {
@list($attributeName, $attributeArguments) = $definition;

if (!isset($attributeArguments)) {
$attributeArguments = [];
}
$prototype = new AttributePrototype($attributeName, $attributeArguments ?? []);

$assemblers[] = self::negotiateAssembler($prototype);
}

return new self(...$assemblers);
}

private static function negotiateAssembler(ReflectionAttribute|AttributePrototype $reflectionPrototype): AttributeAssembler
{
$hasArguments = !empty($reflectionPrototype->getArguments());

$builder->add($attributeName, $attributeArguments);
if ($hasArguments) {
return new AttributeWithArgumentsAssembler($reflectionPrototype);
}

return self::fromBuilder($builder);
return new SimpleAttributeAssembler($reflectionPrototype);
}
}
5 changes: 2 additions & 3 deletions src/Generator/AttributeGenerator/AttributeAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace Laminas\Code\Generator\AttributeGenerator;

use Stringable;

interface AttributeAssembler extends Stringable
interface AttributeAssembler
{
public function assemble(): string;
}
23 changes: 0 additions & 23 deletions src/Generator/AttributeGenerator/AttributeAssemblerBag.php

This file was deleted.

50 changes: 0 additions & 50 deletions src/Generator/AttributeGenerator/AttributeAssemblerFactory.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Generator/AttributeGenerator/AttributeBuilder.php

This file was deleted.

5 changes: 4 additions & 1 deletion src/Generator/AttributeGenerator/AttributePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Laminas\Code\Generator\AttributeGenerator;

//TODO Enum in PHP8.1
/**
* @internal
*/
final class AttributePart
{
public const T_ATTR_START = '#[';
Expand All @@ -13,6 +16,6 @@ final class AttributePart
public const T_ATTR_ARGUMENTS_LIST_START = '(';
public const T_ATTR_ARGUMENTS_LIST_END = ')';

public const T_ATTR_ARGUMENTS_LIST_ASSIGN_OPERAND = ':';
public const T_ATTR_ARGUMENTS_LIST_ASSIGN_OPERAND = ': ';
public const T_ATTR_ARGUMENTS_LIST_SEPARATOR = ', ';
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

final class AttributeWithArgumentsAssembler extends AbstractAttributeAssembler
{
public function __toString(): string
public function assemble(): string
{
$attributeName = $this->getName();

Expand All @@ -24,7 +24,7 @@ private function generateArguments(string &$output): void
$argumentsList = [];

foreach ($this->getArguments() as $argumentName => $argumentValue) {
$argumentsList[] = $argumentName . ': ' . $this->formatArgumentValue($argumentValue);
$argumentsList[] = $argumentName . AttributePart::T_ATTR_ARGUMENTS_LIST_ASSIGN_OPERAND . $this->formatArgumentValue($argumentValue);
}

$output .= implode(AttributePart::T_ATTR_ARGUMENTS_LIST_SEPARATOR, $argumentsList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(ReflectionAttribute $attributePrototype)
$this->assertAttributeWithoutArguments();
}

public function __toString(): string
public function assemble(): string
{
$attributeName = $this->getName();

Expand Down
11 changes: 7 additions & 4 deletions src/Generator/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function fromArray(array $array)
$cg->setDocBlock($docBlock);
break;
case 'attribute':
$generator = $value instanceof AttributeBuilder ? AttributeGenerator::fromBuilder($value) : AttributeGenerator::fromArray($value);
$generator = $value instanceof AttributeGenerator ? $value : AttributeGenerator::fromArray($value);
$cg->setAttributes($generator);
break;
case 'flags':
Expand Down Expand Up @@ -235,17 +235,17 @@ public static function fromArray(array $array)
* @psalm-param array<class-string> $interfaces
* @param PropertyGenerator[]|string[]|array[] $properties
* @param MethodGenerator[]|string[]|array[] $methods
* @param DocBlockGenerator $docBlock
*/
public function __construct(
$name = null,
string $name = null,
$namespaceName = null,
$flags = null,
$extends = null,
array $interfaces = [],
array $properties = [],
array $methods = [],
$docBlock = null
DocBlockGenerator $docBlock = null,
AttributeGenerator $attributeGenerator = null,
) {
$this->traitUsageGenerator = new TraitUsageGenerator($this);

Expand Down Expand Up @@ -273,6 +273,9 @@ public function __construct(
if ($docBlock !== null) {
$this->setDocBlock($docBlock);
}
if ($attributeGenerator) {
$this->setAttributes($attributeGenerator);
}
}

/**
Expand Down
Loading

0 comments on commit e606216

Please sign in to comment.