Skip to content

Commit 7827a97

Browse files
committed
feat: new precognitive guards
1 parent 5be77ec commit 7827a97

File tree

10 files changed

+1217
-48
lines changed

10 files changed

+1217
-48
lines changed

classes/Guards/AkismetGuard.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace tobimori\DreamForm\Guards;
44

55
use Kirby\Cms\App;
6-
use Kirby\Data\Json;
76
use Kirby\Http\Remote;
87
use Kirby\Toolkit\A;
98
use tobimori\DreamForm\DreamForm;
@@ -13,13 +12,6 @@ class AkismetGuard extends Guard
1312
{
1413
public const TYPE = 'akismet';
1514

16-
/**
17-
* Akismet guard doesn't trigger pre-validation checks
18-
*/
19-
public function run(): void
20-
{
21-
}
22-
2315
/**
2416
* Returns the content to be sent to Akismet
2517
*/

classes/Guards/CsrfGuard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function csrf(): string
2828
/**
2929
* Validate the CSRF token
3030
*/
31-
public function run(): void
31+
public function precognitiveRun(): void
3232
{
3333
$token = $this->csrf();
3434
$submitted = SubmissionPage::valueFromBody('dreamform-csrf');

classes/Guards/Guard.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ public function postValidation(SubmissionPage $submission): void
2323
{
2424
}
2525

26+
/**
27+
* Run the guard
28+
*/
29+
public function run(): void
30+
{
31+
$this->precognitiveRun();
32+
}
33+
34+
/**
35+
* Precognitive run
36+
*/
37+
public function precognitiveRun(): void
38+
{
39+
}
40+
2641
/**
2742
* Reports the submission as spam to a third-party service
2843
*/

classes/Guards/HoneypotGuard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function fieldName(): string
2323
return 'dreamform-guard';
2424
}
2525

26-
public function run(): void
26+
public function precognitiveRun(): void
2727
{
2828
$value = SubmissionPage::valueFromBody($this->fieldName());
2929

classes/Guards/LicenseGuard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class LicenseGuard extends Guard
99
{
1010
public const TYPE = 'license';
1111

12-
public function run(): void
12+
public function precognitiveRun(): void
1313
{
1414
$license = License::fromDisk();
1515
if (!$license->isValid() && !App::instance()->system()->isLocal() && !App::instance()->user()?->isAdmin()) {

classes/Models/FormPage.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,20 @@ public function submit(bool $precognition = false): SubmissionPage
271271
->applyHook('before');
272272
}
273273

274-
$submission = $submission->collectMetadata()
275-
->handleGuards()
276-
->handleFields();
274+
$submission = $submission->collectMetadata();
277275

278276
if (!$precognition) {
279277
$submission = $submission
278+
->handleGuards()
279+
->handleFields()
280280
->handleGuards(postValidation: true)
281281
->handleActions()
282282
->finalize()
283283
->handleAfterSubmitFields();
284+
} else {
285+
$submission = $submission
286+
->handlePrecognitiveGuards()
287+
->handleFields();
284288
}
285289
} catch (Exception $e) {
286290
// PerformerExceptions stop the workflow early, and not save the submission

classes/Models/SubmissionHandling.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public function applyHook(string $type = 'before'): SubmissionPage
3232
);
3333
}
3434

35+
/**
36+
* Handles the form submission precognitive guards
37+
* @internal
38+
*/
39+
public function handlePrecognitiveGuards(): SubmissionPage
40+
{
41+
foreach ($this->form()->guards() as $guard) {
42+
$guard->precognitiveRun();
43+
}
44+
45+
return $this;
46+
}
47+
3548
/**
3649
* Handles the form submission guards
3750
* @internal

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"tobimori\\DreamForm\\": "classes"
1616
}
1717
},
18+
"minimum-stability": "RC",
1819
"require": {
1920
"php": ">=8.3.0",
2021
"getkirby/composer-installer": "^1.2.1"
@@ -31,5 +32,8 @@
3132
},
3233
"extra": {
3334
"kirby-cms-path": false
35+
},
36+
"require-dev": {
37+
"getkirby/cms": "^5"
3438
}
3539
}

0 commit comments

Comments
 (0)