|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Koriit\PHPDeps\Application; |
|
4
|
|
|
|
|
5
|
|
|
use DI\Container; |
|
6
|
|
|
use DI\ContainerBuilder; |
|
7
|
|
|
use DI\DependencyException; |
|
8
|
|
|
use DI\NotFoundException; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Psr\Container\ContainerInterface; |
|
11
|
|
|
use RuntimeException; |
|
12
|
|
|
|
|
13
|
|
|
class EntryPoint |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $appClass |
|
17
|
|
|
* @param callable $dependenciesDefFactory |
|
18
|
|
|
*/ |
|
19
|
|
|
public function enter($appClass, callable $dependenciesDefFactory) |
|
20
|
|
|
{ |
|
21
|
|
|
$container = $this->createContainer($dependenciesDefFactory()); |
|
22
|
|
|
|
|
23
|
|
|
try { |
|
24
|
|
|
$application = $this->createApplication($container, $appClass); |
|
25
|
|
|
} catch (Exception $e) { |
|
26
|
|
|
throw new RuntimeException('Could not create application: ' . $appClass, 0, $e); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$application->run(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $definitionSources |
|
34
|
|
|
* |
|
35
|
|
|
* @return Container |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function createContainer(array $definitionSources) |
|
38
|
|
|
{ |
|
39
|
|
|
$containerBuilder = new ContainerBuilder(); |
|
40
|
|
|
$containerBuilder->useAnnotations(false); |
|
41
|
|
|
$containerBuilder->useAutowiring(true); |
|
42
|
|
|
$containerBuilder->addDefinitions($definitionSources); |
|
43
|
|
|
|
|
44
|
|
|
$container = $containerBuilder->build(); |
|
45
|
|
|
$container->set(ContainerInterface::class, $container); |
|
46
|
|
|
|
|
47
|
|
|
return $container; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Container $container |
|
52
|
|
|
* @param string $appClass |
|
53
|
|
|
* |
|
54
|
|
|
* @throws DependencyException |
|
55
|
|
|
* @throws NotFoundException |
|
56
|
|
|
* |
|
57
|
|
|
* @return ApplicationInterface |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function createApplication(Container $container, $appClass) |
|
60
|
|
|
{ |
|
61
|
|
|
$application = $container->get($appClass); |
|
62
|
|
|
$container->set(ApplicationInterface::class, $application); |
|
63
|
|
|
|
|
64
|
|
|
return $application; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|