Skip to content

Commit

Permalink
refactor(facades): Added facades for PbUtilities class / Overrides mo…
Browse files Browse the repository at this point in the history
…ved to 'Overrides' directory
  • Loading branch information
anibalealvarezs committed Feb 14, 2022
1 parent 48e8499 commit b90ac75
Show file tree
Hide file tree
Showing 40 changed files with 147 additions and 115 deletions.
6 changes: 3 additions & 3 deletions src/Actions/Session/PbCheckSingleSessionOnLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class PbCheckSingleSessionOnLogin
public function handle($request, $next)
{
if (!$request->user()) {
redirect()->route('login');
to_route('login');
}
if (!$user = PbUser::find($request->user()->id)) {
redirect()->route('login');
to_route('login');
}
if ((!$lastSession = $user->last_session) || !getConfigValue('_ENABLE_SINGLE_SESSION_')) {
return $next($request);
Expand All @@ -31,7 +31,7 @@ public function handle($request, $next)
auth('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('login')->with('error', 'This user has already started a session.');
return to_route('login')->with('error', 'This user has already started a session.');
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/Actions/Session/PbEnsureLoginIsNotDisabled.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class PbEnsureLoginIsNotDisabled
public function handle($request, $next)
{
if (!$request->user()) {
redirect()->route('login');
to_route('login');
}
if (!$user = PbUser::find($request->user()->id)) {
redirect()->route('login');
to_route('login');
}

if (!PbConfig::getValueByKey('_DISABLE_LOGIN_') || $user->hasAnyRole(['super-admin', 'admin'])) {
Expand All @@ -31,6 +31,6 @@ public function handle($request, $next)
auth('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('login')->with('error', 'You\'re not allowed to login at this moment.');
return to_route('login')->with('error', 'You\'re not allowed to login at this moment.');
}
}
51 changes: 26 additions & 25 deletions src/Controllers/PbBuilderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Anibalealvarezs\Projectbuilder\Controllers;

use Anibalealvarezs\Projectbuilder\Utilities\PbDebugbar;
use Anibalealvarezs\Projectbuilder\Facades\PbUtilitiesFacade;
use Anibalealvarezs\Projectbuilder\Overrides\Classes\PbDebugbar;
use Anibalealvarezs\Projectbuilder\Utilities\PbUtilities;
use Anibalealvarezs\Projectbuilder\Utilities\Shares;
use Anibalealvarezs\Projectbuilder\Utilities\PbShares;
use Anibalealvarezs\Projectbuilder\Models\PbCountry;
use Anibalealvarezs\Projectbuilder\Models\PbCurrentUser;
use Anibalealvarezs\Projectbuilder\Models\PbLanguage;
Expand Down Expand Up @@ -42,9 +43,9 @@ class PbBuilderController extends Controller
function __construct(Request $request, $crud_perms = false)
{
$this->initVars();
$this->vars->helper->prefix = ($this->vars->helper->prefix ?? getAttributeStatically($this->vars->helper->class, 'prefix'));
$this->vars->helper->vendor = ($this->vars->helper->vendor ?? getAttributeStatically($this->vars->helper->class, 'vendor'));
$this->vars->helper->package = ($this->vars->helper->package ?? getAttributeStatically($this->vars->helper->class, 'package'));
$this->vars->helper->prefix = ($this->vars->helper->prefix ?? resolve($this->vars->helper->class)->prefix);
$this->vars->helper->vendor = ($this->vars->helper->vendor ?? resolve($this->vars->helper->class)->vendor);
$this->vars->helper->package = ($this->vars->helper->package ?? resolve($this->vars->helper->class)->package);
if (!isset($this->vars->helper->keys['level'])) {
$this->vars->helper->keys['level'] = ($this->vars->keys['level'] ?? 'Builder');
}
Expand Down Expand Up @@ -104,7 +105,7 @@ public function index(
'delete ' . $this->vars->level->names => 'delete',
];

PbDebugbar::measure('builder crud controller model config load', function() use (&$config) {
PbDebugbar::measure('builder crud controller - model config load', function() use (&$config) {
$config = ($this->vars->config ?? $this->vars->level->modelPath::getCrudConfig(true));
});

Expand All @@ -114,7 +115,7 @@ public function index(
'delete' => []
];
}
$config['enabled_actions'] = Shares::allowed($this->vars->allowed)['allowed'];
$config['enabled_actions'] = PbShares::allowed($this->vars->allowed)['allowed'];

if (!isset($config['model'])) {
$config['model'] = $this->vars->level->modelPath;
Expand All @@ -123,19 +124,19 @@ public function index(
$config['pagination']['page'] = $page;
}
$this->vars->listing = self::buildListingRow($config);
$this->vars->sortable = $this->vars->sortable && app(PbCurrentUser::class)->hasPermissionTo('update '.(new $this->vars->level->modelPath)->getTable());
$this->vars->sortable = $this->vars->sortable && app(PbCurrentUser::class)->hasPermissionTo('update '.resolve($this->vars->level->modelPath)->getTable());
$this->vars->formconfig = ($config['formconfig'] ?? []);
$this->vars->pagination = !$this->vars->sortable ? $config['pagination'] : [];
$this->vars->heading = $config['heading'];
$this->vars->orderby = !$this->vars->sortable && $orderby ? ['field' => $field, 'order' => $order] : [];

PbDebugbar::measure('builder crud controller model building', function() use (&$arrayElements, $element, $multiple, $page, $perpage, $orderby, $field, $order) {
PbDebugbar::measure('builder crud controller - model building', function() use (&$arrayElements, $element, $multiple, $page, $perpage, $orderby, $field, $order) {
$arrayElements = $this->buildModelsArray($element, $multiple, null, true, $page, $perpage, $orderby, $field, $order);
});

PbDebugbar::addMessage($arrayElements, 'data');

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->renderResponse($this->buildRouteString($route, 'index'), $arrayElements);
}

Expand All @@ -150,13 +151,13 @@ public function create(string $route = 'level'): InertiaResponse|JsonResponse
PbDebugbar::stopMeasure('model_controller');
PbDebugbar::startMeasure('builder_controller', 'builder crud controller');

PbDebugbar::measure('builder crud controller model config load', function() {
PbDebugbar::measure('builder crud controller - model config load', function() {
$this->vars->formconfig = $this->vars->level->modelPath::getCrudConfig(true)['formconfig'];
});

PbDebugbar::addMessage($this->vars->formconfig, 'formconfig');

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->renderResponse($this->buildRouteString($route, 'create'));
}

Expand Down Expand Up @@ -190,7 +191,7 @@ public function store(Request $request): Redirector|RedirectResponse|Application
return $this->redirectResponseCRUDFail($request, 'create', "Error saving {$this->vars->level->name}");
}

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->redirectResponseCRUDSuccess($request, 'create');
} catch (Exception $e) {
return $this->redirectResponseCRUDFail($request, 'create', $e->getMessage());
Expand Down Expand Up @@ -228,7 +229,7 @@ public function show(
return $this->redirectResponseCRUDFail(request(), 'show', "You don't have permission to view this {$this->vars->level->name}");
}

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->renderResponse($this->buildRouteString($route, 'show'), $model);
}

Expand All @@ -251,7 +252,7 @@ public function edit(
PbDebugbar::stopMeasure('model_controller');
PbDebugbar::startMeasure('builder_controller', 'builder crud controller');

PbDebugbar::measure('builder crud controller model config load', function() {
PbDebugbar::measure('builder crud controller - model config load', function() {
$this->vars->formconfig = $this->vars->level->modelPath::getCrudConfig(true)['formconfig'];
});

Expand All @@ -269,7 +270,7 @@ public function edit(
return $this->redirectResponseCRUDFail(request(), 'edit', "You don't have permission to edit this {$this->vars->level->name}");
}

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->renderResponse($this->buildRouteString($route, 'edit'), $model);
}

Expand Down Expand Up @@ -314,7 +315,7 @@ public function update(Request $request, int $id): Redirector|RedirectResponse|A
return $this->redirectResponseCRUDFail($request, 'update', "Error updating {$this->vars->level->name}");
}

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->redirectResponseCRUDSuccess($request, 'update');
} catch (Exception $e) {
return $this->redirectResponseCRUDFail($request, 'update', $e->getMessage());
Expand Down Expand Up @@ -349,7 +350,7 @@ public function destroy(Request $request, int $id): Redirector|RedirectResponse|
return $this->redirectResponseCRUDFail($request, 'delete', "Error deleting {$this->vars->level->name}");
}

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->redirectResponseCRUDSuccess($request, 'delete');
} catch (Exception $e) {
return $this->redirectResponseCRUDFail($request, 'delete', $e->getMessage());
Expand Down Expand Up @@ -397,7 +398,7 @@ public function sort(Request $request, int $id): Redirector|RedirectResponse|App
$n++;
}

PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->redirectResponseCRUDSuccess($request, 'sort');
} catch (Exception $e) {
return $this->redirectResponseCRUDFail($request,
Expand Down Expand Up @@ -427,7 +428,7 @@ public function enable(Request $request, int $id): Redirector|RedirectResponse|A
}
try {
if ($model->enable()) {
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->redirectResponseCRUDSuccess($request, 'enable');
}
return $this->redirectResponseCRUDFail($request, 'enable',
Expand Down Expand Up @@ -464,7 +465,7 @@ public function disable(Request $request, int $id): Redirector|RedirectResponse|
}
try {
if ($model->disable()) {
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller response building');
PbDebugbar::startMeasure('builder_controller_response_building', 'builder crud controller - response building');
return $this->redirectResponseCRUDSuccess($request, 'disable');
}
return $this->redirectResponseCRUDFail($request, 'disable',
Expand Down Expand Up @@ -531,8 +532,8 @@ protected function shareVars()
{
$shared = [
...$this->globalInertiaShare(),
...Shares::allowed($this->vars->allowed),
...Shares::list($this->vars->shares),
...PbShares::allowed($this->vars->allowed),
...PbShares::list($this->vars->shares),
...['sort' => $this->vars->sortable],
...['showpos' => $this->vars->showPosition],
...['showid' => $this->vars->showId],
Expand Down Expand Up @@ -637,7 +638,7 @@ protected function renderResponse($view, array $elements = [], bool $nullable =
// If not API
if (!isApi($this->vars->request)) {

PbDebugbar::measure('builder crud controller share building', function() {
PbDebugbar::measure('builder crud controller - share building', function() {
$this->shareVars();
});

Expand Down Expand Up @@ -690,7 +691,7 @@ public function buildControllerVars($key): object
$object->prefixNames = Str::plural($object->prefixName);
$object->modelPath = $this->vars->helper->vendor . "\\" . $this->vars->helper->package . "\\Models\\" . $this->vars->helper->prefix . $key;
$object->viewsPath = $this->vars->helper->package . "/" . $object->keys . "/";
$object->table = (new $object->modelPath())->getTable();
$object->table = resolve($object->modelPath)->getTable();
return $object;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Permission/PbPermissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function destroy(Request $request, int $id): Redirector|RedirectResponse|
$request->session()->flash('flash.banner', 'This permission can not be deleted!');
$request->session()->flash('flash.bannerStyle', 'danger');

return redirect()->route($this->vars->level->names . '.index');
return to_route($this->vars->level->names . '.index');
}
// Model delete
if (!$model->delete()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/User/PbUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Anibalealvarezs\Projectbuilder\Controllers\PbBuilderController;

use Anibalealvarezs\Projectbuilder\Models\PbCurrentUser;
use Anibalealvarezs\Projectbuilder\Utilities\PbDebugbar;
use Anibalealvarezs\Projectbuilder\Overrides\Classes\PbDebugbar;
use App\Http\Requests;

use App\Models\Team;
Expand Down
13 changes: 13 additions & 0 deletions src/Facades/PbUtilitiesFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Anibalealvarezs\Projectbuilder\Facades;

use Illuminate\Support\Facades\Facade;

class PbUtilitiesFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'PbUtilities';
}
}
12 changes: 0 additions & 12 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,6 @@ function setCollectionAttributeDatetimeFormat(
});
}

/**
* Returns existing migration file if found, else uses the current timestamp.
*
* @param string $class
* @param $key
* @return mixed
*/
function getAttributeStatically(string $class, $key): mixed
{
return (new $class())->{$key};
}

/**
* Returns existing migration file if found, else uses the current timestamp.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/PbIsDebugModeEnabledMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Anibalealvarezs\Projectbuilder\Middleware;

use Anibalealvarezs\Projectbuilder\Utilities\PbDebugbar;
use Anibalealvarezs\Projectbuilder\Overrides\Classes\PbDebugbar;
use Closure;
use Illuminate\Http\Request;

Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/PbSingleSessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function handle($request, Closure $next): mixed
}

if (!$user = PbUser::find($request->user()->id)) {
redirect()->route('login');
to_route('login');
}

if (!getConfigValue('_ENABLE_SINGLE_SESSION_')) {
Expand All @@ -42,7 +42,7 @@ public function handle($request, Closure $next): mixed
auth('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect()->route('login')->with('error', 'Your session is no longer valid.');
return to_route('login')->with('error', 'Your session is no longer valid.');
}

return $next($request);
Expand Down
4 changes: 2 additions & 2 deletions src/Models/PbConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Anibalealvarezs\Projectbuilder\Models;

use Anibalealvarezs\Projectbuilder\Interfaces\PbModelCrudInterface;
use Anibalealvarezs\Projectbuilder\Utilities\Shares;
use Anibalealvarezs\Projectbuilder\Utilities\PbShares;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Auth;

Expand Down Expand Up @@ -123,7 +123,7 @@ public static function getCrudConfig(bool $includeForm = false): array
'type' => 'select',
'list' => [
...[['id' => 0, 'name' => '[none]']],
...Shares::getModules()['modules']->toArray()
...PbShares::getModules()['modules']->toArray()
],
],
];
Expand Down
4 changes: 2 additions & 2 deletions src/Models/PbLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Anibalealvarezs\Projectbuilder\Models;

use Anibalealvarezs\Projectbuilder\Interfaces\PbModelCrudInterface;
use Anibalealvarezs\Projectbuilder\Utilities\Shares;
use Anibalealvarezs\Projectbuilder\Utilities\PbShares;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Auth;
Expand Down Expand Up @@ -158,7 +158,7 @@ public static function getCrudConfig(bool $includeForm = false): array
'type' => 'select',
'list' => [
...[['id' => 0, 'name' => '[none]']],
...Shares::getModules()['modules']->toArray()
...PbShares::getModules()['modules']->toArray()
],
],
];
Expand Down
8 changes: 4 additions & 4 deletions src/Models/PbNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Anibalealvarezs\Projectbuilder\Models;

use Anibalealvarezs\Projectbuilder\Interfaces\PbModelCrudInterface;
use Anibalealvarezs\Projectbuilder\Utilities\Shares;
use Anibalealvarezs\Projectbuilder\Utilities\PbShares;
use Anibalealvarezs\Projectbuilder\Traits\PbModelEnableableTrait;
use Anibalealvarezs\Projectbuilder\Traits\PbModelSortableTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -160,12 +160,12 @@ public static function getCrudConfig(bool $includeForm = false): array
'type' => 'select',
'list' => [
...[['id'=>0, 'name'=>'[none]']],
...Shares::getNavigations()['navigations']['full']->toArray()
...PbShares::getNavigations()['navigations']['full']->toArray()
],
],
'permission' => [
'type' => 'select',
'list' => Shares::getPermissionsAll()['permissionsall']->toArray(),
'list' => PbShares::getPermissionsAll()['permissionsall']->toArray(),
],
'status' => [
'type' => 'select',
Expand All @@ -184,7 +184,7 @@ public static function getCrudConfig(bool $includeForm = false): array
'type' => 'select',
'list' => [
...[['id' => 0, 'name' => '[none]']],
...Shares::getModules()['modules']->toArray()
...PbShares::getModules()['modules']->toArray()
],
],
];
Expand Down
Loading

0 comments on commit b90ac75

Please sign in to comment.