Skip to content

Commit

Permalink
Remove custom event contexts. Use one class
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 13, 2020
1 parent 914614b commit 5838c80
Show file tree
Hide file tree
Showing 24 changed files with 231 additions and 782 deletions.
1 change: 0 additions & 1 deletion lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ en:
SilverStripe\Snapshots\Handler\GridField\URLActionHandler:
HANDLER_deleterecord: 'Delete record'
HANDLER_archiverecord: 'Archive record'
SilverStripe\Snapshots\Handler\GridField\AlterationHandler:
HANDLER_handleReorder: 'Reorder items'
8 changes: 3 additions & 5 deletions src/Handler/CMSMain/ActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Handler\HandlerAbstract;
use SilverStripe\Snapshots\Listener\CMSMain\CMSMainContext;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

Expand All @@ -22,19 +21,18 @@ class ActionHandler extends HandlerAbstract
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var CMSMainContext $context */
$action = $context->getAction();
/* @var HTTPResponse $result */
$result = $context->getResult();
$result = $context->get('result');
if (!$result instanceof HTTPResponse) {
return null;
}
if ((int) $result->getStatusCode() !== 200) {
return null;
}

$className = $context->getTreeClass();
$id = (int) $context->getId();
$className = $context->get('treeClass');
$id = (int) $context->get('id');

if (!$id) {
return null;
Expand Down
16 changes: 10 additions & 6 deletions src/Handler/Form/FormSubmissionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@


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

Expand All @@ -20,10 +20,12 @@ class FormSubmissionHandler extends HandlerAbstract
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var FormContext $context */
$action = $context->getAction();
$page = $this->getPage($context);
$record = $context->getForm()->getRecord();
$record = null;
if ($form = $context->get('form')) {
$record = $form->getRecord();
}

if ($page === null || $record === null) {
return null;
Expand All @@ -35,12 +37,14 @@ protected function createSnapshot(EventContext $context): ?Snapshot
}

/**
* @param FormContext $context
* @param EventContext $context
* @return SiteTree|null
*/
protected function getPage(FormContext $context): ?SiteTree
protected function getPage(EventContext $context): ?SiteTree
{
$url = $context->getRequest()->getURL();
/* @var HTTPRequest $request */
$request = $context->get('request');
$url = $request->getURL();
return $this->getCurrentPageFromRequestUrl($url);
}
}
7 changes: 4 additions & 3 deletions src/Handler/Form/PublishHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
namespace SilverStripe\Snapshots\Handler\Form;


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

class PublishHandler extends FormSubmissionHandler
{
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var FormContext $context */
$snapshot = parent::createSnapshot($context);
if ($snapshot) {
// mark publish actions as WasPublished - the status flags rely on this being set correctly
if ($context->getForm()->getName() === 'EditForm') {
/* @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 @@ -5,7 +5,6 @@


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

Expand All @@ -18,7 +17,7 @@ class SaveHandler extends FormSubmissionHandler
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var FormContext $context */

$page = $this->getPage($context);
if (!$page || !$page->isModifiedOnDraft()) {
return null;
Expand Down
2 changes: 0 additions & 2 deletions src/Handler/GraphQL/GenericHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Handler\HandlerAbstract;
use SilverStripe\Snapshots\Listener\GraphQL\GraphQLMiddlewareContext;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

Expand All @@ -19,7 +18,6 @@ class GenericHandler extends HandlerAbstract
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var GraphQLMiddlewareContext $context */
$action = $context->getAction();
$message = $this->getMessage($action);
$page = $this->getPageFromReferrer();
Expand Down
2 changes: 0 additions & 2 deletions src/Handler/GraphQL/MutationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Handler\HandlerAbstract;
use SilverStripe\Snapshots\Listener\GraphQL\GraphQLMutationContext;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

Expand All @@ -21,7 +20,6 @@ class MutationHandler extends HandlerAbstract
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var GraphQLMutationContext $context */
$type = $context->getAction();
$action = static::ACTION_PREFIX . $type;
$message = $this->getMessage($action);
Expand Down
6 changes: 3 additions & 3 deletions src/Handler/GridField/AlterationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace SilverStripe\Snapshots\Handler\GridField;

use SilverStripe\Forms\Form;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Handler\HandlerAbstract;
use SilverStripe\Snapshots\Listener\GridField\GridFieldContext;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

Expand All @@ -18,10 +18,10 @@ class AlterationHandler extends HandlerAbstract
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var GridFieldContext $context */
$action = $context->getAction();
$message = $this->getMessage($action);
$form = $context->getGridField()->getForm();
/* @var Form $form */
$form = $context->get('gridField')->getForm();

if (!$form) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Handler/GridField/URLActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace SilverStripe\Snapshots\Handler\GridField;

use SilverStripe\Forms\Form;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Handler\HandlerAbstract;
use SilverStripe\Snapshots\Listener\GridField\GridFieldContext;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;

Expand All @@ -18,10 +18,10 @@ class URLActionHandler extends HandlerAbstract
*/
protected function createSnapshot(EventContext $context): ?Snapshot
{
/* @var GridFieldContext $context */
$action = $context->getAction();
$message = $this->getMessage($action);
$form = $context->getGridField()->getForm();
/* @var Form $form */
$form = $context->get('gridField')->getForm();

if (!$form) {
return null;
Expand Down
3 changes: 0 additions & 3 deletions src/Handler/HandlerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

namespace SilverStripe\Snapshots\Handler;


use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Snapshots\Dispatch\Context;
use SilverStripe\Snapshots\Listener\CurrentPage;
use SilverStripe\Snapshots\Listener\EventContext;
use SilverStripe\Snapshots\Snapshot;
Expand Down
17 changes: 7 additions & 10 deletions src/Listener/CMSMain/CMSMainActionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
namespace SilverStripe\Snapshots\Listener\CMSMain;

use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Extension;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Snapshots\Dispatch\Context;
use SilverStripe\Snapshots\Dispatch\Dispatcher;
use SilverStripe\Snapshots\Snapshot;
use SilverStripe\Snapshots\Listener\EventContext;

/**
* Class CMSMainAction
Expand All @@ -32,11 +27,13 @@ class CMSMainActionListener extends Extension
public function afterCallActionHandler(HTTPRequest $request, $action, $result): void {
Dispatcher::singleton()->trigger(
'cmsAction',
new CMSMainContext(
new EventContext(
$action,
$result,
$this->owner->config()->get('tree_class'),
$request->requestVar('ID')
[
'result' => $result,
'treeClass' => $this->owner->config()->get('tree_class'),
'id' => $request->requestVar('ID'),
]
)
);
}
Expand Down
76 changes: 0 additions & 76 deletions src/Listener/CMSMain/CMSMainContext.php

This file was deleted.

52 changes: 43 additions & 9 deletions src/Listener/EventContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,55 @@
namespace SilverStripe\Snapshots\Listener;


abstract class EventContext
use phpDocumentor\Reflection\Types\Scalar;

class EventContext
{
abstract public function getAction(): string;
/**
* @var string
*/
private $action;

/**
* @var array
*/
private $meta = [];

/**
* EventContext constructor.
* @param string $action
* @param array $meta
*/
public function __construct(string $action, array $meta = [])
{
$this->action = $action;
$this->meta = $meta;
}

/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}

/**
* @param string $name
* @return string|int|bool|float|null
*/
public function get(string $name)
{
return $this->meta[$name] ?? null;
}

/**
* @param $name
* @return |null
* @return string|int|bool|float|null
*/
public function __get($name)
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return $this->$method();
}

return null;
return $this->get($name);
}

}
Loading

0 comments on commit 5838c80

Please sign in to comment.