1
|
|
|
<?php declare(strict_types=1);
|
2
|
|
|
|
3
|
|
|
namespace Bone;
|
4
|
|
|
|
5
|
|
|
use Barnacle\Container;
|
6
|
|
|
use Bone\Http\Middleware\Stack;
|
7
|
|
|
use Bone\Router\Router;
|
8
|
|
|
use Bone\Server\Environment;
|
9
|
|
|
use Bone\Server\SiteConfig;
|
10
|
|
|
use Del\SessionManager;
|
11
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
12
|
|
|
use Laminas\Diactoros\Response\RedirectResponse;
|
13
|
|
|
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
|
14
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
15
|
|
|
|
16
|
|
|
class Application
|
17
|
|
|
{
|
18
|
|
|
/** @var Container $container */
|
19
|
|
|
private $container;
|
20
|
|
|
|
21
|
|
|
/** @var string $configFolder */
|
22
|
|
|
private $configFolder = 'config';
|
23
|
|
|
|
24
|
|
|
/** @var string $environment */
|
25
|
|
|
private $environment = 'production';
|
26
|
|
|
|
27
|
|
|
/**
|
28
|
|
|
* There be nay feckin wi' constructors on board this ship
|
29
|
|
|
* There be nay copyin' o' th'ship either
|
30
|
|
|
* This ship is a singleton!
|
31
|
|
|
*/
|
32
|
|
|
private function __construct(){}
|
33
|
|
|
private function __clone(){}
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* Ahoy! There nay be boardin without yer configuration
|
38
|
|
|
*
|
39
|
|
|
* @param array $config
|
40
|
|
|
* @return Application
|
41
|
|
|
*/
|
42
|
11 |
|
public static function ahoy()
|
43
|
|
|
{
|
44
|
11 |
|
static $inst = null;
|
45
|
11 |
|
if ($inst === null) {
|
46
|
1 |
|
$inst = new Application();
|
47
|
1 |
|
$session = SessionManager::getInstance();
|
48
|
1 |
|
SessionManager::sessionStart('app');
|
49
|
1 |
|
$inst->container = new Container();
|
50
|
1 |
|
$inst->container[SessionManager::class] = $session;
|
51
|
1 |
|
$env = getenv('APPLICATION_ENV');
|
52
|
|
|
|
53
|
1 |
|
if ($env) {
|
54
|
1 |
|
$inst->setEnvironment($env);
|
55
|
|
|
}
|
56
|
|
|
}
|
57
|
11 |
|
return $inst;
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* Use this to bootstrap Bone without dispatching any request
|
62
|
|
|
* i.e. for when using the framework in a CLI application
|
63
|
|
|
*/
|
64
|
9 |
|
public function bootstrap(): Container
|
65
|
|
|
{
|
66
|
9 |
|
$env = new Environment($_SERVER);
|
67
|
9 |
|
$config = $env->fetchConfig($this->configFolder, $this->environment);
|
68
|
9 |
|
$config[Environment::class] = $env;
|
69
|
9 |
|
$config[SiteConfig::class] = new SiteConfig($config, $env);
|
70
|
9 |
|
$package = new ApplicationPackage($config);
|
71
|
9 |
|
$package->addToContainer($this->container);
|
72
|
9 |
|
|
73
|
|
|
return $this->container;
|
74
|
9 |
|
}
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/**
|
78
|
|
|
*
|
79
|
|
|
* T' the high seas! Garrr!
|
80
|
|
|
*
|
81
|
|
|
* @return bool
|
82
|
|
|
* @throws \Exception
|
83
|
|
|
*/
|
84
|
|
|
public function setSail()
|
85
|
8 |
|
{
|
86
|
|
|
// load in the config and set up the dependency injection container
|
87
|
|
|
$this->bootstrap();
|
88
|
8 |
|
$request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
|
89
|
8 |
|
/** @var RequestHandlerInterface $stack */
|
90
|
|
|
$stack = $this->container->get(Stack::class);
|
91
|
8 |
|
$response = $stack->handle($request);
|
92
|
8 |
|
|
93
|
|
|
(new SapiEmitter)->emit($response);
|
94
|
8 |
|
|
95
|
|
|
return true;
|
96
|
8 |
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* @return Container
|
100
|
|
|
*/
|
101
|
|
|
public function getContainer(): Container
|
102
|
2 |
|
{
|
103
|
|
|
return $this->container;
|
104
|
2 |
|
}
|
105
|
|
|
|
106
|
|
|
/**
|
107
|
|
|
* @param string $configFolder
|
108
|
|
|
*/
|
109
|
|
|
public function setConfigFolder(string $configFolder)
|
110
|
9 |
|
{
|
111
|
|
|
$this->configFolder = $configFolder;
|
112
|
9 |
|
}
|
113
|
9 |
|
|
114
|
|
|
/**
|
115
|
|
|
* @param string $environment
|
116
|
|
|
*/
|
117
|
|
|
public function setEnvironment(string $environment)
|
118
|
1 |
|
{
|
119
|
|
|
$this->environment = $environment;
|
120
|
|
|
}
|
121
|
|
|
} |