1<?php
2
3/*
4 * This file is part of the Silex framework.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Silex\Provider;
13
14use Pimple\Container;
15use Pimple\ServiceProviderInterface;
16use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
17use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
18use Symfony\Component\Form\Extension\Validator\ValidatorExtension as FormValidatorExtension;
19use Symfony\Component\Form\FormFactory;
20use Symfony\Component\Form\FormRegistry;
21use Symfony\Component\Form\ResolvedFormTypeFactory;
22
23/**
24 * Symfony Form component Provider.
25 *
26 * @author Fabien Potencier <fabien@symfony.com>
27 */
28class FormServiceProvider implements ServiceProviderInterface
29{
30    public function register(Container $app)
31    {
32        if (!class_exists('Locale')) {
33            throw new \RuntimeException('You must either install the PHP intl extension or the Symfony Intl Component to use the Form extension.');
34        }
35
36        $app['form.types'] = function ($app) {
37            return array();
38        };
39
40        $app['form.type.extensions'] = function ($app) {
41            return array();
42        };
43
44        $app['form.type.guessers'] = function ($app) {
45            return array();
46        };
47
48        $app['form.extension.csrf'] = function ($app) {
49            if (isset($app['translator'])) {
50                return new CsrfExtension($app['csrf.token_manager'], $app['translator']);
51            }
52
53            return new CsrfExtension($app['csrf.token_manager']);
54        };
55
56        $app['form.extension.silex'] = function ($app) {
57            return new Form\SilexFormExtension($app, $app['form.types'], $app['form.type.extensions'], $app['form.type.guessers']);
58        };
59
60        $app['form.extensions'] = function ($app) {
61            $extensions = array(
62                new HttpFoundationExtension(),
63            );
64
65            if (isset($app['csrf.token_manager'])) {
66                $extensions[] = $app['form.extension.csrf'];
67            }
68
69            if (isset($app['validator'])) {
70                $extensions[] = new FormValidatorExtension($app['validator']);
71            }
72            $extensions[] = $app['form.extension.silex'];
73
74            return $extensions;
75        };
76
77        $app['form.factory'] = function ($app) {
78            return new FormFactory($app['form.registry'], $app['form.resolved_type_factory']);
79        };
80
81        $app['form.registry'] = function ($app) {
82            return new FormRegistry($app['form.extensions'], $app['form.resolved_type_factory']);
83        };
84
85        $app['form.resolved_type_factory'] = function ($app) {
86            return new ResolvedFormTypeFactory();
87        };
88    }
89}
90