-
Notifications
You must be signed in to change notification settings - Fork 7
Sub-component support #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace WMDE\VueJsTemplating; | ||
|
|
||
| use DOMElement; | ||
| use Exception; | ||
| use WMDE\VueJsTemplating\JsParsing\BasicJsExpressionParser; | ||
| use WMDE\VueJsTemplating\JsParsing\CachingExpressionParser; | ||
| use WMDE\VueJsTemplating\JsParsing\JsExpressionParser; | ||
|
|
||
| class App { | ||
|
|
||
| /** @var HtmlParser */ | ||
| private $htmlParser; | ||
|
|
||
| /** @var JsExpressionParser */ | ||
| private $expressionParser; | ||
|
|
||
| /** @var (Component|string|callable)[] */ | ||
| private $components = []; | ||
|
|
||
| /** | ||
| * @param callable[] $methods The available methods. | ||
| * The key is the method name, the value is the corresponding callable. | ||
| */ | ||
| public function __construct( array $methods ) { | ||
| $this->htmlParser = new HtmlParser(); | ||
| $this->expressionParser = new CachingExpressionParser( new BasicJsExpressionParser( $methods ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Register the template for a component. | ||
| * | ||
| * @param string $name The component name. | ||
| * @param string|callable $template Either the template HTML as a string, | ||
| * or a callable that will return the template HTML as a string when called with no arguments. | ||
| * @return void | ||
| */ | ||
| public function registerComponentTemplate( string $name, $template ): void { | ||
| $this->components[$name] = $template; | ||
| } | ||
|
|
||
| public function evaluateExpression( string $expression, array $data ) { | ||
| return $this->expressionParser->parse( $expression ) | ||
| ->evaluate( $data ); | ||
| } | ||
|
|
||
| public function renderComponent( string $componentName, array $data ): string { | ||
| $rendered = $this->renderComponentToDOM( $componentName, $data ); | ||
| return $rendered->ownerDocument->saveHTML( $rendered ); | ||
| } | ||
|
|
||
| public function renderComponentToDOM( string $componentName, array $data ): DOMElement { | ||
| return $this->getComponent( $componentName ) | ||
| ->render( $data ); | ||
| } | ||
|
|
||
| private function getComponent( string $componentName ): Component { | ||
| $component = $this->components[$componentName] ?? null; | ||
| if ( $component === null ) { | ||
| throw new Exception( "Unknown component: $componentName" ); | ||
| } | ||
|
|
||
| if ( !( $component instanceof Component ) ) { | ||
| if ( is_callable( $component ) ) { | ||
| $html = $component(); | ||
| } else { | ||
| $html = $component; | ||
| } | ||
| /** @var string $html */ | ||
| $document = $this->htmlParser->parseHtml( $html ); | ||
| $rootNode = $this->htmlParser->getRootNode( $document ); | ||
| $component = new Component( $rootNode, $this ); | ||
| $this->components[$componentName] = $component; | ||
| } | ||
|
|
||
| return $component; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| declare( strict_types = 1 ); | ||
|
|
||
| namespace WMDE\VueJsTemplating\Test; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use WMDE\VueJsTemplating\App; | ||
|
|
||
| /** | ||
| * @covers \WMDE\VueJsTemplating\App | ||
| * @covers \WMDE\VueJsTemplating\Component | ||
| */ | ||
| class AppTest extends TestCase { | ||
|
|
||
| public function testAppRenderedTwice(): void { | ||
| $app = new App( [] ); | ||
| $app->registerComponentTemplate( 'root', '<p>{{ text }}</p>' ); | ||
|
|
||
| $result1 = $app->renderComponent( 'root', [ 'text' => 'text 1' ] ); | ||
| $this->assertSame( '<p>text 1</p>', $result1 ); | ||
|
|
||
| $result2 = $app->renderComponent( 'root', [ 'text' => 'text 2' ] ); | ||
| $this->assertSame( '<p>text 2</p>', $result2 ); | ||
| } | ||
|
|
||
| public function testAppInitsComponentLazily(): void { | ||
| $app = new App( [] ); | ||
| $called = false; | ||
| $app->registerComponentTemplate( 'root', function () use ( &$called ) { | ||
| $called = true; | ||
| return '<p>{{ text }}</p>'; | ||
| } ); | ||
|
|
||
| $this->assertFalse( $called ); | ||
| $result = $app->renderComponent( 'root', [ 'text' => 'TEXT' ] ); | ||
| $this->assertSame( '<p>TEXT</p>', $result ); | ||
| $this->assertTrue( $called ); | ||
| } | ||
|
|
||
| public function testNestedComponents(): void { | ||
| $app = new App( [] ); | ||
| $app->registerComponentTemplate( 'root', '<div><x-a :a="rootVar"></x-a></div>' ); | ||
| $app->registerComponentTemplate( 'x-a', '<p><x-b :b="a"></x-b></p>' ); | ||
| $app->registerComponentTemplate( 'x-b', '<span>{{ b }}</span>' ); | ||
|
|
||
| $result = $app->renderComponent( 'root', [ 'rootVar' => 'text' ] ); | ||
|
|
||
| $this->assertSame( '<div><p><span>text</span></p></div>', $result ); | ||
| } | ||
|
|
||
| public function testNestedComponentObjectProp(): void { | ||
| $app = new App( [] ); | ||
| $app->registerComponentTemplate( 'root', '<div><x-a :obj="rootObj"></x-a></div>' ); | ||
| $app->registerComponentTemplate( 'x-a', '<p>obj = { a: {{ obj.a }}, b: {{ obj.b }} }</p>' ); | ||
|
|
||
| $result = $app->renderComponent( 'root', [ | ||
| 'rootObj' => [ 'a' => 'A', 'b' => 'B' ], | ||
| ] ); | ||
|
|
||
| $this->assertSame( '<div><p>obj = { a: A, b: B }</p></div>', $result ); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oof...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it’s annoying that it doesn’t happen for me locally, only in CI :S