Skip to content

Commit

Permalink
add controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Oct 20, 2023
1 parent 67f69db commit a5b2773
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 46 deletions.
Empty file removed Controllers/.gitkeep
Empty file.
58 changes: 58 additions & 0 deletions Controllers/Abstracts/AbstractAdministrationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts;

use ArrayAccess\TrayDigita\Traits\Service\TranslatorTrait;
use ArrayAccess\TrayDigita\Util\Filter\DataNormalizer;
use Psr\Http\Message\ServerRequestInterface;
use function var_dump;

abstract class AbstractAdministrationController extends AbstractAuthenticationBasedController
{
use TranslatorTrait;

protected bool $doRedirect = true;

/**
* Doing check
*
* @param ServerRequestInterface $request
* @param string $method
* @param ...$arguments
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpDocSignatureIsNotCompleteInspection
*/
final public function doBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
) {
$redirect = $this->doRedirect ? match ($this->getAuthenticationMethod()) {
self::TYPE_ADMIN => $this->admin ? null : $this->dashboardAuthPath,
self::TYPE_USER => $this->admin ? null : $this->userAuthPath,
default => null,
} : null;
return $redirect
? $this->redirect(
$this
->getView()
->getBaseURI($redirect)
->withQuery(
'redirect='
. DataNormalizer::normalizeUnixDirectorySeparator($request->getUri()->getPath())
)
) : $this->doAfterBeforeDispatch($request, $method, $arguments);
}

/**
* @param ServerRequestInterface $request
* @param string $method
* @param ...$arguments
*/
abstract public function doAfterBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
);
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts;
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts;

use ArrayAccess\TrayDigita\App\Modules\Users\Users;
use ArrayAccess\TrayDigita\Collection\Config;
use ArrayAccess\TrayDigita\Http\Code;
use ArrayAccess\TrayDigita\Kernel\Decorator;
use ArrayAccess\TrayDigita\Routing\AbstractController;
use ArrayAccess\TrayDigita\Util\Filter\ContainerHelper;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use const JSON_PRETTY_PRINT;

abstract class AbstractApiController extends AbstractController
abstract class AbstractApiController extends AbstractAuthenticationBasedController
{
const TYPE_USER = 'user';
const TYPE_ADMIN = 'admin';

protected ?string $authenticationMethod = null;

protected function getAuthenticationMethod() : ?string
{
return $this->authenticationMethod;
}

final public function beforeDispatch(
final public function doBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
) {

): ?ResponseInterface {
$this->statusCode = 404;
// set result as json if return is not string
$this->asJSON = true;
Expand All @@ -44,27 +30,23 @@ final public function beforeDispatch(
static fn ($flags) => JSON_PRETTY_PRINT|$flags
);
}
$response = $this->doBeforeDispatch($request, $method, ...$arguments);
if ($response instanceof ResponseInterface) {
return $response;
}
$method = $this->getAuthenticationMethod();
if ($method === null) {
return null;
}
$jsonResponder = $this->getJsonResponder();
$auth = Decorator::module(Users::class);
$match = match ($method) {
self::TYPE_USER => $auth->isUserLoggedIn()
self::TYPE_USER => $this->user
? null
: $jsonResponder->serve(Code::UNAUTHORIZED),
self::TYPE_ADMIN => $auth->isAdminLoggedIn()
self::TYPE_ADMIN => $this->admin
? null
: $jsonResponder->serve(Code::UNAUTHORIZED),
default => $auth->isAdminLoggedIn() || $auth->isUserLoggedIn()
default => $this->admin || $this->user
? null
: $jsonResponder->serve(Code::UNAUTHORIZED),
};

return $match ?? $this->doAfterBeforeDispatch(
$request,
$method,
Expand All @@ -76,17 +58,7 @@ final public function beforeDispatch(
* @param ServerRequestInterface $request
* @param string $method
* @param ...$arguments
* @return mixed
* @noinspection PhpReturnDocTypeMismatchInspection
* @noinspection PhpInconsistentReturnPointsInspection
*/
public function doBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
) {
}

public function doAfterBeforeDispatch(
ServerRequestInterface $request,
string $method,
Expand Down
59 changes: 59 additions & 0 deletions Controllers/Abstracts/AbstractAuthenticationBasedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts;

use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\Dashboard as DashboardAttribute;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\User as UserAttribute;
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\Admin;
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\User;
use ArrayAccess\TrayDigita\App\Modules\Users\Users;
use ArrayAccess\TrayDigita\Routing\AbstractController;
use Psr\Http\Message\ServerRequestInterface;

abstract class AbstractAuthenticationBasedController extends AbstractController
{
protected Users $users;

protected ?User $user = null;

protected ?Admin $admin = null;

protected string $userAuthPath;

protected string $dashboardAuthPath;

protected ?string $authenticationMethod = null;

const TYPE_USER = 'user';

const TYPE_ADMIN = 'admin';

protected function getAuthenticationMethod() : ?string
{
return $this->authenticationMethod;
}

final public function beforeDispatch(ServerRequestInterface $request, string $method, ...$arguments)
{
$this->userAuthPath = UserAttribute::path('/auth');
$this->dashboardAuthPath = DashboardAttribute::path('/auth');
$this->users = $this->getModule(Users::class);
$this->user = $this->users->getAdminAccount();
$this->admin = $this->users->getUserAccount();
$this->getView()->setParameter('user', $this->user);
$this->getView()->setParameter('admin', $this->admin);
return $this->doBeforeDispatch($request, $method, ...$arguments);
}

/**
* @param ServerRequestInterface $request
* @param string $method
* @param ...$arguments
*/
abstract public function doBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
);
}
23 changes: 23 additions & 0 deletions Controllers/Abstracts/AbstractDashboardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts;

use Psr\Http\Message\ServerRequestInterface;

abstract class AbstractDashboardController extends AbstractAdministrationController
{
protected ?string $authenticationMethod = self::TYPE_ADMIN;

/**
* @param ServerRequestInterface $request
* @param string $method
* @param ...$arguments
*/
public function doAfterBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
) {
}
}
45 changes: 45 additions & 0 deletions Controllers/Abstracts/AbstractUserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use function is_string;
use function reset;
use function str_ends_with;
use function str_starts_with;

abstract class AbstractUserController extends AbstractAdministrationController
{
protected ?string $authenticationMethod = self::TYPE_USER;

/**
* @param ServerRequestInterface $request
* @param string $method
* @param ...$arguments
* @return ResponseInterface|void|null
*/
public function doAfterBeforeDispatch(
ServerRequestInterface $request,
string $method,
...$arguments
) {
$reset = reset($arguments);
if ($request->getMethod() !== 'GET'
|| !($path = (reset($reset)?:[])[0]??null)
|| !is_string($path)
) {
return null;
}
if (($end = str_ends_with($path, '//')) || str_starts_with($path, '//')) {
return $this->redirect(
$this->getView()->getBaseURI(
'/'.
trim($path, '/')
. ($end ? '/': '')
)
);
}
}
}
2 changes: 1 addition & 1 deletion Controllers/Api/Admin/Upload/MediaUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Admin\Upload;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\DashboardAPI;
use ArrayAccess\TrayDigita\App\Modules\Media\Media;
use ArrayAccess\TrayDigita\App\Modules\Users\Users;
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Api/Admin/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Admin;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\DashboardAPI;
use ArrayAccess\TrayDigita\App\Modules\Users\Users;
use ArrayAccess\TrayDigita\Kernel\Decorator;
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Api/GlobalApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\RouteAPI;
use ArrayAccess\TrayDigita\Routing\Attributes\Any;
use Psr\Http\Message\ResponseInterface;
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Api/User/Upload/MediaUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\User\Upload;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\UserAPI;
use ArrayAccess\TrayDigita\App\Modules\Media\Media;
use ArrayAccess\TrayDigita\App\Modules\Users\Users;
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Api/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\User;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\UserAPI;
use ArrayAccess\TrayDigita\App\Modules\Users\Users;
use ArrayAccess\TrayDigita\Routing\Attributes\Abstracts\HttpMethodAttributeAbstract;
Expand Down
41 changes: 41 additions & 0 deletions Controllers/User/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\User;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractUserController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\User;
use ArrayAccess\TrayDigita\Routing\Attributes\Any;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use const PHP_INT_MIN;

#[User('/auth')]
class Auth extends AbstractUserController
{
protected bool $doRedirect = false;

protected bool $asJSON = false;

#[Any(
pattern: '/',
priority: PHP_INT_MIN
)]
public function login(
ServerRequestInterface $request,
ResponseInterface $response,
array $params
): ResponseInterface {
return $this->render(
'user/login',
[
'title' => $this->trans(
'Login to member area',
'core-module'
),
],
$response
);
// return new \ArrayAccess\TrayDigita\Responder\FileResponder\FileResponder(__FILE__);
}
}
30 changes: 30 additions & 0 deletions Controllers/User/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\User;

use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractUserController;
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\User;
use ArrayAccess\TrayDigita\Routing\Attributes\Any;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use const PHP_INT_MAX;

#[User]
class Main extends AbstractUserController
{
#[Any(
pattern: '(/.*)?',
priority: PHP_INT_MAX - 1000
)]
public function dashboard(
ServerRequestInterface $request,
ResponseInterface $response
) : ResponseInterface {
return $this->render(
'/user/dashboard',
[],
$response
);
}
}
Loading

0 comments on commit a5b2773

Please sign in to comment.