mirror of
https://github.com/flarum/core.git
synced 2025-10-12 07:24:27 +02:00
Finish client action refactoring. closes flarum/core#126
This commit is contained in:
@@ -3,61 +3,174 @@
|
||||
use Flarum\Api\Client;
|
||||
use Flarum\Assets\AssetManager;
|
||||
use Flarum\Core\Users\User;
|
||||
use Illuminate\Contracts\Support\Renderable;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Flarum\Locale\JsCompiler;
|
||||
|
||||
class ClientView
|
||||
/**
|
||||
* This class represents a view which boots up Flarum's client.
|
||||
*/
|
||||
class ClientView implements Renderable
|
||||
{
|
||||
/**
|
||||
* The user who is using the client.
|
||||
*
|
||||
* @var User
|
||||
*/
|
||||
protected $actor;
|
||||
|
||||
protected $apiClient;
|
||||
|
||||
/**
|
||||
* The title of the document, displayed in the <title> tag.
|
||||
*
|
||||
* @var null|string
|
||||
*/
|
||||
protected $title;
|
||||
|
||||
/**
|
||||
* An API response that should be preloaded into the page.
|
||||
*
|
||||
* @var null|array|object
|
||||
*/
|
||||
protected $document;
|
||||
|
||||
/**
|
||||
* The SEO content of the page, displayed in <noscript> tags.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $content;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* The name of the client layout view to display.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $layout;
|
||||
|
||||
/**
|
||||
* An array of strings to append to the page's <head>.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $headStrings = [];
|
||||
|
||||
/**
|
||||
* An array of strings to prepend before the page's </body>.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $footStrings = [];
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
protected $apiClient;
|
||||
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var AssetManager
|
||||
*/
|
||||
protected $assets;
|
||||
|
||||
/**
|
||||
* @var JsCompiler
|
||||
*/
|
||||
protected $locale;
|
||||
|
||||
/**
|
||||
* @param Client $apiClient
|
||||
* @param Request $request
|
||||
* @param User $actor
|
||||
* @param AssetManager $assets
|
||||
* @param JsCompiler $locale
|
||||
* @param string $layout
|
||||
*/
|
||||
public function __construct(
|
||||
Client $apiClient,
|
||||
Request $request,
|
||||
User $actor,
|
||||
Client $apiClient,
|
||||
$layout,
|
||||
AssetManager $assets,
|
||||
JsCompiler $locale
|
||||
JsCompiler $locale,
|
||||
$layout
|
||||
) {
|
||||
$this->apiClient = $apiClient;
|
||||
$this->request = $request;
|
||||
$this->actor = $actor;
|
||||
$this->apiClient = $apiClient;
|
||||
$this->layout = $layout;
|
||||
$this->assets = $assets;
|
||||
$this->locale = $locale;
|
||||
$this->layout = $layout;
|
||||
}
|
||||
|
||||
public function setActor(User $actor)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
}
|
||||
|
||||
/**
|
||||
* The title of the document, to be displayed in the <title> tag.
|
||||
*
|
||||
* @param null|string $title
|
||||
*/
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an API response to be preloaded into the page. This should be a
|
||||
* JSON-API document.
|
||||
*
|
||||
* @param null|array|object $document
|
||||
*/
|
||||
public function setDocument($document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SEO content of the page, to be displayed in <noscript> tags.
|
||||
*
|
||||
* @param null|string $content
|
||||
*/
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a string to be appended to the page's <head>.
|
||||
*
|
||||
* @param string $string
|
||||
*/
|
||||
public function addHeadString($string)
|
||||
{
|
||||
$this->headStrings[] = $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a string to be prepended before the page's </body>.
|
||||
*
|
||||
* @param string $string
|
||||
*/
|
||||
public function addFootString($string)
|
||||
{
|
||||
$this->footStrings[] = $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the view's asset manager.
|
||||
*
|
||||
* @return AssetManager
|
||||
*/
|
||||
public function getAssets()
|
||||
{
|
||||
return $this->assets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string contents of the view.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
$view = app('view')->file(__DIR__.'/../../views/app.blade.php');
|
||||
@@ -81,25 +194,43 @@ class ClientView
|
||||
$view->styles = [$this->assets->getCssFile()];
|
||||
$view->scripts = [$this->assets->getJsFile(), $this->locale->getFile()];
|
||||
|
||||
$view->head = implode("\n", $this->headStrings);
|
||||
$view->foot = implode("\n", $this->footStrings);
|
||||
|
||||
return $view->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string contents of the view.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result of an API request to show the forum.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function getForumDocument()
|
||||
{
|
||||
return $this->apiClient->send($this->actor, 'Flarum\Api\Actions\Forum\ShowAction')->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the result of an API request to show the current user.
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function getUserDocument()
|
||||
{
|
||||
// TODO: calling on the API here results in an extra query to get
|
||||
// the user + their groups, when we already have this information on
|
||||
// $this->actor. Can we simply run the CurrentUserSerializer
|
||||
// manually?
|
||||
// manually? Or can we somehow inject this data into the ShowAction?
|
||||
$document = $this->apiClient->send(
|
||||
$this->actor,
|
||||
'Flarum\Api\Actions\Users\ShowAction',
|
||||
@@ -109,6 +240,13 @@ class ClientView
|
||||
return $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of data by merging the 'data' and 'included' keys of a
|
||||
* JSON-API document.
|
||||
*
|
||||
* @param object $document
|
||||
* @return array
|
||||
*/
|
||||
protected function getDataFromDocument($document)
|
||||
{
|
||||
$data[] = $document->data;
|
||||
@@ -120,11 +258,16 @@ class ClientView
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information about the current session.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getSession()
|
||||
{
|
||||
return [
|
||||
'userId' => $this->actor->id,
|
||||
'token' => $this->request->getCookieParams()['flarum_remember'],
|
||||
'token' => array_get($this->request->getCookieParams(), 'flarum_remember'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user