Skip to content

Commit

Permalink
incorporation of code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Beutlin committed Apr 19, 2023
1 parent 6f6cc38 commit e4b3133
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 331 deletions.
6 changes: 0 additions & 6 deletions application/api/controllers/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Shaarli\Api\Controllers;

use DI\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Shaarli\Api\ApiUtils;
Expand All @@ -25,11 +24,6 @@ class Links extends ApiController
*/
public static $DEFAULT_LIMIT = 20;

public function __construct(Container $ci)
{
parent::__construct($ci);
}

/**
* Retrieve a list of bookmarks, allowing different filters.
*
Expand Down
1 change: 0 additions & 1 deletion application/api/exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Shaarli\Api\Exceptions;

use Slim\Http\Response;
use Slim\Psr7\Response as SlimResponse;

/**
Expand Down
5 changes: 2 additions & 3 deletions application/container/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

use DI\Container;
use malkusch\lock\mutex\FlockMutex;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Shaarli\Bookmark\BookmarkFileService;
use Shaarli\Bookmark\BookmarkServiceInterface;
use Shaarli\Config\ConfigManager;
use Shaarli\Feed\FeedBuilder;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\Front\Controller\Visitor\ErrorController;
use Shaarli\Front\Controller\Visitor\ErrorNotFoundController;
use Shaarli\History;
use Shaarli\Http\HttpAccess;
use Shaarli\Http\MetadataRetriever;
Expand Down Expand Up @@ -76,7 +75,7 @@ public function __construct(
$this->logger = $logger;
}

public function build(): Container
public function build(): ContainerInterface
{
$container = new Container();

Expand Down
2 changes: 1 addition & 1 deletion application/front/ShaarliAdminMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __invoke(Request $request, RequestHandler $handler): Response
$this->initBasePath($request);

if (true !== $this->container->get('loginManager')->isLoggedIn()) {
$returnUrl = urlencode($request->getServerParams()['REQUEST_URI'] ?? null);
$returnUrl = urlencode($request->getServerParams()['REQUEST_URI']);

return $this->redirect($this->container->get('basePath') . '/login?returnurl=' . $returnUrl);
}
Expand Down
74 changes: 6 additions & 68 deletions application/front/ShaarliErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Shaarli\Bookmark\BookmarkFilter;
use Shaarli\Front\Controller\PageTrait;
use Shaarli\Front\Exception\ShaarliFrontException;
use Shaarli\Render\TemplatePage;
use Slim\Exception\HttpNotFoundException;
use Slim\Handlers\ErrorHandler;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Psr7\Response;
use Slim\Psr7\Response as SlimResponse;
use Slim\Routing\RouteContext;
use Throwable;

class ShaarliErrorHandler extends ErrorHandler
{
use PageTrait;

private ?Container $container;

public function __construct(
Expand All @@ -41,8 +42,8 @@ public function __invoke(
bool $logErrors,
bool $logErrorDetails
): ResponseInterface {
$resp = parent::__invoke($request, $exception, $displayErrorDetails, $logErrors, $logErrorDetails);
$response = new Response();
parent::__invoke($request, $exception, $displayErrorDetails, $logErrors, $logErrorDetails);
$response = new SlimResponse();
// Unknown error encountered
$this->container->get('pageBuilder')->reset();
if ($exception instanceof HttpNotFoundException) {
Expand Down Expand Up @@ -72,74 +73,11 @@ public function __invoke(

$response = $response->withStatus(500);
}

$response->getBody()->write($this->render(TemplatePage::ERROR));
return $response;
}

protected function assignView(string $name, $value): self
{
$this->container->get('pageBuilder')->assign($name, $value);

return $this;
}

/**
* Call plugin hooks for header, footer and includes, specifying which page will be rendered.
* Then assign generated data to RainTPL.
*/
protected function executeDefaultHooks(string $template): void
{
$common_hooks = [
'includes',
'header',
'footer',
];

$parameters = $this->buildPluginParameters($template);

foreach ($common_hooks as $name) {
$pluginData = [];
$this->container->get('pluginManager')->executeHooks(
'render_' . $name,
$pluginData,
$parameters
);
$this->assignView('plugins_' . $name, $pluginData);
}
}

protected function buildPluginParameters(?string $template): array
{
$basePath = $this->container->get('basePath') ?? '';
return [
'target' => $template,
'loggedin' => $this->container->get('loginManager')->isLoggedIn(),
'basePath' => $this->container->get('basePath'),
'rootPath' => preg_replace('#/index\.php$#', '', $basePath),
'bookmarkService' => $this->container->get('bookmarkService')
];
}

protected function render(string $template): string
{
// Legacy key that used to be injected by PluginManager
$this->assignView('_PAGE_', $template);
$this->assignView('template', $template);

$this->assignView('linkcount', $this->container->get('bookmarkService')->count(BookmarkFilter::$ALL));
$this->assignView('privateLinkcount', $this->container->get('bookmarkService')
->count(BookmarkFilter::$PRIVATE));

$this->executeDefaultHooks($template);

$this->assignView('plugin_errors', $this->container->get('pluginManager')->getErrors());

$basePath = $this->container->get('basePath') ?? '';
return $this->container->get('pageBuilder')->render($template, $basePath);
}

protected function showError404($request): Response
protected function showError404($request): ResponseInterface
{
$response = new SlimResponse();
// Request from the API
Expand Down
2 changes: 1 addition & 1 deletion application/front/ShaarliMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __invoke(Request $request, RequestHandler $handler): Response

return $handler->handle($request);
} catch (UnauthorizedException $e) {
$returnUrl = urlencode($request->getServerParams()['REQUEST_URI'] ?? null);
$returnUrl = urlencode($request->getServerParams()['REQUEST_URI']);

return $this->redirect($this->container->get('basePath') . '/login?returnurl=' . $returnUrl);
}
Expand Down
72 changes: 72 additions & 0 deletions application/front/controller/PageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Shaarli\Front\Controller;

use Shaarli\Bookmark\BookmarkFilter;

trait PageTrait
{
protected function assignView(string $name, $value): self
{
$this->container->get('pageBuilder')->assign($name, $value);

return $this;
}

/**
* Call plugin hooks for header, footer and includes, specifying which page will be rendered.
* Then assign generated data to RainTPL.
*/
protected function executeDefaultHooks(string $template): void
{
$common_hooks = [
'includes',
'header',
'footer',
];

$parameters = $this->buildPluginParameters($template);

foreach ($common_hooks as $name) {
$pluginData = [];
$this->container->get('pluginManager')->executeHooks(
'render_' . $name,
$pluginData,
$parameters
);
$this->assignView('plugins_' . $name, $pluginData);
}
}

protected function buildPluginParameters(?string $template): array
{
$basePath = $this->container->get('basePath') ?? '';
return [
'target' => $template,
'loggedin' => $this->container->get('loginManager')->isLoggedIn(),
'basePath' => $this->container->get('basePath'),
'rootPath' => preg_replace('#/index\.php$#', '', $basePath),
'bookmarkService' => $this->container->get('bookmarkService')
];
}

protected function render(string $template): string
{
// Legacy key that used to be injected by PluginManager
$this->assignView('_PAGE_', $template);
$this->assignView('template', $template);

$this->assignView('linkcount', $this->container->get('bookmarkService')->count(BookmarkFilter::$ALL));
$this->assignView('privateLinkcount', $this->container->get('bookmarkService')
->count(BookmarkFilter::$PRIVATE));

$this->executeDefaultHooks($template);

$this->assignView('plugin_errors', $this->container->get('pluginManager')->getErrors());

$basePath = $this->container->get('basePath') ?? '';
return $this->container->get('pageBuilder')->render($template, $basePath);
}
}
51 changes: 0 additions & 51 deletions application/front/controller/visitor/ErrorController.php

This file was deleted.

35 changes: 0 additions & 35 deletions application/front/controller/visitor/ErrorNotFoundController.php

This file was deleted.

Loading

0 comments on commit e4b3133

Please sign in to comment.