|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Resourceful\IndexControllerProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use Resourceful\Controller\GetResourceController; |
|
7
|
|
|
use Resourceful\ResourcefulServiceProvider\AddSchema; |
|
8
|
|
|
use Silex\Api\ControllerProviderInterface; |
|
9
|
|
|
use Silex\Application; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Twig_Loader_Filesystem; |
|
12
|
|
|
use \Resourceful\StoreHelpers\StoreHelpers; |
|
13
|
|
|
|
|
14
|
|
|
class IndexControllerProvider implements ControllerProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
protected $schema; |
|
17
|
|
|
|
|
18
|
1 |
|
public function __construct($schema) |
|
19
|
|
|
{ |
|
20
|
1 |
|
assert( StoreHelpers::getSchemaType($schema)=='index', 'schema type must be "index"'); |
|
21
|
|
|
|
|
22
|
1 |
|
$this->schema = $schema; |
|
23
|
1 |
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public function connect(Application $app) |
|
26
|
|
|
{ |
|
27
|
1 |
|
assert(isset($app["resources_factory"])); |
|
28
|
1 |
|
assert(isset($app["twig"])); |
|
29
|
|
|
|
|
30
|
1 |
|
$store = StoreHelpers::getStoreForSchema($this->schema, $app); |
|
31
|
|
|
|
|
32
|
1 |
|
$resource = $app["resources_factory"]($this->schema); |
|
33
|
1 |
|
$app["twig.loader"]->addLoader(new Twig_Loader_Filesystem(__DIR__ . "/templates")); |
|
34
|
1 |
|
$addIndexSchema = new AddSchema($this->schema, "index"); |
|
35
|
|
|
|
|
36
|
|
|
// Generate default Index resource |
|
37
|
1 |
|
$resource->before(function (Request $request, Application $app) use($store){ |
|
38
|
|
|
$index = $app["url_generator"]->generate("index"); |
|
39
|
|
|
if (!$store->contains($index)) { |
|
40
|
|
|
$store->save($index, json_decode($app["twig"]->render("default.json.twig"))); |
|
41
|
|
|
} |
|
42
|
1 |
|
}); |
|
43
|
1 |
|
$resource->get("/", new GetResourceController($this->schema)) |
|
44
|
1 |
|
->before($addIndexSchema) |
|
45
|
1 |
|
->bind("index"); |
|
46
|
1 |
|
return $resource; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|