From ec849207469c143eafb9df3e9f33baead53f3e9a Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Wed, 27 Nov 2019 11:16:39 +1300 Subject: [PATCH] Operation extension points --- src/Manager.php | 9 ++++++++- src/Scaffolding/Scaffolders/CRUD/Create.php | 6 +++++- src/Scaffolding/Scaffolders/CRUD/Delete.php | 4 +++- src/Scaffolding/Scaffolders/CRUD/Update.php | 3 +++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Manager.php b/src/Manager.php index 1e9919a1e..245a27a0b 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -128,6 +128,8 @@ public function addMiddleware($middleware) */ protected function callMiddleware(Schema $schema, $query, $context, $params, callable $last) { + $this->extend('onBeforeCallMiddleware', $schema, $query, $context, $params); + // Reverse middlewares $next = $last; // Filter out any middlewares that are set to `false`, e.g. via config @@ -138,7 +140,12 @@ protected function callMiddleware(Schema $schema, $query, $context, $params, cal return $middleware->process($schema, $query, $context, $params, $next); }; } - return $next($schema, $query, $context, $params); + + $result = $next($schema, $query, $context, $params); + + $this->extend('onAfterCallMiddleware', $schema, $query, $context, $params); + + return $result; } /** diff --git a/src/Scaffolding/Scaffolders/CRUD/Create.php b/src/Scaffolding/Scaffolders/CRUD/Create.php index c2fc6fbab..1b1566300 100644 --- a/src/Scaffolding/Scaffolders/CRUD/Create.php +++ b/src/Scaffolding/Scaffolders/CRUD/Create.php @@ -130,6 +130,10 @@ public function resolve($object, array $args, $context, ResolveInfo $info) // Save and return $newObject->write(); - return DataObject::get_by_id($this->getDataObjectClass(), $newObject->ID); + $newObject = DataObject::get_by_id($this->getDataObjectClass(), $newObject->ID); + + $this->extend('afterMutation', $newObject, $args, $context, $info); + + return $newObject; } } diff --git a/src/Scaffolding/Scaffolders/CRUD/Delete.php b/src/Scaffolding/Scaffolders/CRUD/Delete.php index be10dcec4..207e31eab 100644 --- a/src/Scaffolding/Scaffolders/CRUD/Delete.php +++ b/src/Scaffolding/Scaffolders/CRUD/Delete.php @@ -65,7 +65,7 @@ protected function generateInputType() public function resolve($object, array $args, $context, ResolveInfo $info) { - DB::get_conn()->withTransaction(function () use ($args, $context) { + DB::get_conn()->withTransaction(function () use ($args, $context, $info) { // Build list to filter $results = DataList::create($this->getDataObjectClass()) ->byIDs($args['IDs']); @@ -93,6 +93,8 @@ public function resolve($object, array $args, $context, ResolveInfo $info) foreach ($resultsList as $obj) { $obj->delete(); } + + $this->extend('afterMutation', $resultsList, $args, $context, $info); }); } } diff --git a/src/Scaffolding/Scaffolders/CRUD/Update.php b/src/Scaffolding/Scaffolders/CRUD/Update.php index 74b62e68f..2044cdf61 100644 --- a/src/Scaffolding/Scaffolders/CRUD/Update.php +++ b/src/Scaffolding/Scaffolders/CRUD/Update.php @@ -153,6 +153,9 @@ public function resolve($object, array $args, $context, ResolveInfo $info) $obj->update($input); $obj->write(); + + $this->extend('afterMutation', $obj, $args, $context, $info); + return $obj; } }