Skip to content

Commit

Permalink
Revisions per Mojmir
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 13, 2020
1 parent 5838c80 commit 8a10e51
Show file tree
Hide file tree
Showing 22 changed files with 179 additions and 114 deletions.
51 changes: 34 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,38 @@ extensions to key classes in the admin that then trigger these events through th
#### Event: formSubmitted
* **Description**: Any form submitted in the CMS
* **Example**: save, publish, unpublish, delete
* **Listener**: `SilverStripe\Snapshots\Listener\Form\FormSubmissionListener`
* **Handler**: `SilverStripe\Snapshots\Handler\Form\FormSubmissionHandler`
* **Listener**: `SilverStripe\Snapshots\Listener\Form\Listener`
* **Handler**: `SilverStripe\Snapshots\Handler\Form\Handler`

#### Event: cmsAction
* **Description**: A `CMSMain` controller action
* **Example**: `savetreenode` (reorder site tree)
* **Listener**: `SilverStripe\Snapshots\Listener\CMSMain\CMSMainActionListener`
* **Handler**: `SilverStripe\Snapshots\Handler\CMSMain\ActionHandler`
* **Listener**: `SilverStripe\Snapshots\Listener\CMSMain\Listener`
* **Handler**: `SilverStripe\Snapshots\Handler\CMSMain\Handler`

#### Event: gridFieldAction
* **Description**: A standard GridField action invoked via a URL (`GridField_URLHandler`)
* **Example**: `handleReorder` (reorder items)
* **Listener**: `SilverStripe\Snapshots\Listener\GridField\GridFieldURLListener`
* **Handler**: `SilverStripe\Snapshots\Handler\GridField\URLActionHandler`
* **Listener**: `SilverStripe\Snapshots\Listener\GridField\Action\Listener`
* **Handler**: `SilverStripe\Snapshots\Handler\GridField\Action\Handler`

#### Event: gridFieldAlteration
* **Description**: A GridField action invoked via a URL (`GridField_ActionProvider`)
* **Example**: `deleterecord`, `archiverecord`
* **Listener**: `SilverStripe\Snapshots\Listener\GridField\GridFieldAlterationListener`
* **Handler**: `SilverStripe\Snapshots\Handler\GridField\AlterationHandler`
* **Listener**: `SilverStripe\Snapshots\Listener\GridField\Alteration\Listener`
* **Handler**: `SilverStripe\Snapshots\Handler\GridField\Alteration\Handler`

#### Event: graphqlMutation
* **Description**: A scaffolded GraphQL mutation
* **Example**: `mutation createMyDataObject(Input: $Input)`
* **Listener**: `SilverStripe\Snapshots\Listener\GraphQL\GraphQLMutationListener`
* **Handler**: `SilverStripe\Snapshots\Handler\GraphQL\MutationHandler`
* **Listener**: `SilverStripe\Snapshots\Listener\GraphQL\Mutation\Listener`
* **Handler**: `SilverStripe\Snapshots\Handler\GraphQL\Mutation\Handler`

#### Event: graphqlOperation
* **Description**: Any generic GraphQL operation
* **Example**: `mutation publishAllFiles`, `query allTheThings`
* **Listener**: `SilverStripe\Snapshots\Listener\GraphQL\GraphQLMiddlewareListener`
* **Handler**: `SilverStripe\Snapshots\Handler\GraphQL\GenericHandler`
* **Listener**: `SilverStripe\Snapshots\Listener\GraphQL\Middleware\Listener`
* **Handler**: `SilverStripe\Snapshots\Handler\GraphQL\Middleware\Handler`

### Action identifiers

Expand Down Expand Up @@ -213,23 +213,40 @@ SilverStripe\Core\Injector\Injector:
SilverStripe\Snapshots\Dispatch\Dispatcher:
properties:
handlers:
-
on: [ formSubmitted.myFormHandler ]
myForm:
on:
'formSubmitted.myFormHandler': true
handler: %$MyProject\Handlers\MyHandler
```

Notice that the event name is in the key of the configuration. This makes it possible for another layer of
configuration to disable it. See below.

### Removing snapshot creators

The configuration API doesn't make it easy to remove items from arrays, so this is best done procedurally.
To remove an event from a handler, simply set its value to `false`.

```yaml
SilverStripe\Core\Injector\Injector:
SilverStripe\Snapshots\Dispatch\Dispatcher:
properties:
handlers:
myForm:
on:
'formSubmitted.myFormHandler': false
```

### Procedurally adding event handlers

You can register a `EventHandlerLoader` implementation with `Dispatcher` to procedurally register and unregister
events.

```yaml
SilverStripe\Core\Injector\Injector:
SilverStripe\Snapshots\Dispatch\Dispatcher:
constructor:
myLoader: %$MyProject\MyEventLoader
properties:
loaders:
myLoader: %$MyProject\MyEventLoader
```

```php
Expand Down
63 changes: 36 additions & 27 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ Name: snapshot-listeners
---
SilverStripe\CMS\Controllers\CMSMain:
extensions:
- SilverStripe\Snapshots\Listener\CMSMain\CMSMainActionListener
- SilverStripe\Snapshots\Listener\CMSMain\Listener

SilverStripe\Forms\GridField\GridField:
extensions:
- SilverStripe\Snapshots\Listener\GridField\GridFieldAlterationListener
- SilverStripe\Snapshots\Listener\GridField\GridFieldURLListener
- SilverStripe\Snapshots\Listener\GridField\Alteration\Listener
- SilverStripe\Snapshots\Listener\GridField\Action\Listener

SilverStripe\Forms\FormRequestHandler:
extensions:
- SilverStripe\Snapshots\Listener\Form\FormSubmissionListener
- SilverStripe\Snapshots\Listener\Form\Listener

SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Create:
extensions:
- SilverStripe\Snapshots\Listener\GraphQL\GraphQLMutationListener
- SilverStripe\Snapshots\Listener\GraphQL\Mutation\Listener

SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Delete:
extensions:
- SilverStripe\Snapshots\Listener\GraphQL\GraphQLMutationListener
- SilverStripe\Snapshots\Listener\GraphQL\Mutation\Listener

SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Update:
extensions:
- SilverStripe\Snapshots\Listener\GraphQL\GraphQLMutationListener
- SilverStripe\Snapshots\Listener\GraphQL\Mutation\Listener

SilverStripe\GraphQL\Manager:
extensions:
- SilverStripe\Snapshots\Listener\GraphQL\GraphQLMiddlewareListener
- SilverStripe\Snapshots\Listener\GraphQL\Middleware\Listener

---
Name: snapshot-events
Expand All @@ -61,24 +61,33 @@ SilverStripe\Core\Injector\Injector:
SilverStripe\Snapshots\Dispatch\Dispatcher:
properties:
handlers:
-
on: [ formSubmitted.unpublish, formSubmitted.doSave, formSubmitted.doDelete]
handler: %$SilverStripe\Snapshots\Handler\Form\FormSubmissionHandler
-
on: [ formSubmitted.save ]
formGeneric:
on:
'formSubmitted.unpublish': true
'formSubmitted.doSave': true
'formSubmitted.doDelete': true
handler: %$SilverStripe\Snapshots\Handler\Form\Handler
save:
on:
'formSubmitted.save': true
handler: %$SilverStripe\Snapshots\Handler\Form\SaveHandler
-
on: [ formSubmitted.publish ]
publish:
on:
'formSubmitted.publish': true
handler: %$SilverStripe\Snapshots\Handler\Form\PublishHandler
-
on: [ cmsAction.savetreenode ]
handler: %$SilverStripe\Snapshots\Handler\CMSMain\ActionHandler
-
on: [ gridFieldAlteration.deleterecord, gridFieldAlteration.archiverecord ]
handler: %$SilverStripe\Snapshots\Handler\GridField\URLActionHandler
-
on: [ gridFieldAction.handleReorder ]
handler: %$SilverStripe\Snapshots\Handler\GridField\URLActionHandler
-
on: [ graphqlMutation.create, graphqlMutation.update, graphqlMutation.delete ]
handler: %$SilverStripe\Snapshots\Handler\GraphQL\MutationHandler
cmsMain:
on:
'cmsAction.savetreenode': true
handler: %$SilverStripe\Snapshots\Handler\CMSMain\Handler
gridFieldAction:
on:
'gridFieldAction.handleReorder': true
'gridFieldAlteration.deleterecord': true
'gridFieldAlteration.archiverecord': true
handler: %$SilverStripe\Snapshots\Handler\GridField\Action\Handler
graphqlMutation:
on:
'graphqlMutation.create': true
'graphqlMutation.update': true
'graphqlMutation.delete': true
handler: %$SilverStripe\Snapshots\Handler\GraphQL\Mutation\Handler
45 changes: 39 additions & 6 deletions src/Dispatch/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,26 @@ class Dispatcher
{
use Injectable;

/**
* @var EventHandlerLoader[]
*/
private $loaders = [];

/**
* @var array HandlerInterface[]
*/
private $handlers = [];

/**
* Dispatcher constructor.
* @var bool
*/
private $initialised = false;

/**
* @param EventHandlerLoader[] $loaders
* @return $this
*/
public function __construct($loaders = [])
public function setLoaders($loaders = [])
{
foreach ($loaders as $loader) {
if (!$loader instanceof EventHandlerLoader) {
Expand All @@ -31,9 +41,10 @@ public function __construct($loaders = [])
EventHandlerLoader::class
));
}

$loader->addToDispatcher($this);
}
$this->loaders = $loaders;

return $this;
}

/**
Expand All @@ -57,8 +68,10 @@ public function setHandlers(array $handlers)
));
}

foreach ($on as $eventName) {
$this->addListener($eventName, $handler);
foreach ($on as $eventName => $shouldInclude) {
if ($shouldInclude) {
$this->addListener($eventName, $handler);
}
}
}
}
Expand Down Expand Up @@ -124,7 +137,15 @@ public function removeListenerByClassName(string $event, string $className): sel
*/
public function trigger(string $event, EventContext $context): void
{
// TODO: This could be moved to procedural code in something like _config.php,
// or add a new class that bootstraps the dispatcher.
$this->initialise();

$action = $context->getAction();
if ($action === null) {
return;
}

// First fire listeners to <eventName.actionName>, then just fire generic <eventName> listeners
$eventsToFire = [ $event . '.' . $action, $event];
foreach ($eventsToFire as $event) {
Expand All @@ -135,4 +156,16 @@ public function trigger(string $event, EventContext $context): void
}
}
}

private function initialise(): void
{
if ($this->initialised) {
return;
}

foreach ($this->loaders as $loader) {
$loader->addToDispatcher($this);
}
$this->initialised = true;
}
}
1 change: 0 additions & 1 deletion src/Dispatch/EventHandlerLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace SilverStripe\Snapshots\Dispatch;


interface EventHandlerLoader
{
public function addToDispatcher(Dispatcher $dispatcher): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace SilverStripe\Snapshots\Handler\CMSMain;


use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\ORM\DataObject;
Expand All @@ -12,7 +11,7 @@
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

class ActionHandler extends HandlerAbstract
class Handler extends HandlerAbstract
{
/**
* @param EventContext $context
Expand All @@ -22,6 +21,10 @@ class ActionHandler extends HandlerAbstract
protected function createSnapshot(EventContext $context): ?Snapshot
{
$action = $context->getAction();
if ($action === null) {
return null;
}

/* @var HTTPResponse $result */
$result = $context->get('result');
if (!$result instanceof HTTPResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@

namespace SilverStripe\Snapshots\Handler\Form;


use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Handler\HandlerAbstract;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

class FormSubmissionHandler extends HandlerAbstract
class Handler extends HandlerAbstract
{
/**
* @param EventContext $context
Expand All @@ -21,6 +20,10 @@ class FormSubmissionHandler extends HandlerAbstract
protected function createSnapshot(EventContext $context): ?Snapshot
{
$action = $context->getAction();
if ($action === null) {
return null;
}

$page = $this->getPage($context);
$record = null;
if ($form = $context->get('form')) {
Expand Down
23 changes: 12 additions & 11 deletions src/Handler/Form/PublishHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@

namespace SilverStripe\Snapshots\Handler\Form;


use SilverStripe\Forms\Form;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

class PublishHandler extends FormSubmissionHandler
class PublishHandler extends Handler
{
protected function createSnapshot(EventContext $context): ?Snapshot
{
$snapshot = parent::createSnapshot($context);
if ($snapshot) {
// mark publish actions as WasPublished - the status flags rely on this being set correctly
/* @var Form $form */
$form = $context->get('form');
if ($form->getName() === 'EditForm') {
foreach ($snapshot->Items() as $item) {
$item->WasPublished = true;
$item->write();
}
if (!$snapshot) {
return null;
}

// mark publish actions as WasPublished - the status flags rely on this being set correctly
/* @var Form $form */
$form = $context->get('form');
if ($form->getName() === 'EditForm') {
foreach ($snapshot->Items() as $item) {
$item->WasPublished = true;
$item->write();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/Handler/Form/SaveHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@

namespace SilverStripe\Snapshots\Handler\Form;


use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

class SaveHandler extends FormSubmissionHandler
class SaveHandler extends Handler
{
/**
* @param EventContext $context
Expand Down
Loading

0 comments on commit 8a10e51

Please sign in to comment.