-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppFactory.php
73 lines (66 loc) · 1.99 KB
/
AppFactory.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
<?php
namespace Duktig\Core;
use Duktig\Core\DI\ContainerFactory;
use Duktig\Core\DI\ContainerInterface;
class AppFactory
{
/**
* Resolves and returns an instance of $class.
*
* @param string $configAppFile
* @param string $class
* @return mixed Resolved instance
*/
public function make(string $configAppFile, string $class)
{
$configArr = $this->getConfigArr($configAppFile);
$container = $this->getContainer($configArr);
$instance = $container->get($class);
return $instance;
}
/**
* Gets a configuration array.
*
* @param string $configAppFile
* @return array
*/
protected function getConfigArr(string $configAppFile) : array
{
$configCoreFile = __DIR__.'/../Config/config.php';
$configCore = $this->requireFile($configCoreFile);
$configApp = $this->requireFile($configAppFile);
$services = [];
$skipCoreServices = $configApp['skipCoreServices'] ?? false;
if (isset($configCore['services']) && !$skipCoreServices) {
$services[] = $configCore['services'];
}
if (isset($configApp['services'])) {
$services[] = $configApp['services'];
}
$configArr = array_replace_recursive($configCore, $configApp);
$configArr['services'] = $services;
return $configArr;
}
/**
* @param string $file
* @throws \InvalidArgumentException
* @return mixed
*/
protected function requireFile(string $file)
{
if (!file_exists($file)) {
throw new \InvalidArgumentException('Can not open file for reading '.$file);
}
return require $file;
}
/**
* Returns the configured container.
*
* @param array $configArr
* @return ContainerInterface
*/
protected function getContainer(array $configArr) : ContainerInterface
{
return (new ContainerFactory())->make($configArr);
}
}