-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
executable file
·177 lines (144 loc) · 5.09 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
* (c) Smalte - 2013 ~ Until the end of the world...
*
* Thanks a lot to our community!
*
* @link http://smalte.org Official website
* @link http://github.com/Smalte/Smalte For the source repository
* @license Read LICENSE.md file for more information.
*/
// ===== SECTION: Debug =====
function p($a){echo '<xmp>';print_r($a);echo '</xmp>';}
function d($a){p($a);exit;}
// ===== SECTION: Auto-load =====
require_once __DIR__.'/libraries/Symfony/Component/ClassLoader/UniversalClassLoader.php';
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(include __DIR__.'/data/cache/loader/namespaces.php');
$loader->registerPrefixes(include __DIR__.'/data/cache/loader/prefixes.php');
$loader->register();
// @todo quick fix for swift to move
require dirname(__FILE__).'/libraries/Swift/swift_required.php';
// ===== SECTION: Configuration =====
use Smalte\Utils\ArrayUtils;
use Smalte\Environment\Factory;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpFoundation\Request;
// Create request and get current client IP
$request = Request::createFromGlobals();
$clientIp = $request->getClientIp() ?: '0.0.0.0';
// Get main configuration file
$configuration = Yaml::parse(file_get_contents(__DIR__.'/data/config/config.yml'));
// Check all environments and return current environment
$environmentConfiguration = Yaml::parse(file_get_contents(__DIR__.'/data/config/environments.yml'));
$currentEnvironment = Factory::getCurrentEnvironment($environmentConfiguration, $clientIp, $_ENV, $_COOKIE);
if ($currentEnvironment)
{
// Test if environment file exist
$environmentConfigurationFile = __DIR__.'/data/config/config.'.$currentEnvironment->getName().'.yml';
if (file_exists($environmentConfigurationFile))
{
// Get environment configuration file and merge with main configuration
$configurationEnvironment = Yaml::parse(file_get_contents($environmentConfigurationFile));
if ($configurationEnvironment)
{
$configuration = ArrayUtils::merge($configuration, $configurationEnvironment);
}
}
}
// ===== SECTION: Dependency Injection Container =====
use Smalte\DependencyInjection\ContainerFactory;
$servicesConfigDirectory = __DIR__.'/data/config/';
$useCache = ($currentEnvironment->getName() === 'prod');
$container = ContainerFactory::create($servicesConfigDirectory, $configuration, $useCache);
// ===== SECTION: ORM =====
$em = $container->get('entityManager');
// ===== SECTION: Router =====
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Smalte\Routing\Loader\Main;
use Smalte\Controller\ControllerResolver;
if (!defined('INSTALL'))
{
// Get repositories
try
{
$applications = $em->getRepository('Entities\Application');
$languages = $em->getRepository('Entities\Language');
$routes = $em->getRepository('Entities\Route');
}
catch (Exception $exception)
{
exit('Please goto <a href="install.php">Install / Upgrade</a>.');
}
// Set router options
$options = array(
//'cache_dir' => realpath(__DIR__.'/../../../data/cache/routing/'),
'matcher_cache_class' => 'Matcher',
'generator_cache_class' => 'Generator',
);
// Create request and context
$request = $container->get('request');
$context = new RequestContext();
$context->fromRequest($request);
// Get main language
$mainLanguage = $languages->findOneBy(array('isMain' => 1));
// Redirect to translation
$currentPath = ltrim($request->getPathInfo(), '/');
$currentPrefix = substr($currentPath, 0, strpos($currentPath, '/'));
$currentPrefix = $currentPrefix ? $currentPrefix : '';
$currentApplication = $applications->getByPrefix($currentPrefix);
$currentApplication = $currentApplication === NULL ? $applications->getDefault() : $currentApplication;
// Create router and main loader
$loader = new Main($applications, $languages, $mainLanguage->getId());
$router = new Router($loader, $routes, $options, $context);
// Get matcher and generator, ho yeah!!
$matcher = $router->getMatcher();
$generator = $router->getGenerator();
try
{
$parameters = $matcher->match($request->getPathInfo());
}
catch (MethodNotAllowedException $exception)
{
$parameters = array(
'_locale' => 'en',
'_route' => 'error403',
'_application' => $currentApplication,
'_controller' => 'Error',
'_action' => 'display403',
);
}
catch (Exception $exception)
{
$parameters = array(
'_locale' => 'en',
'_route' => 'error404',
'_application' => $currentApplication,
'_controller' => 'Error',
'_action' => 'display404',
);
}
// ===== SECTION: Controller Resolver =====
try
{
$resolver = new ControllerResolver($parameters, $container);
$response = $resolver->getResponse();
$response->send();
}
catch (Exception $exception)
{
$parameters = array(
'_locale' => 'en',
'_route' => 'error500',
'_application' => $currentApplication,
'_controller' => 'Error',
'_action' => 'display500',
);
$resolver = new ControllerResolver($parameters, $container);
$response = $resolver->getResponse();
$response->send();
}
}