Skip to content

Commit

Permalink
Fix plugin loading issue with phar files
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Nov 15, 2024
1 parent 28d8c23 commit 608a409
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 1,739 deletions.
63 changes: 0 additions & 63 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions app/Core/Controller/Frontcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,18 @@ public function parseRequestParts(IncomingRequest $request)
$method = $segments[2];
}

//If a third segment is set it is the id
if(isset($segments[3])) {
$id = $segments[3];
$method = $segments[2];
$request_parts = join('.', array_slice($segments, 3));
$this->incomingRequest->query->set('request_parts', $request_parts);
}

$this->incomingRequest->query->set('act', $moduleName.'.'.$controllerName.'.'.$method);
$this->incomingRequest->setCurrentRoute($moduleName.'.'.$controllerName);

if (! empty($id)) {
if ($id === "0" || !empty($id)) {
$this->incomingRequest->query->set('id', $id);
}

Expand Down Expand Up @@ -213,20 +221,20 @@ public function getValidControllerCall(string $moduleName, string $actionName, s

$moduleName = Str::studly($moduleName);
$actionName = Str::studly($actionName);
$methodName = Str::lower($methodName);
$methodNameLower = Str::lower($methodName);
$routepath = $moduleName.'.'.$controllerType.'.'.$actionName;
$actionPath = $moduleName.'\\'.$controllerType.'\\'.$actionName;

if ($this->config->debug == false) {
if (Cache::store('installation')->has('routes.'.$routepath.'.'.$methodName)) {
return Cache::store('installation')->get('routes.'.$routepath.'.'.$methodName);
if (Cache::store('installation')->has('routes.'.$routepath.'.'.$methodNameLower)) {
return Cache::store('installation')->get('routes.'.$routepath.'.'.$methodNameLower);
}
}

$classPath = $this->getClassPath($controllerType, $moduleName, $actionName);
$classMethod = $this->getValidControllerMethod($classPath, $methodName);

Cache::store('installation')->set('routes.'.$routepath.'.'.($classMethod == 'run' ? $methodName : $classMethod), ['class' => $classPath, 'method' => $classMethod]);
Cache::store('installation')->set('routes.'.$routepath.'.'.($classMethod == 'run' ? $methodNameLower : $classMethod), ['class' => $classPath, 'method' => $classMethod]);

return ['class' => $classPath, 'method' => $classMethod];
}
Expand Down
21 changes: 17 additions & 4 deletions app/Core/Http/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,25 @@ protected function sendRequestThroughRouter($request)

//This filter only works for system plugins
//Regular plugins are not available until after install verification
$this->middleware = self::dispatch_filter('plugins_middleware', $this->middleware, ['request' => $request]);
$this->middleware = self::dispatch_filter('middleware', $this->middleware, ['request' => $request]);

return (new \Illuminate\Routing\Pipeline($this->app))
//Main Pipeline
$response = (new \Illuminate\Routing\Pipeline($this->app))
->send($request)
->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
->then(fn () => Frontcontroller::dispatch_request($request));
->through($this->middleware)
->then(fn ($request) =>
//Then run through plugin pipeline
(new \Illuminate\Routing\Pipeline($this->app))
->send($request)
->through(self::dispatch_filter(
hook: 'plugins_middleware',
payload: [],
function: 'handle',
))
->then(fn () => Frontcontroller::dispatch_request($request))
);

return $response;
}

public function handle($request)
Expand Down
1 change: 0 additions & 1 deletion app/Core/Middleware/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Auth

public function __construct(
private AuthService $authService,
private ProjectsService $projectsService,
) {
$this->publicActions = self::dispatchFilter('publicActions', $this->publicActions, ['bootloader' => $this]);
}
Expand Down
32 changes: 30 additions & 2 deletions app/Core/Providers/Views.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function registerFactory()
foreach ($composers as $key => $composerClass) {
if (
(is_subclass_of($composerClass, \Leantime\Core\UI\Composer::class)
|| is_subclass_of($composerClass, \Leantime\Core\Controller\Composer::class))
|| is_subclass_of($composerClass, \Leantime\Core\Controller\Composer::class))
&&

! (new \ReflectionClass($composerClass))->isAbstract()
Expand Down Expand Up @@ -254,7 +254,35 @@ private function discoverComposerPaths()

$enabledPlugins = $this->app->make(\Leantime\Domain\Plugins\Services\Plugins::class)->getEnabledPlugins();
$pluginComposerClasses = collect($enabledPlugins)
->map(fn ($plugin) => glob(APP_ROOT.'/app/Plugins/'.$plugin->foldername.'/Composers/*.php'))
->map(function ($plugin) {
if ($plugin->format === 'phar') {
$pharPath = APP_ROOT.'/app/Plugins/'.$plugin->foldername.'/'.$plugin->foldername.'.phar';

if (!file_exists($pharPath)) {
return [];
}

try {

$composers = [];
$composerPath = 'phar://'.$pharPath.'/Composers';
$p = new \Phar($composerPath, 0);
$paths = collect(new \RecursiveIteratorIterator($p));

foreach($paths as $path) {
$something = $path;
$composers[] = 'Plugins/'.$plugin->foldername.'/Composers/'.$path->getFileName();
}

return $composers;

} catch (\Exception $e) {
return [];
}
}

return glob($basePath.'/Composers/*.php') ?: [];

Check failure on line 284 in app/Core/Providers/Views.php

View workflow job for this annotation

GitHub Actions / phpstan

Undefined variable: $basePath
})
->flatten();

$composerList = $appComposerClasses
Expand Down
3 changes: 2 additions & 1 deletion app/Domain/Plugins/Hxcontrollers/Details.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public function install(): string

//Parse and clean up error message
$errorJson = str_replace('HTTP request returned status code 500:', '', $e->getMessage());
$errorJson = str_replace('HTTP request returned status code 200:', '', $errorJson);
$errors = json_decode(trim($errorJson));
report($e);

$this->tpl->assign('formError', $errors->error ?? 'There was an error installing the plugin');
$this->tpl->assign('formError', $errors->error ?? $errors->message ?? 'There was an error installing the plugin');

return 'plugin-installation';
}
Expand Down
5 changes: 4 additions & 1 deletion app/Domain/Plugins/Services/Plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ public function removePlugin(int $id): bool

return $this->pluginRepository->removePlugin($id);

//TODO remove files savely
}

/**
Expand Down Expand Up @@ -579,6 +578,10 @@ public function clearCache()
$files = app()->make(\Illuminate\Filesystem\Filesystem::class);
$viewPathCachePath = storage_path('framework/viewPaths.php');
$files->delete($viewPathCachePath);

$composerPathCachePath = storage_path('framework/composerPaths.php');
$files->delete($composerPathCachePath);

}
}
}
8 changes: 5 additions & 3 deletions app/Domain/Plugins/Templates/plugindetails.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@
<div class="tw-text-green-500">{!! $formNotification !!}</div>
@else
<div id="installForm{{ $plugin->marketplaceId }}">


@if (! empty($formError))
<div class="tw-text-red-500">{!! $formError !!}</div>
@endif
<form
class="tw-flex tw-gap-2 tw-items-center"
hx-post="{{ BASE_URL }}/hx/plugins/details/install"
hx-swap="outerHTML"
hx-indicator=".htmx-indicator, .htmx-loaded-content"
hx-indicator=".htmx-indicator-small, .htmx-loaded-content"
hx-target="#installForm{{ $plugin->marketplaceId }}"
>
@php
Expand All @@ -133,8 +135,8 @@ class="tw-flex tw-gap-2 tw-items-center"
:tag="'button'"
:type="'secondary'"
>Install</x-global::button>
<div class="htmx-indicator">
<x-global::loadingText type="text" :count="5" />
<div class="htmx-indicator-small">
<x-global::loader id="loadingthis" size="25px" />
</div>
</form>
</div>
Expand Down
Loading

0 comments on commit 608a409

Please sign in to comment.