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:
Toby Zerner
2015-10-08 14:28:02 +10:30
parent 8c7cdb184f
commit dd67291ce0
434 changed files with 8676 additions and 7997 deletions

View File

@ -0,0 +1,231 @@
<?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\Controller;
use Flarum\Api\JsonApiResponse;
use Flarum\Http\Controller\ControllerInterface;
use Illuminate\Contracts\Container\Container;
use Flarum\Event\ConfigureApiController;
use Flarum\Event\PrepareApiData;
use Illuminate\Contracts\Events\Dispatcher;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
use Tobscure\JsonApi\Parameters;
use Tobscure\JsonApi\SerializerInterface;
abstract class AbstractSerializeController implements ControllerInterface
{
/**
* The name of the serializer class to output results with.
*
* @var string
*/
public $serializer;
/**
* The relationships that are included by default.
*
* @var array
*/
public $include = [];
/**
* The relationships that are available to be included.
*
* @var array
*/
public $optionalInclude = [];
/**
* The maximum number of records that can be requested.
*
* @var int
*/
public $maxLimit = 50;
/**
* The number of records included by default.
*
* @var int
*/
public $limit = 20;
/**
* The fields that are available to be sorted by.
*
* @var array
*/
public $sortFields = [];
/**
* The default sort field and order to user.
*
* @var array|null
*/
public $sort;
/**
* @var Container
*/
protected static $container;
/**
* @var Dispatcher
*/
protected static $events;
/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request)
{
$document = new Document;
static::$events->fire(
new ConfigureApiController($this)
);
$data = $this->data($request, $document);
static::$events->fire(
new PrepareApiData($this, $data, $request, $document)
);
$serializer = static::$container->make($this->serializer);
$serializer->setActor($request->getAttribute('actor'));
$element = $this->createElement($data, $serializer)
->with($this->extractInclude($request))
->fields($this->extractFields($request));
$document->setData($element);
return new JsonApiResponse($document);
}
/**
* Get the data to be serialized and assigned to the response document.
*
* @param ServerRequestInterface $request
* @param Document $document
* @return mixed
*/
abstract protected function data(ServerRequestInterface $request, Document $document);
/**
* Create a PHP JSON-API Element for output in the document.
*
* @param mixed $data
* @param SerializerInterface $serializer
* @return \Tobscure\JsonApi\ElementInterface
*/
abstract protected function createElement($data, SerializerInterface $serializer);
/**
* @param ServerRequestInterface $request
* @return array
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException
*/
protected function extractInclude(ServerRequestInterface $request)
{
$available = array_merge($this->include, $this->optionalInclude);
return $this->buildParameters($request)->getInclude($available) ?: $this->include;
}
/**
* @param ServerRequestInterface $request
* @return array
*/
protected function extractFields(ServerRequestInterface $request)
{
return $this->buildParameters($request)->getFields();
}
/**
* @param ServerRequestInterface $request
* @return array|null
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException
*/
protected function extractSort(ServerRequestInterface $request)
{
return $this->buildParameters($request)->getSort($this->sortFields) ?: $this->sort;
}
/**
* @param ServerRequestInterface $request
* @return int
* @throws \Tobscure\JsonApi\Exception\InvalidParameterException
*/
protected function extractOffset(ServerRequestInterface $request)
{
return $this->buildParameters($request)->getOffset($this->extractLimit($request)) ?: 0;
}
/**
* @param ServerRequestInterface $request
* @return int
*/
protected function extractLimit(ServerRequestInterface $request)
{
return $this->buildParameters($request)->getLimit($this->maxLimit) ?: $this->limit;
}
/**
* @param ServerRequestInterface $request
* @return array
*/
protected function extractFilter(ServerRequestInterface $request)
{
return $this->buildParameters($request)->getFilter();
}
/**
* @param ServerRequestInterface $request
* @return Parameters
*/
protected function buildParameters(ServerRequestInterface $request)
{
return new Parameters($request->getQueryParams());
}
/**
* @return Dispatcher
*/
public static function getEventDispatcher()
{
return static::$events;
}
/**
* @param Dispatcher $events
*/
public static function setEventDispatcher(Dispatcher $events)
{
static::$events = $events;
}
/**
* @return Container
*/
public static function getContainer()
{
return static::$container;
}
/**
* @param Container $container
*/
public static function setContainer(Container $container)
{
static::$container = $container;
}
}