1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CRUDlex package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CRUDlex\Silex; |
13
|
|
|
|
14
|
|
|
use CRUDlex\Controller; |
15
|
|
|
use CRUDlex\ControllerInterface; |
16
|
|
|
use Silex\Api\ControllerProviderInterface; |
17
|
|
|
use Silex\Application; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This is the ControllerProvider offering all CRUD pages. |
22
|
|
|
* |
23
|
|
|
* It offers this routes: |
24
|
|
|
* |
25
|
|
|
* "/resource/static" serving static resources |
26
|
|
|
* |
27
|
|
|
* "/{entity}/create" creation page of the entity |
28
|
|
|
* |
29
|
|
|
* "/{entity}" list page of the entity |
30
|
|
|
* |
31
|
|
|
* "/{entity}/{id}" details page of a single entity instance |
32
|
|
|
* |
33
|
|
|
* "/{entity}/{id}/edit" edit page of a single entity instance |
34
|
|
|
* |
35
|
|
|
* "/{entity}/{id}/delete" POST only deletion route for an entity instance |
36
|
|
|
* |
37
|
|
|
* "/{entity}/{id}/{field}/file" renders a file field of an entity instance |
38
|
|
|
* |
39
|
|
|
* "/{entity}/{id}/{field}/delete" POST only deletion of a file field of an entity instance |
40
|
|
|
*/ |
41
|
|
|
class ControllerProvider implements ControllerProviderInterface |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Setups the templates. |
46
|
|
|
* |
47
|
|
|
* @param Application $app |
48
|
|
|
* the Application instance of the Silex application |
49
|
|
|
*/ |
50
|
1 |
|
protected function setupTemplates(Application $app) |
51
|
|
|
{ |
52
|
1 |
|
if ($app->offsetExists('twig.loader.filesystem')) { |
53
|
1 |
|
$app['twig.loader.filesystem']->addPath(__DIR__.'/../../views/', 'crud'); |
54
|
|
|
} |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Setups the routes. |
59
|
|
|
* |
60
|
|
|
* @param Application $app |
61
|
|
|
* the Application instance of the Silex application |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
* the created controller factory |
65
|
|
|
*/ |
66
|
1 |
|
protected function setupRoutes(Application $app) |
67
|
|
|
{ |
68
|
1 |
|
$controller = $app->offsetExists('crud.controller') ? $app['crud.controller'] : new Controller($app['crud'], $app['crud.filesystem'], $app['twig'], $app['session'], $app['translator']); |
69
|
1 |
|
if (!$controller instanceof ControllerInterface) { |
70
|
1 |
|
throw new \InvalidArgumentException('crud.controller doesn\'t implement CRUDlex\ControllerInterface.'); |
71
|
|
|
} |
72
|
1 |
|
$localeAndCheckEntity = [$controller, 'setLocaleAndCheckEntity']; |
73
|
1 |
|
$factory = $app['controllers_factory']; |
74
|
1 |
|
$factory->get('/resource/static', [$controller, 'staticFile'])->bind('crudStatic'); |
75
|
1 |
|
$factory->match('/{entity}/create', [$controller, 'create'])->bind('crudCreate')->before($localeAndCheckEntity, 10); |
76
|
1 |
|
$factory->get('/{entity}', [$controller, 'showList'])->bind('crudList')->before($localeAndCheckEntity, 10); |
77
|
1 |
|
$factory->get('/{entity}/{id}', [$controller, 'show'])->bind('crudShow')->before($localeAndCheckEntity, 10); |
78
|
1 |
|
$factory->match('/{entity}/{id}/edit', [$controller, 'edit'])->bind('crudEdit')->before($localeAndCheckEntity, 10); |
79
|
1 |
|
$factory->post('/{entity}/{id}/delete', [$controller, 'delete'])->bind('crudDelete')->before($localeAndCheckEntity, 10); |
80
|
1 |
|
$factory->get('/{entity}/{id}/{field}/file', [$controller, 'renderFile'])->bind('crudRenderFile')->before($localeAndCheckEntity, 10); |
81
|
1 |
|
$factory->post('/{entity}/{id}/{field}/delete', [$controller, 'deleteFile'])->bind('crudDeleteFile')->before($localeAndCheckEntity, 10); |
82
|
1 |
|
$factory->get('/setting/locale/{locale}', [$controller, 'setLocale'])->bind('crudSetLocale'); |
83
|
|
|
|
84
|
1 |
|
return $factory; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Setups i18n. |
89
|
|
|
* |
90
|
|
|
* @param Application $app |
91
|
|
|
* the Application instance of the Silex application |
92
|
|
|
*/ |
93
|
1 |
|
protected function setupI18n(Application $app) |
94
|
|
|
{ |
95
|
|
|
$app->before(function(Request $request, Application $app) { |
96
|
|
|
$manageI18n = $app['crud']->isManageI18n(); |
97
|
|
|
if ($manageI18n) { |
98
|
|
|
$locale = $app['session']->get('locale', 'en'); |
99
|
|
|
$app['translator']->setLocale($locale); |
100
|
|
|
} |
101
|
1 |
|
}, 1); |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Implements ControllerProviderInterface::connect() connecting this |
106
|
|
|
* controller. |
107
|
|
|
* |
108
|
|
|
* @param Application $app |
109
|
|
|
* the Application instance of the Silex application |
110
|
|
|
* |
111
|
|
|
* @return \Silex\ControllerCollection |
112
|
|
|
* this method is expected to return the used ControllerCollection instance |
113
|
|
|
*/ |
114
|
1 |
|
public function connect(Application $app) |
115
|
|
|
{ |
116
|
1 |
|
$this->setupTemplates($app); |
117
|
1 |
|
$factory = $this->setupRoutes($app); |
118
|
1 |
|
$this->setupI18n($app); |
119
|
1 |
|
return $factory; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|