Skip to content

Commit c6237db

Browse files
authored
fix(view)!: properly handle scoped view-components (#1435)
1 parent 960f2f4 commit c6237db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+600
-552
lines changed

packages/http/src/Session/CsrfTokenComponent.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/http/src/Session/Session.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public function get(string $key, mixed $default = null): mixed
8383
return $value;
8484
}
8585

86+
/** @return \Tempest\Validation\Rule[] */
87+
public function getErrorsFor(string $name): array
88+
{
89+
return $this->get(self::VALIDATION_ERRORS)[$name] ?? [];
90+
}
91+
92+
public function getOriginalValueFor(string $name, mixed $default = ''): mixed
93+
{
94+
return $this->get(self::ORIGINAL_VALUES)[$name] ?? $default;
95+
}
96+
8697
public function getPreviousUrl(): string
8798
{
8899
return $this->get(self::PREVIOUS_URL, default: '');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Tempest\Http\Session\Session;
4+
5+
use function Tempest\Http\csrf_token;
6+
7+
$name = Session::CSRF_TOKEN_KEY;
8+
$value = csrf_token();
9+
?>
10+
11+
<input type="hidden" :name="$name" :value="$value">

packages/icon/src/Icon.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Tempest\Http\HttpRequestFailed;
88
use Tempest\Http\Status;
99
use Tempest\HttpClient\HttpClient;
10-
use Tempest\Icon\IconCache;
1110
use Tempest\Support\Str;
1211
use Tempest\Support\Str\ImmutableString;
1312

packages/intl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"tempest/support": "dev-main"
1313
},
1414
"suggest": {
15-
"tempest/datetime": "In order to use the `datetime` function"
15+
"tempest/datetime": "In order to use the `datetime` function",
16+
"tempest/icon": "In order to use the `icon` function"
1617
},
1718
"autoload": {
1819
"files": [

packages/intl/src/MessageFormat/Markup/IconMarkupFormatter.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
namespace Tempest\Intl\MessageFormat\Markup;
44

55
use Tempest\Container\Container;
6+
use Tempest\Icon\Icon;
67
use Tempest\Intl\MessageFormat\StandaloneMarkupFormatter;
7-
use Tempest\Support\Arr;
88
use Tempest\Support\Str;
9-
use Tempest\View\Components\Icon;
109

1110
final readonly class IconMarkupFormatter implements StandaloneMarkupFormatter
1211
{
@@ -22,12 +21,11 @@ public function supportsTag(string $tag): bool
2221
public function format(string $tag, array $options): string
2322
{
2423
if (! class_exists(Icon::class)) {
25-
throw new \RuntimeException('The `tempest\view` package is required to use the `icon` tag inside a translation string.');
24+
throw new \RuntimeException('The `tempest\icon` package is required to use the `icon` tag inside a translation string.');
2625
}
2726

28-
return $this->container->get(Icon::class)->render(
29-
name: Str\after_first($tag, 'icon-'),
30-
class: Arr\get_by_key($options, 'class'),
31-
);
27+
$icon = Str\after_first($tag, 'icon-');
28+
29+
return $this->container->get(Icon::class)->render($icon);
3230
}
3331
}

packages/view/src/Attribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface Attribute
88
{
9-
public function apply(Element $element): ?Element;
9+
public function apply(Element $element): Element;
1010
}

packages/view/src/Attributes/DataAttribute.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,10 @@ public function apply(Element $element): Element
2828

2929
$element->setAttribute($this->name, $value);
3030

31-
// Data attributes should only be parsed for view components
32-
if ($element->unwrap(ViewComponentElement::class) === null) {
33-
return $element;
34-
}
35-
36-
$value = $element->getAttribute($this->name);
37-
38-
if (str($value)->startsWith(TempestViewCompiler::PHP_TOKENS)) {
31+
if ($element->unwrap(ViewComponentElement::class) && str($value)->startsWith(TempestViewCompiler::PHP_TOKENS)) {
3932
throw new DataAttributeWasInvalid($this->name, $value);
4033
}
4134

42-
return new PhpDataElement(
43-
name: $this->name,
44-
value: $element->getAttribute($this->name),
45-
wrappingElement: $element,
46-
);
35+
return $element;
4736
}
4837
}

packages/view/src/Attributes/ElseAttribute.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
use Tempest\View\Element;
99
use Tempest\View\Elements\PhpIfElement;
1010
use Tempest\View\Exceptions\ElementWasInvalid;
11+
use Tempest\View\ShouldBeRemoved;
1112

12-
final readonly class ElseAttribute implements Attribute
13+
final readonly class ElseAttribute implements Attribute, ShouldBeRemoved
1314
{
14-
public function apply(Element $element): ?Element
15+
public function apply(Element $element): Element
1516
{
1617
$previous = $element->getPrevious()?->unwrap(PhpIfElement::class);
1718

@@ -21,6 +22,6 @@ public function apply(Element $element): ?Element
2122

2223
$previous->setElse($element);
2324

24-
return null;
25+
return $element;
2526
}
2627
}

packages/view/src/Attributes/ElseIfAttribute.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
use Tempest\View\Element;
1111
use Tempest\View\Elements\PhpIfElement;
1212
use Tempest\View\Exceptions\ElementWasInvalid;
13+
use Tempest\View\ShouldBeRemoved;
1314

14-
final readonly class ElseIfAttribute implements Attribute
15+
final readonly class ElseIfAttribute implements Attribute, ShouldBeRemoved
1516
{
16-
public function apply(Element $element): ?Element
17+
public function apply(Element $element): Element
1718
{
1819
$previous = $element->getPrevious()?->unwrap(PhpIfElement::class);
1920

@@ -23,6 +24,6 @@ public function apply(Element $element): ?Element
2324

2425
$previous->addElseif($element);
2526

26-
return null;
27+
return $element;
2728
}
2829
}

0 commit comments

Comments
 (0)