1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resourceful\CrudControllerProvider; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Resourceful\Controller\CreateResourceController; |
7
|
|
|
use Resourceful\Controller\DeleteResourceController; |
8
|
|
|
use Resourceful\Controller\GetResourceController; |
9
|
|
|
use Resourceful\Controller\PutResourceController; |
10
|
|
|
use Resourceful\ResourcefulServiceProvider\AddSchema; |
11
|
|
|
use Silex\Api\ControllerProviderInterface; |
12
|
|
|
use Silex\Application; |
13
|
|
|
use Twig_Loader_Filesystem; |
14
|
|
|
use \Resourceful\StoreHelpers\StoreHelpers; |
15
|
|
|
|
16
|
|
|
class CrudControllerProvider implements ControllerProviderInterface |
17
|
|
|
{ |
18
|
|
|
protected $schema; |
19
|
|
|
|
20
|
1 |
|
public function __construct($schema) |
21
|
|
|
{ |
22
|
1 |
|
assert( !empty(StoreHelpers::getSchemaType($schema))); |
23
|
|
|
|
24
|
1 |
|
$this->schema = $schema; |
25
|
1 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function connect(Application $app) |
28
|
|
|
{ |
29
|
1 |
|
assert(isset($app["resources_factory"])); |
30
|
1 |
|
assert(isset($app["twig.loader"])); |
31
|
|
|
|
32
|
1 |
|
$resource = $app["resources_factory"]($this->schema); |
33
|
|
|
|
34
|
1 |
|
$type = StoreHelpers::getSchemaType($this->schema); |
35
|
1 |
|
$app["twig.loader"]->addLoader(new Twig_Loader_Filesystem(__DIR__ . "/templates")); |
36
|
1 |
|
$replacements = array("type" => $type, "title" => ucfirst($type)); |
37
|
1 |
|
$addCrudSchema = new AddSchema($this->schema, "crud", $replacements); |
38
|
|
|
|
39
|
1 |
|
$resource->get("/{id}", new GetResourceController($this->schema)) |
40
|
1 |
|
->before($addCrudSchema) |
41
|
1 |
|
->bind($this->schema); |
42
|
1 |
|
$resource->put("/{id}", new PutResourceController($this->schema)) |
43
|
1 |
|
->before($addCrudSchema); |
44
|
1 |
|
$resource->delete("/{id}", new DeleteResourceController($this->schema)) |
45
|
1 |
|
->before($addCrudSchema); |
46
|
1 |
|
$resource->post("/", new CreateResourceController($this->schema)) |
47
|
1 |
|
->before($addCrudSchema); |
48
|
|
|
|
49
|
1 |
|
return $resource; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|