Skip to content

Commit

Permalink
Move remaining loading logic to PanelServiceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
giuscris committed Jun 7, 2024
1 parent cc3054f commit ba8706a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 115 deletions.
112 changes: 4 additions & 108 deletions formwork/src/Panel/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,19 @@

namespace Formwork\Panel;

use Formwork\App;
use Formwork\Assets;
use Formwork\Config\Config;
use Formwork\Http\Request;
use Formwork\Http\Session\MessageType;
use Formwork\Languages\LanguageCodes;
use Formwork\Panel\Controllers\ErrorsController;
use Formwork\Panel\Users\Permissions;
use Formwork\Panel\Users\Role;
use Formwork\Panel\Users\RoleCollection;
use Formwork\Panel\Users\User;
use Formwork\Panel\Users\UserCollection;
use Formwork\Panel\Users\UserFactory;
use Formwork\Parsers\Yaml;
use Formwork\Services\Container;
use Formwork\Translations\Translations;
use Formwork\Utils\FileSystem;
use Formwork\Utils\Str;
use Formwork\Utils\Uri;
use Throwable;

final class Panel
{
/**
* All the registered users
*/
protected UserCollection $users;

/**
* Errors controller
*/
protected ErrorsController $errors;

/**
* Assets instance
*/
Expand All @@ -44,29 +24,12 @@ final class Panel
* Create a new Panel instance
*/
public function __construct(
protected Container $container,
protected App $app,
protected Config $config,
protected Request $request,
protected Translations $translations,
protected UserFactory $userFactory
protected UserCollection $userCollection
) {
}

public function load(): void
{

// TODO: Move to service loader
$this->loadSchemes();
$this->loadUsers();

$this->loadTranslations();

if ($this->isLoggedIn()) {
$this->loadErrorHandler();
}
}

/**
* Return whether a user is logged in
*/
Expand All @@ -76,15 +39,15 @@ public function isLoggedIn(): bool
return false;
}
$username = $this->request->session()->get('FORMWORK_USERNAME');
return !empty($username) && $this->users->has($username);
return !empty($username) && $this->userCollection->has($username);
}

/**
* Return all registered users
*/
public function users(): UserCollection
{
return $this->users;
return $this->userCollection;
}

/**
Expand All @@ -93,7 +56,7 @@ public function users(): UserCollection
public function user(): User
{
$username = $this->request->session()->get('FORMWORK_USERNAME');
return $this->users->get($username);
return $this->userCollection->get($username);
}

/**
Expand Down Expand Up @@ -215,71 +178,4 @@ public function availableTranslations(): array

return $translations;
}

protected function getRoles(): RoleCollection
{
$roles = [];

foreach (FileSystem::listFiles($path = $this->config->get('system.panel.paths.roles')) as $file) {
$parsedData = Yaml::parseFile(FileSystem::joinPaths($path, $file));
$id = FileSystem::name($file);
$permissions = new Permissions($parsedData['permissions']);
$roles[$id] = new Role($id, $parsedData['title'], $permissions, $this->translations);
}

return new RoleCollection($roles);
}

protected function loadUsers(): void
{
$roleCollection = $this->getRoles();

$users = [];
foreach (FileSystem::listFiles($path = $this->config->get('system.panel.paths.accounts')) as $file) {
/**
* @var array{username: string, fullname: string, hash: string, email: string, language: string, role?: string, image?: string, colorScheme?: string}
*/
$parsedData = Yaml::parseFile(FileSystem::joinPaths($path, $file));
$role = $roleCollection->get($parsedData['role'] ?? 'user', 'user');

$users[$parsedData['username']] = $this->userFactory->make($parsedData, $role);
}

$this->users = new UserCollection($users, $roleCollection);
}

/**
* Load proper panel translation
*/
protected function loadTranslations(): void
{
$path = $this->config->get('system.translations.paths.panel');
$this->translations->loadFromPath($path);

if ($this->isLoggedIn()) {
$this->translations->setCurrent($this->user()->language());
} else {
$this->translations->setCurrent($this->config->get('system.panel.translation'));
}
}

protected function loadSchemes(): void
{
$path = $this->config->get('system.schemes.paths.panel');
$this->app->schemes()->loadFromPath($path);
}

/**
* Load the panel-styled error handler
*/
protected function loadErrorHandler(): void
{
if ($this->config->get('system.errors.setHandlers')) {
$this->errors = $this->container->build(ErrorsController::class);
set_exception_handler(function (Throwable $throwable): never {
$this->errors->internalServerError($throwable)->send();
throw $throwable;
});
}
}
}
9 changes: 6 additions & 3 deletions formwork/src/Panel/Users/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class User extends Model
*
* @param array<string, mixed> $data
*/
public function __construct(array $data, protected Role $role, protected App $app, protected Config $config, protected Request $request, protected Panel $panel)
public function __construct(array $data, protected Role $role, protected App $app, protected Config $config, protected Request $request)
{
$this->scheme = $app->schemes()->get('users.user');

Expand All @@ -71,10 +71,13 @@ public function image(): UserImage
$filename = (string) $this->data['image'];
$path = FileSystem::joinPaths($this->config->get('system.panel.paths.assets'), 'images/users/', $filename);

/** @var Panel */
$panel = $this->app->panel();

if (FileSystem::isFile($path, assertExists: false)) {
$uri = $this->panel->realUri('/assets/images/users/' . basename($path));
$uri = $panel->realUri('/assets/images/users/' . basename($path));
} else {
$uri = $this->panel->realUri('/assets/images/user-image.svg');
$uri = $panel->realUri('/assets/images/user-image.svg');
}

return new UserImage($path, $uri);
Expand Down
79 changes: 75 additions & 4 deletions formwork/src/Services/Loaders/PanelServiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,40 @@
use Formwork\Config\Config;
use Formwork\Http\Request;
use Formwork\Log\Registry;
use Formwork\Panel\Controllers\ErrorsController;
use Formwork\Panel\Modals\ModalFactory;
use Formwork\Panel\Panel;
use Formwork\Panel\Security\AccessLimiter;
use Formwork\Panel\Users\Permissions;
use Formwork\Panel\Users\Role;
use Formwork\Panel\Users\RoleCollection;
use Formwork\Panel\Users\UserCollection;
use Formwork\Panel\Users\UserFactory;
use Formwork\Parsers\Yaml;
use Formwork\Schemes\Schemes;
use Formwork\Services\Container;
use Formwork\Services\ResolutionAwareServiceLoaderInterface;
use Formwork\Translations\Translations;
use Formwork\Updater\Updater;
use Formwork\Utils\FileSystem;
use Formwork\View\ViewFactory;
use Throwable;

class PanelServiceLoader implements ResolutionAwareServiceLoaderInterface
{
public function __construct(protected Config $config, protected ViewFactory $viewFactory, protected Request $request)
{
protected RoleCollection $roleCollection;

protected UserCollection $userCollection;

public function __construct(
protected Container $container,
protected Config $config,
protected ViewFactory $viewFactory,
protected Request $request,
protected Schemes $schemes,
protected Translations $translations,
protected UserFactory $userFactory
) {
}

public function load(Container $container): Panel
Expand All @@ -33,7 +54,11 @@ public function load(Container $container): Panel

$container->define(ModalFactory::class);

return $container->build(Panel::class);
$this->roleCollection = new RoleCollection();

$this->userCollection = new UserCollection([], $this->roleCollection);

return $container->build(Panel::class, ['userCollection' => $this->userCollection]);
}

/**
Expand All @@ -43,6 +68,52 @@ public function onResolved(object $service, Container $container): void
{
$this->viewFactory->setMethods($container->call(require FileSystem::joinPaths($this->config->get('system.panel.path'), 'helpers.php')));

$service->load();
$this->schemes->loadFromPath($this->config->get('system.schemes.paths.panel'));

$this->translations->loadFromPath($this->config->get('system.translations.paths.panel'));

$this->loadRoles();

$this->loadUsers();

if ($service->isLoggedIn()) {
$this->translations->setCurrent($service->user()->language());
} else {
$this->translations->setCurrent($this->config->get('system.panel.translation'));
}

if ($service->isLoggedIn() && $this->config->get('system.errors.setHandlers')) {
$errorsController = $this->container->build(ErrorsController::class);
set_exception_handler(function (Throwable $throwable) use ($errorsController): never {
$errorsController->internalServerError($throwable)->send();
throw $throwable;
});
}
}

protected function loadRoles(): void
{
foreach (FileSystem::listFiles($path = $this->config->get('system.panel.paths.roles')) as $file) {
/**
* @var array{title: string, permissions: array<string, bool>}
*/
$data = Yaml::parseFile(FileSystem::joinPaths($path, $file));
$id = FileSystem::name($file);
$permissions = new Permissions($data['permissions']);
$this->roleCollection->set($id, new Role($id, $data['title'], $permissions, $this->translations));
}
}

protected function loadUsers(): void
{
foreach (FileSystem::listFiles($path = $this->config->get('system.panel.paths.accounts')) as $file) {
/**
* @var array{username: string, fullname: string, hash: string, email: string, language: string, role?: string, image?: string, colorScheme?: string}
*/
$data = Yaml::parseFile(FileSystem::joinPaths($path, $file));
$role = $this->roleCollection->get($data['role'] ?? 'user');
$username = $data['username'];
$this->userCollection->set($username, $this->userFactory->make($data, $role));
}
}
}

0 comments on commit ba8706a

Please sign in to comment.