-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.php
155 lines (114 loc) · 3.86 KB
/
bootstrap.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
148
149
150
151
152
153
154
155
<?php
require 'vendor/autoload.php';
define('DS', DIRECTORY_SEPARATOR);
define('PUBLICPATH', __DIR__.DS.'public'.DS);
define('ROOTPATH', realpath(__DIR__).DS);
$config_path = ROOTPATH . 'config';
$env = getenv('SLIM_ENV') ?: 'development';
$config = new Art4\YouthwebEvent\Config($config_path, $env);
$app = new \Slim\App($config->getAll());
$container = $app->getContainer();
// Register entity manager on container
$container['em'] = function ($container)
{
$db_settings = $container['settings']['database'];
$connection = $db_settings['active'];
if ( ! isset($db_settings['doctrine2']) )
{
throw new \Exception('Missing "doctrine2" key in DB settings');
}
if ( ! isset($db_settings[$connection]) or ! isset($db_settings[$connection]['connection']) )
{
throw new \Exception("No connection configuration for '$connection'");
}
$connection_settings = $db_settings[$connection];
$settings = $db_settings['doctrine2'];
// Required settings
foreach ( ['proxy_dir', 'entity_pathes', 'migrations'] as $key )
{
if ( ! array_key_exists($key, $settings) )
{
throw new \Exception("'$key' not configured for connection '$connection'");
}
}
// Translate DB config for doctrine
$options = [];
if ( isset($connection_settings['type']) )
{
$options['user'] = $connection_settings['connection']['username'];
switch ($connection_settings['type'])
{
case 'pdo':
$parts = explode(':', $connection_settings['connection']['dsn']);
if ( ! in_array($parts[0], array('mysql', 'sqlite', 'pgsql', 'oci', 'sqlsrv')) )
{
throw new \Exception('Unsupported driver ' . $parts[0]);
}
$options['driver'] = 'pdo_' . $parts[0];
// SQLite DSN has only a path
if ( $parts[0] === 'sqlite' )
{
$path_parts = $parts;
array_shift($path_parts);
$options['path'] = implode(':', $path_parts);
break;
}
$conf = explode(';', $parts[1]);
foreach ( $conf as $opt )
{
$v = explode('=', $opt);
$options[$v[0]] = $v[1];
}
break;
default:
throw new \Exception('Unsupported connection type ' . $connection_settings['type']);
}
}
if ( isset($connection_settings['charset']) )
{
$options['charset'] = $connection_settings['charset'];
}
$options = array_filter($options);
$settings['connection'] = array_merge($options, $connection_settings['connection']);
$is_dev_mode = ($container['settings']['environment'] !== 'production');
$cache = null;
if ( ! $is_dev_mode )
{
$pool = new $container['cachepool'];
$cache = new DoctrineCacheBridge($pool);
}
$config = \Doctrine\ORM\Tools\Setup::createConfiguration($is_dev_mode, $settings['proxy_dir'], $cache);
$config->setMetadataDriverImpl(new \Doctrine\Common\Persistence\Mapping\Driver\StaticPHPDriver($settings['entity_pathes']));
return \Doctrine\ORM\EntityManager::create($settings['connection'], $config);
};
// Register component on container
$container['view'] = function ($container)
{
$view = new \Slim\Views\Twig(
$container['settings']['views']['twig']['template_path'],
$container['settings']['views']['twig']['environment']
);
// Instantiate and add Slim specific extension
$basePath = rtrim(str_ireplace('index.php', '', $container['request']->getUri()->getBasePath()), '/');
$view->addExtension(new Slim\Views\TwigExtension($container['router'], $basePath));
return $view;
};
// Register cachepool on container
$container['cachepool'] = function ($container)
{
$filesystemAdapter = new \League\Flysystem\Adapter\Local(
$container['settings']['cachepool']['cache_path']
);
$filesystem = new \League\Flysystem\Filesystem($filesystemAdapter);
$pool = new \Cache\Adapter\Filesystem\FilesystemCachePool($filesystem, '/');
return $pool;
};
// Add routes to app
foreach ($container['settings']['routes'] as $pattern => $target)
{
foreach ($target as $method => $callable)
{
$app->map([$method], $pattern, $callable);
}
}
return $app;