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