Completed
Pull Request — master (#138)
by Marko
17:05
created

FOSCKEditorExtension::addDefaultToolbars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSCKEditor Bundle.
5
 *
6
 * (c) 2018 - present  Friends of Symfony
7
 * (c) 2009 - 2017     Eric GELOEN <[email protected]>
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace FOS\CKEditorBundle\DependencyInjection;
14
15
use FOS\CKEditorBundle\Exception\DependencyInjectionException;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
19
use Symfony\Component\Form\AbstractType;
20
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
21
22
/**
23
 * @author GeLo <[email protected]>
24
 */
25
class FOSCKEditorExtension extends ConfigurableExtension
26
{
27
    private const DEFAULT_TOOLBAR_ITEMS = [
28
        'basic.about' => ['About'],
29
        'basic.basic_styles' => ['Bold', 'Italic'],
30
        'basic.links' => ['Link', 'Unlink'],
31
        'basic.paragraph' => ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent'],
32
        'standard.about' => ['Styles', 'Format', 'About'],
33
        'standard.basic_styles' => ['Bold', 'Italic', 'Strike', '-', 'RemoveFormat'],
34
        'standard.clipboard' => ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'],
35
        'standard.document' => ['Source'],
36
        'standard.editing' => ['Scayt'],
37
        'standard.links' => ['Link', 'Unlink', 'Anchor'],
38
        'standard.insert' => ['Image', 'Table', 'HorizontalRule', 'SpecialChar'],
39
        'standard.paragraph' => ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote'],
40
        'standard.tools' => ['Maximize'],
41
        'full.about' => ['About'],
42
        'full.basic_styles' => [
43
            'Bold',
44
            'Italic',
45
            'Underline',
46
            'Strike',
47
            'Subscript',
48
            'Superscript',
49
            '-',
50
            'RemoveFormat',
51
        ],
52
        'full.clipboard' => ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord', '-', 'Undo', 'Redo'],
53
        'full.colors' => ['TextColor', 'BGColor'],
54
        'full.document' => ['Source', '-', 'NewPage', 'Preview', 'Print', '-', 'Templates'],
55
        'full.editing' => ['Find', 'Replace', '-', 'SelectAll', '-', 'Scayt'],
56
        'full.forms' => [
57
            'Form',
58
            'Checkbox',
59
            'Radio',
60
            'TextField',
61
            'Textarea',
62
            'SelectField',
63
            'Button',
64
            'ImageButton',
65
            'HiddenField',
66
        ],
67
        'full.insert' => ['Image', 'Flash', 'Table', 'HorizontalRule', 'SpecialChar', 'Smiley', 'PageBreak', 'Iframe'],
68
        'full.links' => ['Link', 'Unlink', 'Anchor'],
69
        'full.paragraph' => [
70
            'NumberedList',
71
            'BulletedList',
72
            '-',
73
            'Outdent',
74
            'Indent',
75
            '-',
76
            'Blockquote',
77
            'CreateDiv',
78
            '-',
79
            'JustifyLeft',
80
            'JustifyCenter',
81
            'JustifyRight',
82
            'JustifyBlock',
83
            '-',
84
            'BidiLtr',
85
            'BidiRtl',
86
        ],
87
        'full.styles' => ['Styles', 'Format', 'Font', 'FontSize', 'TextColor', 'BGColor'],
88
        'full.tools' => ['Maximize', 'ShowBlocks'],
89
    ];
90
91
    private const DEFAULT_TOOLBAR_CONFIGS = [
92
        'basic' => [
93
            '@basic.basic_styles',
94
            '@basic.paragraph',
95
            '@basic.links',
96
            '@basic.about',
97
        ],
98
        'standard' => [
99
            '@standard.clipboard',
100
            '@standard.editing',
101
            '@standard.links',
102
            '@standard.insert',
103
            '@standard.tools',
104
            '@standard.document',
105
            '/',
106
            '@standard.basic_styles',
107
            '@standard.paragraph',
108
            '@standard.about',
109
        ],
110
        'full' => [
111
            '@full.document',
112
            '@full.clipboard',
113
            '@full.editing',
114
            '@full.forms',
115
            '/',
116
            '@full.basic_styles',
117
            '@full.paragraph',
118
            '@full.links',
119
            '@full.insert',
120
            '/',
121
            '@full.styles',
122
            '@full.colors',
123
            '@full.tools',
124
            '@full.about',
125
        ],
126
    ];
127
128
    protected function loadInternal(array $config, ContainerBuilder $container)
129
    {
130
        $this->loadResources($container);
131
132
        $config = $this->addDefaultToolbars($config);
133
134
        if ($config['enable']) {
135
            $config = $this->resolveConfigs($config, $container);
0 ignored issues
show
Unused Code introduced by
The call to FOS\CKEditorBundle\Depen...nsion::resolveConfigs() has too many arguments starting with $container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
            /** @scrutinizer ignore-call */ 
136
            $config = $this->resolveConfigs($config, $container);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
136
            $config = $this->resolveStylesSet($config, $container);
0 ignored issues
show
Unused Code introduced by
The call to FOS\CKEditorBundle\Depen...ion::resolveStylesSet() has too many arguments starting with $container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
            /** @scrutinizer ignore-call */ 
137
            $config = $this->resolveStylesSet($config, $container);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
137
        }
138
139
        $container->getDefinition('fos_ck_editor.form.type')
140
            ->setArgument(0, $config);
141
142
        if (!method_exists(AbstractType::class, 'getBlockPrefix')) {
143
            $container->getDefinition('fos_ck_editor.form.type')
144
                ->clearTag('form.type')
145
                ->addTag('form.type', ['alias' => 'ckeditor']);
146
        }
147
148
        $bundles = $container->getParameter('kernel.bundles');
149
150
        if (isset($bundles['IvoryCKEditorBundle'])) {
151
            @trigger_error(
152
                "IvoryCKEditorBundle isn't maintained anymore and should be replaced with FOSCKEditorBundle.",
153
                E_USER_DEPRECATED
154
            );
155
        }
156
    }
157
158
    private function loadResources(ContainerBuilder $container)
159
    {
160
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
161
162
        $resources = [
163
            'builder',
164
            'command',
165
            'form',
166
            'installer',
167
            'renderer',
168
            'twig',
169
        ];
170
171
        foreach ($resources as $resource) {
172
            $loader->load($resource.'.xml');
173
        }
174
    }
175
176
    private function addDefaultToolbars(array $config)
177
    {
178
        $config['toolbars']['items'] = array_merge(self::DEFAULT_TOOLBAR_ITEMS, $config['toolbars']['items']);
179
        $config['toolbars']['configs'] = array_merge(self::DEFAULT_TOOLBAR_CONFIGS, $config['toolbars']['configs']);
180
181
        return $config;
182
    }
183
184
    /**
185
     * @throws DependencyInjectionException
186
     */
187
    private function resolveConfigs(array $config): array
188
    {
189
        if (empty($config['configs'])) {
190
            return $config;
191
        }
192
193
        if (!isset($config['default_config']) && !empty($config['configs'])) {
194
            reset($config['configs']);
195
            $config['default_config'] = key($config['configs']);
196
        }
197
198
        if (isset($config['default_config']) && !isset($config['configs'][$config['default_config']])) {
199
            throw DependencyInjectionException::invalidDefaultConfig($config['default_config']);
200
        }
201
202
        return $config;
203
    }
204
205
    private function resolveStylesSet(array $config)
206
    {
207
        if (empty($config['styles'])) {
208
            return $config;
209
        }
210
211
        $stylesSets = $config['styles'];
212
213
        foreach ($stylesSets as &$stylesSet) {
214
            foreach ($stylesSet as &$value) {
215
                $value = array_filter($value);
216
            }
217
        }
218
219
        return $config;
220
    }
221
222
    public function getAlias()
223
    {
224
        return 'fos_ck_editor';
225
    }
226
}
227