Skip to content
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

Add PreSave and PostSave events #78

Merged
merged 1 commit into from
Jul 28, 2023
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
17 changes: 17 additions & 0 deletions Controller/ParameterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sherlockode\ConfigurationBundle\Controller;

use Sherlockode\ConfigurationBundle\Event\PostSaveEvent;
use Sherlockode\ConfigurationBundle\Event\PreSaveEvent;
use Sherlockode\ConfigurationBundle\Event\SaveEvent;
use Sherlockode\ConfigurationBundle\Form\Type\ImportType;
use Sherlockode\ConfigurationBundle\Form\Type\ParametersType;
Expand Down Expand Up @@ -122,13 +124,28 @@ public function listAction(Request $request)

if ($form->isSubmitted() && $form->isValid()) {
$params = $form->getData();

$preSaveEvent = new PreSaveEvent($request, $params);
$this->eventDispatcher->dispatch($preSaveEvent);

if ($preSaveEvent->getResponse()) {
return $preSaveEvent->getResponse();
}

foreach ($params as $path => $value) {
$this->parameterManager->set($path, $value);
}
$this->parameterManager->save();

$this->eventDispatcher->dispatch(new SaveEvent());

$postSaveEvent = new PostSaveEvent($request);
$this->eventDispatcher->dispatch($postSaveEvent);

if ($postSaveEvent->getResponse()) {
return $postSaveEvent->getResponse();
}

return $this->redirectToRoute('sherlockode_configuration.parameters');
}

Expand Down
19 changes: 19 additions & 0 deletions Event/PostSaveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Sherlockode\ConfigurationBundle\Event;

use Symfony\Component\HttpFoundation\Request;

class PostSaveEvent
{
use ResponseEventTrait;
use RequestEventTrait;

/**
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
}
33 changes: 33 additions & 0 deletions Event/PreSaveEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Sherlockode\ConfigurationBundle\Event;

use Symfony\Component\HttpFoundation\Request;

class PreSaveEvent
{
use ResponseEventTrait;
use RequestEventTrait;

/**
* @var array
*/
private $parameters;

/**
* @param array $parameters
*/
public function __construct(Request $request, $parameters)
{
$this->request = $request;
$this->parameters = $parameters;
}

/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
}
21 changes: 21 additions & 0 deletions Event/RequestEventTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Sherlockode\ConfigurationBundle\Event;

use Symfony\Component\HttpFoundation\Request;

trait RequestEventTrait
{
/**
* @var Request
*/
private $request;

/**
* @return Request
*/
public function getRequest()
{
return $this->request;
}
}
33 changes: 33 additions & 0 deletions Event/ResponseEventTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Sherlockode\ConfigurationBundle\Event;

use Symfony\Component\HttpFoundation\Response;

trait ResponseEventTrait
{
/**
* @var Response
*/
private $response;

/**
* @return Response
*/
public function getResponse()
{
return $this->response;
}

/**
* @param Response $response
*
* @return $this
*/
public function setResponse(Response $response)
{
$this->response = $response;

return $this;
}
}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ So be sure to generate your credentials with the following command:
$ php bin/console secrets:generate-keys
```

### Events

When parameters are saved in database, multiple events are dispatched

* `Sherlockode\ConfigurationBundle\Event\PreSaveEvent` is dispatch before saving parameters;
* `Sherlockode\ConfigurationBundle\Event\SaveEvent` is dispatch just after saving new parameters values
in database;
* `Sherlockode\ConfigurationBundle\Event\PostSaveEvent` is dispatch just after the SaveEvent. It can be
useful for customizing redirection or adding flash message for example;

## Field types

### Default types
Expand Down