Skip to content

Commit

Permalink
[Config] Fix using null values with config builders
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Mar 21, 2022
1 parent d65e1bd commit 05624c3
Show file tree
Hide file tree
Showing 26 changed files with 253 additions and 110 deletions.
7 changes: 4 additions & 3 deletions Builder/ClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function build(): string
USE
/**
* This class is automatically generated to help creating config.
* This class is automatically generated to help in creating a config.
*/
class CLASS IMPLEMENTS
{
Expand Down Expand Up @@ -124,14 +124,15 @@ public function addMethod(string $name, string $body, array $params = []): void
$this->methods[] = new Method(strtr($body, ['NAME' => $this->camelCase($name)] + $params));
}

public function addProperty(string $name, string $classType = null): Property
public function addProperty(string $name, string $classType = null, string $defaultValue = null): Property
{
$property = new Property($name, '_' !== $name[0] ? $this->camelCase($name) : $name);
if (null !== $classType) {
$property->setType($classType);
}
$this->properties[] = $property;
$property->setContent(sprintf('private $%s;', $property->getName()));
$defaultValue = null !== $defaultValue ? sprintf(' = %s', $defaultValue) : '';
$property->setContent(sprintf('private $%s%s;', $property->getName(), $defaultValue));

return $property;
}
Expand Down
30 changes: 21 additions & 9 deletions Builder/ConfigBuilderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
*/
class ConfigBuilderGenerator implements ConfigBuilderGeneratorInterface
{
/**
* @var ClassBuilder[]
*/
private $classes;
private $outputDir;

Expand Down Expand Up @@ -89,6 +92,9 @@ private function writeClasses(): void
foreach ($this->classes as $class) {
$this->buildConstructor($class);
$this->buildToArray($class);
if ($class->getProperties()) {
$class->addProperty('_usedProperties', null, '[]');
}
$this->buildSetExtraKey($class);

file_put_contents($this->getFullPath($class), $class->build());
Expand Down Expand Up @@ -135,6 +141,7 @@ private function handleArrayNode(ArrayNode $node, ClassBuilder $class, string $n
public function NAME(array $value = []): CLASS
{
if (null === $this->PROPERTY) {
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = new CLASS($value);
} elseif ([] !== $value) {
throw new InvalidConfigurationException(\'The node created by "NAME()" has already been initialized. You cannot pass values the second time you call NAME().\');
Expand All @@ -160,6 +167,7 @@ private function handleVariableNode(VariableNode $node, ClassBuilder $class): vo
*/
public function NAME($valueDEFAULT): self
{
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = $value;
return $this;
Expand All @@ -186,6 +194,7 @@ private function handlePrototypedArrayNode(PrototypedArrayNode $node, ClassBuild
*/
public function NAME($value): self
{
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = $value;
return $this;
Expand All @@ -200,6 +209,7 @@ public function NAME($value): self
*/
public function NAME(string $VAR, $VALUE): self
{
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY[$VAR] = $VALUE;
return $this;
Expand All @@ -223,6 +233,8 @@ public function NAME(string $VAR, $VALUE): self
$body = '
public function NAME(array $value = []): CLASS
{
$this->_usedProperties[\'PROPERTY\'] = true;
return $this->PROPERTY[] = new CLASS($value);
}';
$class->addMethod($methodName, $body, ['PROPERTY' => $property->getName(), 'CLASS' => $childClass->getFqcn()]);
Expand All @@ -231,9 +243,11 @@ public function NAME(array $value = []): CLASS
public function NAME(string $VAR, array $VALUE = []): CLASS
{
if (!isset($this->PROPERTY[$VAR])) {
return $this->PROPERTY[$VAR] = new CLASS($value);
$this->_usedProperties[\'PROPERTY\'] = true;
return $this->PROPERTY[$VAR] = new CLASS($VALUE);
}
if ([] === $value) {
if ([] === $VALUE) {
return $this->PROPERTY[$VAR];
}
Expand All @@ -258,6 +272,7 @@ private function handleScalarNode(ScalarNode $node, ClassBuilder $class): void
*/
public function NAME($value): self
{
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = $value;
return $this;
Expand Down Expand Up @@ -367,7 +382,7 @@ private function buildToArray(ClassBuilder $class): void
}

$body .= strtr('
if (null !== $this->PROPERTY) {
if (isset($this->_usedProperties[\'PROPERTY\'])) {
$output[\'ORG_NAME\'] = '.$code.';
}', ['PROPERTY' => $p->getName(), 'ORG_NAME' => $p->getOriginalName()]);
}
Expand Down Expand Up @@ -397,7 +412,8 @@ private function buildConstructor(ClassBuilder $class): void
}

$body .= strtr('
if (isset($value[\'ORG_NAME\'])) {
if (array_key_exists(\'ORG_NAME\', $value)) {
$this->_usedProperties[\'PROPERTY\'] = true;
$this->PROPERTY = '.$code.';
unset($value[\'ORG_NAME\']);
}
Expand Down Expand Up @@ -441,11 +457,7 @@ private function buildSetExtraKey(ClassBuilder $class): void
*/
public function NAME(string $key, $value): self
{
if (null === $value) {
unset($this->_extraKeys[$key]);
} else {
$this->_extraKeys[$key] = $value;
}
$this->_extraKeys[$key] = $value;
return $this;
}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@


/**
* This class is automatically generated to help creating config.
* This class is automatically generated to help in creating a config.
*/
class ReceivingConfig
{
private $priority;
private $color;
private $_usedProperties = [];

/**
* @default null
Expand All @@ -22,6 +23,7 @@ class ReceivingConfig
*/
public function priority($value): self
{
$this->_usedProperties['priority'] = true;
$this->priority = $value;

return $this;
Expand All @@ -34,6 +36,7 @@ public function priority($value): self
*/
public function color($value): self
{
$this->_usedProperties['color'] = true;
$this->color = $value;

return $this;
Expand All @@ -42,12 +45,14 @@ public function color($value): self
public function __construct(array $value = [])
{

if (isset($value['priority'])) {
if (array_key_exists('priority', $value)) {
$this->_usedProperties['priority'] = true;
$this->priority = $value['priority'];
unset($value['priority']);
}

if (isset($value['color'])) {
if (array_key_exists('color', $value)) {
$this->_usedProperties['color'] = true;
$this->color = $value['color'];
unset($value['color']);
}
Expand All @@ -60,10 +65,10 @@ public function __construct(array $value = [])
public function toArray(): array
{
$output = [];
if (null !== $this->priority) {
if (isset($this->_usedProperties['priority'])) {
$output['priority'] = $this->priority;
}
if (null !== $this->color) {
if (isset($this->_usedProperties['color'])) {
$output['color'] = $this->color;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@


/**
* This class is automatically generated to help creating config.
* This class is automatically generated to help in creating a config.
*/
class RoutingConfig
{
private $senders;
private $_usedProperties = [];

/**
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
* @return $this
*/
public function senders($value): self
{
$this->_usedProperties['senders'] = true;
$this->senders = $value;

return $this;
Expand All @@ -28,7 +30,8 @@ public function senders($value): self
public function __construct(array $value = [])
{

if (isset($value['senders'])) {
if (array_key_exists('senders', $value)) {
$this->_usedProperties['senders'] = true;
$this->senders = $value['senders'];
unset($value['senders']);
}
Expand All @@ -41,7 +44,7 @@ public function __construct(array $value = [])
public function toArray(): array
{
$output = [];
if (null !== $this->senders) {
if (isset($this->_usedProperties['senders'])) {
$output['senders'] = $this->senders;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@


/**
* This class is automatically generated to help creating config.
* This class is automatically generated to help in creating a config.
*/
class MessengerConfig
{
private $routing;
private $receiving;
private $_usedProperties = [];

public function routing(string $message_class, array $value = []): \Symfony\Config\AddToList\Messenger\RoutingConfig
{
if (!isset($this->routing[$message_class])) {
$this->_usedProperties['routing'] = true;

return $this->routing[$message_class] = new \Symfony\Config\AddToList\Messenger\RoutingConfig($value);
}
if ([] === $value) {
Expand All @@ -30,18 +33,22 @@ public function routing(string $message_class, array $value = []): \Symfony\Conf

public function receiving(array $value = []): \Symfony\Config\AddToList\Messenger\ReceivingConfig
{
$this->_usedProperties['receiving'] = true;

return $this->receiving[] = new \Symfony\Config\AddToList\Messenger\ReceivingConfig($value);
}

public function __construct(array $value = [])
{

if (isset($value['routing'])) {
if (array_key_exists('routing', $value)) {
$this->_usedProperties['routing'] = true;
$this->routing = array_map(function ($v) { return new \Symfony\Config\AddToList\Messenger\RoutingConfig($v); }, $value['routing']);
unset($value['routing']);
}

if (isset($value['receiving'])) {
if (array_key_exists('receiving', $value)) {
$this->_usedProperties['receiving'] = true;
$this->receiving = array_map(function ($v) { return new \Symfony\Config\AddToList\Messenger\ReceivingConfig($v); }, $value['receiving']);
unset($value['receiving']);
}
Expand All @@ -54,10 +61,10 @@ public function __construct(array $value = [])
public function toArray(): array
{
$output = [];
if (null !== $this->routing) {
if (isset($this->_usedProperties['routing'])) {
$output['routing'] = array_map(function ($v) { return $v->toArray(); }, $this->routing);
}
if (null !== $this->receiving) {
if (isset($this->_usedProperties['receiving'])) {
$output['receiving'] = array_map(function ($v) { return $v->toArray(); }, $this->receiving);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@


/**
* This class is automatically generated to help creating config.
* This class is automatically generated to help in creating a config.
*/
class TranslatorConfig
{
private $fallbacks;
private $sources;
private $_usedProperties = [];

/**
* @param ParamConfigurator|list<mixed|ParamConfigurator> $value
* @return $this
*/
public function fallbacks($value): self
{
$this->_usedProperties['fallbacks'] = true;
$this->fallbacks = $value;

return $this;
Expand All @@ -32,6 +34,7 @@ public function fallbacks($value): self
*/
public function source(string $source_class, $value): self
{
$this->_usedProperties['sources'] = true;
$this->sources[$source_class] = $value;

return $this;
Expand All @@ -40,12 +43,14 @@ public function source(string $source_class, $value): self
public function __construct(array $value = [])
{

if (isset($value['fallbacks'])) {
if (array_key_exists('fallbacks', $value)) {
$this->_usedProperties['fallbacks'] = true;
$this->fallbacks = $value['fallbacks'];
unset($value['fallbacks']);
}

if (isset($value['sources'])) {
if (array_key_exists('sources', $value)) {
$this->_usedProperties['sources'] = true;
$this->sources = $value['sources'];
unset($value['sources']);
}
Expand All @@ -58,10 +63,10 @@ public function __construct(array $value = [])
public function toArray(): array
{
$output = [];
if (null !== $this->fallbacks) {
if (isset($this->_usedProperties['fallbacks'])) {
$output['fallbacks'] = $this->fallbacks;
}
if (null !== $this->sources) {
if (isset($this->_usedProperties['sources'])) {
$output['sources'] = $this->sources;
}

Expand Down
Loading

0 comments on commit 05624c3

Please sign in to comment.