mirror of
https://github.com/flarum/core.git
synced 2025-10-14 08:24:28 +02:00
Move events to Flarum\Api namespace
This commit is contained in:
@@ -11,9 +11,9 @@
|
||||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Api\Event\WillGetData;
|
||||
use Flarum\Api\Event\WillSerializeData;
|
||||
use Flarum\Api\JsonApiResponse;
|
||||
use Flarum\Event\ConfigureApiController;
|
||||
use Flarum\Event\PrepareApiData;
|
||||
use Flarum\Http\Controller\ControllerInterface;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
@@ -91,13 +91,13 @@ abstract class AbstractSerializeController implements ControllerInterface
|
||||
$document = new Document;
|
||||
|
||||
static::$events->fire(
|
||||
new ConfigureApiController($this)
|
||||
new WillGetData($this)
|
||||
);
|
||||
|
||||
$data = $this->data($request, $document);
|
||||
|
||||
static::$events->fire(
|
||||
new PrepareApiData($this, $data, $request, $document)
|
||||
new WillSerializeData($this, $data, $request, $document)
|
||||
);
|
||||
|
||||
$serializer = static::$container->make($this->serializer);
|
||||
|
83
src/Api/Event/Serializing.php
Normal file
83
src/Api/Event/Serializing.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Api\Event;
|
||||
|
||||
use DateTime;
|
||||
use Flarum\Api\Serializer\AbstractSerializer;
|
||||
|
||||
/**
|
||||
* Prepare API attributes.
|
||||
*
|
||||
* This event is fired when a serializer is constructing an array of resource
|
||||
* attributes for API output.
|
||||
*/
|
||||
class Serializing
|
||||
{
|
||||
/**
|
||||
* The class doing the serializing.
|
||||
*
|
||||
* @var AbstractSerializer
|
||||
*/
|
||||
public $serializer;
|
||||
|
||||
/**
|
||||
* The model being serialized.
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
public $model;
|
||||
|
||||
/**
|
||||
* The serialized attributes of the resource.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $attributes;
|
||||
|
||||
/**
|
||||
* @var \Flarum\User\User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* @param AbstractSerializer $serializer The class doing the serializing.
|
||||
* @param object|array $model The model being serialized.
|
||||
* @param array $attributes The serialized attributes of the resource.
|
||||
*/
|
||||
public function __construct(AbstractSerializer $serializer, $model, array &$attributes)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
}
|
139
src/Api/Event/WillGetData.php
Normal file
139
src/Api/Event/WillGetData.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Api\Event;
|
||||
|
||||
use Flarum\Api\Controller\AbstractSerializeController;
|
||||
|
||||
class WillGetData
|
||||
{
|
||||
/**
|
||||
* @var AbstractSerializeController
|
||||
*/
|
||||
public $controller;
|
||||
|
||||
/**
|
||||
* @param AbstractSerializeController $controller
|
||||
*/
|
||||
public function __construct(AbstractSerializeController $controller)
|
||||
{
|
||||
$this->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)
|
||||
{
|
||||
array_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)
|
||||
{
|
||||
array_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)
|
||||
{
|
||||
array_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;
|
||||
}
|
||||
}
|
72
src/Api/Event/WillSerializeData.php
Normal file
72
src/Api/Event/WillSerializeData.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Api\Event;
|
||||
|
||||
use Flarum\Api\Controller\AbstractSerializeController;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
||||
class WillSerializeData
|
||||
{
|
||||
/**
|
||||
* @var AbstractSerializeController
|
||||
*/
|
||||
public $controller;
|
||||
|
||||
/**
|
||||
* @var mixed
|
||||
*/
|
||||
public $data;
|
||||
|
||||
/**
|
||||
* @var ServerRequestInterface
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* @var Document
|
||||
*/
|
||||
public $document;
|
||||
|
||||
/**
|
||||
* @var \Flarum\User\User
|
||||
*/
|
||||
public $actor;
|
||||
|
||||
/**
|
||||
* @param AbstractSerializeController $controller
|
||||
* @param mixed $data
|
||||
* @param ServerRequestInterface $request
|
||||
* @param Document $document
|
||||
*/
|
||||
public function __construct(
|
||||
AbstractSerializeController $controller,
|
||||
&$data,
|
||||
ServerRequestInterface $request,
|
||||
Document $document
|
||||
) {
|
||||
$this->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;
|
||||
}
|
||||
}
|
@@ -13,8 +13,8 @@ namespace Flarum\Api\Serializer;
|
||||
|
||||
use Closure;
|
||||
use DateTime;
|
||||
use Flarum\Api\Event\Serializing;
|
||||
use Flarum\Event\GetApiRelationship;
|
||||
use Flarum\Event\PrepareApiAttributes;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
@@ -71,7 +71,7 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
|
||||
$attributes = $this->getDefaultAttributes($model);
|
||||
|
||||
static::$dispatcher->fire(
|
||||
new PrepareApiAttributes($this, $model, $attributes)
|
||||
new Serializing($this, $model, $attributes)
|
||||
);
|
||||
|
||||
return $attributes;
|
||||
|
Reference in New Issue
Block a user