Skip to content

Commit

Permalink
Add PreSave and PostSave events (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilPeyssard committed Jul 28, 2023
1 parent c376c5f commit 0d5bea8
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
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

0 comments on commit 0d5bea8

Please sign in to comment.