1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the BEAR.Package package. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
6
|
|
|
*/ |
7
|
|
|
namespace BEAR\Package; |
8
|
|
|
|
9
|
|
|
use BEAR\AppMeta\AbstractAppMeta; |
10
|
|
|
use BEAR\AppMeta\AppMeta; |
11
|
|
|
use BEAR\Sunday\Extension\Application\AbstractApp; |
12
|
|
|
use BEAR\Sunday\Extension\Application\AppInterface; |
13
|
|
|
use Doctrine\Common\Cache\ApcuCache; |
14
|
|
|
use Doctrine\Common\Cache\Cache; |
15
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
16
|
|
|
use Doctrine\Common\Cache\VoidCache; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Bootstrap |
20
|
|
|
* |
21
|
|
|
* Create an app object that contains all the objects used in the bootstrap script The bootstrap script uses the public |
22
|
|
|
* property of $ app to run the application. |
23
|
|
|
* |
24
|
|
|
* AppModule knows the binding of all interfaces. Other context modules override bindings on the interface. For example, |
25
|
|
|
* `app` binds JsonRenderer and outputs JSON. In` html-prod`, HtmlModule overwrites the binding on TwigRenderer and |
26
|
|
|
* outputs html. |
27
|
|
|
*/ |
28
|
|
|
final class Bootstrap |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Return application instance by name and contexts |
32
|
|
|
* |
33
|
|
|
* Use newApp() instead for your own AppMeta and Cache. |
34
|
|
|
* |
35
|
|
|
* @param string $name application name 'koriym\blog' (vendor\package) |
36
|
|
|
* @param string $contexts application context 'prd-html-app' |
37
|
|
|
* |
38
|
|
|
* @return AbstractApp |
39
|
|
|
*/ |
40
|
3 |
|
public function getApp($name, $contexts) |
41
|
|
|
{ |
42
|
3 |
|
return $this->newApp(new AppMeta($name, $contexts), $contexts); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return application instance by AppMeta and Cache |
47
|
|
|
* |
48
|
|
|
* @param AbstractAppMeta $appMeta |
49
|
|
|
* @param string $contexts |
50
|
|
|
* @param Cache $cache |
51
|
|
|
* |
52
|
|
|
* @return AbstractApp |
53
|
|
|
*/ |
54
|
5 |
|
public function newApp(AbstractAppMeta $appMeta, $contexts, Cache $cache = null) |
55
|
|
|
{ |
56
|
5 |
|
$cache = $cache ?: $this->getCache($appMeta, $contexts); |
57
|
5 |
|
$appId = $appMeta->name . $contexts; |
58
|
5 |
|
$app = $cache->fetch($appId); |
59
|
5 |
|
if ($app) { |
60
|
1 |
|
return $app; |
61
|
|
|
} |
62
|
5 |
|
$app = (new AppInjector($appMeta->name, $contexts))->getInstance(AppInterface::class); |
63
|
4 |
|
$cache->save($appId, $app); |
64
|
|
|
|
65
|
4 |
|
return $app; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return contextual cache |
70
|
|
|
* |
71
|
|
|
* @param AbstractAppMeta $appMeta |
72
|
|
|
* @param string $contexts |
73
|
|
|
* |
74
|
|
|
* @return ApcuCache|FilesystemCache|VoidCache |
75
|
|
|
*/ |
76
|
3 |
|
private function getCache(AbstractAppMeta $appMeta, $contexts) |
77
|
|
|
{ |
78
|
3 |
|
$isDeveop = ! is_int(strpos($contexts, 'prod')); |
79
|
3 |
|
if ($isDeveop) { |
80
|
2 |
|
return new VoidCache; |
81
|
|
|
} |
82
|
1 |
|
if (function_exists('apcu_fetch')) { |
83
|
|
|
return new ApcuCache; // @codeCoverageIgnore |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return new FilesystemCache($appMeta->tmpDir); // @codeCoverageIgnore |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|