Skip to content

Commit 630d578

Browse files
committed
Setting ServiceProviders
1 parent 283e0f7 commit 630d578

15 files changed

+827
-265
lines changed

app/Core/Application.php

+487-131
Large diffs are not rendered by default.

app/Core/Bootloader.php

-46
Original file line numberDiff line numberDiff line change
@@ -126,36 +126,8 @@ public function boot(): void
126126

127127
$this->app = new Application();
128128

129-
$test = 1;
130-
131-
$this->app->make(AppSettings::class)->loadSettings();
132-
133-
$this->app->clearCache();
134-
135-
Events::discover_listeners();
136-
137129
$this->app = self::dispatch_filter("initialized", $this->app, ['bootloader' => $this]);
138130

139-
$config = $this->app['config'];
140-
141-
$this->setErrorHandler($config->debug ?? 0);
142-
143-
self::dispatch_event('config_initialized');
144-
145-
$request = $this->app->make(IncomingRequest::class);
146-
147-
if (! defined('BASE_URL')) {
148-
if (isset($config->appUrl) && !empty($config->appUrl)) {
149-
define('BASE_URL', $config->appUrl);
150-
} else {
151-
define('BASE_URL', $request->getSchemeAndHttpHost());
152-
}
153-
}
154-
155-
if (! defined('CURRENT_URL')) {
156-
define('CURRENT_URL', BASE_URL . $request->getRequestUri());
157-
}
158-
159131
self::dispatch_event("beginning", ['bootloader' => $this]);
160132

161133
if ($this->app::hasBeenBootstrapped()) {
@@ -204,25 +176,7 @@ private function handleRequest(): void
204176
}
205177
}
206178

207-
/**
208-
* @param int $debug
209-
* @return void
210-
*/
211-
private function setErrorHandler(int $debug): void
212-
{
213-
$incomingRequest = app(IncomingRequest::class);
214-
app()->bind(\Illuminate\Contracts\Debug\ExceptionHandler::class, \Leantime\Core\ExceptionHandler::class);
215-
216-
if (
217-
$debug == 0
218-
|| $incomingRequest instanceof HtmxRequest
219-
|| $incomingRequest instanceof ApiRequest
220-
) {
221-
return;
222-
}
223179

224-
Debug::enable();
225-
}
226180

227181

228182
}

app/Core/CurrentUser.php

-72
This file was deleted.

app/Core/Environment.php

-8
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ public function __construct(DefaultConfig $defaultConfiguration)
120120

121121
$end = microtime(true);
122122

123-
//Cache is not available until after install.
124-
Events::add_event_listener(
125-
'leantime.core.middleware.installed.handle.after_install',
126-
function () {
127-
//
128-
},
129-
20
130-
);
131123
}
132124

133125
public function updateCache() {

app/Core/HttpKernel.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ function: 'handle',
8181

8282
error_log($e);
8383

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

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

8989
if ($request instanceof HtmxRequest) {
@@ -127,7 +127,7 @@ public function terminate($request, $response)
127127
continue;
128128
}
129129

130-
app()->make($middleware)->terminate($request, $response);
130+
$this->app->make($middleware)->terminate($request, $response);
131131
}
132132

133133
//error_log("Before Request Terminated");

app/Core/Middleware/Installed.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Leantime\Core\Middleware;
44

55
use Closure;
6+
use Illuminate\Support\Facades\Cache;
67
use Leantime\Core\Eventhelpers;
78
use Leantime\Core\Frontcontroller;
89
use Leantime\Core\IncomingRequest;
@@ -50,8 +51,6 @@ public function handle(IncomingRequest $request, Closure $next): Response
5051

5152
self::dispatch_event('after_install');
5253

53-
\Illuminate\Support\Facades\Cache::set('installed', true);
54-
5554
$route = Frontcontroller::getCurrentRoute();
5655

5756
if($session_says && $route == "install") {

app/Core/Middleware/TrustProxies.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ public function __construct(Environment $config)
6060
['bootloader' => $this]
6161
);
6262

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

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

76+
$request::setTrustedProxies($this->proxies, $this->headers);
77+
7778
if (!$request->isFromTrustedProxy()) {
7879
return new Response(json_encode(['error' => 'Not a trusted proxy']), 403);
7980
}

app/Core/Providers/Auth.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Leantime\Core\Providers;
4+
5+
use Illuminate\Cache\MemcachedConnector;
6+
use Illuminate\Support\ServiceProvider;
7+
use Leantime\Core\CliRequest;
8+
use Leantime\Core\Events;
9+
use Leantime\Core\IncomingRequest;
10+
use Leantime\Domain\Auth\Services\Auth as AuthService;
11+
use Leantime\Domain\Oidc\Services\Oidc as OidcService;
12+
use Leantime\Domain\Setting\Services\Setting as SettingsService;
13+
14+
class Auth extends ServiceProvider
15+
{
16+
/**
17+
* Register any application services.
18+
*
19+
* @return void
20+
*/
21+
public function register()
22+
{
23+
$this->app->singleton(AuthService::class, AuthService::class);
24+
$this->app->singleton(OidcService::class, OidcService::class);
25+
26+
}
27+
28+
29+
}

app/Core/Providers/Cache.php

+81-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace Leantime\Core\Providers;
44

5+
use Illuminate\Cache\MemcachedConnector;
56
use Illuminate\Support\ServiceProvider;
7+
use Leantime\Core\AppSettings;
8+
use Leantime\Core\CliRequest;
9+
use Leantime\Core\Events;
10+
use Leantime\Core\IncomingRequest;
11+
use Leantime\Domain\Setting\Services\Setting as SettingsService;
612

713
class Cache extends ServiceProvider
814
{
@@ -13,14 +19,88 @@ class Cache extends ServiceProvider
1319
*/
1420
public function register()
1521
{
22+
23+
/**
24+
* @todo the following should eventually automatically turn caches into redis if available,
25+
* then memcached if available,
26+
* then fileStore
27+
*/
28+
$this->app->singleton(\Illuminate\Cache\CacheManager::class, function ($app) {
29+
30+
//installation cache is per server
31+
$this->app['config']['cache.stores.installation'] = [
32+
'driver' => 'file',
33+
'connection' => 'default',
34+
'path' => APP_ROOT . '/cache/installation',
35+
];
36+
37+
//Instance is per company id
38+
$instanceStore = fn () =>
39+
$this->app['config']['cache.stores.instance'] = [
40+
'driver' => 'file',
41+
'connection' => 'default',
42+
'path' => APP_ROOT . "/cache/" . $this->app->make(SettingsService::class)->getCompanyId(),
43+
];
44+
45+
if ($this->app->make(IncomingRequest::class) instanceof CliRequest) {
46+
if (empty($this->app->make(SettingsService::class)->getCompanyId())) {
47+
throw new \RuntimeException('You can\'t run this CLI command until you have installed Leantime.');
48+
}
49+
50+
$instanceStore();
51+
} else {
52+
//Initialize instance cache store only after install was successfull
53+
Events::add_event_listener(
54+
'leantime.core.middleware.installed.handle.after_install',
55+
function () use ($instanceStore) {
56+
if (! session("isInstalled")) {
57+
return;
58+
}
59+
$instanceStore();
60+
}
61+
);
62+
}
63+
64+
$cacheManager = new \Illuminate\Cache\CacheManager($app);
65+
66+
$cacheManager->setDefaultDriver('instance');
67+
68+
return $cacheManager;
69+
});
70+
$this->app->singleton('cache.store', fn ($app) => $app['cache']->driver());
71+
$this->app->singleton('cache.psr6', fn ($app) => new \Symfony\Component\Cache\Adapter\Psr16Adapter($app['cache.store']));
72+
$this->app->singleton('memcached.connector', fn () => new MemcachedConnector());
73+
74+
75+
$this->app->alias(\Illuminate\Cache\CacheManager::class, 'cache');
76+
$this->app->alias(\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class);
77+
78+
}
79+
80+
public function boot() {
81+
82+
83+
84+
$currentVersion = $this->app->make(AppSettings::class)->appVersion;
85+
$cachedVersion = \Illuminate\Support\Facades\Cache::store('installation')->rememberForever('version', fn () => $currentVersion);
86+
87+
if ($currentVersion == $cachedVersion) {
88+
return;
89+
}
90+
91+
\Illuminate\Support\Facades\Cache::store('installation')->flush();
92+
1693
}
1794

1895
/**
1996
* Manages the instance cache.
2097
*
2198
* @return void
2299
*/
23-
private function instanceCacheManager()
100+
public function checkCacheVersion(): void
24101
{
102+
103+
25104
}
105+
26106
}

app/Core/Providers/Db.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Leantime\Core\Providers;
4+
5+
use Illuminate\Cache\MemcachedConnector;
6+
use Illuminate\Support\ServiceProvider;
7+
use Leantime\Core\CliRequest;
8+
use Leantime\Core\Events;
9+
use Leantime\Core\IncomingRequest;
10+
use Leantime\Domain\Auth\Services\Auth as AuthService;
11+
use Leantime\Domain\Oidc\Services\Oidc as OidcService;
12+
use Leantime\Domain\Setting\Services\Setting as SettingsService;
13+
14+
class Db extends ServiceProvider
15+
{
16+
/**
17+
* Register any application services.
18+
*
19+
* @return void
20+
*/
21+
public function register()
22+
{
23+
$this->app->singleton(\Leantime\Core\Db::class, \Leantime\Core\Db::class);
24+
}
25+
26+
27+
}

0 commit comments

Comments
 (0)