mirror of
https://github.com/flarum/core.git
synced 2025-07-28 20:20:34 +02:00
Major refactor and improvements
- Reorganised all namespaces and class names for consistency and structure. Following PSR bylaws (Abstract prefix, Interface/Trait suffix). - Move models into root of Core, because writing `use Flarum\Core\Discussion` is nice. Namespace the rest by type. (Namespacing by entity was too arbitrary.) - Moved some non-domain stuff out of Core: Database, Formatter, Settings. - Renamed config table and all references to "settings" for consistency. - Remove Core class and add url()/isInstalled()/inDebugMode() as instance methods of Foundation\Application. - Cleanup, docblocking, etc. - Improvements to HTTP architecture - API and forum/admin Actions are now actually all the same thing (simple PSR-7 Request handlers), renamed to Controllers. - Upgrade to tobscure/json-api 0.2 branch. - Where possible, moved generic functionality to tobscure/json-api (e.g. pagination links). I'm quite happy with the backend balance now re: #262 - Improvements to other architecture - Use Illuminate's Auth\Access\Gate interface/implementation instead of our old Locked trait. We still use events to actually determine the permissions though. Our Policy classes are actually glorified event subscribers. - Extract model validation into Core\Validator classes. - Make post visibility permission stuff much more efficient and DRY. - Renamed Flarum\Event classes for consistency. ref #246 - `Configure` prefix for events dedicated to configuring an object. - `Get` prefix for events whose listeners should return something. - `Prepare` prefix when a variable is passed by reference so it can be modified. - `Scope` prefix when a query builder is passed. - Miscellaneous improvements/bug-fixes. I'm easily distracted! - Increase default height of post composer. - Improve post stream redraw flickering in Safari by keying loading post placeholders with their IDs. ref #451 - Use a PHP JavaScript minification library for minifying TextFormatter's JavaScript, instead of ClosureCompilerService (can't rely on external service!) - Use UrlGenerator properly in various places. closes #123 - Make Api\Client return Response object. closes #128 - Allow extensions to specify custom icon images. - Allow external API/admin URLs to be optionally specified in config.php. If the value or "url" is an array, we look for the corresponding path inside. Otherwise, we append the path to the base URL, using the corresponding value in "paths" if present. closes #244
This commit is contained in:
203
src/Api/Serializer/AbstractSerializer.php
Normal file
203
src/Api/Serializer/AbstractSerializer.php
Normal file
@@ -0,0 +1,203 @@
|
||||
<?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\Serializer;
|
||||
|
||||
use DateTime;
|
||||
use Closure;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\PrepareApiAttributes;
|
||||
use Flarum\Event\GetApiRelationship;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use LogicException;
|
||||
use Tobscure\JsonApi\AbstractSerializer as BaseAbstractSerializer;
|
||||
use Flarum\Api\Relationship\HasOneBuilder;
|
||||
use Flarum\Api\Relationship\HasManyBuilder;
|
||||
use Tobscure\JsonApi\Relationship\BuilderInterface;
|
||||
|
||||
abstract class AbstractSerializer extends BaseAbstractSerializer
|
||||
{
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
protected $actor;
|
||||
|
||||
/**
|
||||
* @var Dispatcher
|
||||
*/
|
||||
protected static $dispatcher;
|
||||
|
||||
/**
|
||||
* @var Container
|
||||
*/
|
||||
protected static $container;
|
||||
|
||||
/**
|
||||
* @return User
|
||||
*/
|
||||
public function getActor()
|
||||
{
|
||||
return $this->actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $actor
|
||||
*/
|
||||
public function setActor(User $actor)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getAttributes($model, array $fields = null)
|
||||
{
|
||||
if (! is_object($model) && ! is_array($model)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$attributes = $this->getDefaultAttributes($model);
|
||||
|
||||
static::$dispatcher->fire(
|
||||
new PrepareApiAttributes($this, $model, $attributes)
|
||||
);
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default set of serialized attributes for a model.
|
||||
*
|
||||
* @param object|array $model
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getDefaultAttributes($model);
|
||||
|
||||
/**
|
||||
* @param DateTime|null $date
|
||||
* @return string|null
|
||||
*/
|
||||
protected function formatDate(DateTime $date = null)
|
||||
{
|
||||
if ($date) {
|
||||
return $date->format(DateTime::RFC3339);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRelationshipBuilder($name)
|
||||
{
|
||||
if ($relationship = $this->getCustomRelationship($name)) {
|
||||
return $relationship;
|
||||
}
|
||||
|
||||
return parent::getRelationshipBuilder($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a custom relationship.
|
||||
*
|
||||
* @param string $name
|
||||
* @return BuilderInterface|null
|
||||
*/
|
||||
protected function getCustomRelationship($name)
|
||||
{
|
||||
$builder = static::$dispatcher->until(
|
||||
new GetApiRelationship($this, $name)
|
||||
);
|
||||
|
||||
if ($builder && ! ($builder instanceof BuilderInterface)) {
|
||||
throw new LogicException('GetApiRelationship handler must return an instance of '
|
||||
. BuilderInterface::class);
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relationship builder for a has-one relationship.
|
||||
*
|
||||
* @param string|Closure|\Tobscure\JsonApi\SerializerInterface $serializer
|
||||
* @param string|Closure|null $relation
|
||||
* @return HasOneBuilder
|
||||
*/
|
||||
public function hasOne($serializer, $relation = null)
|
||||
{
|
||||
if (is_null($relation)) {
|
||||
$relation = $this->getRelationCaller();
|
||||
}
|
||||
|
||||
return new HasOneBuilder($serializer, $relation, $this->actor, static::$container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relationship builder for a has-many relationship.
|
||||
*
|
||||
* @param string|Closure|\Tobscure\JsonApi\SerializerInterface $serializer
|
||||
* @param string|Closure|null $relation
|
||||
* @return HasManyBuilder
|
||||
*/
|
||||
public function hasMany($serializer, $relation = null)
|
||||
{
|
||||
if (is_null($relation)) {
|
||||
$relation = $this->getRelationCaller();
|
||||
}
|
||||
|
||||
return new HasManyBuilder($serializer, $relation, $this->actor, static::$container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess the name of a relation from the stack trace.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getRelationCaller()
|
||||
{
|
||||
list(, , $caller) = debug_backtrace(false, 3);
|
||||
|
||||
return $caller['function'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Dispatcher
|
||||
*/
|
||||
public static function getEventDispatcher()
|
||||
{
|
||||
return static::$dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Dispatcher $dispatcher
|
||||
*/
|
||||
public static function setEventDispatcher(Dispatcher $dispatcher)
|
||||
{
|
||||
static::$dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Container
|
||||
*/
|
||||
public static function getContainer()
|
||||
{
|
||||
return static::$container;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Container $container
|
||||
*/
|
||||
public static function setContainer(Container $container)
|
||||
{
|
||||
static::$container = $container;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user