Completed
Push — master ( 9a8de2...45acf5 )
by Philip
10:02
created

ControllerProvider::setupRoutes()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
ccs 18
cts 18
cp 1
rs 9.0856
cc 2
eloc 18
nc 1
nop 1
crap 2
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 10
        $controller           = new Controller();
68
        $localeAndCheckEntity = function(Request $request, Application $app) use ($controller) {
69 9
            $locale = $app['translator']->getLocale();
70 9
            $app['crud']->setLocale($locale);
71 9
            if (!$app['crud']->getData($request->get('entity'))) {
72 7
                return $controller->getNotFoundPage($app, $app['translator']->trans('crudlex.entityNotFound'));
73
            }
74 10
        };
75 10
        $factory = $app['controllers_factory'];
76 10
        $factory->get('/resource/static', [$controller, 'staticFile'])->bind('crudStatic');
77 10
        $factory->match('/{entity}/create', [$controller, 'create'])->bind('crudCreate')->before($localeAndCheckEntity, 10);
78 10
        $factory->get('/{entity}', [$controller, 'showList'])->bind('crudList')->before($localeAndCheckEntity, 10);
79 10
        $factory->get('/{entity}/{id}', [$controller, 'show'])->bind('crudShow')->before($localeAndCheckEntity, 10);
80 10
        $factory->match('/{entity}/{id}/edit', [$controller, 'edit'])->bind('crudEdit')->before($localeAndCheckEntity, 10);
81 10
        $factory->post('/{entity}/{id}/delete', [$controller, 'delete'])->bind('crudDelete')->before($localeAndCheckEntity, 10);
82 10
        $factory->get('/{entity}/{id}/{field}/file', [$controller, 'renderFile'])->bind('crudRenderFile')->before($localeAndCheckEntity, 10);
83 10
        $factory->post('/{entity}/{id}/{field}/delete', [$controller, 'deleteFile'])->bind('crudDeleteFile')->before($localeAndCheckEntity, 10);
84 10
        $factory->get('/setting/locale/{locale}', [$controller, 'setLocale'])->bind('crudSetLocale');
85
86 10
        return $factory;
87
    }
88
89
    /**
90
     * Setups i18n.
91
     *
92
     * @param Application $app
93
     * the Application instance of the Silex application
94
     */
95
    protected function setupI18n(Application $app)
96
    {
97 10
        $app->before(function(Request $request, Application $app) {
98 10
            $manageI18n = $app['crud']->isManageI18n();
99 10
            if ($manageI18n) {
100 10
                $locale = $app['session']->get('locale', 'en');
101 10
                $app['translator']->setLocale($locale);
102
            }
103 10
        }, 1);
104 10
    }
105
106
    /**
107
     * Implements ControllerProviderInterface::connect() connecting this
108
     * controller.
109
     *
110
     * @param Application $app
111
     * the Application instance of the Silex application
112
     *
113
     * @return \Silex\ControllerCollection
114
     * this method is expected to return the used ControllerCollection instance
115
     */
116 10
    public function connect(Application $app)
117
    {
118 10
        $this->setupTemplates($app);
119 10
        $factory = $this->setupRoutes($app);
120 10
        $this->setupI18n($app);
121 10
        return $factory;
122
    }
123
124
}
125