Skip to content

Commit

Permalink
Setting ServiceProviders
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron committed Jun 9, 2024
1 parent 283e0f7 commit 630d578
Show file tree
Hide file tree
Showing 15 changed files with 827 additions and 265 deletions.
618 changes: 487 additions & 131 deletions app/Core/Application.php

Large diffs are not rendered by default.

46 changes: 0 additions & 46 deletions app/Core/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,8 @@ public function boot(): void

$this->app = new Application();

$test = 1;

$this->app->make(AppSettings::class)->loadSettings();

$this->app->clearCache();

Events::discover_listeners();

$this->app = self::dispatch_filter("initialized", $this->app, ['bootloader' => $this]);

$config = $this->app['config'];

$this->setErrorHandler($config->debug ?? 0);

self::dispatch_event('config_initialized');

$request = $this->app->make(IncomingRequest::class);

if (! defined('BASE_URL')) {
if (isset($config->appUrl) && !empty($config->appUrl)) {
define('BASE_URL', $config->appUrl);
} else {
define('BASE_URL', $request->getSchemeAndHttpHost());
}
}

if (! defined('CURRENT_URL')) {
define('CURRENT_URL', BASE_URL . $request->getRequestUri());
}

self::dispatch_event("beginning", ['bootloader' => $this]);

if ($this->app::hasBeenBootstrapped()) {
Expand Down Expand Up @@ -204,25 +176,7 @@ private function handleRequest(): void
}
}

/**
* @param int $debug
* @return void
*/
private function setErrorHandler(int $debug): void
{
$incomingRequest = app(IncomingRequest::class);
app()->bind(\Illuminate\Contracts\Debug\ExceptionHandler::class, \Leantime\Core\ExceptionHandler::class);

if (
$debug == 0
|| $incomingRequest instanceof HtmxRequest
|| $incomingRequest instanceof ApiRequest
) {
return;
}

Debug::enable();
}


}
72 changes: 0 additions & 72 deletions app/Core/CurrentUser.php

This file was deleted.

8 changes: 0 additions & 8 deletions app/Core/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ public function __construct(DefaultConfig $defaultConfiguration)

$end = microtime(true);

//Cache is not available until after install.
Events::add_event_listener(
'leantime.core.middleware.installed.handle.after_install',
function () {
//
},
20
);
}

public function updateCache() {
Expand Down
6 changes: 3 additions & 3 deletions app/Core/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function: 'handle',

error_log($e);

if (! app()->make(Environment::class)->debug) {
if (! $this->app->make(Environment::class)->debug) {

return app()->make(Template::class)->display('errors.error500', 'error');
return $this->app->make(Template::class)->display('errors.error500', 'error');
}

if ($request instanceof HtmxRequest) {
Expand Down Expand Up @@ -127,7 +127,7 @@ public function terminate($request, $response)
continue;
}

app()->make($middleware)->terminate($request, $response);
$this->app->make($middleware)->terminate($request, $response);
}

//error_log("Before Request Terminated");
Expand Down
3 changes: 1 addition & 2 deletions app/Core/Middleware/Installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Leantime\Core\Middleware;

use Closure;
use Illuminate\Support\Facades\Cache;
use Leantime\Core\Eventhelpers;
use Leantime\Core\Frontcontroller;
use Leantime\Core\IncomingRequest;
Expand Down Expand Up @@ -50,8 +51,6 @@ public function handle(IncomingRequest $request, Closure $next): Response

self::dispatch_event('after_install');

\Illuminate\Support\Facades\Cache::set('installed', true);

$route = Frontcontroller::getCurrentRoute();

if($session_says && $route == "install") {
Expand Down
3 changes: 2 additions & 1 deletion app/Core/Middleware/TrustProxies.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public function __construct(Environment $config)
['bootloader' => $this]
);

IncomingRequest::setTrustedProxies($this->proxies, $this->headers);
}

/**
Expand All @@ -74,6 +73,8 @@ public function __construct(Environment $config)
public function handle(IncomingRequest $request, Closure $next): Response
{

$request::setTrustedProxies($this->proxies, $this->headers);

if (!$request->isFromTrustedProxy()) {
return new Response(json_encode(['error' => 'Not a trusted proxy']), 403);
}
Expand Down
29 changes: 29 additions & 0 deletions app/Core/Providers/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Leantime\Core\Providers;

use Illuminate\Cache\MemcachedConnector;
use Illuminate\Support\ServiceProvider;
use Leantime\Core\CliRequest;
use Leantime\Core\Events;
use Leantime\Core\IncomingRequest;
use Leantime\Domain\Auth\Services\Auth as AuthService;
use Leantime\Domain\Oidc\Services\Oidc as OidcService;
use Leantime\Domain\Setting\Services\Setting as SettingsService;

class Auth extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(AuthService::class, AuthService::class);
$this->app->singleton(OidcService::class, OidcService::class);

}


}
82 changes: 81 additions & 1 deletion app/Core/Providers/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

namespace Leantime\Core\Providers;

use Illuminate\Cache\MemcachedConnector;
use Illuminate\Support\ServiceProvider;
use Leantime\Core\AppSettings;
use Leantime\Core\CliRequest;
use Leantime\Core\Events;
use Leantime\Core\IncomingRequest;
use Leantime\Domain\Setting\Services\Setting as SettingsService;

class Cache extends ServiceProvider
{
Expand All @@ -13,14 +19,88 @@ class Cache extends ServiceProvider
*/
public function register()
{

/**
* @todo the following should eventually automatically turn caches into redis if available,
* then memcached if available,
* then fileStore
*/
$this->app->singleton(\Illuminate\Cache\CacheManager::class, function ($app) {

//installation cache is per server
$this->app['config']['cache.stores.installation'] = [
'driver' => 'file',
'connection' => 'default',
'path' => APP_ROOT . '/cache/installation',
];

//Instance is per company id
$instanceStore = fn () =>
$this->app['config']['cache.stores.instance'] = [
'driver' => 'file',
'connection' => 'default',
'path' => APP_ROOT . "/cache/" . $this->app->make(SettingsService::class)->getCompanyId(),
];

if ($this->app->make(IncomingRequest::class) instanceof CliRequest) {
if (empty($this->app->make(SettingsService::class)->getCompanyId())) {
throw new \RuntimeException('You can\'t run this CLI command until you have installed Leantime.');
}

$instanceStore();
} else {
//Initialize instance cache store only after install was successfull
Events::add_event_listener(
'leantime.core.middleware.installed.handle.after_install',
function () use ($instanceStore) {
if (! session("isInstalled")) {
return;
}
$instanceStore();
}
);
}

$cacheManager = new \Illuminate\Cache\CacheManager($app);

$cacheManager->setDefaultDriver('instance');

return $cacheManager;
});
$this->app->singleton('cache.store', fn ($app) => $app['cache']->driver());
$this->app->singleton('cache.psr6', fn ($app) => new \Symfony\Component\Cache\Adapter\Psr16Adapter($app['cache.store']));
$this->app->singleton('memcached.connector', fn () => new MemcachedConnector());


$this->app->alias(\Illuminate\Cache\CacheManager::class, 'cache');
$this->app->alias(\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class);

}

public function boot() {



$currentVersion = $this->app->make(AppSettings::class)->appVersion;
$cachedVersion = \Illuminate\Support\Facades\Cache::store('installation')->rememberForever('version', fn () => $currentVersion);

if ($currentVersion == $cachedVersion) {
return;
}

\Illuminate\Support\Facades\Cache::store('installation')->flush();

}

/**
* Manages the instance cache.
*
* @return void
*/
private function instanceCacheManager()
public function checkCacheVersion(): void
{


}

}
27 changes: 27 additions & 0 deletions app/Core/Providers/Db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Leantime\Core\Providers;

use Illuminate\Cache\MemcachedConnector;
use Illuminate\Support\ServiceProvider;
use Leantime\Core\CliRequest;
use Leantime\Core\Events;
use Leantime\Core\IncomingRequest;
use Leantime\Domain\Auth\Services\Auth as AuthService;
use Leantime\Domain\Oidc\Services\Oidc as OidcService;
use Leantime\Domain\Setting\Services\Setting as SettingsService;

class Db extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(\Leantime\Core\Db::class, \Leantime\Core\Db::class);
}


}
Loading

0 comments on commit 630d578

Please sign in to comment.