-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
247 lines (182 loc) · 9.16 KB
/
app.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
declare(strict_types = 1);
use App\Doctrine\Entity\Language;
use App\Middleware\LanguageDetectionMiddleware;
use App\Orm\ContentManagement\ContentDispatcher;
use App\Orm\ContentManagement\ContentPersistenceManager;
use App\Orm\Repository\ObjectRepository;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use App\Orm\ContentManagement\ContentProvider;
use App\Orm\EntityManager\EntityManager as JsonEntityManager;
use App\Orm\Factory\LayoutObjectFactory;
use Doctrine\ORM\EntityManager;
use Slim\Interfaces\RouteCollectorProxyInterface;
use Slim\Middleware\ContentLengthMiddleware;
require __DIR__.'/vendor/autoload.php';
$builder = new DI\ContainerBuilder();
$builder->enableCompilation(__DIR__.'/tmp');
$builder->writeProxiesToFile(true, __DIR__.'/tmp/proxies');
$builder->addDefinitions(__DIR__.'/config.php');
$builder->useAutowiring(false);
$builder->useAnnotations(false);
$container = $builder->build();
/**
* Instantiate App
*
* In order for the factory to work you need to ensure you have installed
* a supported PSR-7 implementation of your choice e.g.: Slim PSR-7 and a supported
* ServerRequest creator (included with Slim PSR-7)
*/
AppFactory::setContainer($container);
$app = AppFactory::create();
// Parse json, form data and xml
$app->addBodyParsingMiddleware();
$app->add(new ContentLengthMiddleware());
// Add Routing Middleware
$app->addRoutingMiddleware();
$app->add(LanguageDetectionMiddleware::class);
/**
* Add Error Handling Middleware
*
* @param bool $displayErrorDetails -> Should be set to false in production
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler
* @param bool $logErrorDetails -> Display error details in error log
* which can be replaced by a callable of your choice.
* Note: This middleware should be added last. It will not handle any exceptions/errors
* for middleware added after it.
*/
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
// Define app routes
$app->group('/languages', function (RouteCollectorProxyInterface $proxy) {
$proxy->get('', function (Request $request, Response $response) {
/** @var EntityManager $entityManager */
$entityManager = $this->get(EntityManager::class);
/** @var \App\Doctrine\Repository\LanguageRepository $languageRepository */
$languageRepository = $entityManager->getRepository(Language::class);
$languages = $languageRepository->findAll();
$response->getBody()->write(json_encode($languages));
return $response->withHeader('Content-Type', 'application/json');
});
$proxy->post('', function (Request $request, Response $response) {
/** @var EntityManager $entityManager */
$entityManager = $this->get(EntityManager::class);
$content = $request->getParsedBody();
$language = new Language();
$language->setName($content['name']);
$language->setCode($content['code']);
$language->setIsDefault($content['isDefault']);
$entityManager->persist($language);
$entityManager->flush();
$response->getBody()->write(json_encode($language));
return $response->withStatus(201)->withHeader('Content-Type', 'application/json');
});
$proxy->patch('/{id}', function (Request $request, Response $response, array $args) {
/** @var EntityManager $entityManager */
$entityManager = $this->get(EntityManager::class);
/** @var \Doctrine\ORM\EntityRepository $languageRepository */
$languageRepository = $entityManager->getRepository(Language::class);
/** @var Language $language */
$language = $languageRepository->find($args['id']);
if (null === $language) {
$response->getBody()->write(json_encode(['message' => 'Not found!']));
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
}
$content = $request->getParsedBody();
$language->setName($content['name']);
$language->setCode($content['code']);
$language->setIsDefault($content['isDefault']);
$entityManager->flush();
$response->getBody()->write(json_encode($language));
return $response->withHeader('Content-Type', 'application/json');
});
$proxy->delete('/{id}', function (Request $request, Response $response, array $args) {
/** @var EntityManager $entityManager */
$entityManager = $this->get(EntityManager::class);
/** @var \Doctrine\ORM\EntityRepository $languageRepository */
$languageRepository = $entityManager->getRepository(Language::class);
/** @var Language $language */
$language = $languageRepository->find($args['id']);
if (null === $language) {
$response->getBody()->write(json_encode(['message' => 'Not found!']));
return $response->withStatus(404)->withHeader('Content-Type', 'application/json');
}
$entityManager->remove($language);
$entityManager->flush();
$response->getBody()->write('');
return $response->withStatus(204)->withHeader('Content-Type', 'application/json');
});
});
$app->group('/layouts', function (RouteCollectorProxyInterface $proxy) {
$proxy->get('/{filename}', function (Request $request, Response $response, $args) {
/** @var ContentProvider $contentProvider */
$contentProvider = $this->get(ContentProvider::class);
/** @var ObjectRepository $objectRepository */
$objectRepository = $this->get(ObjectRepository::class);
$layout = $objectRepository->find($args['filename']);
$contents = $contentProvider->findByHashes($layout->getHashes());
$layout->setContents($contents);
$response->getBody()->write(json_encode($layout));
return $response->withHeader('Content-Type', 'application/json');
});
$proxy->post('', function (Request $request, Response $response) {
$content = $request->getParsedBody();
/** @var ContentDispatcher $dispatcher */
$dispatcher = $this->get(ContentDispatcher::class);
/** @var LayoutObjectFactory $factory */
$factory = $this->get(LayoutObjectFactory::class);
$layoutObject = $factory->createLayoutObject($content, md5((string) time()));
/** @var ContentProvider $contentProvider */
$contentProvider = $this->get(ContentProvider::class);
// Fetch existing content.
$existingContents = $contentProvider->findByHashes($layoutObject->getHashes());
/** @var JsonEntityManager $jsonEntityManager */
$jsonEntityManager = $this->get(JsonEntityManager::class);
$jsonEntityManager->persist($layoutObject);
// Fetch upcoming content
$upcomingContents = $layoutObject->getContents();
$dispatched = $dispatcher->dispatch($upcomingContents, $existingContents);
// Persist dispatched content
/** @var ContentPersistenceManager $contentPersistenceManager */
$contentPersistenceManager = $this->get(ContentPersistenceManager::class);
$contentPersistenceManager->persist($dispatched);
$layoutObject->setContents($upcomingContents);
$response->getBody()->write(json_encode([
$layoutObject->getName() => $layoutObject
]));
return $response->withHeader('Content-Type', 'application/json');
});
$proxy->patch('/{hash}', function (Request $request, Response $response, array $args) {
/** @var ObjectRepository $objectRepository */
$objectRepository = $this->get(ObjectRepository::class);
$layoutObject = $objectRepository->find($args['hash']);
/** @var ContentProvider $contentProvider */
$contentProvider = $this->get(ContentProvider::class);
// Fetch existing content.
$existingContents = $contentProvider->findByHashes($layoutObject->getHashes());
$json = $request->getParsedBody();
/** @var LayoutObjectFactory $factory */
$factory = $this->get(LayoutObjectFactory::class);
$layoutObject = $factory->createLayoutObject($json, $args['hash']);
/** @var JsonEntityManager $jsonEntityManager */
$jsonEntityManager = $this->get(JsonEntityManager::class);
$jsonEntityManager->persist($layoutObject);
// Fetch upcoming content
$upcomingContents = $layoutObject->getContents();
/** @var ContentDispatcher $dispatcher */
$dispatcher = $this->get(ContentDispatcher::class);
$dispatched = $dispatcher->dispatch($upcomingContents, $existingContents);
// Persist dispatched content
/** @var ContentPersistenceManager $contentPersistenceManager */
$contentPersistenceManager = $this->get(ContentPersistenceManager::class);
$contentPersistenceManager->persist($dispatched);
$layoutObject->setContents($upcomingContents);
$response->getBody()->write(json_encode([
$layoutObject->getName() => $layoutObject
]));
return $response->withHeader('Content-Type', 'application/json');
});
});
// Run app
$app->run();