diff --git a/lang/en.yml b/lang/en.yml index 97b117d..59c538f 100644 --- a/lang/en.yml +++ b/lang/en.yml @@ -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' diff --git a/src/Handler/CMSMain/ActionHandler.php b/src/Handler/CMSMain/ActionHandler.php index 4c48fe2..589ddfb 100644 --- a/src/Handler/CMSMain/ActionHandler.php +++ b/src/Handler/CMSMain/ActionHandler.php @@ -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; @@ -22,10 +21,9 @@ 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; } @@ -33,8 +31,8 @@ protected function createSnapshot(EventContext $context): ?Snapshot return null; } - $className = $context->getTreeClass(); - $id = (int) $context->getId(); + $className = $context->get('treeClass'); + $id = (int) $context->get('id'); if (!$id) { return null; diff --git a/src/Handler/Form/FormSubmissionHandler.php b/src/Handler/Form/FormSubmissionHandler.php index b6663cf..379c413 100644 --- a/src/Handler/Form/FormSubmissionHandler.php +++ b/src/Handler/Form/FormSubmissionHandler.php @@ -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; @@ -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; @@ -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); } } diff --git a/src/Handler/Form/PublishHandler.php b/src/Handler/Form/PublishHandler.php index 47275c9..a51c749 100644 --- a/src/Handler/Form/PublishHandler.php +++ b/src/Handler/Form/PublishHandler.php @@ -4,7 +4,7 @@ namespace SilverStripe\Snapshots\Handler\Form; -use SilverStripe\Snapshots\Listener\Form\FormContext; +use SilverStripe\Forms\Form; use SilverStripe\Snapshots\Listener\EventContext; use SilverStripe\Snapshots\Snapshot; @@ -12,11 +12,12 @@ 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(); diff --git a/src/Handler/Form/SaveHandler.php b/src/Handler/Form/SaveHandler.php index abdd31f..98378f9 100644 --- a/src/Handler/Form/SaveHandler.php +++ b/src/Handler/Form/SaveHandler.php @@ -5,7 +5,6 @@ use SilverStripe\ORM\ValidationException; -use SilverStripe\Snapshots\Listener\Form\FormContext; use SilverStripe\Snapshots\Listener\EventContext; use SilverStripe\Snapshots\Snapshot; @@ -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; diff --git a/src/Handler/GraphQL/GenericHandler.php b/src/Handler/GraphQL/GenericHandler.php index 2e49183..46f8d41 100644 --- a/src/Handler/GraphQL/GenericHandler.php +++ b/src/Handler/GraphQL/GenericHandler.php @@ -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; @@ -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(); diff --git a/src/Handler/GraphQL/MutationHandler.php b/src/Handler/GraphQL/MutationHandler.php index 068e6fc..d99bc06 100644 --- a/src/Handler/GraphQL/MutationHandler.php +++ b/src/Handler/GraphQL/MutationHandler.php @@ -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; @@ -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); diff --git a/src/Handler/GridField/AlterationHandler.php b/src/Handler/GridField/AlterationHandler.php index f54adc1..7ec5ab2 100644 --- a/src/Handler/GridField/AlterationHandler.php +++ b/src/Handler/GridField/AlterationHandler.php @@ -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; @@ -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; diff --git a/src/Handler/GridField/URLActionHandler.php b/src/Handler/GridField/URLActionHandler.php index 33b4504..062b342 100644 --- a/src/Handler/GridField/URLActionHandler.php +++ b/src/Handler/GridField/URLActionHandler.php @@ -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; @@ -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; diff --git a/src/Handler/HandlerAbstract.php b/src/Handler/HandlerAbstract.php index 0efa45a..0f33cc3 100644 --- a/src/Handler/HandlerAbstract.php +++ b/src/Handler/HandlerAbstract.php @@ -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; diff --git a/src/Listener/CMSMain/CMSMainActionListener.php b/src/Listener/CMSMain/CMSMainActionListener.php index c6874bd..cacffac 100644 --- a/src/Listener/CMSMain/CMSMainActionListener.php +++ b/src/Listener/CMSMain/CMSMainActionListener.php @@ -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 @@ -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'), + ] ) ); } diff --git a/src/Listener/CMSMain/CMSMainContext.php b/src/Listener/CMSMain/CMSMainContext.php deleted file mode 100644 index 4c99fa8..0000000 --- a/src/Listener/CMSMain/CMSMainContext.php +++ /dev/null @@ -1,76 +0,0 @@ -action = $action; - $this->result = $result; - $this->treeClass = $treeClass; - $this->id = $id; - } - - /** - * @return string - */ - public function getAction(): string - { - return $this->action; - } - - /** - * @return null - */ - public function getResult() - { - return $this->result; - } - - /** - * @return string|null - */ - public function getTreeClass(): ?string - { - return $this->treeClass; - } - - /** - * @return string|null - */ - public function getId(): ?string - { - return $this->id; - } -} diff --git a/src/Listener/EventContext.php b/src/Listener/EventContext.php index 4ecdf0a..6cce51a 100644 --- a/src/Listener/EventContext.php +++ b/src/Listener/EventContext.php @@ -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); } + } diff --git a/src/Listener/Form/FormContext.php b/src/Listener/Form/FormContext.php deleted file mode 100644 index 5914026..0000000 --- a/src/Listener/Form/FormContext.php +++ /dev/null @@ -1,81 +0,0 @@ -action = $action; - $this->form = $form; - $this->request = $request; - $this->vars = $vars; - } - - /** - * @return string - */ - public function getAction(): string - { - return $this->action; - } - - /** - * @return Form|null - */ - public function getForm(): ?Form - { - return $this->form; - } - - /** - * @return HTTPRequest|null - */ - public function getRequest(): ?HTTPRequest - { - return $this->request; - } - - /** - * @return array - */ - public function getVars(): array - { - return $this->vars; - } - - -} diff --git a/src/Listener/Form/FormSubmissionListener.php b/src/Listener/Form/FormSubmissionListener.php index 9b77801..37587f1 100644 --- a/src/Listener/Form/FormSubmissionListener.php +++ b/src/Listener/Form/FormSubmissionListener.php @@ -7,6 +7,7 @@ use SilverStripe\Forms\Form; use SilverStripe\Forms\FormRequestHandler; use SilverStripe\Snapshots\Dispatch\Dispatcher; +use SilverStripe\Snapshots\Listener\EventContext; /** * Class Submission @@ -28,6 +29,16 @@ class FormSubmissionListener extends Extension */ public function afterCallFormHandler (HTTPRequest $request, $funcName, $vars, $form): void { - Dispatcher::singleton()->trigger('formSubmitted', new FormContext($funcName, $form, $request, $vars)); + Dispatcher::singleton()->trigger( + 'formSubmitted', + new EventContext( + $funcName, + [ + 'form' => $form, + 'request' => $request, + 'vars' => $vars + ] + ) + ); } } diff --git a/src/Listener/Form/Submission.php b/src/Listener/Form/Submission.php deleted file mode 100644 index dd5e699..0000000 --- a/src/Listener/Form/Submission.php +++ /dev/null @@ -1,166 +0,0 @@ -processAction($action, $form, $request); - } - - /** - * Extension point in @see FormRequestHandler::httpSubmission - * form handler action via form submission action - * - * @param HTTPRequest $request - * @param $action - * @param $vars - * @param $form - * @param $result - * @throws ValidationException - */ - public function afterCallFormHandlerMethod( // phpcs:ignore SlevomatCodingStandard.TypeHints - HTTPRequest $request, - $action, - $vars, - $form, - $result - ): void { - $this->processAction($action, $form, $request); - } - - /** - * Extension point in @see FormRequestHandler::httpSubmission - * form method action via form submission action - * - * @param HTTPRequest $request - * @param $action - * @param $vars - * @param $form - * @param $result - * @throws ValidationException - */ - public function afterCallFormHandlerFormMethod( // phpcs:ignore SlevomatCodingStandard.TypeHints - HTTPRequest $request, - $action, - $vars, - $form, - $result - ): void { - $this->processAction($action, $form, $request); - } - - /** - * Extension point in @see FormRequestHandler::httpSubmission - * form field method action via form submission action - * - * @param HTTPRequest $request - * @param $action - * @param $vars - * @param $form - * @param $result - * @throws ValidationException - */ - public function afterCallFormHandlerFieldMethod( // phpcs:ignore SlevomatCodingStandard.TypeHints - HTTPRequest $request, - $action, - $vars, - $form, - $result - ): void { - $this->processAction($action, $form, $request); - } - - /** - * @param string|null $action - * @param Form $form - * @param HTTPRequest $request - * @throws ValidationException - */ - private function processAction(?string $action, Form $form, HTTPRequest $request): void - { - $snapshot = Snapshot::singleton(); - - if (!$snapshot->isActionTriggerActive()) { - return; - } - - $message = $snapshot->getActionMessage($action); - - if ($message === null) { - return; - } - - $record = $form->getRecord(); - - if ($record === null) { - return; - } - - $url = $request->getURL(); - $page = $this->getCurrentPageFromRequestUrl($url); - - if ($page === null) { - return; - } - - // avoid recording useless save actions to prevent multiple snapshots of the same version - if ($form->getName() === 'EditForm' && $action === 'save' && !$page->isModifiedOnDraft()) { - return; - } - - // attempt to create a custom snapshot first - $customSnapshot = $snapshot->formSubmissionSnapshot($form, $request, $page, $action, $message); - - if ($customSnapshot) { - return; - } - - // fall back to default snapshot - $snapshotInstance = $snapshot->createSnapshotFromAction($page, $record, $message); - - // mark publish actions as WasPublished - the status flags rely on this being set correctly - if ($form->getName() === 'EditForm' && $action === 'publish') { - foreach ($snapshotInstance->Items() as $item) { - $item->WasPublished = true; - $item->write(); - } - } - } -} diff --git a/src/Listener/GraphQL/GraphQLMiddlewareContext.php b/src/Listener/GraphQL/GraphQLMiddlewareContext.php deleted file mode 100644 index fb71055..0000000 --- a/src/Listener/GraphQL/GraphQLMiddlewareContext.php +++ /dev/null @@ -1,113 +0,0 @@ -query = $query; - $this->schema = $schema; - $this->graphqlContext = $graphqlContext; - $this->params = $params; - } - - /** - * @return string - */ - public function getQuery(): string - { - return $this->query; - } - - /** - * @return Schema|null - */ - public function getSchema(): ?Schema - { - return $this->schema; - } - - /** - * @return array - */ - public function getGraphqlContext(): array - { - return $this->graphqlContext; - } - - /** - * @return array - */ - public function getParams(): array - { - return $this->params; - } - - /** - * @return string - */ - public function getAction(): string - { - $document = Parser::parse(new Source($this->getQuery() ?: 'GraphQL')); - $defs = $document->definitions; - foreach ($defs as $statement) { - $options = [ - NodeKind::OPERATION_DEFINITION, - NodeKind::OPERATION_TYPE_DEFINITION - ]; - if (!in_array($statement->kind, $options, true)) { - continue; - } - if (in_array($statement->operation, [Manager::MUTATION_ROOT, Manager::QUERY_ROOT])) { - $selectionSet = $statement->selectionSet; - if ($selectionSet) { - $selections = $selectionSet->selections; - if (!empty($selections)) { - $firstField = $selections[0]; - - return $firstField->name->value; - } - } - return $statement->operation; - } - } - return 'graphql'; - } -} diff --git a/src/Listener/GraphQL/GraphQLMiddlewareListener.php b/src/Listener/GraphQL/GraphQLMiddlewareListener.php index aee98b5..b992349 100644 --- a/src/Listener/GraphQL/GraphQLMiddlewareListener.php +++ b/src/Listener/GraphQL/GraphQLMiddlewareListener.php @@ -2,10 +2,15 @@ namespace SilverStripe\Snapshots\Listener\GraphQL; +use GraphQL\Error\SyntaxError; +use GraphQL\Language\AST\NodeKind; +use GraphQL\Language\Parser; +use GraphQL\Language\Source; use GraphQL\Type\Schema; use SilverStripe\Core\Extension; use SilverStripe\GraphQL\Manager; use SilverStripe\Snapshots\Dispatch\Dispatcher; +use SilverStripe\Snapshots\Listener\EventContext; /** * Class CustomAction @@ -24,13 +29,54 @@ class GraphQLMiddlewareListener extends Extension * @param string $query * @param array $context * @param array|null $params + * @throws SyntaxError */ public function onAfterCallMiddleware(Schema $schema, string $query, array $context, $params): void { Dispatcher::singleton()->trigger( 'graphqlOperation', - new GraphQLMiddlewareContext($query, $schema, $context, $params) + new EventContext( + $this->getActionFromQuery($query), + [ + 'schema' => $schema, + 'context' => $context, + 'params' => $params, + ] + ) ); } + /** + * @param string|null $query + * @return string + * @throws SyntaxError + */ + private function getActionFromQuery(?string $query = null): string + { + $document = Parser::parse(new Source($query ?: 'GraphQL')); + $defs = $document->definitions; + foreach ($defs as $statement) { + $options = [ + NodeKind::OPERATION_DEFINITION, + NodeKind::OPERATION_TYPE_DEFINITION + ]; + if (!in_array($statement->kind, $options, true)) { + continue; + } + if (in_array($statement->operation, [Manager::MUTATION_ROOT, Manager::QUERY_ROOT])) { + $selectionSet = $statement->selectionSet; + if ($selectionSet) { + $selections = $selectionSet->selections; + if (!empty($selections)) { + $firstField = $selections[0]; + + return $firstField->name->value; + } + } + return $statement->operation; + } + } + return 'graphql'; + } + } diff --git a/src/Listener/GraphQL/GraphQLMutationContext.php b/src/Listener/GraphQL/GraphQLMutationContext.php deleted file mode 100644 index c11d175..0000000 --- a/src/Listener/GraphQL/GraphQLMutationContext.php +++ /dev/null @@ -1,145 +0,0 @@ -mutation = $mutation; - $this->list = $list; - $this->record = $record; - $this->args = $args; - $this->graphqlContext = $graphqlContext; - $this->info = $info; - } - - /** - * @return MutationScaffolder - */ - public function getMutation(): MutationScaffolder - { - return $this->mutation; - } - - /** - * @return SS_List|null - */ - public function getList(): ?SS_List - { - return $this->list; - } - - /** - * @return ViewableData|null - */ - public function getRecord(): ?ViewableData - { - return $this->record; - } - - /** - * @return array - */ - public function getArgs(): array - { - return $this->args; - } - - /** - * @return array - */ - public function getGraphqlContext(): array - { - return $this->graphqlContext; - } - - /** - * @return ResolveInfo|null - */ - public function getInfo(): ?ResolveInfo - { - return $this->info; - } - - public function getAction(): string - { - $scaffolder = $this->getMutation(); - if ($scaffolder instanceof Create) { - return static::TYPE_CREATE; - } - - if ($scaffolder instanceof Delete) { - return static::TYPE_DELETE; - } - - if ($scaffolder instanceof Update) { - return static::TYPE_UPDATE; - } - - return null; - } -} diff --git a/src/Listener/GraphQL/GraphQLMutationListener.php b/src/Listener/GraphQL/GraphQLMutationListener.php index 5c84f5a..5c395a6 100644 --- a/src/Listener/GraphQL/GraphQLMutationListener.php +++ b/src/Listener/GraphQL/GraphQLMutationListener.php @@ -4,11 +4,13 @@ use GraphQL\Type\Definition\ResolveInfo; use SilverStripe\Core\Extension; +use SilverStripe\GraphQL\OperationResolver; use SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Create; use SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Delete; use SilverStripe\GraphQL\Scaffolding\Scaffolders\CRUD\Update; use SilverStripe\ORM\SS_List; use SilverStripe\Snapshots\Dispatch\Dispatcher; +use SilverStripe\Snapshots\Listener\EventContext; /** * Class GenericAction @@ -19,6 +21,11 @@ */ class GraphQLMutationListener extends Extension { + + const TYPE_CREATE = 'create'; + const TYPE_DELETE = 'delete'; + const TYPE_UPDATE = 'update'; + /** * Extension point in @see Create::resolve * Extension point in @see Delete::resolve @@ -34,14 +41,33 @@ public function afterMutation($recordOrList, array $args, $context, ResolveInfo { Dispatcher::singleton()->trigger( 'graphqlMutation', - new GraphQLMutationContext( - $this->owner, - $recordOrList instanceof SS_List ? $recordOrList : null, - !$recordOrList instanceof SS_List ? $recordOrList : null, - $args, - $context, - $info + new EventContext( + $this->getActionFromScaffolder($this->owner), + [ + 'list' => $recordOrList instanceof SS_List ? $recordOrList : null, + 'record' => !$recordOrList instanceof SS_List ? $recordOrList : null, + 'args' => $args, + 'context' => $context, + 'info' => $info, + ] ) ); } + + private function getActionFromScaffolder(OperationResolver $scaffolder): ?string + { + if ($scaffolder instanceof Create) { + return static::TYPE_CREATE; + } + + if ($scaffolder instanceof Delete) { + return static::TYPE_DELETE; + } + + if ($scaffolder instanceof Update) { + return static::TYPE_UPDATE; + } + + return null; + } } diff --git a/src/Listener/GridField/GridFieldAlterationContext.php b/src/Listener/GridField/GridFieldAlterationContext.php deleted file mode 100644 index 4013948..0000000 --- a/src/Listener/GridField/GridFieldAlterationContext.php +++ /dev/null @@ -1,60 +0,0 @@ -getActionData( - $this->getRequest()->requestVars(), - $this->getGridField() - ); - if (!$actionData) { - return null; - } - - return array_shift($actionData); - } - - /** - * @param array $data - * @param GridField $gridField - * @return array|null - */ - private function getActionData(array $data, GridField $gridField): ?array - { - // Fetch the store for the "state" of actions (not the GridField) - /** @var StateStore $store */ - $store = Injector::inst()->create(StateStore::class . '.' . $gridField->getName()); - - foreach ($data as $dataKey => $dataValue) { - if (!preg_match('/^action_gridFieldAlterAction\?StateID=(.*)/', $dataKey, $matches)) { - continue; - } - - $stateChange = $store->load($matches[1]); - - $actionName = $stateChange['actionName']; - $arguments = array_key_exists('args', $stateChange) ? $stateChange['args'] : []; - $arguments = is_array($arguments) ? $arguments : []; - - if ($actionName) { - return [ - $actionName, - $arguments, - $data, - ]; - } - } - - return null; - } - -} diff --git a/src/Listener/GridField/GridFieldAlterationListener.php b/src/Listener/GridField/GridFieldAlterationListener.php index 3a6ce95..c7cb79f 100644 --- a/src/Listener/GridField/GridFieldAlterationListener.php +++ b/src/Listener/GridField/GridFieldAlterationListener.php @@ -4,8 +4,11 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Extension; +use SilverStripe\Core\Injector\Injector; +use SilverStripe\Forms\GridField\FormAction\StateStore; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Snapshots\Dispatch\Dispatcher; +use SilverStripe\Snapshots\Listener\EventContext; /** * Class AlterAction @@ -30,10 +33,62 @@ public function afterCallActionHandler(HTTPRequest $request, $action, $result): if (!in_array($action, ['index', 'gridFieldAlterAction'])) { return; } + $actionName = null; + $arguments = []; + $actionData = $this->getActionData($request->requestVars(), $this->owner); + if ($actionData) { + list ($actionName, $arguments) = $actionData; + } + if (!$actionName === null) { + return; + } Dispatcher::singleton()->trigger( 'gridFieldAlteration', - new GridFieldAlterationContext($action, $request, $result, $this->owner) + new EventContext( + $actionName, + [ + 'request' => $request, + 'result' => $result, + 'gridField' => $this->owner, + 'args' => $arguments, + ] + ) ); } + /** + * @param array $data + * @param GridField $gridField + * @return array|null + */ + private function getActionData(array $data, GridField $gridField): ?array + { + // Fetch the store for the "state" of actions (not the GridField) + /** @var StateStore $store */ + $store = Injector::inst()->create(StateStore::class . '.' . $gridField->getName()); + + foreach ($data as $dataKey => $dataValue) { + if (!preg_match('/^action_gridFieldAlterAction\?StateID=(.*)/', $dataKey, $matches)) { + continue; + } + + $stateChange = $store->load($matches[1]); + + $actionName = $stateChange['actionName']; + $arguments = array_key_exists('args', $stateChange) ? $stateChange['args'] : []; + $arguments = is_array($arguments) ? $arguments : []; + + if ($actionName) { + return [ + $actionName, + $arguments, + $data, + ]; + } + } + + return null; + } + + } diff --git a/src/Listener/GridField/GridFieldContext.php b/src/Listener/GridField/GridFieldContext.php deleted file mode 100644 index 99f9e4b..0000000 --- a/src/Listener/GridField/GridFieldContext.php +++ /dev/null @@ -1,81 +0,0 @@ -action = $action; - $this->request = $request; - $this->result = $result; - $this->gridField = $gridField; - } - - /** - * @return string - */ - public function getAction(): string - { - return $this->action; - } - - /** - * @return HTTPRequest|null - */ - public function getRequest(): ?HTTPRequest - { - return $this->request; - } - - /** - * @return null - */ - public function getResult() - { - return $this->result; - } - - /** - * @return GridField|null - */ - public function getGridField(): ?GridField - { - return $this->gridField; - } - - -} diff --git a/src/Listener/GridField/GridFieldURLListener.php b/src/Listener/GridField/GridFieldURLListener.php index 4d426b7..e0451a8 100644 --- a/src/Listener/GridField/GridFieldURLListener.php +++ b/src/Listener/GridField/GridFieldURLListener.php @@ -6,6 +6,7 @@ use SilverStripe\Core\Extension; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Snapshots\Dispatch\Dispatcher; +use SilverStripe\Snapshots\Listener\EventContext; /** * Class UrlHandlerAction @@ -29,7 +30,14 @@ class GridFieldURLListener extends Extension public function afterCallActionURLHandler(HTTPRequest $request, $action, $result): void { Dispatcher::singleton()->trigger( 'gridFieldAction', - new GridFieldContext($action, $request, $result, $this->owner) + new EventContext( + $action, + [ + 'request' => $request, + 'result' => $result, + 'gridField' => $this->owner + ] + ) ); } }