Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiController Extender #2451

Merged
merged 17 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions src/Api/Controller/AbstractSerializeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,47 @@ abstract class AbstractSerializeController implements RequestHandlerInterface
*/
protected static $events;

/**
* @var array
*/
protected static $beforeDataCallbacks = [];

/**
* @var array
*/
protected static $beforeSerializationCallbacks = [];

/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$document = new Document;

foreach (array_reverse(array_merge([static::class], class_parents($this))) as $class) {
if (isset(static::$beforeDataCallbacks[$class])) {
foreach (static::$beforeDataCallbacks[$class] as $callback) {
$callback($this);
}
}
}

// 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) {
if (isset(static::$beforeSerializationCallbacks[$class])) {
foreach (static::$beforeSerializationCallbacks[$class] as $callback) {
$callback($this, $data, $request, $document);
}
}
}

// Deprecated in beta 15, removed in beta 16
static::$events->dispatch(
new WillSerializeData($this, $data, $request, $document)
);
Expand Down Expand Up @@ -197,6 +225,106 @@ protected function buildParameters(ServerRequestInterface $request)
return new Parameters($request->getQueryParams());
}

/**
* Set the serializer that will serialize data for the endpoint.
*
* @param string $serializer
*/
public function setSerializer(string $serializer)
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->serializer = $serializer;
}

/**
* Include the given relationship by default.
*
* @param string|array $name
*/
public function addInclude($name)
{
$this->include = array_merge($this->include, (array) $name);
}

/**
* Don't include the given relationship by default.
*
* @param string|array $name
*/
public function removeInclude($name)
{
$this->include = array_diff($this->include, (array) $name);
askvortsov1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Make the given relationship available for inclusion.
*
* @param string|array $name
*/
public function addOptionalInclude($name)
{
$this->optionalInclude = array_merge($this->optionalInclude, (array) $name);
}

/**
* Don't allow the given relationship to be included.
*
* @param string|array $name
*/
public function removeOptionalInclude($name)
{
$this->optionalInclude = array_diff($this->optionalInclude, (array) $name);
}

/**
* Set the default number of results.
*
* @param int $limit
*/
public function setLimit(int $limit)
{
$this->limit = $limit;
}

/**
* Set the maximum number of results.
*
* @param int $max
*/
public function setMaxLimit(int $max)
{
$this->maxLimit = $max;
}

/**
* Allow sorting results by the given field.
*
* @param string|array $field
*/
public function addSortField($field)
{
$this->sortFields = array_merge($this->sortFields, (array) $field);
}

/**
* Disallow sorting results by the given field.
*
* @param string|array $field
*/
public function removeSortField($field)
{
$this->sortFields = array_diff($this->sortFields, (array) $field);
}

/**
* Set the default sort order for the results.
*
* @param array $sort
*/
public function setSort(array $sort)
{
$this->sort = $sort;
}

/**
* @return Dispatcher
*/
Expand Down Expand Up @@ -228,4 +356,30 @@ public static function setContainer(Container $container)
{
static::$container = $container;
}

/**
* @param string $controllerClass
* @param callable $callback
*/
public static function addDataPreparationCallback(string $controllerClass, callable $callback)
{
if (! isset(static::$beforeDataCallbacks[$controllerClass])) {
static::$beforeDataCallbacks[$controllerClass] = [];
}

static::$beforeDataCallbacks[$controllerClass][] = $callback;
}

/**
* @param string $controllerClass
* @param callable $callback
*/
public static function addSerializationPreparationCallback(string $controllerClass, callable $callback)
{
if (! isset(static::$beforeSerializationCallbacks[$controllerClass])) {
static::$beforeSerializationCallbacks[$controllerClass] = [];
}

static::$beforeSerializationCallbacks[$controllerClass][] = $callback;
}
}
3 changes: 3 additions & 0 deletions src/Api/Event/WillGetData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Flarum\Api\Controller\AbstractSerializeController;
use Illuminate\Support\Arr;

/**
* @deprecated in beta 15, removed in beta 16
*/
class WillGetData
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Api/Event/WillSerializeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

/**
* @deprecated in beta 15, removed in beta 16
*/
class WillSerializeData
{
/**
Expand Down
Loading