-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
202 lines (173 loc) · 6.49 KB
/
index.php
File metadata and controls
202 lines (173 loc) · 6.49 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Created by PhpStorm.
* User: Valentí
* Date: 05/11/2018
* Time: 11:31
*/
require_once __DIR__.'/../vendor/autoload.php';
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use CTIC\App\Base\Infrastructure\Kernel\Kernel;
use CTIC\App\Base\Application\CreateResourceController;
use CTIC\App\Base\Infrastructure\Controller\Command\ResourceControllerCommand;
use CTIC\App\Base\Infrastructure\Controller\ResourceController;
use CTIC\App\Permission\Domain\Permission;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Translation\Translator;
use Symfony\Component\DependencyInjection\Container;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\AnnotationException;
use Metadata\Driver\FileLocator as MetadataFileLocator;
use Symfony\Component\Yaml\Yaml;
$configYaml = Yaml::parse(file_get_contents(__DIR__.'/../config.yml'));
// General params
$isDevMode = $configYaml['general']['dev_mode'];
$defaultLocale = $configYaml['general']['locale']['default'];
$currentModule = 'config';
$kernel = new Kernel();
// General settings
ini_set('session.cookie_domain', '.' . $configYaml['general']['host']);
// Metadata FileLocator initialize
$metadataFileLocator = new MetadataFileLocator(array('/'));
// Routes params
$fileLocator = new FileLocator($kernel);
if (array_key_exists('REQUEST_URI', $_SERVER)) {
$path = explode('?', $_SERVER['REQUEST_URI']);
$queryString = (isset($path[1])) ? $path[1] : '';
$path = $path[0];
} else {
$path = '/';
$queryString = '';
}
$requestContext = new RequestContext('http://' . $_SERVER['SERVER_NAME'], $_SERVER['REQUEST_METHOD'], $_SERVER['SERVER_NAME'], 'http', 80, 443, $path, $queryString);
$router = new Router(
new YamlFileLoader($fileLocator),
__DIR__ . '/routes.yaml',
array(), //'cache_dir' => __DIR__.'/cache'
$requestContext
);
$routeParams = $router->match($path);
// Initialize container
$container = new Container();
// Initialize session
$session = new Session();
// Initialize Translator
$translator = new Translator($defaultLocale);
// DB params
$dbUser = ($session->get('dbUser'))? $session->get('dbUser') : false;
$dbPassword = ($session->get('dbUser'))? $session->get('dbPassword') : false;
$dbHost = ($session->get('dbUser'))? $session->get('dbHost') : false;
$dbName = ($session->get('dbUser'))? $session->get('dbName') : false;
if ( empty($dbUser) ||
empty($dbPassword) ||
empty($dbHost) ||
empty($dbName)
) {
header('Location: http://' . $configYaml['modlue_account']['host']);
exit();
}
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => $dbUser,
'password' => $dbPassword,
'host' => $dbHost,
'dbname' => $dbName
);
$dbPaths = array();
foreach ($configYaml['modules'] as $module) {
$dbPaths[] = '../' . $module['path'] . "/src";
}
$config = Setup::createConfiguration($isDevMode);
try{
$driver = new AnnotationDriver(new AnnotationReader(), $dbPaths);
}catch (AnnotationException $ae)
{
echo $ae->getMessage();
}
// registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
$config->setAutoGenerateProxyClasses(false);
$config->setProxyDir('../proxy');
// App configuration
$app = new Silex\Application();
$twigParams = array(
'path' => array(
__DIR__.'/views'
)
//'cache' => __DIR__.'/cache'
);
$method = strtolower($_SERVER['REQUEST_METHOD']);
$app->$method($path, function (Request $request) use($app, $dbParams, $config, $routeParams, $router, $defaultLocale, $metadataFileLocator, $container, $session, $translator, $twigParams) {
try{
$entityManager = EntityManager::create($dbParams, $config);
}catch (ORMException $oe)
{
throw new Exception('Ha ocurrido un error con la base de datos.');
}
try{
if (empty($routeParams['_controller']) || empty($routeParams['_controller_method'])) {
throw new Exception('No controller found in current route.');
} else {
$controllerClass = $routeParams['_controller'];
$controllerMethod = $routeParams['_controller_method'];
}
if (empty($routeParams['_controller_command'])) {
$controllerCommand = 'CTIC\App\Base\Infrastructure\Controller\Command\ResourceControllerCommand';
} else {
$controllerCommand = $routeParams['_controller_command'];
}
if(!empty($routeParams['id']))
{
$request->attributes->add(array('id' => $routeParams['id']));
}
if (empty($routeParams['_controller_create'])) {
$controller = new $controllerClass();
} else {
$controllerCreate = $routeParams['_controller_create'];
$command = new $controllerCommand();
$command->manager = $entityManager;
$permissionRepository = $entityManager->getRepository(Permission::class);
$controller = $controllerCreate::create(
$command,
$permissionRepository,
$router,
$session,
$translator,
$defaultLocale,
$request,
$metadataFileLocator,
$container,
$twigParams
);
}
return $controller->$controllerMethod($request);
} catch (Exception $e) {
throw new Exception('Ha ocurrido un error al cargar la url.');
}
});
$app->error(function (\Exception $e, Request $request, $code) {
switch ($code) {
case 500:
$message = 'Ha ocurrido un error en el servidor.';
break;
case 404:
$message = 'La página no se ha encontrado.';
break;
default:
$message = 'Ha ocurrido un error inesperado.';
}
return new Response($message);
});
$app->run();