-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.php
147 lines (123 loc) · 3.78 KB
/
Core.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php declare(strict_types=1);
namespace Rezonans\Core;
use Closure;
use Illuminate\Container\Container;
use Monolog\Logger;
use Rezonans\Core\Contracts\ResponseInterface;
use Rezonans\Core\Contracts\TemplateInterface;
use Rezonans\Core\Facades\RouterStore;
use Rezonans\Core\Facades\ShutdownPromisePool;
use Rezonans\Core\Flow\ContainerManager;
use Rezonans\Core\Flow\PromiseManager;
use Rezonans\Core\Flow\ServiceRegistrator;
use Rezonans\Core\Flow\Path;
use Rezonans\Core\Flow\PromisePool;
use Rezonans\Core\Http\WpResponse;
use Rezonans\Core\Render\MustacheTemplate;
use Rezonans\Core\Render\View;
use Rezonans\Core\Facades\Core as CoreFacade;
use Rezonans\Core\Wordpress\WpProvider;
/**
* Class Core
* @package Core
*/
final class Core
{
/** @var ContainerManager */
protected $containerManager;
/** @var Path */
protected $path;
/** @var string */
protected $coreRoot = __DIR__;
/** @var Logger */
protected $logger = null;
/**
* App constructor. Bootstrap
* @throws \Exception
*/
public function __construct()
{
$this->containerManager = new ContainerManager();
$this->containerManager->initInstructions(function (Container $container) {
$this->initServiceContainer($container);
});
$this->containerManager->createContainer();
CoreFacade::setFacadeRoot($this);
}
/**
* Run the App
* @param callable $applicationThick
*/
public function run(callable $applicationThick)
{
try {
/** @var PromiseManager $promiseManager */
$promiseManager = $this->getContainerManager()
->getContainer()
->get(PromiseManager::class);
$promiseManager->addPromise('shutdown', function () {
ShutdownPromisePool::callAllPromises();
});
$applicationThick();
RouterStore::makeBinding();
} catch (\Throwable $e) {
if (!is_null($this->logger)) {
$this->logger->error($e->getMessage(), [
'stacktrace' => $e->getTrace(),
]);
}
} finally {
ShutdownPromisePool::callAllPromises();
}
}
/**
* Accessor to ContainerManager
* @return ContainerManager
*/
public function getContainerManager(): ContainerManager
{
return $this->containerManager;
}
/**
* @param Logger $logger
*/
public function setLogger(Logger $logger)
{
$this->logger = $logger;
}
/**
* @return Logger
*/
public function getLogger()
{
return $this->logger;
}
/**
* Service container inti instructions
* @param Container $container
* @throws \Exception
* @throws \Error
*/
protected function initServiceContainer(Container $container)
{
$sr = new ServiceRegistrator($this->coreRoot, "Rezonans\\Core\\", $container);
$sr->exclude(Path::class);
$sr->prepareArguments(View::class,
[
TemplateInterface::class => MustacheTemplate::class,
ResponseInterface::class => WpResponse::class
]
);
$sr->walkDirForServices('Flow');
$sr->walkDirForServices('DataSource');
$sr->walkDirForServices('Helpers');
$sr->walkDirForServices('Http');
$sr->walkDirForServices('Render');
$sr->walkDirForServices('Wordpress');
$container->instance('core', $this);
$container->singleton('router-store', \Rezonans\Core\Http\RouterStore::class);
$container->instance('promise-pool.shutdown', new PromisePool());
$container->when(Path::class)->needs('$corePath')->give($this->coreRoot);
$container->singleton(Path::class);
}
}