Skip to content

Commit 8e46fdd

Browse files
committed
Update Leantime Core to be Laravel Compliant
1 parent 8300c85 commit 8e46fdd

28 files changed

+1458
-821
lines changed

app/Core/Application.php

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
namespace Leantime\Core;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Foundation\Http\Kernel;
8+
use Illuminate\Foundation\Mix;
9+
use Illuminate\Foundation\PackageManifest;
10+
use Illuminate\Foundation\ProviderRepository;
11+
use Illuminate\Log\LogServiceProvider;
12+
use Illuminate\Routing\Router;
13+
use Illuminate\Routing\RoutingServiceProvider;
14+
use Illuminate\Support\Collection;
15+
use Illuminate\Support\Str;
16+
use Leantime\Core\Configuration\Environment;
17+
use Leantime\Core\Console\ConsoleKernel;
18+
use Leantime\Core\Events\DispatchesEvents;
19+
use Leantime\Core\Http\ApiRequest;
20+
use Leantime\Core\Http\HttpKernel;
21+
use Leantime\Core\Http\IncomingRequest;
22+
use Leantime\Core\Providers\Events;
23+
use Leantime\Core\Providers\Logging;
24+
25+
26+
/**
27+
* Class Application
28+
*
29+
* Represents an application.
30+
*/
31+
class Application extends \Illuminate\Foundation\Application
32+
{
33+
use DispatchesEvents;
34+
35+
/**
36+
* Constructor for the class.
37+
*
38+
* @param string $basePath The base path for the application.
39+
* @return void
40+
*/
41+
public function __construct($basePath = null)
42+
{
43+
44+
$this->publicPath = 'public/';
45+
$this->namespace = 'Leantime\\';
46+
47+
if ($basePath) {
48+
$this->setBasePath($basePath);
49+
}
50+
51+
$this->appPath = $basePath."/app/Core";
52+
53+
//Larevel stores cache in bootstrap folder
54+
//Cache files are in root in Leantime not in bootstrap
55+
//Env vars are not available in config
56+
57+
//putenv('APP_EVENTS_CACHE=cache/events.php');
58+
//putenv('APP_CONFIG_CACHE=cache/config.php');
59+
//putenv('APP_ROUTES_CACHE=cache/routes.php');
60+
//putenv('APP_SERVICES_CACHE=cache/services.php');
61+
//putenv('APP_PACKAGES_CACHE=cache/packages.php');
62+
63+
//Our folder structure is different and we shall not bow to the bourgeoisie
64+
$this->useAppPath($this->basePath.'/app');
65+
$this->useConfigPath($this->basePath.'/config');
66+
$this->useEnvironmentPath($this->basePath.'/config');
67+
$this->useBootstrapPath($this->basePath.'/bootstrap');
68+
$this->usePublicPath($this->basePath.'/public');
69+
$this->useStoragePath($this->basePath.'/storage');
70+
$this->useLangPath($this->basePath.'/app/Language');
71+
72+
$this->registerBaseBindings();
73+
74+
//Loading some config vars so we can run events
75+
//$this->register(new \Leantime\Core\Providers\Environment($this));
76+
77+
$this->registerBaseServiceProviders();
78+
//$this->registerCoreContainerAliases();
79+
80+
//Overriding some of the aliases
81+
$this->registerLeantimeAliases();
82+
83+
}
84+
85+
/**
86+
* Register the base service providers.
87+
*
88+
* This method is used to register the base service providers required for the application.
89+
*
90+
* @return void
91+
*/
92+
protected function registerBaseServiceProviders()
93+
{
94+
//Loading some config vars so we can run events
95+
//$this->register(new \Leantime\Core\Providers\Environment($this));
96+
97+
$this->register(new \Leantime\Core\Providers\Events($this));
98+
$this->register(new LogServiceProvider($this));
99+
$this->register(new RoutingServiceProvider($this));
100+
101+
//Todo: Add event and see if that works here.
102+
103+
}
104+
105+
public function registerLeantimeAliases()
106+
{
107+
108+
foreach ([
109+
'app' => [self::class, \Illuminate\Foundation\Application::class, \Illuminate\Contracts\Container\Container::class, \Illuminate\Contracts\Foundation\Application::class, \Psr\Container\ContainerInterface::class],
110+
'cache' => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class],
111+
'cache.store' => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class, \Psr\SimpleCache\CacheInterface::class],
112+
'cache.psr6' => [\Symfony\Component\Cache\Adapter\Psr16Adapter::class, \Symfony\Component\Cache\Adapter\AdapterInterface::class, \Psr\Cache\CacheItemPoolInterface::class],
113+
'config' => [Environment::class, \Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class],
114+
'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\StringEncrypter::class],
115+
'events' => [\Leantime\Core\Events\EventDispatcher::class, \Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
116+
'request' => [IncomingRequest::class, ApiRequest::class, Console\CliRequest::class, \Illuminate\Http\Request::class, \Symfony\Component\HttpFoundation\Request::class],
117+
'redis' => [\Illuminate\Redis\RedisManager::class, \Illuminate\Contracts\Redis\Factory::class],
118+
'redis.connection' => [\Illuminate\Redis\Connections\Connection::class, \Illuminate\Contracts\Redis\Connection::class],
119+
'session' => [\Illuminate\Session\SessionManager::class],
120+
'session.store' => [\Illuminate\Session\Store::class, \Illuminate\Contracts\Session\Session::class],
121+
'router' => [Router::class, \Illuminate\Contracts\Routing\Registrar::class, \Illuminate\Contracts\Routing\BindingRegistrar::class],
122+
'view' => [\Illuminate\View\Factory::class, \Illuminate\Contracts\View\Factory::class],
123+
] as $key => $aliases) {
124+
foreach ($aliases as $alias) {
125+
$this->alias($key, $alias);
126+
}
127+
}
128+
129+
$this->alias(Application::class, \Illuminate\Contracts\Foundation\Application::class);
130+
131+
//$this->alias(DispatchesEvents::class, 'events');
132+
//$this->alias(Environment::class, 'config');
133+
}
134+
135+
//Boot with Leantime event dispatcher
136+
137+
138+
/**
139+
* Boot the application.
140+
*
141+
* @return void
142+
*/
143+
public function boot()
144+
{
145+
146+
//We need to discover events a lot earlier than Laravel wants us to.
147+
//So we're just doing it.
148+
\Illuminate\Support\Facades\Event::discoverListeners();
149+
150+
//Calling the first event
151+
self::dispatchEvent('beforeBootingServiceProviders');
152+
153+
parent::boot();
154+
155+
self::dispatchEvent('afterBootingServiceProviders');
156+
157+
}
158+
}

app/Core/Bootloader.php

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace Leantime\Core;
4+
5+
use Illuminate\Contracts\Container\BindingResolutionException;
6+
use Leantime\Core\Console\ConsoleKernel;
7+
use Leantime\Core\Events\DispatchesEvents;
8+
use Leantime\Core\Http\HttpKernel;
9+
use Leantime\Core\Http\IncomingRequest;
10+
11+
/**
12+
* Bootloader
13+
*/
14+
class Bootloader
15+
{
16+
use DispatchesEvents;
17+
18+
/**
19+
* Bootloader instance
20+
*
21+
* @var static
22+
*/
23+
protected static ?Bootloader $instance = null;
24+
25+
protected Application $app;
26+
27+
/**
28+
* Get the Bootloader instance
29+
*
30+
* @param Application $app
31+
*/
32+
public static function getInstance(): self
33+
{
34+
35+
if (is_null(static::$instance)) {
36+
static::$instance = new self;
37+
}
38+
39+
return static::$instance;
40+
}
41+
42+
/**
43+
* Constructor
44+
*/
45+
private function __construct() {}
46+
47+
/**
48+
* Execute the Application lifecycle.
49+
*
50+
* @return void
51+
*
52+
* @throws BindingResolutionException
53+
*/
54+
public function boot(Application $app)
55+
{
56+
if (! defined('LEANTIME_START')) {
57+
define('LEANTIME_START', microtime(true));
58+
}
59+
60+
//Start Application
61+
//Load the bindings and service providers
62+
$this->app = $app;
63+
64+
//Capture the request and instantiate the correct type
65+
$request = IncomingRequest::capture();
66+
67+
68+
//Use the right kernel for the job and handle the request.
69+
$this->handleRequest($request);
70+
71+
self::dispatchEvent('end', ['bootloader' => $this]);
72+
73+
}
74+
75+
/**
76+
* Handle the request
77+
*
78+
* @throws BindingResolutionException
79+
*/
80+
private function handleRequest($request): void
81+
{
82+
83+
if (! $this->app->runningInConsole()) {
84+
85+
/** @var HttpKernel $kernel */
86+
$kernel = $this->app->make(HttpKernel::class);
87+
88+
$kernelHandler = $kernel->handle($request);
89+
$response = $kernelHandler->send();
90+
91+
$kernel->terminate($request, $response);
92+
93+
} else {
94+
95+
/** @var ConsoleKernel $kernel */
96+
$kernel = $this->app->make(ConsoleKernel::class);
97+
98+
$status = $kernel->handle(
99+
$input = new \Symfony\Component\Console\Input\ArgvInput,
100+
new \Symfony\Component\Console\Output\ConsoleOutput
101+
);
102+
103+
$kernel->terminate($input, $status);
104+
105+
exit($status);
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)