Skip to content

Commit cc9b51c

Browse files
authored
Merge pull request #38 from wmde/setup
Add support for setup functions
2 parents ce95664 + bcbf80f commit cc9b51c

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

src/App.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class App {
2121
/** @var (Component|string|callable)[] */
2222
private $components = [];
2323

24+
private $componentSetups = [];
25+
2426
/**
2527
* @param callable[] $methods The available methods.
2628
* The key is the method name, the value is the corresponding callable.
@@ -36,10 +38,17 @@ public function __construct( array $methods ) {
3638
* @param string $name The component name.
3739
* @param string|callable $template Either the template HTML as a string,
3840
* or a callable that will return the template HTML as a string when called with no arguments.
41+
* @param callable|null $setup An optional setup function.
42+
* If set, the callable is called with the array of data used to render the component,
43+
* and whichever array it returns is then used to actually render the template.
44+
* This can be used, for example, to add additional data (the equivalent of `computed` in JS).
3945
* @return void
4046
*/
41-
public function registerComponentTemplate( string $name, $template ): void {
47+
public function registerComponentTemplate( string $name, $template, ?callable $setup = null ): void {
4248
$this->components[$name] = $template;
49+
if ( $setup !== null ) {
50+
$this->componentSetups[$name] = $setup;
51+
}
4352
}
4453

4554
public function evaluateExpression( string $expression, array $data ) {
@@ -53,6 +62,10 @@ public function renderComponent( string $componentName, array $data ): string {
5362
}
5463

5564
public function renderComponentToDOM( string $componentName, array $data ): DOMElement {
65+
$setup = $this->componentSetups[$componentName] ?? null;
66+
if ( $setup !== null ) {
67+
$data = $setup( $data );
68+
}
5669
return $this->getComponent( $componentName )
5770
->render( $data );
5871
}

tests/php/AppTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,66 @@ public function testComponentPropKebabCase(): void {
7474
$this->assertSame( '<div><p>A B C</p><p>X Y Z</p></div>', $result );
7575
}
7676

77+
public function testComputedProperties(): void {
78+
$app = new App( [] );
79+
80+
$rootTemplate = <<< HTML
81+
<template>
82+
<div>
83+
<x-property :property-id="propertyId"></x-property>
84+
</div>
85+
</template>
86+
<script>
87+
// import ...
88+
module.exports = exports = defineComponent( {
89+
// name, components, props, ...
90+
computed: {
91+
propertyId() {
92+
return statement.mainsnak.property;
93+
}
94+
}
95+
} );
96+
</script>
97+
HTML;
98+
$app->registerComponentTemplate( 'root', $rootTemplate, function ( array $data ): array {
99+
$data['propertyId'] = $data['statement']['mainsnak']['property'];
100+
return $data;
101+
} );
102+
103+
$propertyTemplate = <<< HTML
104+
<template>
105+
<a :href="propertyUrl">{{ propertyLabel }}</a>
106+
</template>
107+
<script>
108+
// import ...
109+
module.exports = exports = defineComponent( {
110+
// name, props, ...
111+
computed: {
112+
propertyUrl() {
113+
return util.getPropertyUrl( this.propertyId );
114+
},
115+
propertyLabel() {
116+
return labelsStore.getLabel( this.propertyId );
117+
}
118+
}
119+
} );
120+
</script>
121+
HTML;
122+
$app->registerComponentTemplate( 'x-property', $propertyTemplate, function ( array $data ): array {
123+
$propertyId = $data['propertyId'];
124+
$data['propertyUrl'] = "https://wiki.example/wiki/Property:$propertyId";
125+
$data['propertyLabel'] = "property $propertyId";
126+
return $data;
127+
} );
128+
129+
$result = $app->renderComponent( 'root',
130+
[ 'statement' => [ 'mainsnak' => [ 'property' => 'P123' ] ] ] );
131+
132+
$expected = <<< HTML
133+
<div>
134+
<a href="https://wiki.example/wiki/Property:P123">property P123</a></div>
135+
HTML;
136+
$this->assertSame( $expected, $result );
137+
}
138+
77139
}

0 commit comments

Comments
 (0)