1
|
|
|
<?php |
2
|
|
|
namespace Resourceful; |
3
|
|
|
|
4
|
|
|
use Silex\Api\ControllerProviderInterface; |
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* TODO: this class colud be extended to allow sub indexed. |
10
|
|
|
*/ |
11
|
|
|
class IndexControllerProvider implements ControllerProviderInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* N.B. the same 'index' string is used to name different concepts- |
15
|
|
|
*/ |
16
|
4 |
|
public function connect(Application $app) |
17
|
|
|
{ |
18
|
4 |
|
$controllers = $app["controllers_factory"]; |
19
|
|
|
|
20
|
|
|
// ensure IndexControllerProvider is called once |
21
|
4 |
|
if( isset($app['index.controller'])){ |
22
|
1 |
|
$app->abort(501, 'Sorry, only one index admited.'); |
23
|
|
|
} else { |
24
|
|
|
$app['index.controller'] = function(){ |
25
|
|
|
return new ReadResourceController('index'); // here 'index' is a schema id |
26
|
|
|
}; |
27
|
|
|
} |
28
|
|
|
|
29
|
4 |
|
$controllers->get("/", 'index.controller:read')->bind('index') // here 'index is the route name |
30
|
4 |
|
->before(function (Request $request, Application $app){ |
31
|
|
|
// Generate the default index resource on the fly |
32
|
|
|
assert(isset($app['twig'])); |
33
|
|
|
assert(isset($app['data.store'])); |
34
|
|
|
|
35
|
|
|
$datastore=$app['data.store']; |
36
|
|
|
|
37
|
|
|
if($app["createDefault"]){ |
38
|
|
|
$resource = $app["url_generator"]->generate('index').'/index'; // first 'index' is the route name, the last is the the resource basename |
39
|
|
|
if (!$datastore->contains($resource)) { |
40
|
|
|
$datastore->save($resource, json_decode($app["twig"]->render("index.json.twig"))); |
41
|
|
|
} |
42
|
|
|
} |
43
|
4 |
|
}) |
44
|
4 |
|
->before(new SchemaHandler('index')) // here 'index' is a schema id |
45
|
4 |
|
->after( new DescribedBySchemaHandler('index')) // here 'index' is a schema id |
46
|
|
|
; |
47
|
|
|
|
48
|
4 |
|
return $controllers; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|