1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Bone\Mvc;
|
4
|
|
|
|
5
|
|
|
use Bone\Server\Environment;
|
6
|
|
|
use Pimple\Container;
|
7
|
|
|
use Zend\Diactoros\ServerRequestFactory;
|
8
|
|
|
use Zend\Diactoros\Response;
|
9
|
|
|
|
10
|
|
|
class Application
|
11
|
|
|
{
|
12
|
|
|
/** @var Registry $registry */
|
13
|
|
|
private $registry;
|
14
|
|
|
|
15
|
|
|
/** @var Container $registry */
|
16
|
|
|
private $treasureChest;
|
17
|
|
|
|
18
|
|
|
/** @var string $configFolder */
|
19
|
|
|
private $configFolder = 'config';
|
20
|
|
|
|
21
|
|
|
/** @var string $environment */
|
22
|
|
|
private $environment = 'production';
|
23
|
|
|
|
24
|
|
|
/**
|
25
|
|
|
* There be nay feckin wi' constructors on board this ship
|
26
|
|
|
* There be nay copyin' o' th'ship either
|
27
|
|
|
* This ship is a singleton!
|
28
|
|
|
*/
|
29
|
|
|
public function __construct(){}
|
30
|
|
|
public function __clone(){}
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/**
|
34
|
|
|
* Ahoy! There nay be boardin without yer configuration
|
35
|
|
|
*
|
36
|
|
|
* @param array $config
|
37
|
|
|
* @return Application
|
38
|
|
|
*/
|
39
|
2 |
|
public static function ahoy(array $config = [])
|
40
|
2 |
|
{
|
41
|
2 |
|
static $inst = null;
|
42
|
2 |
|
if ($inst === null)
|
43
|
|
|
{
|
44
|
1 |
|
$inst = new Application();
|
45
|
1 |
|
$inst->registry = Registry::ahoy();
|
46
|
1 |
|
$inst->treasureChest = new Container();
|
47
|
1 |
|
$inst->setConfig($config);
|
48
|
|
|
}
|
49
|
2 |
|
return $inst;
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* @param string $environment
|
54
|
|
|
*/
|
55
|
|
|
public function setApplicationEnv(string $environment)
|
56
|
|
|
{
|
57
|
|
|
$this->environment = $environment;
|
58
|
|
|
}
|
59
|
|
|
|
60
|
|
|
/**
|
61
|
|
|
* @param array $config
|
62
|
|
|
*/
|
63
|
1 |
|
private function setConfig(array $config)
|
64
|
1 |
|
{
|
65
|
1 |
|
foreach($config as $key => $value)
|
66
|
|
|
{
|
67
|
1 |
|
$this->registry->set($key,$value);
|
68
|
1 |
|
$this->treasureChest[$key] = $value;
|
69
|
|
|
}
|
70
|
1 |
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* T' the high seas! Garrr!
|
74
|
|
|
*
|
75
|
|
|
* @throws \Exception
|
76
|
|
|
*/
|
77
|
1 |
|
public function setSail()
|
78
|
1 |
|
{
|
79
|
1 |
|
$env = new Environment($_SERVER);
|
80
|
1 |
|
if (!count($this->registry->getAll())) {
|
81
|
|
|
$config = $env->fetchConfig($this->configFolder, $this->environment);
|
82
|
|
|
$this->setConfig($config);
|
83
|
|
|
}
|
84
|
1 |
|
$request = ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
|
85
|
1 |
|
$response = new Response();
|
86
|
1 |
|
$dispatcher = new Dispatcher($request, $response, $env);
|
87
|
1 |
|
$dispatcher->fireCannons();
|
88
|
1 |
|
}
|
89
|
|
|
|
90
|
|
|
/**
|
91
|
|
|
* @return Container
|
92
|
|
|
*/
|
93
|
|
|
public function getContainer(): Container
|
94
|
|
|
{
|
95
|
|
|
return $this->treasureChest;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* @param string $configFolder
|
100
|
|
|
*/
|
101
|
|
|
public function setConfigFolder(string $configFolder)
|
102
|
|
|
{
|
103
|
|
|
$this->configFolder = $configFolder;
|
104
|
|
|
}
|
105
|
|
|
|
106
|
|
|
/**
|
107
|
|
|
* @param string $environment
|
108
|
|
|
*/
|
109
|
|
|
public function setEnvironment(string $environment)
|
110
|
|
|
{
|
111
|
|
|
$this->environment = $environment;
|
112
|
|
|
}
|
113
|
|
|
} |