-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move property mock logic to Twig compiler (#42)
- Loading branch information
Showing
25 changed files
with
329 additions
and
168 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,8 +1,16 @@ | ||
import ProductTable from './ProductTable.html.twig'; | ||
import {twig} from "@sensiolabs/storybook-symfony-webpack5"; | ||
|
||
export default { | ||
component: ProductTable, | ||
}; | ||
|
||
export const Default = { | ||
} | ||
|
||
export const EmbeddedRender = { | ||
render: () => ({ | ||
components: { ProductTable }, | ||
template: twig`<twig:ProductTable></twig:ProductTable>`, | ||
}), | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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 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,38 @@ | ||
<?php | ||
|
||
namespace Storybook\Twig\Node; | ||
|
||
use Storybook\Twig\StoryExtension; | ||
use Twig\Attribute\YieldReady; | ||
use Twig\Compiler; | ||
use Twig\Node\Node; | ||
|
||
/** | ||
* Mock context array with provided Storybook context helper if available. | ||
* | ||
* @author Nicolas Rigaud <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
#[YieldReady] | ||
final class MockContextNode extends Node | ||
{ | ||
public function compile(Compiler $compiler): void | ||
{ | ||
$storybookExtension = $compiler->getVarName(); | ||
|
||
$compiler | ||
->write('if (isset($context["__storybook"])) {') | ||
->raw("\n") | ||
->indent() | ||
->write(\sprintf('$%s = $this->env->getExtension(', $storybookExtension)) | ||
->string(StoryExtension::class) | ||
->raw(");\n") | ||
->write(\sprintf('$%s->prepareContext($context);', $storybookExtension)) | ||
->raw("\n") | ||
->outdent() | ||
->write('}') | ||
->raw("\n\n") | ||
; | ||
} | ||
} |
This file contains 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 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,38 @@ | ||
<?php | ||
|
||
namespace Storybook\Twig\NodeVisitor; | ||
|
||
use Storybook\Twig\Node\MockContextNode; | ||
use Twig\Environment; | ||
use Twig\Node\ModuleNode; | ||
use Twig\Node\Node; | ||
use Twig\NodeVisitor\NodeVisitorInterface; | ||
|
||
/** | ||
* Node visitor to inject mock context logic before displaying a module node. | ||
* | ||
* @author Nicolas Rigaud <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class MockContextNodeVisitor implements NodeVisitorInterface | ||
{ | ||
public function enterNode(Node $node, Environment $env): Node | ||
{ | ||
return $node; | ||
} | ||
|
||
public function leaveNode(Node $node, Environment $env): ?Node | ||
{ | ||
if ($node instanceof ModuleNode && '' !== $node->getSourceContext()->getPath()) { | ||
$node->setNode('display_start', new Node([new MockContextNode(), $node->getNode('display_start')])); | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
src/Twig/Sandbox/SandboxNodeVisitor.php → src/Twig/NodeVisitor/SandboxNodeVisitor.php
This file contains 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 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
Oops, something went wrong.