Skip to content

Commit

Permalink
fix: view controller
Browse files Browse the repository at this point in the history
  • Loading branch information
yulei745 committed Dec 22, 2020
1 parent a5ed642 commit 6b9198a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"overtrue/easy-sms": "^1.1",
"dragonmantank/cron-expression": "^2.3",
"ramsey/uuid": "^3.9",
"phpoffice/phpspreadsheet": "^1.10"
"phpoffice/phpspreadsheet": "^1.10",
"wikimedia/less.php": "^3.1",
"fortawesome/font-awesome": "^5.15"
},
"autoload": {
"files": [
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(Application $app)
$this->app = $app;
}

public function send($controller, $actor, $query, $body) : ResponseInterface
public function send($controller, $actor = null, $query = [], $body = []) : ResponseInterface
{
$controller = $this->app->make($controller);

Expand Down
76 changes: 76 additions & 0 deletions src/Web/Controller/AbstractWebController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@

namespace Discuz\Web\Controller;

use App\Api\Controller\Settings\ForumSettingsController;
use Discuz\Api\Client;
use Discuz\Auth\Guest;
use Discuz\Foundation\Application;
use Discuz\Http\DiscuzResponseFactory;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Arr;
use Illuminate\View\Factory;
use Less_Parser;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
Expand All @@ -32,6 +38,8 @@ abstract class AbstractWebController implements RequestHandlerInterface

protected $view;

protected $apiDocument;

public function __construct(Application $app, Factory $view)
{
$this->app = $app;
Expand All @@ -45,12 +53,80 @@ public function __construct(Application $app, Factory $view)
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$filename = $this->app->publicPath().'/assets/css/forum.css';
if (!file_exists($filename)) {
$less = [];
$less[] = $this->app->resourcePath() . '/less/common/variables.less';
$less[] = $this->app->resourcePath() . '/less/common/mixins.less';
$less[] = $this->app->resourcePath() . '/less/forum.less';

$css = $this->compile($less);

file_put_contents($this->app->publicPath() . '/assets/css/forum.css', $css);
}
$request = $request->withAttribute('actor', new Guest());
$forum = $this->getForum($request);
$this->view->share('forum', Arr::get($forum, 'attributes'));

$view = $this->render($request, $this->view);
/** @var UrlGenerator $url */
$url = $this->app->make(UrlGenerator::class);

$view->with([
'head' => implode("\n", [
'<link rel="stylesheet" href="'.$url->to('/assets/css/forum.css').'">',
]),
'payload' => [
'resources' => [
$forum
],
'apiDocument' => $this->apiDocument
]
]);




if ($view instanceof Renderable) {
$view = $view->render();
}
return DiscuzResponseFactory::HtmlResponse($view);
}

protected function compile(array $sources): string
{
if (! count($sources)) {
return '';
}

ini_set('xdebug.max_nesting_level', 200);

$parser = new Less_Parser([
'compress' => true,
'cache_dir' => $this->app->storagePath().'/less',
'import_dirs' => [
$this->app->basePath('vendor/fortawesome/font-awesome/less') => ''
]
]);

foreach ($sources as $source) {
$parser->parseFile($source);
}

return $parser->getCss();
}

abstract public function render(ServerRequestInterface $request, Factory $view);

protected function getApiForum(ServerRequestInterface $request)
{
return $this->app->make(Client::class)->send(ForumSettingsController::class, $request->getAttribute('actor'));
}

protected function getForum(ServerRequestInterface $request)
{
/** @var ResponseInterface $response */
$response = $this->getApiForum($request);
return Arr::get(json_decode($response->getBody(), true), 'data');
}
}

0 comments on commit 6b9198a

Please sign in to comment.