Completed
Push — master ( b05893...eedd1b )
by Julito
09:17
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 Chamilo\CoreBundle\Component\Editor\Editor;
7
use Chamilo\CoreBundle\Hook\Interfaces\HookEventInterface;
8
use Chamilo\CoreBundle\Repository\AccessUrlRepository;
9
use Chamilo\CoreBundle\ToolChain;
10
use Chamilo\CourseBundle\Repository\CDocumentRepository;
11
use Chamilo\CourseBundle\Repository\CExerciseCategoryRepository;
12
use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository;
13
use Chamilo\CourseBundle\Repository\CQuizRepository;
14
use Chamilo\PageBundle\Entity\Page;
15
use Chamilo\SettingsBundle\Manager\SettingsManager;
16
use Sonata\PageBundle\Entity\SiteManager;
17
use Sonata\UserBundle\Entity\UserManager;
18
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Session\Session;
22
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
23
use Symfony\Component\Security\Core\Role\RoleHierarchy;
24
use Symfony\Contracts\Translation\TranslatorInterface;
25
use Zend\EventManager\EventInterface;
26
27
/**
28
 * Class Container
29
 * This class is a way to access Symfony2 services in legacy Chamilo code.
30
 *
31
 * @package Chamilo\CoreBundle\Framework
32
 */
33
class Container
34
{
35
    /**
36
     * @var ContainerInterface
37
     */
38
    public static $container;
39
    public static $session;
40
    public static $request;
41
    public static $configuration;
42
    public static $environment;
43
    public static $urlGenerator;
44
    public static $checker;
45
    /** @var TranslatorInterface */
46
    public static $translator;
47
    public static $mailer;
48
    public static $template;
49
50
    public static $rootDir;
51
    public static $logDir;
52
    public static $tempDir;
53
    public static $dataDir;
54
    public static $courseDir;
55
    public static $configDir;
56
    public static $assets;
57
    public static $htmlEditor;
58
    public static $twig;
59
    public static $roles;
60
    /** @var string */
61
    public static $legacyTemplate = '@ChamiloTheme/Layout/layout_one_col.html.twig';
62
    private static $settingsManager;
63
    private static $userManager;
64
    private static $siteManager;
65
66
    /**
67
     * @param ContainerInterface $container
68
     */
69
    public static function setContainer($container)
70
    {
71
        self::$container = $container;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public static function getConfigDir()
78
    {
79
        return self::$configDir;
80
    }
81
82
    /**
83
     * @param string $parameter
84
     *
85
     * @return mixed
86
     */
87
    public static function getParameter($parameter)
88
    {
89
        if (self::$container->hasParameter($parameter)) {
90
            return self::$container->getParameter($parameter);
91
        }
92
93
        return false;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public static function getEnvironment()
100
    {
101
        return self::$container->get('kernel')->getEnvironment();
102
    }
103
104
    /**
105
     * @return RoleHierarchy
106
     */
107
    public static function getRoles()
108
    {
109
        return self::$container->get('security.role_hierarchy');
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public static function getLogDir()
116
    {
117
        return self::$container->get('kernel')->getLogDir();
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    public static function getTempDir()
124
    {
125
        return self::$container->get('kernel')->getCacheDir().'/';
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public static function getRootDir()
132
    {
133
        if (isset(self::$container)) {
134
            return self::$container->get('kernel')->getRealRootDir();
135
        }
136
137
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public static function getUrlAppend()
144
    {
145
        return self::$container->get('kernel')->getUrlAppend();
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public static function isInstalled()
152
    {
153
        return self::$container->get('kernel')->isInstalled();
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public static function getDataDir()
160
    {
161
        return self::$dataDir;
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public static function getCourseDir()
168
    {
169
        return self::$courseDir;
170
    }
171
172
    /**
173
     * @return \Twig_Environment
174
     */
175
    public static function getTwig()
176
    {
177
        return self::$container->get('twig');
178
    }
179
180
    /**
181
     * @return \Symfony\Bundle\TwigBundle\TwigEngine
182
     */
183
    public static function getTemplating()
184
    {
185
        return self::$container->get('templating');
186
    }
187
188
    /**
189
     * @return Editor
190
     */
191
    public static function getHtmlEditor()
192
    {
193
        return self::$container->get('chamilo_core.html_editor');
194
    }
195
196
    /**
197
     * @deprecated
198
     *
199
     * @return \Symfony\Bundle\FrameworkBundle\Routing\Router
200
     */
201
    public static function getUrlGenerator()
202
    {
203
        return self::$container->get('router.default');
204
    }
205
206
    /**
207
     * @return object|Request
208
     */
209
    public static function getRequest()
210
    {
211
        if (!empty(self::$request)) {
212
            return self::$request;
213
        }
214
215
        return self::$container->get('request_stack');
216
    }
217
218
    public static function setRequest($request)
219
    {
220
        self::$request = $request;
221
    }
222
223
    /**
224
     * @return Session|false
225
     */
226
    public static function getSession()
227
    {
228
        if (self::$container && self::$container->has('session')) {
229
            return self::$container->get('session');
230
        }
231
232
        return false;
233
    }
234
235
    /**
236
     * @return AuthorizationChecker
237
     */
238
    public static function getAuthorizationChecker()
239
    {
240
        return self::$container->get('security.authorization_checker');
241
    }
242
243
    /**
244
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
245
     */
246
    public static function getTokenStorage()
247
    {
248
        return self::$container->get('security.token_storage');
249
    }
250
251
    /**
252
     * @return TranslatorInterface
253
     */
254
    public static function getTranslator()
255
    {
256
        if (isset(self::$translator)) {
257
            return self::$translator;
258
        }
259
260
        if (self::$container) {
261
            return self::$container->get('translator');
262
        }
263
264
        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\Contracts\Translation\TranslatorInterface.
Loading history...
265
    }
266
267
    /**
268
     * @return AssetsHelper
269
     */
270
    public static function getAsset()
271
    {
272
        return self::$container->get('templating.helper.assets');
273
    }
274
275
    /**
276
     * @return \Swift_Mailer
277
     */
278
    public static function getMailer()
279
    {
280
        return self::$container->get('mailer');
281
    }
282
283
    /**
284
     * @return \Elao\WebProfilerExtraBundle\TwigProfilerEngine
285
     */
286
    public static function getTemplate()
287
    {
288
        return self::$container->get('templating');
289
    }
290
291
    /**
292
     * @return SettingsManager
293
     */
294
    public static function getSettingsManager()
295
    {
296
        return self::$settingsManager;
297
    }
298
299
    /**
300
     * @param SettingsManager $manager
301
     */
302
    public static function setSettingsManager($manager)
303
    {
304
        self::$settingsManager = $manager;
305
    }
306
307
    /**
308
     * @return \Chamilo\CourseBundle\Manager\SettingsManager
309
     */
310
    public static function getCourseSettingsManager()
311
    {
312
        return self::$container->get('chamilo_course.settings.manager');
313
    }
314
315
    /**
316
     * @return \Doctrine\ORM\EntityManager
317
     */
318
    public static function getEntityManager()
319
    {
320
        return \Database::getManager();
321
    }
322
323
    /**
324
     * @return UserManager
325
     */
326
    public static function getUserManager()
327
    {
328
        return self::$userManager;
329
    }
330
331
    /**
332
     * @return CDocumentRepository
333
     */
334
    public static function getDocumentRepository()
335
    {
336
        return self::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository');
337
    }
338
339
    /**
340
     * @return CQuizRepository
341
     */
342
    public static function getExerciseRepository()
343
    {
344
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizRepository');
345
    }
346
347
    /**
348
     * @return CExerciseCategoryRepository
349
     */
350
    public static function getExerciseCategoryRepository()
351
    {
352
        return self::$container->get('Chamilo\CourseBundle\Repository\CExerciseCategoryRepository');
353
    }
354
355
    /**
356
     * @return CQuizQuestionCategoryRepository
357
     */
358
    public static function getQuestionCategoryRepository()
359
    {
360
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository');
361
    }
362
363
    /**
364
     * @return AccessUrlRepository
365
     */
366
    public static function getAccessUrlRepository()
367
    {
368
        return self::$container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
369
    }
370
371
    /**
372
     * @param $manager UserManager
373
     */
374
    public static function setUserManager($manager)
375
    {
376
        self::$userManager = $manager;
377
    }
378
379
    /**
380
     * @return SiteManager
381
     */
382
    public static function getSiteManager()
383
    {
384
        return self::$siteManager;
385
    }
386
387
    /**
388
     * @param $manager UserManager
389
     */
390
    public static function setSiteManager($manager)
391
    {
392
        self::$siteManager = $manager;
393
    }
394
395
    /**
396
     * @return \Sonata\UserBundle\Entity\GroupManager
397
     */
398
    public static function getGroupManager()
399
    {
400
        return self::$container->get('fos_user.group_manager');
401
    }
402
403
    /**
404
     * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
405
     */
406
    public static function getEventDispatcher()
407
    {
408
        return self::$container->get('event_dispatcher');
409
    }
410
411
    /**
412
     * @return \Symfony\Component\Form\FormFactory
413
     */
414
    public static function getFormFactory()
415
    {
416
        return self::$container->get('form.factory');
417
    }
418
419
    /**
420
     * @param string $message
421
     * @param string $type    error|success|warning|danger
422
     */
423
    public static function addFlash($message, $type = 'success')
424
    {
425
        $session = self::getSession();
426
        $session->getFlashBag()->add($type, $message);
427
    }
428
429
    /**
430
     * @return object|\Symfony\Cmf\Component\Routing\ChainRouter
431
     */
432
    public static function getRouter()
433
    {
434
        return self::$container->get('router');
435
    }
436
437
    /**
438
     * @return ToolChain
439
     */
440
    public static function getToolChain()
441
    {
442
        return self::$container->get('chamilo_core.tool_chain');
443
    }
444
445
    /**
446
     * @param ContainerInterface $container
447
     * @param bool               $setSession
448
     */
449
    public static function setLegacyServices($container, $setSession = true)
450
    {
451
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
452
        $em = $container->get('doctrine.orm.entity_manager');
453
        \Database::setManager($em);
454
        \CourseManager::setEntityManager($em);
455
456
        self::setSettingsManager($container->get('chamilo.settings.manager'));
457
        self::setUserManager($container->get('fos_user.user_manager'));
458
        self::setSiteManager($container->get('sonata.page.manager.site'));
459
460
        \CourseManager::setCourseSettingsManager($container->get('chamilo_course.settings.manager'));
461
        \CourseManager::setCourseManager($container->get('chamilo_core.entity.manager.course_manager'));
462
463
        // Setting course tool chain (in order to create tools to a course)
464
        \CourseManager::setToolList($container->get('chamilo_core.tool_chain'));
465
466
        if ($setSession) {
467
            self::$session = $container->get('session');
468
        }
469
        // Setting legacy properties.
470
        self::$dataDir = $container->get('kernel')->getDataDir();
471
        self::$courseDir = $container->get('kernel')->getDataDir();
472
    }
473
474
    /**
475
     * Gets a sonata page.
476
     *
477
     * @param string $slug
478
     *
479
     * @return Page
480
     */
481
    public static function getPage($slug)
482
    {
483
        $container = self::$container;
484
        /*$siteSelector = $container->get('sonata.page.site.selector');
485
        $site = $siteSelector->retrieve();*/
486
        $siteManager = $container->get('sonata.page.manager.site');
487
        $request = Container::getRequest();
488
        $page = null;
489
        if ($request) {
490
            $host = $request->getHost();
491
            $criteria = [
492
                'locale' => $request->getLocale(),
493
                'host' => $host,
494
            ];
495
            $site = $siteManager->findOneBy($criteria);
496
497
            $pageManager = $container->get('sonata.page.manager.page');
498
            // Parents only of homepage
499
            $criteria = ['site' => $site, 'enabled' => true, 'slug' => $slug];
500
            /** @var Page $page */
501
            $page = $pageManager->findOneBy($criteria);
502
503
            return $page;
504
        }
505
506
        return $page;
507
    }
508
509
    /**
510
     * @param string $class
511
     *
512
     * @throws \Exception
513
     *
514
     * @return HookEventInterface
515
     */
516
    public static function instantiateHook(string $class): HookEventInterface
517
    {
518
        return self::$container->get('chamilo_core.hook_factory')->build($class);
519
    }
520
}
521