Defer loading of Admin

This commit is contained in:
Giuseppe Criscione 2022-11-27 19:41:15 +01:00
parent 23e8d19d74
commit 47654dd6ee
3 changed files with 27 additions and 35 deletions

View File

@ -8,7 +8,7 @@ use Formwork\Admin\Users\Users;
use Formwork\Assets;
use Formwork\Formwork;
use Formwork\Languages\LanguageCodes;
use Formwork\Page;
use Formwork\Pages\Page;
use Formwork\Response\RedirectResponse;
use Formwork\Utils\FileSystem;
use Formwork\Utils\HTTPRequest;
@ -40,18 +40,15 @@ final class Admin
*/
public function __construct()
{
$this->load();
}
public function load(): void
{
$this->loadSchemes();
$this->users = Users::load();
$this->loadTranslations();
$this->loadErrorHandler();
$this->loadRoutes();
}
/**
@ -254,13 +251,12 @@ final class Admin
*/
protected function loadTranslations(): void
{
$languageCode = Formwork::instance()->config()->get('admin.lang');
if ($this->isLoggedIn()) {
$languageCode = $this->user()->language();
}
$path = Formwork::instance()->config()->get('translations.paths.admin');
Formwork::instance()->translations()->loadFromPath($path);
Formwork::instance()->translations()->setCurrent($languageCode);
if ($this->isLoggedIn()) {
Formwork::instance()->translations()->setCurrent($this->user()->language());
}
}
protected function loadSchemes(): void
@ -282,15 +278,4 @@ final class Admin
});
}
}
/**
* Load administration panel routes
*/
protected function loadRoutes(): void
{
Formwork::instance()->router()->loadFromFile(
Formwork::instance()->config()->get('routes.files.admin'),
Str::wrap(Formwork::instance()->config()->get('admin.root'), '/')
);
}
}

View File

@ -3,11 +3,12 @@
namespace Formwork\Admin\Users;
use Formwork\Admin\Security\Password;
use Formwork\Data\Contracts\Arrayable;
use Formwork\Formwork;
use Formwork\Utils\Registry;
use Formwork\Utils\Session;
class User
class User implements Arrayable
{
/**
* Array containing user data
@ -83,7 +84,6 @@ class User
$this->$var = $this->data[$var];
}
$this->avatar = new Avatar($this->data['avatar']);
$this->permissions = new Permissions($this->role);
}
@ -132,7 +132,11 @@ class User
*/
public function avatar(): Avatar
{
return $this->avatar;
if (isset($this->avatar)) {
return $this->avatar;
}
return $this->avatar = new Avatar($this->data['avatar']);
}
/**

View File

@ -83,7 +83,6 @@ final class Formwork
$this->loadSite();
$this->loadCache();
$this->loadRouter();
$this->loadAdmin();
$this->loadRoutes();
}
@ -156,7 +155,12 @@ final class Formwork
*/
public function admin(): ?Admin
{
return $this->admin;
if (isset($this->admin)) {
return $this->admin;
}
return $this->admin = $this->config->get('admin.enabled')
? new Admin()
: null;
}
/**
@ -265,15 +269,7 @@ final class Formwork
protected function loadRouter(): void
{
$this->router = new Router();
}
protected function loadAdmin(): void
{
if ($this->config->get('admin.enabled')) {
$this->admin = new Admin();
$this->admin->load();
}
$this->router = new Router($this->request);
}
/**
@ -281,6 +277,13 @@ final class Formwork
*/
protected function loadRoutes(): void
{
if ($this->config->get('admin.enabled')) {
$this->router->loadFromFile(
$this->config()->get('routes.files.admin'),
Str::wrap($this->config()->get('admin.root'), '/')
);
}
$this->router->loadFromFile($this->config()->get('routes.files.system'));
}
}