Skip to content

refactor action handler execution #3

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

Merged
merged 1 commit into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CHANGELOG
=========

0.1.3
-----
* [Breaking Changes] Refactor action handler execution

*Before*
```php
$flow->handleAction();
```

*After*
```php
$flow->getClickedActionButton()->handle();
```

0.1.2
-----
* Fixed profile collector

0.1.1
-----
* Add more tests

0.1.0
-----
* Initial Proposal
23 changes: 23 additions & 0 deletions src/Form/Flow/ActionButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Yceruto\FormFlowBundle\Form\Flow;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\SubmitButton;

/**
Expand All @@ -10,6 +11,7 @@
class ActionButton extends SubmitButton implements ActionButtonInterface
{
private mixed $data = null;
private bool $handled = false;

public function submit(array|string|null $submittedData, bool $clearMissing = true): static
{
Expand Down Expand Up @@ -41,6 +43,27 @@ public function getHandler(): callable
return $this->getConfig()->getOption('handler');
}

public function isHandled(): bool
{
return $this->handled;
}

public function handle(): void
{
/** @var FormInterface $form */
$form = $this->getParent();
$data = $form->getData();

while ($form && !$form instanceof FormFlowInterface) {
$form = $form->getParent();
}

$handler = $this->getHandler();
$handler($data, $this, $form);

$this->handled = true;
}

public function isResetAction(): bool
{
return 'reset' === $this->getAction();
Expand Down
10 changes: 10 additions & 0 deletions src/Form/Flow/ActionButtonInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ public function getAction(): string;
*/
public function getHandler(): callable;

/**
* Checks if the callable handler was already called.
*/
public function isHandled(): bool;

/**
* Executes the callable handler.
*/
public function handle(): void;

/**
* Checks if the button's action is 'reset'.
*/
Expand Down
20 changes: 2 additions & 18 deletions src/Form/Flow/FormFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Symfony\Component\Form\Exception\AlreadySubmittedException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Exception\RuntimeException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Form;
Expand All @@ -19,7 +18,6 @@ class FormFlow extends Form implements FormFlowInterface
{
private ?ActionButtonInterface $clickedActionButton = null;
private bool $finished = false;
private bool $handled = false;

public function __construct(
private readonly FormFlowConfigInterface $config,
Expand Down Expand Up @@ -61,20 +59,6 @@ public function submit(mixed $submittedData, bool $clearMissing = true): static
return $this;
}

public function handleAction(): void
{
if (!$this->clickedActionButton) {
throw new LogicException('No action button was clicked.');
}

/** @var FormInterface $form */
$form = $this->clickedActionButton->getParent();
$handler = $this->clickedActionButton->getHandler();
$handler($form->getData(), $this->clickedActionButton, $this);

$this->handled = true;
}

public function reset(): void
{
$this->config->getDataStorage()->clear();
Expand Down Expand Up @@ -112,8 +96,8 @@ public function getStepForm(): static
return $this;
}

if ($this->clickedActionButton && !$this->handled) {
$this->handleAction();
if ($this->clickedActionButton && !$this->clickedActionButton->isHandled()) {
$this->clickedActionButton->handle();
}

if (!$this->isValid()) {
Expand Down
8 changes: 7 additions & 1 deletion src/Form/Flow/FormFlowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ public function setInitialOptions(array $options): static

public function getInitialStep(): string
{
return $this->stepAccessor->getStep($this->initialOptions['data']) ?: (string) key($this->steps);
$defaultStep = (string) key($this->steps);

if (!isset($this->initialOptions['data'])) {
return $defaultStep;
}

return (string) $this->stepAccessor->getStep($this->initialOptions['data'], $defaultStep);
}

public function getInitialOptions(): array
Expand Down
7 changes: 0 additions & 7 deletions src/Form/Flow/FormFlowInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ interface FormFlowInterface extends FormInterface
*/
public function getClickedActionButton(): ?ActionButtonInterface;

/**
* Executes the handler associated with the clicked action button.
*
* @throws LogicException If no action button was clicked
*/
public function handleAction(): void;

/**
* Resets the flow by clearing stored data and setting the cursor to the initial step.
*/
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy