|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CRUDlexSilex2 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
|
|
|
* Holds the path to the templates. |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $templatePath; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Setups the templates. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Application $app |
|
54
|
|
|
* the Application instance of the Silex application |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function setupTemplates(Application $app) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($app->offsetExists('twig.loader.filesystem')) { |
|
59
|
|
|
$app['twig.loader.filesystem']->addPath($this->templatePath, 'crud'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Setups the routes. |
|
65
|
|
|
* |
|
66
|
|
|
* @param Application $app |
|
67
|
|
|
* the Application instance of the Silex application |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
* the created controller factory |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function setupRoutes(Application $app) |
|
73
|
|
|
{ |
|
74
|
|
|
$controller = $app->offsetExists('crud.controller') ? $app['crud.controller'] : new Controller($app['crud'], $app['crud.filesystem'], $app['twig'], $app['session'], $app['translator']); |
|
75
|
|
|
if (!$controller instanceof ControllerInterface) { |
|
76
|
|
|
throw new \InvalidArgumentException('crud.controller doesn\'t implement CRUDlex\ControllerInterface.'); |
|
77
|
|
|
} |
|
78
|
|
|
$localeAndCheckEntity = [$controller, 'setLocaleAndCheckEntity']; |
|
79
|
|
|
$factory = $app['controllers_factory']; |
|
80
|
|
|
$factory->get('/resource/static', [$controller, 'staticFile'])->bind('crudStatic'); |
|
81
|
|
|
$factory->match('/{entity}/create', [$controller, 'create'])->bind('crudCreate')->before($localeAndCheckEntity, 10); |
|
82
|
|
|
$factory->get('/{entity}', [$controller, 'showList'])->bind('crudList')->before($localeAndCheckEntity, 10); |
|
83
|
|
|
$factory->get('/{entity}/{id}', [$controller, 'show'])->bind('crudShow')->before($localeAndCheckEntity, 10); |
|
84
|
|
|
$factory->match('/{entity}/{id}/edit', [$controller, 'edit'])->bind('crudEdit')->before($localeAndCheckEntity, 10); |
|
85
|
|
|
$factory->post('/{entity}/{id}/delete', [$controller, 'delete'])->bind('crudDelete')->before($localeAndCheckEntity, 10); |
|
86
|
|
|
$factory->get('/{entity}/{id}/{field}/file', [$controller, 'renderFile'])->bind('crudRenderFile')->before($localeAndCheckEntity, 10); |
|
87
|
|
|
$factory->post('/{entity}/{id}/{field}/delete', [$controller, 'deleteFile'])->bind('crudDeleteFile')->before($localeAndCheckEntity, 10); |
|
88
|
|
|
$factory->get('/setting/locale/{locale}', [$controller, 'setLocale'])->bind('crudSetLocale'); |
|
89
|
|
|
|
|
90
|
|
|
return $factory; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* ControllerProvider constructor. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __construct() |
|
97
|
|
|
{ |
|
98
|
|
|
$this->templatePath = __DIR__.'/../../../../CRUDlex/src/views/'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets the path to the templates. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $templatePath |
|
105
|
|
|
* the new template path |
|
106
|
|
|
*/ |
|
107
|
|
|
public function setTemplatePath($templatePath) |
|
108
|
|
|
{ |
|
109
|
|
|
$this->templatePath = $templatePath; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Setups i18n. |
|
114
|
|
|
* |
|
115
|
|
|
* @param Request $request |
|
116
|
|
|
* the current request |
|
117
|
|
|
* @param Application $app |
|
118
|
|
|
* the Application instance of the Silex application |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setupI18n(Request $request, Application $app) |
|
121
|
|
|
{ |
|
122
|
|
|
$manageI18n = $app['crud']->isManageI18n(); |
|
123
|
|
|
if ($manageI18n) { |
|
124
|
|
|
$locale = $app['session']->get('locale', 'en'); |
|
125
|
|
|
$app['translator']->setLocale($locale); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Implements ControllerProviderInterface::connect() connecting this |
|
131
|
|
|
* controller. |
|
132
|
|
|
* |
|
133
|
|
|
* @param Application $app |
|
134
|
|
|
* the Application instance of the Silex application |
|
135
|
|
|
* |
|
136
|
|
|
* @return \Silex\ControllerCollection |
|
137
|
|
|
* this method is expected to return the used ControllerCollection instance |
|
138
|
|
|
*/ |
|
139
|
|
|
public function connect(Application $app) |
|
140
|
|
|
{ |
|
141
|
|
|
$this->setupTemplates($app); |
|
142
|
|
|
$factory = $this->setupRoutes($app); |
|
143
|
|
|
$app->before([$this, 'setupI18n']); |
|
144
|
|
|
return $factory; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|