From 1927d142773a674b252bdfb0b7fea20c455d75db Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Fri, 1 Nov 2019 09:14:39 -0400 Subject: [PATCH 1/2] Fixing bug when a yaml key with no value ends a file --- src/Util/YamlSourceManipulator.php | 8 +++++++- tests/Util/yaml_fixtures/last_value_is_blank.test | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/Util/yaml_fixtures/last_value_is_blank.test diff --git a/src/Util/YamlSourceManipulator.php b/src/Util/YamlSourceManipulator.php index b75e5173a..5524ba697 100644 --- a/src/Util/YamlSourceManipulator.php +++ b/src/Util/YamlSourceManipulator.php @@ -217,7 +217,7 @@ private function updateData(array $newData) } // 3b) value DID change - $this->log('updating value'); + $this->log(sprintf('updating value to {%s}', is_array($newVal) ? '' : $newVal)); $this->changeValueInYaml($newVal); } @@ -765,6 +765,12 @@ private function findEndPositionOfValue($value, $offset = null) $offset = null === $offset ? $this->currentPosition : $offset; + // a value like "foo:" can simply end a file + // this means the value is null + if ($offset === strlen($this->contents)) { + return $offset; + } + preg_match(sprintf('#%s#', $pattern), $this->contents, $matches, PREG_OFFSET_CAPTURE, $offset); if (empty($matches)) { throw new YamlManipulationFailedException(sprintf('Cannot find the original value "%s"', $value)); diff --git a/tests/Util/yaml_fixtures/last_value_is_blank.test b/tests/Util/yaml_fixtures/last_value_is_blank.test new file mode 100644 index 000000000..03322a0bf --- /dev/null +++ b/tests/Util/yaml_fixtures/last_value_is_blank.test @@ -0,0 +1,7 @@ +security: + access_control: +=== +$data['security']['access_control'] = 'foo'; +=== +security: + access_control: foo \ No newline at end of file From aa6c35897a6bb9cbde2da4f167058dac95ae3209 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sun, 3 Nov 2019 14:53:39 -0500 Subject: [PATCH 2/2] Fixing bug where a scalar value would become and array, but comment placement was wrong --- src/Util/YamlSourceManipulator.php | 22 +++++++++++++++++-- .../simple_sequence_add_array.test | 13 +++++++++++ ...mple_sequence_add_array_with_comments.test | 17 ++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/Util/yaml_fixtures/simple_sequence_add_array.test create mode 100644 tests/Util/yaml_fixtures/simple_sequence_add_array_with_comments.test diff --git a/src/Util/YamlSourceManipulator.php b/src/Util/YamlSourceManipulator.php index 5524ba697..cf6af0ff5 100644 --- a/src/Util/YamlSourceManipulator.php +++ b/src/Util/YamlSourceManipulator.php @@ -461,12 +461,30 @@ private function changeValueInYaml($value) // empty space between key & value $newYamlValue = ' '.$newYamlValue; } + + $newPosition = $this->currentPosition + \strlen($newYamlValue); + $isNextContentComment = $this->isPreviousLineComment($newPosition); + if ($isNextContentComment) { + $newPosition++; + } + $newContents = substr($this->contents, 0, $this->currentPosition) .$newYamlValue + /* + * If the next line is a comment, this means we probably had + * a structure that looks like this: + * access_control: + * # - { path: ^/admin, roles: ROLE_ADMIN } + * + * In this odd case, we need to know that the next line + * is a comment, so we can add an extra line break. + * Otherwise, the result is something like: + * access_control: + * - { path: /foo, roles: ROLE_USER } # - { path: ^/admin, roles: ROLE_ADMIN } + */ + .($isNextContentComment ? "\n" : '') .substr($this->contents, $endValuePosition); - $newPosition = $this->currentPosition + \strlen($newYamlValue); - $newData = $this->currentData; $newData = $this->setValueAtCurrentPath($value, $newData); diff --git a/tests/Util/yaml_fixtures/simple_sequence_add_array.test b/tests/Util/yaml_fixtures/simple_sequence_add_array.test new file mode 100644 index 000000000..eb22ac57f --- /dev/null +++ b/tests/Util/yaml_fixtures/simple_sequence_add_array.test @@ -0,0 +1,13 @@ +# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers +security: + access_control: +=== +$data['security']['access_control'] = []; +$data['security']['access_control'][] = ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY']; +=== +# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers +security: + access_control: + - + path: ^/login$ + roles: IS_AUTHENTICATED_ANONYMOUSLY \ No newline at end of file diff --git a/tests/Util/yaml_fixtures/simple_sequence_add_array_with_comments.test b/tests/Util/yaml_fixtures/simple_sequence_add_array_with_comments.test new file mode 100644 index 000000000..e44df43a2 --- /dev/null +++ b/tests/Util/yaml_fixtures/simple_sequence_add_array_with_comments.test @@ -0,0 +1,17 @@ +# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers +security: + access_control: + # - { path: ^/admin, roles: ROLE_ADMIN } + # - { path: ^/profile, roles: ROLE_USER } +=== +$data['security']['access_control'] = []; +$data['security']['access_control'][] = ['path' => '^/login$', 'roles' => 'IS_AUTHENTICATED_ANONYMOUSLY']; +=== +# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers +security: + access_control: + - + path: ^/login$ + roles: IS_AUTHENTICATED_ANONYMOUSLY + # - { path: ^/admin, roles: ROLE_ADMIN } + # - { path: ^/profile, roles: ROLE_USER } \ No newline at end of file