Failed Conditions
Branch master (20b0e7)
by Gawain
05:59
created

Application::initProviders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 39

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 47
rs 9.0303
cc 1
eloc 39
nc 1
nop 0
1
<?php
2
3
namespace Bolt;
4
5
use Bolt\Events\ControllerEvents;
6
use Bolt\Events\MountEvent;
7
use Bolt\Provider\LoggerServiceProvider;
8
use Bolt\Provider\PathServiceProvider;
9
use Bolt\Provider\WhoopsServiceProvider;
10
use Cocur\Slugify\Bridge\Silex\SlugifyServiceProvider;
11
use Silex;
12
use Symfony\Component\Stopwatch;
13
14
class Application extends Silex\Application
15
{
16
    /**
17
     * @deprecated Deprecated since 3.0, to be removed in 4.0. Use $app['locale_fallbacks'] instead.
18
     */
19
    const DEFAULT_LOCALE = 'en_GB';
20
21
    /**
22
     * @param array $values
23
     */
24
    public function __construct(array $values = [])
25
    {
26
        $values['bolt_version'] = '3.0.0';
27
        $values['bolt_name'] = 'alpha 3';
28
        $values['bolt_released'] = false; // `true` for stable releases, `false` for alpha, beta and RC.
29
        $values['bolt_long_version'] = function ($app) {
30
            return $app['bolt_version'] . ' ' . $app['bolt_name'];
31
        };
32
33
        /** @internal Parameter to track a deprecated PHP version */
34
        $values['deprecated.php'] = version_compare(PHP_VERSION, '5.5.9', '<');
35
36
        parent::__construct($values);
37
38
        $this->register(new PathServiceProvider());
39
40
        // Initialize the config. Note that we do this here, on 'construct'.
41
        // All other initialisation is triggered from bootstrap.php
42
        // Warning!
43
        // One of a valid ResourceManager ['resources'] or ClassLoader ['classloader']
44
        // must be defined for working properly
45
        if (!isset($this['resources'])) {
46
            $this['resources'] = new Configuration\ResourceManager($this);
0 ignored issues
show
Deprecated Code introduced by
The class Bolt\Configuration\ResourceManager has been deprecated with message: Deprecated since 3.0, to be removed in 4.0.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
47
            $this['resources']->compat();
48
        } else {
49
            $this['classloader'] = $this['resources']->getClassLoader();
50
        }
51
52
        // Register a PHP shutdown function to catch fatal errors with the application object
53
        register_shutdown_function(['\Bolt\Exception\LowlevelException', 'catchFatalErrors'], $this);
54
55
        $this['resources']->setApp($this);
56
        $this->initConfig();
57
        $this->initLogger();
58
        $this['resources']->initialize();
59
60
        $this['debug'] = $this['config']->get('general/debug', false);
61
62
        $locales = (array) $this['config']->get('general/locale');
63
        $this['locale'] = reset($locales);
64
65
        // Initialize the 'editlink' and 'edittitle'.
66
        $this['editlink'] = '';
67
        $this['edittitle'] = '';
68
69
        // Initialize the JavaScript data gateway.
70
        $this['jsdata'] = [];
71
    }
72
73
    protected function initConfig()
74
    {
75
        $this->register(new Provider\DatabaseSchemaServiceProvider())
76
            ->register(new Provider\ConfigServiceProvider())
77
        ;
78
        $this['config']->initialize();
79
    }
80
81
    protected function initSession()
82
    {
83
        $this
84
            ->register(new Provider\TokenServiceProvider())
85
            ->register(new Provider\SessionServiceProvider())
86
        ;
87
    }
88
89
    public function initialize()
90
    {
91
        // Set up session handling
92
        $this->initSession();
93
94
        // Set up locale and translations.
95
        $this->initLocale();
96
97
        // Initialize Twig and our rendering Provider.
98
        $this->initRendering();
99
100
        // Initialize debugging
101
        $this->initDebugging();
102
103
        // Initialize the Database Providers.
104
        $this->initDatabase();
105
106
        // Initialize the rest of the Providers.
107
        $this->initProviders();
108
109
        // Do a version check
110
        $this['config.environment']->checkVersion();
111
112
        // Calling for BC. Controllers are mounted in ControllerServiceProvider now.
113
        $this->initMountpoints();
0 ignored issues
show
Deprecated Code introduced by
The method Bolt\Application::initMountpoints() has been deprecated with message: Deprecated since 3.0, to be removed in 4.0. Use {@see ControllerEvents::MOUNT} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
114
115
        // Initialize enabled extensions before executing handlers.
116
        $this->initExtensions();
0 ignored issues
show
Deprecated Code introduced by
The method Bolt\Application::initExtensions() has been deprecated with message: Deprecated since 3.0, to be removed in 4.0. Use {@see ControllerEvents::MOUNT} instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
117
118
        // Initialize the global 'before' handler.
119
        $this->before([$this, 'beforeHandler']);
120
121
        // Initialize the global 'after' handler.
122
        $this->after([$this, 'afterHandler']);
123
124
        // Calling for BC. Initialize the 'error' handler.
125
        $this->error([$this, 'errorHandler']);
126
    }
127
128
    /**
129
     * Initialize the loggers.
130
     */
131
    public function initLogger()
132
    {
133
        $this->register(new LoggerServiceProvider(), []);
134
    }
135
136
    /**
137
     * Initialize the database providers.
138
     */
139
    public function initDatabase()
140
    {
141
        $this->register(new Provider\DatabaseServiceProvider());
142
        $this->checkDatabaseConnection();
0 ignored issues
show
Deprecated Code introduced by
The method Bolt\Application::checkDatabaseConnection() has been deprecated with message: Deprecated since 3.0, to be removed in 4.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
143
    }
144
145
    /**
146
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
147
     */
148
    protected function checkDatabaseConnection()
149
    {
150
    }
151
152
    /**
153
     * Initialize the rendering providers.
154
     */
155
    public function initRendering()
156
    {
157
        $this
158
            ->register(new Provider\TwigServiceProvider())
159
            ->register(new Provider\RenderServiceProvider())
160
            ->register(new Silex\Provider\HttpCacheServiceProvider(),
161
                ['http_cache.cache_dir' => $this['resources']->getPath('cache')]
162
            );
163
    }
164
165
    /**
166
     * Set up the debugging if required.
167
     */
168
    public function initDebugging()
169
    {
170
        if (!$this['debug']) {
171
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_USER_DEPRECATED);
172
173
            return;
174
        }
175
176
        // Set the error_reporting to the level specified in config.yml
177
        error_reporting($this['config']->get('general/debug_error_level'));
178
179
        // Register Whoops, to handle errors for logged in users only.
180
        if ($this['config']->get('general/debug_enable_whoops')) {
181
            $this->register(new WhoopsServiceProvider());
182
        }
183
184
        $this->register(new Provider\DumperServiceProvider());
185
186
        // Initialize Web Profiler providers
187
        $this->initProfiler();
188
    }
189
190
    /**
191
     * Set up the profilers for the toolbar.
192
     */
193
    public function initProfiler()
194
    {
195
        $this->register(new Provider\ProfilerServiceProvider());
196
    }
197
198
    public function initLocale()
199
    {
200
        $this->register(new Provider\TranslationServiceProvider());
201
    }
202
203
    public function initProviders()
204
    {
205
        $this
206
            ->register(new Silex\Provider\HttpFragmentServiceProvider())
207
            ->register(new Silex\Provider\UrlGeneratorServiceProvider())
208
            ->register(new Silex\Provider\ValidatorServiceProvider())
209
            ->register(new Provider\RoutingServiceProvider())
210
            ->register(new Silex\Provider\ServiceControllerServiceProvider()) // must be after Routing
211
            ->register(new Provider\RandomGeneratorServiceProvider())
212
            ->register(new Provider\PermissionsServiceProvider())
213
            ->register(new Provider\StorageServiceProvider())
214
            ->register(new Provider\QueryServiceProvider())
215
            ->register(new Provider\AccessControlServiceProvider())
216
            ->register(new Provider\UsersServiceProvider())
217
            ->register(new Provider\CacheServiceProvider())
218
            ->register(new Provider\ExtensionServiceProvider())
219
            ->register(new Provider\StackServiceProvider())
220
            ->register(new Provider\OmnisearchServiceProvider())
221
            ->register(new Provider\TemplateChooserServiceProvider())
222
            ->register(new Provider\CronServiceProvider())
223
            ->register(new Provider\FilePermissionsServiceProvider())
224
            ->register(new Provider\MenuServiceProvider())
225
            ->register(new Provider\UploadServiceProvider())
226
            ->register(new Provider\FilesystemServiceProvider())
227
            ->register(new Provider\ThumbnailsServiceProvider())
228
            ->register(new Provider\NutServiceProvider())
229
            ->register(new Provider\GuzzleServiceProvider())
230
            ->register(new Provider\PrefillServiceProvider())
231
            ->register(new SlugifyServiceProvider())
232
            ->register(new Provider\MarkdownServiceProvider())
233
            ->register(new Provider\ControllerServiceProvider())
234
            ->register(new Provider\EventListenerServiceProvider())
235
            ->register(new Provider\AssetServiceProvider())
236
            ->register(new Provider\FormServiceProvider())
237
            ->register(new Provider\MailerServiceProvider())
238
            ->register(new Provider\PagerServiceProvider())
239
        ;
240
241
        $this['paths'] = $this['resources']->getPaths();
242
243
        // Initialize stopwatch even if debug is not enabled.
244
        $this['stopwatch'] = $this->share(
245
            function () {
246
                return new Stopwatch\Stopwatch();
247
            }
248
        );
249
    }
250
251
    /**
252
     * @deprecated Deprecated since 3.0, to be removed in 4.0. Use {@see ControllerEvents::MOUNT} instead.
253
     */
254
    public function initExtensions()
255
    {
256
        $this['extensions']->addManagedExtensions();
257
        $this['extensions']->register($this);
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function mount($prefix, $controllers)
264
    {
265
        if (!$this->booted) {
266
            // Forward call to mount event if we can (which handles prioritization).
267
            $this->on(
268
                ControllerEvents::MOUNT,
269
                function (MountEvent $event) use ($prefix, $controllers) {
270
                    $event->mount($prefix, $controllers);
271
                }
272
            );
273
        } else {
274
            // Already missed mounting event just append it to bottom of controller list
275
            parent::mount($prefix, $controllers);
276
        }
277
278
        return $this;
279
    }
280
281
    /**
282
     * @deprecated Deprecated since 3.0, to be removed in 4.0. Use {@see ControllerEvents::MOUNT} instead.
283
     */
284
    public function initMountpoints()
285
    {
286
    }
287
288
    /**
289
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
290
     */
291
    public function beforeHandler()
292
    {
293
    }
294
295
    /**
296
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
297
     */
298
    public function afterHandler()
299
    {
300
    }
301
302
    /**
303
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
304
     */
305
    public function errorHandler()
306
    {
307
    }
308
309
    /**
310
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
311
     *
312
     * @param string $name
313
     *
314
     * @return boolean
315
     */
316
    public function __isset($name)
317
    {
318
        return isset($this[$name]);
319
    }
320
321
    /**
322
     * Get the Bolt version string
323
     *
324
     * @param boolean $long TRUE returns 'version name', FALSE 'version'
325
     *
326
     * @return string
327
     *
328
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
329
     *             Use parameters in application instead
330
     */
331
    public function getVersion($long = true)
332
    {
333
        return $this[$long ? 'bolt_long_version' : 'bolt_version'];
334
    }
335
336
    /**
337
     * Generates a path from the given parameters.
338
     *
339
     * @param string $route      The name of the route
340
     * @param array  $parameters An array of parameters
341
     *
342
     * @return string The generated path
343
     *
344
     * @deprecated Deprecated since 3.0, to be removed in 4.0.
345
     *             Use {@see \Symfony\Component\Routing\Generator\UrlGeneratorInterface} instead.
346
     */
347
    public function generatePath($route, $parameters = [])
348
    {
349
        return $this['url_generator']->generate($route, $parameters);
350
    }
351
}
352