From 1ae7dbe464aedd13705ae860e54f53a13f2e62e1 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Wed, 20 Jan 2021 16:08:22 -0500 Subject: [PATCH] Remove deprecated API events --- framework/core/src/Api/ApiServiceProvider.php | 2 - .../AbstractSerializeController.php | 34 ----- framework/core/src/Api/Event/Serializing.php | 83 ----------- framework/core/src/Api/Event/WillGetData.php | 141 ------------------ .../core/src/Api/Event/WillSerializeData.php | 73 --------- .../src/Api/Serializer/AbstractSerializer.php | 51 +------ .../core/src/Event/GetApiRelationship.php | 65 -------- .../integration/api/discussions/ShowTest.php | 2 - 8 files changed, 8 insertions(+), 443 deletions(-) delete mode 100644 framework/core/src/Api/Event/Serializing.php delete mode 100644 framework/core/src/Api/Event/WillGetData.php delete mode 100644 framework/core/src/Api/Event/WillSerializeData.php delete mode 100644 framework/core/src/Event/GetApiRelationship.php diff --git a/framework/core/src/Api/ApiServiceProvider.php b/framework/core/src/Api/ApiServiceProvider.php index 739a44dfc..a3d5b7fb0 100644 --- a/framework/core/src/Api/ApiServiceProvider.php +++ b/framework/core/src/Api/ApiServiceProvider.php @@ -110,10 +110,8 @@ class ApiServiceProvider extends AbstractServiceProvider $this->setNotificationSerializers(); AbstractSerializeController::setContainer($this->app); - AbstractSerializeController::setEventDispatcher($events = $this->app->make('events')); AbstractSerializer::setContainer($this->app); - AbstractSerializer::setEventDispatcher($events); } /** diff --git a/framework/core/src/Api/Controller/AbstractSerializeController.php b/framework/core/src/Api/Controller/AbstractSerializeController.php index 4be366e2d..e9e05ba29 100644 --- a/framework/core/src/Api/Controller/AbstractSerializeController.php +++ b/framework/core/src/Api/Controller/AbstractSerializeController.php @@ -9,11 +9,8 @@ namespace Flarum\Api\Controller; -use Flarum\Api\Event\WillGetData; -use Flarum\Api\Event\WillSerializeData; use Flarum\Api\JsonApiResponse; use Illuminate\Contracts\Container\Container; -use Illuminate\Contracts\Events\Dispatcher; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -77,11 +74,6 @@ abstract class AbstractSerializeController implements RequestHandlerInterface */ protected static $container; - /** - * @var Dispatcher - */ - protected static $events; - /** * @var array */ @@ -107,11 +99,6 @@ abstract class AbstractSerializeController implements RequestHandlerInterface } } - // Deprected in beta 15, removed in beta 16 - static::$events->dispatch( - new WillGetData($this) - ); - $data = $this->data($request, $document); foreach (array_reverse(array_merge([static::class], class_parents($this))) as $class) { @@ -122,11 +109,6 @@ abstract class AbstractSerializeController implements RequestHandlerInterface } } - // Deprecated in beta 15, removed in beta 16 - static::$events->dispatch( - new WillSerializeData($this, $data, $request, $document) - ); - $serializer = static::$container->make($this->serializer); $serializer->setRequest($request); @@ -325,22 +307,6 @@ abstract class AbstractSerializeController implements RequestHandlerInterface $this->sort = $sort; } - /** - * @return Dispatcher - */ - public static function getEventDispatcher() - { - return static::$events; - } - - /** - * @param Dispatcher $events - */ - public static function setEventDispatcher(Dispatcher $events) - { - static::$events = $events; - } - /** * @return Container */ diff --git a/framework/core/src/Api/Event/Serializing.php b/framework/core/src/Api/Event/Serializing.php deleted file mode 100644 index d0e1dae48..000000000 --- a/framework/core/src/Api/Event/Serializing.php +++ /dev/null @@ -1,83 +0,0 @@ -serializer = $serializer; - $this->model = $model; - $this->attributes = &$attributes; - $this->actor = $serializer->getActor(); - } - - /** - * @param string $serializer - * @return bool - */ - public function isSerializer($serializer) - { - return $this->serializer instanceof $serializer; - } - - /** - * @param DateTime|null $date - * @return string|null - */ - public function formatDate(DateTime $date = null) - { - if ($date) { - return $date->format(DateTime::RFC3339); - } - } -} diff --git a/framework/core/src/Api/Event/WillGetData.php b/framework/core/src/Api/Event/WillGetData.php deleted file mode 100644 index 973d77336..000000000 --- a/framework/core/src/Api/Event/WillGetData.php +++ /dev/null @@ -1,141 +0,0 @@ -controller = $controller; - } - - /** - * @param string $controller - * @return bool - */ - public function isController($controller) - { - return $this->controller instanceof $controller; - } - - /** - * Set the serializer that will serialize data for the endpoint. - * - * @param string $serializer - */ - public function setSerializer($serializer) - { - $this->controller->serializer = $serializer; - } - - /** - * Include the given relationship by default. - * - * @param string|array $name - */ - public function addInclude($name) - { - $this->controller->include = array_merge($this->controller->include, (array) $name); - } - - /** - * Don't include the given relationship by default. - * - * @param string $name - */ - public function removeInclude($name) - { - Arr::forget($this->controller->include, $name); - } - - /** - * Make the given relationship available for inclusion. - * - * @param string $name - */ - public function addOptionalInclude($name) - { - $this->controller->optionalInclude[] = $name; - } - - /** - * Don't allow the given relationship to be included. - * - * @param string $name - */ - public function removeOptionalInclude($name) - { - Arr::forget($this->controller->optionalInclude, $name); - } - - /** - * Set the default number of results. - * - * @param int $limit - */ - public function setLimit($limit) - { - $this->controller->limit = $limit; - } - - /** - * Set the maximum number of results. - * - * @param int $max - */ - public function setMaxLimit($max) - { - $this->controller->maxLimit = $max; - } - - /** - * Allow sorting results by the given field. - * - * @param string $field - */ - public function addSortField($field) - { - $this->controller->sortFields[] = $field; - } - - /** - * Disallow sorting results by the given field. - * - * @param string $field - */ - public function removeSortField($field) - { - Arr::forget($this->controller->sortFields, $field); - } - - /** - * Set the default sort order for the results. - * - * @param array $sort - */ - public function setSort(array $sort) - { - $this->controller->sort = $sort; - } -} diff --git a/framework/core/src/Api/Event/WillSerializeData.php b/framework/core/src/Api/Event/WillSerializeData.php deleted file mode 100644 index 0cae29589..000000000 --- a/framework/core/src/Api/Event/WillSerializeData.php +++ /dev/null @@ -1,73 +0,0 @@ -controller = $controller; - $this->data = &$data; - $this->request = $request; - $this->document = $document; - $this->actor = $request->getAttribute('actor'); - } - - /** - * @param string $controller - * @return bool - */ - public function isController($controller) - { - return $this->controller instanceof $controller; - } -} diff --git a/framework/core/src/Api/Serializer/AbstractSerializer.php b/framework/core/src/Api/Serializer/AbstractSerializer.php index b20874405..a5165452f 100644 --- a/framework/core/src/Api/Serializer/AbstractSerializer.php +++ b/framework/core/src/Api/Serializer/AbstractSerializer.php @@ -11,11 +11,8 @@ namespace Flarum\Api\Serializer; use Closure; use DateTime; -use Flarum\Api\Event\Serializing; -use Flarum\Event\GetApiRelationship; use Flarum\User\User; use Illuminate\Contracts\Container\Container; -use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; use InvalidArgumentException; use LogicException; @@ -38,11 +35,6 @@ abstract class AbstractSerializer extends BaseAbstractSerializer */ protected $actor; - /** - * @var Dispatcher - */ - protected static $dispatcher; - /** * @var Container */ @@ -105,11 +97,6 @@ abstract class AbstractSerializer extends BaseAbstractSerializer } } - // Deprecated in beta 15, removed in beta 16 - static::$dispatcher->dispatch( - new Serializing($this, $model, $attributes) - ); - return $attributes; } @@ -153,27 +140,21 @@ abstract class AbstractSerializer extends BaseAbstractSerializer */ protected function getCustomRelationship($model, $name) { - // Deprecated in beta 15, removed in beta 16 - $relationship = static::$dispatcher->until( - new GetApiRelationship($this, $name, $model) - ); - foreach (array_merge([static::class], class_parents($this)) as $class) { $callback = Arr::get(static::$customRelations, "$class.$name"); if (is_callable($callback)) { $relationship = $callback($this, $model); - break; + + if (isset($relationship) && ! ($relationship instanceof Relationship)) { + throw new LogicException( + 'GetApiRelationship handler must return an instance of '.Relationship::class + ); + } + + return $relationship; } } - - if ($relationship && ! ($relationship instanceof Relationship)) { - throw new LogicException( - 'GetApiRelationship handler must return an instance of '.Relationship::class - ); - } - - return $relationship; } /** @@ -282,22 +263,6 @@ abstract class AbstractSerializer extends BaseAbstractSerializer return $serializer; } - /** - * @return Dispatcher - */ - public static function getEventDispatcher() - { - return static::$dispatcher; - } - - /** - * @param Dispatcher $dispatcher - */ - public static function setEventDispatcher(Dispatcher $dispatcher) - { - static::$dispatcher = $dispatcher; - } - /** * @return Container */ diff --git a/framework/core/src/Event/GetApiRelationship.php b/framework/core/src/Event/GetApiRelationship.php deleted file mode 100644 index 1905292b0..000000000 --- a/framework/core/src/Event/GetApiRelationship.php +++ /dev/null @@ -1,65 +0,0 @@ -serializer = $serializer; - $this->relationship = $relationship; - $this->model = $model; - } - - /** - * @param string $serializer - * @param string $relationship - * @return bool - */ - public function isRelationship($serializer, $relationship) - { - return $this->serializer instanceof $serializer && $this->relationship === $relationship; - } -} diff --git a/framework/core/tests/integration/api/discussions/ShowTest.php b/framework/core/tests/integration/api/discussions/ShowTest.php index 7e0c375b8..a9f091c14 100644 --- a/framework/core/tests/integration/api/discussions/ShowTest.php +++ b/framework/core/tests/integration/api/discussions/ShowTest.php @@ -10,10 +10,8 @@ namespace Flarum\Tests\integration\api\discussions; use Carbon\Carbon; -use Flarum\Event\ScopeModelVisibility; use Flarum\Tests\integration\RetrievesAuthorizedUsers; use Flarum\Tests\integration\TestCase; -use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Support\Arr; class ShowTest extends TestCase