From ed91568accc3252f99a199fe21324d92178aab4c Mon Sep 17 00:00:00 2001 From: Kamil Kuzminski Date: Wed, 9 Mar 2022 16:54:08 +0100 Subject: [PATCH 1/3] Make the importer extendable --- src/Importer/Importer.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Importer/Importer.php b/src/Importer/Importer.php index 776835570..15c386a52 100644 --- a/src/Importer/Importer.php +++ b/src/Importer/Importer.php @@ -63,9 +63,9 @@ public static function import($paths) } }); } else if (preg_match('/\.ya?ml$/i', $path)) { - self::$recipeFilename = basename($path); - self::$recipeSource = file_get_contents($path); - $root = array_filter(Yaml::parse(self::$recipeSource), static function (string $key) { + static::$recipeFilename = basename($path); + static::$recipeSource = file_get_contents($path); + $root = array_filter(Yaml::parse(static::$recipeSource), static function (string $key) { return substr($key, 0, 1) !== '.'; }, ARRAY_FILTER_USE_KEY); @@ -79,7 +79,7 @@ public static function import($paths) $validator = new Validator(new Factory($schemaStorage)); $validator->validate($root, $yamlSchema, Constraint::CHECK_MODE_TYPE_CAST); if (!$validator->isValid()) { - $msg = "YAML " . self::$recipeFilename . " does not validate. Violations:\n"; + $msg = "YAML " . static::$recipeFilename . " does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) { $msg .= "[{$error['property']}] {$error['message']}\n"; } @@ -87,7 +87,7 @@ public static function import($paths) } foreach (array_keys($root) as $key) { - self::$key($root[$key]); + static::$key($root[$key]); } } else { throw new Exception("Unknown file format: $path\nOnly .php and .yaml supported."); @@ -145,8 +145,8 @@ protected static function tasks(array $tasks) try { run($run); } catch (Exception $e) { - $e->setTaskFilename(self::$recipeFilename); - $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run)); + $e->setTaskFilename(static::$recipeFilename); + $e->setTaskLineNumber(find_line_number(static::$recipeSource, $run)); throw $e; } }; @@ -163,8 +163,8 @@ protected static function tasks(array $tasks) try { runLocally($run_locally); } catch (Exception $e) { - $e->setTaskFilename(self::$recipeFilename); - $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run_locally)); + $e->setTaskFilename(static::$recipeFilename); + $e->setTaskLineNumber(find_line_number(static::$recipeSource, $run_locally)); throw $e; } }; From a839763db8f9a65247c2f28b6326076d9b43b8d1 Mon Sep 17 00:00:00 2001 From: Kamil Kuzminski Date: Wed, 9 Mar 2022 17:10:27 +0100 Subject: [PATCH 2/3] Fix the existing unit tests --- src/Importer/Importer.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Importer/Importer.php b/src/Importer/Importer.php index 15c386a52..00e48d279 100644 --- a/src/Importer/Importer.php +++ b/src/Importer/Importer.php @@ -63,9 +63,9 @@ public static function import($paths) } }); } else if (preg_match('/\.ya?ml$/i', $path)) { - static::$recipeFilename = basename($path); - static::$recipeSource = file_get_contents($path); - $root = array_filter(Yaml::parse(static::$recipeSource), static function (string $key) { + self::$recipeFilename = basename($path); + self::$recipeSource = file_get_contents($path); + $root = array_filter(Yaml::parse(self::$recipeSource), static function (string $key) { return substr($key, 0, 1) !== '.'; }, ARRAY_FILTER_USE_KEY); @@ -79,7 +79,7 @@ public static function import($paths) $validator = new Validator(new Factory($schemaStorage)); $validator->validate($root, $yamlSchema, Constraint::CHECK_MODE_TYPE_CAST); if (!$validator->isValid()) { - $msg = "YAML " . static::$recipeFilename . " does not validate. Violations:\n"; + $msg = "YAML " . self::$recipeFilename . " does not validate. Violations:\n"; foreach ($validator->getErrors() as $error) { $msg .= "[{$error['property']}] {$error['message']}\n"; } @@ -145,8 +145,8 @@ protected static function tasks(array $tasks) try { run($run); } catch (Exception $e) { - $e->setTaskFilename(static::$recipeFilename); - $e->setTaskLineNumber(find_line_number(static::$recipeSource, $run)); + $e->setTaskFilename(self::$recipeFilename); + $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run)); throw $e; } }; @@ -163,8 +163,8 @@ protected static function tasks(array $tasks) try { runLocally($run_locally); } catch (Exception $e) { - $e->setTaskFilename(static::$recipeFilename); - $e->setTaskLineNumber(find_line_number(static::$recipeSource, $run_locally)); + $e->setTaskFilename(self::$recipeFilename); + $e->setTaskLineNumber(find_line_number(self::$recipeSource, $run_locally)); throw $e; } }; From 9321c4ea2bbebcad10052cde191fb5bbd6632577 Mon Sep 17 00:00:00 2001 From: Kamil Kuzminski Date: Thu, 10 Mar 2022 11:01:26 +0100 Subject: [PATCH 3/3] Add the unit test --- tests/src/Importer/ImporterTest.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/src/Importer/ImporterTest.php b/tests/src/Importer/ImporterTest.php index e3d6751b0..1f03602db 100644 --- a/tests/src/Importer/ImporterTest.php +++ b/tests/src/Importer/ImporterTest.php @@ -24,6 +24,29 @@ public function tearDown(): void Deployer::get()->output = $this->previousOutput; } + public function testCanOneOverrideStaticMethod(): void + { + $extendedImporter = new class extends Importer + { + public static $config = []; + + protected static function config(array $config) + { + static::$config = $config; + } + }; + + $data = << 'bar'], $extendedImporter::$config); + } + public function testImporterIgnoresYamlHiddenKeys(): void { $data = <<