-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.php
More file actions
131 lines (113 loc) · 3.02 KB
/
App.php
File metadata and controls
131 lines (113 loc) · 3.02 KB
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
<?php
namespace Lib\Framework;
use App\ServiceProviders\ProviderInterface;
use DI\ContainerBuilder;
use Lib\Utils\DotNotation;
use Slim\Factory\AppFactory;
/**
* Class App
* @package Lib\Framework
* @author Jerfeson Guerreiro <jerfeson@codeis.com.br>
* @since 1.0.0
* @version 1.0.0
*/
class App
{
const DEVELOPMENT = 'development';
const PRODUCTION = 'production';
public $appType;
private $settings;
/**
* @var \Slim\App
*/
private $app;
/**
* App constructor.
* @param string $appType
* @param array $settings
* @throws \Exception
*/
public function __construct($appType = '', $settings = [])
{
$this->appType = $appType;
$this->settings = $settings;
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
// Build PHP-DI Container instance
$container = $containerBuilder->build();
AppFactory::setContainer($container);
$this->app = AppFactory::create();
$callableResolver = $this->app->getCallableResolver();
// Should be set to true in production
if ($this->isProduction($this->settings)) {
$containerBuilder->enableCompilation(__DIR__ . '/../var/cache');
}
}
private static $instance = null;
/**
* Application Singleton Factory
*
* @param string $appName
* @param array $settings
* @return App
*/
final public static function instance($appName = '', $settings = [])
{
if (null === static::$instance) {
static::$instance = new static($appName, $settings);
}
return static::$instance;
}
/**
* @param $settings
* @return bool
*/
public function isProduction($settings)
{
if ($settings['settings']['env'] == self::PRODUCTION) {
return true;
}
return false;
}
/**
* register providers
*
* @return void
*/
public function registerProviders()
{
$providers = (array)$this->getConfig('providers');
array_walk($providers, function(&$appName, $provider) {
if (strpos($appName, $this->appName) !== false) {
/** @var $provider ProviderInterface */
$provider::register();
}
});
}
/**
* register providers
*
* @return void
*/
public function registerMiddleware()
{
$middlewares = array_reverse((array)$this->getConfig('middleware'));
array_walk($middlewares, function($appType, $middleware) {
if (strpos($appType, $this->appType) !== false) {
$this->app->add(new $middleware);
}
});
}
/**
* get configuration param
*
* @param string $param
* @param string $defaultValue
* @return mixed
*/
public function getConfig($param, $defaultValue = null)
{
$dn = new DotNotation($this->settings);
return $dn->get($param, $defaultValue);
}
}