Skip to content

Commit aa8d2b4

Browse files
committed
Rename Sse classes
1 parent f9d3211 commit aa8d2b4

File tree

17 files changed

+112
-59
lines changed

17 files changed

+112
-59
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "putyourlightson/craft-datastar-module",
33
"description": "A template-driven, reactive hypermedia framework for Craft.",
4-
"version": "1.0.0-RC.6",
4+
"version": "1.0.0-RC.7",
55
"type": "yii-module",
66
"license": "mit",
77
"require": {

src/Datastar.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
use craft\web\Response;
1010
use putyourlightson\datastar\assets\DatastarAssetBundle;
1111
use putyourlightson\datastar\models\Settings;
12-
use putyourlightson\datastar\services\Sse;
12+
use putyourlightson\datastar\services\SseService;
1313
use putyourlightson\datastar\twigextensions\DatastarTwigExtension;
1414
use yii\base\Event;
1515
use yii\base\Module;
1616

1717
/**
18-
* @property-read Sse $sse
18+
* @property-read SseService $sse
1919
* @property-read Settings $settings
2020
*/
2121
class Datastar extends Module
@@ -81,7 +81,7 @@ public function getSettings(): Settings
8181
private function registerComponents(): void
8282
{
8383
$this->setComponents([
84-
'sse' => Sse::class,
84+
'sse' => SseService::class,
8585
]);
8686
}
8787

src/controllers/DefaultController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
use Craft;
99
use craft\web\Controller;
1010
use putyourlightson\datastar\models\Config;
11-
use putyourlightson\datastar\traits\SseTrait;
11+
use putyourlightson\datastar\traits\Sse;
1212
use yii\web\BadRequestHttpException;
1313
use yii\web\ForbiddenHttpException;
1414
use yii\web\Response;
1515

1616
class DefaultController extends Controller
1717
{
18-
use SseTrait;
18+
use Sse;
1919

2020
/**
2121
* @inheritdoc

src/helpers/Action.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ class Action
1717
/**
1818
* Returns a Datastar action.
1919
*/
20-
public static function getAction(string $method, string $route, array $params = [], array $options = []): string
20+
public static function getAction(string $method, string $route, array $params = [], array|string $options = []): string
2121
{
2222
$url = self::getUrl($route, $params);
23-
$args = ["'$url'"];
23+
$args = ['"' . $url . '"'];
2424

2525
if ($method !== 'get') {
26-
$headers = $options['headers'] ?? [];
27-
$headers[Request::CSRF_HEADER] = Craft::$app->getRequest()->getCsrfToken();
28-
$options['headers'] = $headers;
26+
$options = self::addCsrfToken($options);
27+
} else {
28+
$options = is_array($options) ? Json::encode($options) : $options;
2929
}
3030

31-
if (!empty($options)) {
32-
$args[] = Json::encode($options);
31+
if ($options !== '{}') {
32+
$args[] = $options;
3333
}
3434

3535
$args = implode(', ', $args);
3636

37-
return "@$method($args)";
37+
return '@' . $method . '(' . $args . ')';
3838
}
3939

4040
/**
@@ -56,4 +56,46 @@ public static function getUrl(string $route, array $params = []): string
5656
'config' => $config->getHashed(),
5757
]);
5858
}
59+
60+
private static function addCsrfToken(array|string $options): string
61+
{
62+
$token = Craft::$app->getRequest()->getCsrfToken();
63+
$csrfHeader = Request::CSRF_HEADER;
64+
65+
if (is_array($options)) {
66+
return self::addCsrfToArray($options, $token, $csrfHeader);
67+
}
68+
69+
return self::addCsrfToString($options, $token, $csrfHeader);
70+
}
71+
72+
private static function addCsrfToArray(array $options, string $token, string $csrfHeader): string
73+
{
74+
$headers = $options['headers'] ?? [];
75+
$headers[$csrfHeader] = $token;
76+
$options['headers'] = $headers;
77+
78+
return Json::encode($options);
79+
}
80+
81+
private static function addCsrfToString(string $options, string $token, string $csrfHeader): string
82+
{
83+
if (preg_match('/headers:\s*\{/i', $options)) {
84+
return preg_replace(
85+
'/headers:\s*\{/i',
86+
'headers: {"' . $csrfHeader . '": "' . $token . '", ',
87+
$options
88+
);
89+
}
90+
91+
if (preg_match('/}\s*$/', $options)) {
92+
return preg_replace(
93+
'/}\s*$/',
94+
', headers: {"' . $csrfHeader . '": "' . $token . '"}}',
95+
$options
96+
);
97+
}
98+
99+
return Json::encode(['headers' => [$csrfHeader => $token]]);
100+
}
59101
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Throwable;
2121
use yii\web\BadRequestHttpException;
2222

23-
class Sse extends Component
23+
class SseService extends Component
2424
{
2525
/**
2626
* Whether the response is a streamed response.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
namespace putyourlightson\datastar\traits;
77

88
use putyourlightson\datastar\Datastar;
9-
use putyourlightson\datastar\services\Sse;
9+
use putyourlightson\datastar\services\SseService;
1010

11-
trait SseTrait
11+
trait Sse
1212
{
1313
/**
1414
* Returns the `SseService` instance.
1515
*/
16-
protected function sse(): Sse
16+
protected function sse(): SseService
1717
{
1818
return Datastar::getInstance()->sse;
1919
}

src/twigextensions/nodes/CompileWithOptionsTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
namespace putyourlightson\datastar\twigextensions\nodes;
77

88
use putyourlightson\datastar\Datastar;
9-
use putyourlightson\datastar\services\Sse;
9+
use putyourlightson\datastar\services\SseService;
1010
use Twig\Compiler;
1111

1212
trait CompileWithOptionsTrait
1313
{
1414
/**
1515
* Compiles a node with options.
1616
*
17-
* @uses Sse::setSseMethodInProcess()
17+
* @uses SseService::setSseMethodInProcess()
1818
*/
1919
public function compileWithOptions(Compiler $compiler, string $method): void
2020
{

src/twigextensions/nodes/ExecuteScriptNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace putyourlightson\datastar\twigextensions\nodes;
77

8-
use putyourlightson\datastar\services\Sse;
8+
use putyourlightson\datastar\services\SseService;
99
use Twig\Compiler;
1010
use Twig\Node\Node;
1111

@@ -14,7 +14,7 @@ class ExecuteScriptNode extends Node
1414
use CompileWithOptionsTrait;
1515

1616
/**
17-
* @uses Sse::executeScript()
17+
* @uses SseService::executeScript()
1818
*/
1919
public function compile(Compiler $compiler): void
2020
{

src/twigextensions/nodes/LocationNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
namespace putyourlightson\datastar\twigextensions\nodes;
77

88
use putyourlightson\datastar\Datastar;
9-
use putyourlightson\datastar\services\Sse;
9+
use putyourlightson\datastar\services\SseService;
1010
use Twig\Compiler;
1111
use Twig\Node\Node;
1212

1313
class LocationNode extends Node
1414
{
1515
/**
16-
* @uses Sse::location()
16+
* @uses SseService::location()
1717
*/
1818
public function compile(Compiler $compiler): void
1919
{

src/twigextensions/nodes/PatchElementsNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace putyourlightson\datastar\twigextensions\nodes;
77

8-
use putyourlightson\datastar\services\Sse;
8+
use putyourlightson\datastar\services\SseService;
99
use Twig\Compiler;
1010
use Twig\Node\Node;
1111

@@ -14,7 +14,7 @@ class PatchElementsNode extends Node
1414
use CompileWithOptionsTrait;
1515

1616
/**
17-
* @uses Sse::patchElements()
17+
* @uses SseService::patchElements()
1818
*/
1919
public function compile(Compiler $compiler): void
2020
{

0 commit comments

Comments
 (0)