Completed
Push — master ( 4310f8...9a8de2 )
by Philip
07:00
created

ControllerProvider::connect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
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 Silex\Api\ControllerProviderInterface;
16
use Silex\Application;
17
use Symfony\Component\HttpFoundation\Request;
18
19
/**
20
 * This is the ControllerProvider offering all CRUD pages.
21
 *
22
 * It offers this routes:
23
 *
24
 * "/resource/static" serving static resources
25
 *
26
 * "/{entity}/create" creation page of the entity
27
 *
28
 * "/{entity}" list page of the entity
29
 *
30
 * "/{entity}/{id}" details page of a single entity instance
31
 *
32
 * "/{entity}/{id}/edit" edit page of a single entity instance
33
 *
34
 * "/{entity}/{id}/delete" POST only deletion route for an entity instance
35
 *
36
 * "/{entity}/{id}/{field}/file" renders a file field of an entity instance
37
 *
38
 * "/{entity}/{id}/{field}/delete" POST only deletion of a file field of an entity instance
39
 */
40
class ControllerProvider implements ControllerProviderInterface
41
{
42
43
    /**
44
     * Setups the templates.
45
     *
46
     * @param Application $app
47
     * the Application instance of the Silex application
48
     */
49 10
    protected function setupTemplates(Application $app)
50
    {
51 10
        if ($app->offsetExists('twig.loader.filesystem')) {
52 10
            $app['twig.loader.filesystem']->addPath(__DIR__.'/../../views/', 'crud');
53
        }
54 10
    }
55
56
    /**
57
     * Setups the routes.
58
     *
59
     * @param Application $app
60
     * the Application instance of the Silex application
61
     *
62
     * @return mixed
63
     * the created controller factory
64
     */
65 10
    protected function setupRoutes(Application $app)
66
    {
67
68 10
        $controller = new Controller();
69
70 10
        $self                 = $this;
71
        $localeAndCheckEntity = function(Request $request, Application $app) use ($self, $controller) {
72 9
            $locale = $app['translator']->getLocale();
73 9
            $app['crud']->setLocale($locale);
74 9
            if (!$app['crud']->getData($request->get('entity'))) {
75 7
                return $controller->getNotFoundPage($app, $app['translator']->trans('crudlex.entityNotFound'));
76
            }
77 10
        };
78
79 10
        $factory = $app['controllers_factory'];
80 10
        $factory->get('/resource/static', [$controller, 'staticFile'])->bind('crudStatic');
81 10
        $factory->match('/{entity}/create', [$controller, 'create'])->bind('crudCreate')->before($localeAndCheckEntity, 10);
82 10
        $factory->get('/{entity}', [$controller, 'showList'])->bind('crudList')->before($localeAndCheckEntity, 10);
83 10
        $factory->get('/{entity}/{id}', [$controller, 'show'])->bind('crudShow')->before($localeAndCheckEntity, 10);
84 10
        $factory->match('/{entity}/{id}/edit', [$controller, 'edit'])->bind('crudEdit')->before($localeAndCheckEntity, 10);
85 10
        $factory->post('/{entity}/{id}/delete', [$controller, 'delete'])->bind('crudDelete')->before($localeAndCheckEntity, 10);
86 10
        $factory->get('/{entity}/{id}/{field}/file', [$controller, 'renderFile'])->bind('crudRenderFile')->before($localeAndCheckEntity, 10);
87 10
        $factory->post('/{entity}/{id}/{field}/delete', [$controller, 'deleteFile'])->bind('crudDeleteFile')->before($localeAndCheckEntity, 10);
88 10
        $factory->get('/setting/locale/{locale}', [$controller, 'setLocale'])->bind('crudSetLocale');
89
90 10
        return $factory;
91
    }
92
93
    /**
94
     * Setups i18n.
95
     *
96
     * @param Application $app
97
     * the Application instance of the Silex application
98
     */
99
    protected function setupI18n(Application $app)
100
    {
101 10
        $app->before(function(Request $request, Application $app) {
102 10
            $manageI18n = $app['crud']->isManageI18n();
103 10
            if ($manageI18n) {
104 10
                $locale = $app['session']->get('locale', 'en');
105 10
                $app['translator']->setLocale($locale);
106
            }
107 10
        }, 1);
108 10
    }
109
110
    /**
111
     * Implements ControllerProviderInterface::connect() connecting this
112
     * controller.
113
     *
114
     * @param Application $app
115
     * the Application instance of the Silex application
116
     *
117
     * @return \Silex\ControllerCollection
118
     * this method is expected to return the used ControllerCollection instance
119
     */
120 10
    public function connect(Application $app)
121
    {
122 10
        $this->setupTemplates($app);
123 10
        $factory = $this->setupRoutes($app);
124 10
        $this->setupI18n($app);
125 10
        return $factory;
126
    }
127
128
}
129