Passed
Push — master ( 53af2f...2f4516 )
by Julito
11:40
created

Container::getSettingsManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Framework;
5
6
use Sonata\PageBundle\Entity\SiteManager;
7
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\HttpFoundation\Session\Session;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
12
use Symfony\Component\Security\Core\Role\RoleHierarchy;
13
use Chamilo\CoreBundle\Component\Editor\Editor;
14
use Symfony\Component\Translation\TranslatorInterface;
15
use Chamilo\SettingsBundle\Manager\SettingsManager;
16
use Sonata\UserBundle\Entity\UserManager;
17
18
/**
19
 * Class Container
20
 * This class is a way to access Symfony2 services in legacy Chamilo code.
21
 * @package Chamilo\CoreBundle\Framework
22
 */
23
class Container
24
{
25
    /**
26
     * @var ContainerInterface
27
     */
28
    public static $container;
29
    public static $session;
30
    public static $request;
31
    public static $configuration;
32
    public static $environment;
33
    public static $urlGenerator;
34
    public static $checker;
35
    public static $translator;
36
    public static $mailer;
37
    public static $template;
38
    private static $settingsManager;
39
    private static $userManager;
40
    private static $siteManager;
41
42
    public static $rootDir;
43
    public static $logDir;
44
    public static $tempDir;
45
    public static $dataDir;
46
    public static $courseDir;
47
    public static $configDir;
48
    public static $assets;
49
    public static $htmlEditor;
50
    public static $twig;
51
    public static $roles;
52
    /** @var  string  */
53
    public static $legacyTemplate = '@ChamiloTheme/Layout/layout_one_col.html.twig';
54
55
    /**
56
     * @param ContainerInterface $container
57
     */
58
    public static function setContainer($container)
59
    {
60
        self::$container = $container;
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public static function getConfigDir()
67
    {
68
        return self::$configDir;
69
    }
70
71
    /**
72
     * @param string $parameter
73
     *
74
     * @return mixed
75
     */
76
    public static function getParameter($parameter)
77
    {
78
        if (self::$container->hasParameter($parameter)) {
79
            return self::$container->getParameter($parameter);
80
        }
81
82
        return false;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public static function getEnvironment()
89
    {
90
        return self::$container->get('kernel')->getEnvironment();
91
    }
92
93
    /**
94
     * @return RoleHierarchy
95
     */
96
    public static function getRoles()
97
    {
98
        return self::$container->get('security.role_hierarchy');
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public static function getLogDir()
105
    {
106
        return self::$container->get('kernel')->getLogDir();
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public static function getTempDir()
113
    {
114
        return self::$container->get('kernel')->getCacheDir().'/';
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public static function getRootDir()
121
    {
122
        if (isset(self::$container)) {
123
            return self::$container->get('kernel')->getRealRootDir();
124
        }
125
126
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public static function getUrlAppend()
133
    {
134
        return self::$container->get('kernel')->getUrlAppend();
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public static function isInstalled()
141
    {
142
        return self::$container->get('kernel')->isInstalled();
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public static function getDataDir()
149
    {
150
        return self::$dataDir;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public static function getCourseDir()
157
    {
158
        return self::$courseDir;
159
    }
160
161
    /**
162
     * @return \Twig_Environment
163
     */
164
    public static function getTwig()
165
    {
166
        return self::$container->get('twig');
167
    }
168
169
    /**
170
     * @return \Symfony\Bundle\TwigBundle\TwigEngine
171
     */
172
    public static function getTemplating()
173
    {
174
        return self::$container->get('templating');
175
    }
176
177
    /**
178
     * @return Editor
179
     */
180
    public static function getHtmlEditor()
181
    {
182
        return self::$container->get('chamilo_core.html_editor');
183
    }
184
185
    /**
186
     * @return \Symfony\Bundle\FrameworkBundle\Routing\Router
187
     */
188
    public static function getUrlGenerator()
189
    {
190
        return self::$container->get('router');
191
    }
192
193
    /**
194
     * @return object|Request
195
     */
196
    public static function getRequest()
197
    {
198
        if (!empty(self::$request)) {
199
            return self::$request;
200
        }
201
        return self::$container->get('request');
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::container->get('request') returns the type Request which is incompatible with the documented return type object|Symfony\Component\HttpFoundation\Request.
Loading history...
202
    }
203
204
    public static function setRequest($request)
205
    {
206
        self::$request = $request;
207
    }
208
209
    /**
210
     * @return Session|false
211
     */
212
    public static function getSession()
213
    {
214
        if (self::$container && self::$container->has('session')) {
215
            return self::$container->get('session');
216
        }
217
        return false;
218
    }
219
220
    /**
221
     * @return AuthorizationChecker
222
     */
223
    public static function getAuthorizationChecker()
224
    {
225
        return self::$container->get('security.authorization_checker');
226
    }
227
228
    /**
229
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
230
     */
231
    public static function getTokenStorage()
232
    {
233
        return self::$container->get('security.token_storage');
234
    }
235
236
    /**
237
     * @return TranslatorInterface
238
     */
239
    public static function getTranslator()
240
    {
241
        if (isset(self::$translator)) {
242
            return self::$translator;
243
        }
244
245
        if (self::$container) {
246
            return self::$container->get('translator.default');
247
        }
248
249
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Symfony\Component\Translation\TranslatorInterface.
Loading history...
250
    }
251
252
    /**
253
     * @return CoreAssetsHelper
254
     */
255
    public static function getAsset()
256
    {
257
        return self::$container->get('templating.helper.assets');
258
    }
259
260
    /**
261
     * @return \Swift_Mailer
262
     */
263
    public static function getMailer()
264
    {
265
        return self::$container->get('mailer');
266
    }
267
268
    /**
269
     * @return \Elao\WebProfilerExtraBundle\TwigProfilerEngine
270
     */
271
    public static function getTemplate()
272
    {
273
        return self::$container->get('templating');
274
    }
275
276
    /**
277
     * @return SettingsManager
278
     */
279
    public static function getSettingsManager()
280
    {
281
        return self::$settingsManager;
282
    }
283
284
    /**
285
     * @param SettingsManager $manager
286
     */
287
    public static function setSettingsManager($manager)
288
    {
289
        self::$settingsManager = $manager;
290
    }
291
292
    /**
293
     * @return \Chamilo\CourseBundle\Manager\SettingsManager
294
     */
295
    public static function getCourseSettingsManager()
296
    {
297
        return self::$container->get('chamilo_course.settings.manager');
298
    }
299
300
    /**
301
     * @return \Doctrine\ORM\EntityManager
302
     */
303
    public static function getEntityManager()
304
    {
305
        return \Database::getManager();
306
    }
307
308
    /**
309
     * @return UserManager
310
     */
311
    public static function getUserManager()
312
    {
313
        return self::$userManager;
314
    }
315
316
    /**
317
     * @param UserManager
318
     */
319
    public static function setUserManager($manager)
320
    {
321
        self::$userManager = $manager;
322
    }
323
324
325
    /**
326
     * @return SiteManager
327
     */
328
    public static function getSiteManager()
329
    {
330
        return self::$siteManager;
331
    }
332
333
    /**
334
     * @param UserManager
335
     */
336
    public static function setSiteManager($manager)
337
    {
338
        self::$siteManager = $manager;
339
    }
340
341
    /**
342
     * @return \Sonata\UserBundle\Entity\GroupManager
343
     */
344
    public static function getGroupManager()
345
    {
346
        return self::$container->get('fos_user.group_manager');
347
    }
348
349
    /**
350
     * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
351
     */
352
    public static function getEventDispatcher()
353
    {
354
        return self::$container->get('event_dispatcher');
355
    }
356
357
    /**
358
     * @return \Symfony\Component\Form\FormFactory
359
     */
360
    public static function getFormFactory()
361
    {
362
        return self::$container->get('form.factory');
363
    }
364
365
    /**
366
     * @param string $message
367
     * @param string $type error|success|warning|danger
368
     */
369
    public static function addFlash($message, $type = 'success')
370
    {
371
        $session = self::getSession();
372
        $session->getFlashBag()->add($type, $message);
373
    }
374
375
    /**
376
     * @return object|\Symfony\Cmf\Component\Routing\ChainRouter
377
     */
378
    public static function getRouter()
379
    {
380
        return self::$container->get('router');
381
    }
382
383
    /**
384
     * @return \Chamilo\CourseBundle\ToolChain
385
     */
386
    public static function getToolChain()
387
    {
388
        return self::$container->get('chamilo_course.tool_chain');
389
    }
390
391
    /**
392
     * @param ContainerInterface $container
393
     */
394
    public static function setLegacyServices($container)
395
    {
396
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
397
        \Database::setManager($container->get('doctrine.orm.entity_manager'));
398
399
        Container::setSettingsManager($container->get('chamilo.settings.manager'));
400
        Container::setUserManager($container->get('sonata.user.user_manager'));
401
        Container::setSiteManager($container->get('sonata.page.manager.site'));
402
        Container::$session = $container->get('session');
403
404
405
    }
406
}
407