Skip to content

Commit

Permalink
WIP Separate Dashboard class
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Jan 27, 2025
1 parent 3649762 commit f874360
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 347 deletions.
87 changes: 87 additions & 0 deletions src/Platform/Configuration/ManagesMenu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Orchid\Platform\Configuration;

use Illuminate\Support\Collection;
use Orchid\Screen\Actions\Menu;

trait ManagesMenu
{
/**
* The collection of menu items.
*
* @var Collection<Menu>
*/
private ?Collection $menuItems;

/**
* @return Collection<Menu>
*/
public function menu(): Collection
{
$this->menuItems = $this->menuItems ?? collect();

return $this->menuItems;
}

/**
* Register a menu element with the Dashboard.
*
* @param Menu $menu The menu element to add.
*
* @return $this
*/
public function registerMenuElement(Menu $menu): static
{
if ($menu->get('sort', 0) === 0) {
$menu->sort($this->menu->count() + 1);

Check failure on line 37 in src/Platform/Configuration/ManagesMenu.php

View workflow job for this annotation

GitHub Actions / psalm

UndefinedThisPropertyFetch

src/Platform/Configuration/ManagesMenu.php:37:25: UndefinedThisPropertyFetch: Instance property Orchid\Platform\Dashboard::$menu is not defined (see https://psalm.dev/041)
}

$this->menu()->add($menu);

return $this;
}

/**
* Render the menu as a string for display.
*
* @throws \Throwable If rendering fails.
*
* @return string The rendered menu HTML.
*/
public function renderMenu(): string
{
return $this->menu()
->sort(fn (Menu $current, Menu $next) => $current->get('sort', 0) <=> $next->get('sort', 0))
->map(fn (Menu $menu) => (string) $menu->render())
->implode('');
}

/**
* Check if the menu is empty.
*
* @return bool True if the menu is empty, otherwise false.
*/
public function isEmptyMenu(): bool
{
return $this->menu()->isEmpty();
}

/**
* Add submenu items to a menu element identified by its slug.
*
* @param string $slug The slug of the menu element to update.
* @param Menu[] $list Array of submenu items to add.
*
* @return $this
*/
public function addMenuSubElements(string $slug, array $list): static
{
$this->menuItems = $this->menu()
->map(fn (Menu $menu) => $slug === $menu->get('slug')
? $menu->list($list)
: $menu);

return $this;
}
}
36 changes: 36 additions & 0 deletions src/Platform/Configuration/ManagesModelOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Orchid\Platform\Configuration;

use Illuminate\Support\Arr;

trait ManagesModelOptions
{
/**
* Get the model instance for a given key or class name.
*
* @return mixed
*/
public static function modelClass(string $key, ?string $default = null)
{
$model = static::model($key, $default);

return class_exists($model) ? new $model : $model;
}

/**
* Get the class name for a given Dashboard model.
*/
public static function model(string $key, ?string $default = null): string
{
return Arr::get(static::$options, 'models.'.$key, $default ?? $key);
}

/**
* Get the user model class name.
*/
public static function useModel(string $key, string $custom): void
{
static::$options['models'][$key] = $custom;
}
}
26 changes: 26 additions & 0 deletions src/Platform/Configuration/ManagesPackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Orchid\Platform\Configuration;

use Composer\InstalledVersions;

trait ManagesPackage
{
/**
* Get the version number of the application.
*/
public static function version(): string
{
return InstalledVersions::getVersion('orchid/platform');
}

/**
* The real path to the package files.
*/
public static function path(string $path = ''): string
{
$current = InstalledVersions::getInstallPath('orchid/platform');

return realpath($current.($path ? DIRECTORY_SEPARATOR.$path : $path));
}
}
87 changes: 87 additions & 0 deletions src/Platform/Configuration/ManagesPermissions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Orchid\Platform\Configuration;

use Illuminate\Support\Collection;
use Orchid\Platform\ItemPermission;

trait ManagesPermissions
{
/**
* Collection of permissions for the application.
*
* @var Collection
*/
private $permission;

/**
* Registers a ItemPermission that defines authentication permissions.
*
* @return $this
*/
public function registerPermissions(ItemPermission $permission): self
{
if (empty($permission->group)) {
return $this;
}

$old = $this->permission->get('all')
->get($permission->group, []);

$this->permission->get('all')
->put($permission->group, array_merge_recursive($old, $permission->items));

return $this;
}

/**
* Retrieve permissions based on specified groups.
*
* @param array|string $groups
*/
public function getPermission($groups = []): Collection
{
$all = $this->permission->get('all')
->when(! empty($groups), fn (Collection $collection) => $collection->only($groups));

$removed = $this->permission->get('removed');

if (! $removed->count()) {
return $all;
}

return $all->map(static function ($group) use ($removed) {
foreach ($group[key($group)] as $key => $item) {
if ($removed->contains($item)) {
unset($group[key($group)]);
}
}

return $group;
});
}

/**
* Get all registered permissions with the enabled state.
*
* @param array|string $groups
*/
public function getAllowAllPermission($groups = []): Collection
{
return $this->getPermission($groups)
->collapse()
->reduce(static fn (Collection $permissions, array $item) => $permissions->put($item['slug'], true), collect());
}

/**
* Remove a specific permission by key.
*
* @return $this
*/
public function removePermission(string $key): self
{
$this->permission->get('removed')->push($key);

return $this;
}
}
64 changes: 64 additions & 0 deletions src/Platform/Configuration/ManagesResources.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Orchid\Platform\Configuration;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use RuntimeException;

trait ManagesResources
{
/**
* Collection of JS and CSS resources for the panel.
*
* @var Collection
*/
public $resources;

/**
* Register a resource with the given key.
*
* @param string|array $value
*/
public function registerResource(string $key, $value): self
{
$item = $this->resources->get($key, []);

$this->resources[$key] = array_merge($item, Arr::wrap($value));

return $this;
}

/**
* Return CSS\JS.
*
* @param null $key
*
* @return array|Collection|mixed
*/
public function getResource($key = null)
{
if ($key === null) {
return $this->resources;
}

return $this->resources->get($key);
}

/**
* Determine published assets are up-to-date.
*
* @throws \RuntimeException
*
* @return bool
*/
public static function assetsAreCurrent(): bool
{
$publishedPath = public_path('vendor/orchid/mix-manifest.json');

throw_unless(File::exists($publishedPath), new RuntimeException('Orchid assets are not published. Please run: `php artisan orchid:publish`'));

return File::get($publishedPath) === File::get(__DIR__.'/../../public/mix-manifest.json');
}
}
65 changes: 65 additions & 0 deletions src/Platform/Configuration/ManagesScreens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Orchid\Platform\Configuration;

use Illuminate\Support\Facades\App;
use Orchid\Screen\Screen;

trait ManagesScreens
{
/**
* The current screen instance.
*
* @var Screen|null
*/
private ?Screen $currentScreen;

/**
* Determines whether the current request is a partial request or not.
* A partial request is a request that only loads a specific part of the page, such as a modal window or a section of content,
* instead of loading the entire page.
*
* @var bool Set to true if the current request is a partial request, false otherwise.
*/
private bool $partialRequest = false;

/**
* Get the current screen instance.
*
* @return Screen|null The current screen instance or null if not set.
*/
public function getCurrentScreen(): ?Screen
{
return $this->currentScreen;
}

/**
* Get the current screen instance.
*
* @return $this
*/
public function setCurrentScreen(Screen $screen, bool $partialRequest = false): self
{
$this->currentScreen = $screen;
$this->partialRequest = $partialRequest;

App::singleton($screen::class, static fn () => $screen);
App::rebinding($screen::class, static fn () => app($screen::class));

return $this;
}

/**
* Determines whether the current request is a partial request or not.
*
* A partial request is a request that only loads a specific part of the page, such as a modal window or a section of content,
* instead of loading the entire page. This method returns a boolean value indicating whether the current request is a partial
* request or not, based on the value of the $partialRequest property.
*
* @return bool True if the current request is a partial request, false otherwise.
*/
public function isPartialRequest(): bool
{
return $this->partialRequest;
}
}
Loading

0 comments on commit f874360

Please sign in to comment.