-
-
Notifications
You must be signed in to change notification settings - Fork 364
Anonymous TwigComponent #802
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
Changes from all commits
c8a10a7
718f3a1
1b7020e
2bffe7c
78a576d
7f4e71e
6906714
6a162a1
1d55891
03504fc
3453468
58c927a
708ff5d
8defd7a
5369c25
226c301
82d9a9f
b19c71a
904a613
3c9ec5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; | ||
|
||
/** | ||
* @author Matheo Daninos <matheo.daninos@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class AnonymousComponent | ||
{ | ||
private array $props; | ||
|
||
public function mount($props = []): void | ||
{ | ||
$this->props = $props; | ||
} | ||
|
||
#[ExposeInTemplate(destruct: true)] | ||
public function getProps(): array | ||
{ | ||
return $this->props; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
use Twig\Environment; | ||
|
||
/** | ||
* @author Matheo Daninos <matheo.daninos@gmail.com> | ||
*/ | ||
final class ComponentTemplateFinder implements ComponentTemplateFinderInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels a bit like a premature abstraction, what about in-lining in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @weaverryan ask me to do it in this comment: #802 (comment). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, it felt odd to inject ALL of |
||
{ | ||
public function __construct( | ||
private Environment $environment | ||
) { | ||
} | ||
|
||
public function findAnonymousComponentTemplate(string $name): ?string | ||
{ | ||
$loader = $this->environment->getLoader(); | ||
$componentPath = rtrim(str_replace(':', '/', $name)); | ||
|
||
if ($loader->exists('components/'.$componentPath.'.html.twig')) { | ||
return 'components/'.$componentPath.'.html.twig'; | ||
} | ||
|
||
if ($loader->exists($componentPath.'.html.twig')) { | ||
return $componentPath.'.html.twig'; | ||
} | ||
|
||
if ($loader->exists('components/'.$componentPath)) { | ||
return 'components/'.$componentPath; | ||
} | ||
|
||
if ($loader->exists($componentPath)) { | ||
return $componentPath; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent; | ||
|
||
/** | ||
* @author Matheo Daninos <matheo.daninos@gmail.com> | ||
*/ | ||
interface ComponentTemplateFinderInterface | ||
{ | ||
public function findAnonymousComponentTemplate(string $name): ?string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Twig; | ||
|
||
use Twig\Compiler; | ||
use Twig\Node\Node; | ||
|
||
/** | ||
* @author Matheo Daninos <matheo.daninos@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class PropsNode extends Node | ||
{ | ||
public function __construct(array $propsNames, array $values, $lineno = 0, string $tag = null) | ||
{ | ||
parent::__construct($values, ['names' => $propsNames], $lineno, $tag); | ||
} | ||
|
||
public function compile(Compiler $compiler): void | ||
{ | ||
foreach ($this->getAttribute('names') as $name) { | ||
$compiler | ||
->addDebugInfo($this) | ||
->write('if (!isset($context[\''.$name.'\'])) {') | ||
WebMamba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; | ||
|
||
if (!$this->hasNode($name)) { | ||
$compiler | ||
->write('throw new \Twig\Error\RuntimeError("'.$name.' should be defined for component '.$this->getTemplateName().'");') | ||
->write('}') | ||
; | ||
|
||
continue; | ||
} | ||
|
||
$compiler | ||
->write('$context[\''.$name.'\'] = ') | ||
->subcompile($this->getNode($name)) | ||
->raw(";\n") | ||
->write('}') | ||
; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\TwigComponent\Twig; | ||
|
||
use Twig\Node\Node; | ||
use Twig\Token; | ||
use Twig\TokenParser\AbstractTokenParser; | ||
|
||
/** | ||
* @author Matheo Daninos <matheo.daninos@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
class PropsTokenParser extends AbstractTokenParser | ||
{ | ||
public function parse(Token $token): Node | ||
{ | ||
$parser = $this->parser; | ||
$stream = $parser->getStream(); | ||
|
||
$names = []; | ||
$values = []; | ||
while (!$stream->nextIf(Token::BLOCK_END_TYPE)) { | ||
$name = $stream->expect(\Twig\Token::NAME_TYPE)->getValue(); | ||
|
||
if ($stream->nextIf(Token::OPERATOR_TYPE, '=')) { | ||
$values[$name] = $parser->getExpressionParser()->parseExpression(); | ||
} | ||
|
||
$names[] = $name; | ||
|
||
if (!$stream->nextIf(Token::PUNCTUATION_TYPE)) { | ||
break; | ||
} | ||
} | ||
|
||
$stream->expect(\Twig\Token::BLOCK_END_TYPE); | ||
|
||
return new PropsNode($names, $values, $token->getLine(), $token->getValue()); | ||
} | ||
|
||
public function getTag(): string | ||
{ | ||
return 'props'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<twig:Button label='Click me'/> | ||
kbond marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.