From cbea8d700dfd95be019e91a4ff6ad0cd0e9a84ff Mon Sep 17 00:00:00 2001 From: siad007 Date: Sat, 18 Jan 2020 14:59:18 +0100 Subject: [PATCH 1/3] Added support for creating custom attributes in the parsing phase. --- classes/phing/IntrospectionHelper.php | 4 +++ classes/phing/parser/DynamicAttribute.php | 35 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 classes/phing/parser/DynamicAttribute.php diff --git a/classes/phing/IntrospectionHelper.php b/classes/phing/IntrospectionHelper.php index dd0b200d1c..ce44fd28c4 100644 --- a/classes/phing/IntrospectionHelper.php +++ b/classes/phing/IntrospectionHelper.php @@ -312,6 +312,10 @@ public function setAttribute(Project $project, $element, $attributeName, &$value $as = "set" . strtolower($attributeName); if (!isset($this->attributeSetters[$as])) { + if ($element instanceof DynamicAttribute) { + $element->setDynamicAttribute($attributeName, (string) $value); + return; + } $msg = $this->getElementName($project, $element) . " doesn't support the '$attributeName' attribute."; throw new BuildException($msg); } diff --git a/classes/phing/parser/DynamicAttribute.php b/classes/phing/parser/DynamicAttribute.php new file mode 100644 index 0000000000..b40d553944 --- /dev/null +++ b/classes/phing/parser/DynamicAttribute.php @@ -0,0 +1,35 @@ +. + */ + +/** + * Interface for elements that want to be able to create custom child elements + * at runtime. + * + * @author keith.rogers@unit4.com + * @package phing.parser + */ +interface DynamicAttribute +{ + /** + * @param string $name the name of the attribute + * @param string $value the new value of the attribute + * @throws BuildException when any error occurs + */ + public function setDynamicAttribute(string $name, string $value): void; +} From 181951ccb3c4c113a130395d9328e0dc5bdad61d Mon Sep 17 00:00:00 2001 From: siad007 Date: Sat, 18 Jan 2020 15:04:37 +0100 Subject: [PATCH 2/3] Cleanup phpdocs --- classes/phing/parser/DynamicAttribute.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/classes/phing/parser/DynamicAttribute.php b/classes/phing/parser/DynamicAttribute.php index b40d553944..55bafb84da 100644 --- a/classes/phing/parser/DynamicAttribute.php +++ b/classes/phing/parser/DynamicAttribute.php @@ -18,15 +18,16 @@ */ /** - * Interface for elements that want to be able to create custom child elements - * at runtime. + * Enables a task to control unknown attributes. * - * @author keith.rogers@unit4.com + * @author Siad Ardroumli * @package phing.parser */ interface DynamicAttribute { /** + * Set a named attribute to the given value. + * * @param string $name the name of the attribute * @param string $value the new value of the attribute * @throws BuildException when any error occurs From 3fc115ed68d8e0523e437289b1fc36ac3778699e Mon Sep 17 00:00:00 2001 From: siad007 Date: Sat, 18 Jan 2020 16:27:12 +0100 Subject: [PATCH 3/3] Added tests --- classes/phing/parser/DynamicConfigurator.php | 28 +++++++++ classes/phing/tasks/system/DynamicTask.php | 60 +++++++++++++++++++ .../phing/tasks/system/DynamicTest.php | 48 +++++++++++++++ test/etc/tasks/system/DynamicTest.xml | 19 ++++++ 4 files changed, 155 insertions(+) create mode 100644 classes/phing/parser/DynamicConfigurator.php create mode 100644 classes/phing/tasks/system/DynamicTask.php create mode 100644 test/classes/phing/tasks/system/DynamicTest.php create mode 100644 test/etc/tasks/system/DynamicTest.xml diff --git a/classes/phing/parser/DynamicConfigurator.php b/classes/phing/parser/DynamicConfigurator.php new file mode 100644 index 0000000000..0b09ccae95 --- /dev/null +++ b/classes/phing/parser/DynamicConfigurator.php @@ -0,0 +1,28 @@ +. + */ + +/** + * Enables a task to control unknown attributes and elements. + * + * @author Siad Ardroumli + * @package phing.parser + */ +interface DynamicConfigurator extends DynamicAttribute, CustomChildCreator +{ +} diff --git a/classes/phing/tasks/system/DynamicTask.php b/classes/phing/tasks/system/DynamicTask.php new file mode 100644 index 0000000000..76dfa2aacf --- /dev/null +++ b/classes/phing/tasks/system/DynamicTask.php @@ -0,0 +1,60 @@ +. + */ + +/** + * + * @author Siad Ardroumli + * @package phing.tasks.system + */ +class DynamicTask extends Task implements DynamicConfigurator +{ + public function main() + { + } + + public function setDynamicAttribute(string $name, string $value): void + { + $this->getProject()->setNewProperty($name, $value); + } + + public function customChildCreator($name, Project $project) + { + return new class ($project) implements DynamicConfigurator { + /** + * @var Project + */ + private $project; + + public function __construct(Project $project) + { + $this->project = $project; + } + + public function setDynamicAttribute(string $name, string $value): void + { + $this->project->setNewProperty($name, $value); + } + + public function customChildCreator($name, Project $project) + { + return null; + } + }; + } +} diff --git a/test/classes/phing/tasks/system/DynamicTest.php b/test/classes/phing/tasks/system/DynamicTest.php new file mode 100644 index 0000000000..72fa835dcb --- /dev/null +++ b/test/classes/phing/tasks/system/DynamicTest.php @@ -0,0 +1,48 @@ +. + */ + +/** + * Tests dynamics + * + * @author Siad Ardroumli + * @package phing.tasks.system + */ +class DynamicTest extends BuildFileTest +{ + public function setUp(): void + { + $this->configureProject( + PHING_TEST_BASE + . "/etc/tasks/system/DynamicTest.xml" + ); + } + + public function testSimple() + { + $this->executeTarget('simple'); + + /** @var Project $project */ + $project = $this->getProject(); + + $this->assertSame('1', $project->getProperty('prop1')); + $this->assertSame('2', $project->getProperty('prop2')); + $this->assertSame('3', $project->getProperty('prop3')); + $this->assertSame('4', $project->getProperty('prop4')); + } +} diff --git a/test/etc/tasks/system/DynamicTest.xml b/test/etc/tasks/system/DynamicTest.xml new file mode 100644 index 0000000000..babe3f3e67 --- /dev/null +++ b/test/etc/tasks/system/DynamicTest.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + +