Completed
Push — master ( 9dadb1...edd250 )
by Julito
12:09 queued 19s
created

Container::getRoles()   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
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Framework;
6
7
use Chamilo\CoreBundle\Component\Editor\Editor;
8
use Chamilo\CoreBundle\Manager\SettingsManager;
9
use Chamilo\CoreBundle\Repository\AccessUrlRepository;
10
use Chamilo\CoreBundle\Repository\CourseCategoryRepository;
11
use Chamilo\CoreBundle\Repository\CourseRepository;
12
use Chamilo\CoreBundle\Repository\IllustrationRepository;
13
use Chamilo\CoreBundle\Repository\SequenceRepository;
14
use Chamilo\CoreBundle\Repository\SequenceResourceRepository;
15
use Chamilo\CoreBundle\Repository\SessionRepository;
16
use Chamilo\CoreBundle\Repository\UserRepository;
17
use Chamilo\CoreBundle\ToolChain;
18
use Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository;
19
use Chamilo\CourseBundle\Repository\CAnnouncementRepository;
20
use Chamilo\CourseBundle\Repository\CAttendanceRepository;
21
use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository;
22
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
23
use Chamilo\CourseBundle\Repository\CDocumentRepository;
24
use Chamilo\CourseBundle\Repository\CExerciseCategoryRepository;
25
use Chamilo\CourseBundle\Repository\CForumAttachmentRepository;
26
use Chamilo\CourseBundle\Repository\CForumCategoryRepository;
27
use Chamilo\CourseBundle\Repository\CForumForumRepository;
28
use Chamilo\CourseBundle\Repository\CForumPostRepository;
29
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
30
use Chamilo\CourseBundle\Repository\CGroupCategoryRepository;
31
use Chamilo\CourseBundle\Repository\CGroupRepository;
32
use Chamilo\CourseBundle\Repository\CLinkCategoryRepository;
33
use Chamilo\CourseBundle\Repository\CLinkRepository;
34
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
35
use Chamilo\CourseBundle\Repository\CLpRepository;
36
use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository;
37
use Chamilo\CourseBundle\Repository\CQuizQuestionRepository;
38
use Chamilo\CourseBundle\Repository\CQuizRepository;
39
use Chamilo\CourseBundle\Repository\CShortcutRepository;
40
use Chamilo\CourseBundle\Repository\CStudentPublicationAssignmentRepository;
41
use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository;
42
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
43
use Chamilo\CourseBundle\Repository\CThematicAdvanceRepository;
44
use Chamilo\CourseBundle\Repository\CThematicPlanRepository;
45
use Chamilo\CourseBundle\Repository\CThematicRepository;
46
use Symfony\Component\DependencyInjection\ContainerInterface;
47
use Symfony\Component\HttpFoundation\Request;
48
use Symfony\Component\HttpFoundation\Session\Session;
49
use Symfony\Component\Routing\Router;
50
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
51
use Symfony\Component\Security\Core\Role\RoleHierarchy;
52
use Symfony\Contracts\Translation\TranslatorInterface;
53
use Twig\Environment;
54
55
/**
56
 * Class Container
57
 * This class is a way to access Symfony2 services in legacy Chamilo code.
58
 */
59
class Container
60
{
61
    /**
62
     * @var ContainerInterface
63
     */
64
    public static $container;
65
    public static $session;
66
    public static $request;
67
    public static $configuration;
68
    public static $environment;
69
    public static $urlGenerator;
70
    public static $checker;
71
    /** @var TranslatorInterface */
72
    public static $translator;
73
    public static $mailer;
74
    public static $template;
75
76
    public static $rootDir;
77
    public static $logDir;
78
    public static $tempDir;
79
    public static $dataDir;
80
    public static $courseDir;
81
    public static $assets;
82
    public static $htmlEditor;
83
    public static $twig;
84
    public static $roles;
85
    /** @var string */
86
    public static $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig';
87
    //private static $settingsManager;
88
    //private static $userManager;
89
    //private static $siteManager;
90
91
    /**
92
     * @param ContainerInterface $container
93
     */
94
    public static function setContainer($container)
95
    {
96
        self::$container = $container;
97
    }
98
99
    /**
100
     * @param string $parameter
101
     */
102
    public static function getParameter($parameter)
103
    {
104
        if (self::$container->hasParameter($parameter)) {
105
            return self::$container->getParameter($parameter);
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public static function getEnvironment()
115
    {
116
        return self::$container->get('kernel')->getEnvironment();
117
    }
118
119
    /**
120
     * @return RoleHierarchy
121
     */
122
    public static function getRoles()
123
    {
124
        return self::$container->get('security.role_hierarchy');
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public static function getLogDir()
131
    {
132
        return self::$container->get('kernel')->getLogDir();
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public static function getCacheDir()
139
    {
140
        return self::$container->get('kernel')->getCacheDir().'/';
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public static function getProjectDir()
147
    {
148
        if (isset(self::$container)) {
149
            return self::$container->get('kernel')->getProjectDir().'/';
150
        }
151
152
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public static function isInstalled()
159
    {
160
        return self::$container->get('kernel')->isInstalled();
161
    }
162
163
    /**
164
     * @return Environment
165
     */
166
    public static function getTwig()
167
    {
168
        return self::$container->get('twig');
169
    }
170
171
    /**
172
     * @return object|Environment|null
173
     */
174
    public static function getTemplating()
175
    {
176
        return self::$container->get('twig');
177
    }
178
179
    /**
180
     * @return Editor
181
     */
182
    public static function getHtmlEditor()
183
    {
184
        return self::$container->get('chamilo_core.html_editor');
185
    }
186
187
    /**
188
     * @return object|Request
189
     */
190
    public static function getRequest()
191
    {
192
        if (null === self::$container) {
193
            return null;
194
        }
195
196
        if (!empty(self::$request)) {
197
            return self::$request;
198
        }
199
200
        return self::$container->get('request_stack');
201
    }
202
203
    /**
204
     * @param Request $request
205
     */
206
    public static function setRequest($request)
207
    {
208
        self::$request = $request;
209
    }
210
211
    /**
212
     * @return Session|false
213
     */
214
    public static function getSession()
215
    {
216
        if (self::$container && self::$container->has('session')) {
217
            return self::$container->get('session');
218
        }
219
220
        return false;
221
    }
222
223
    /**
224
     * @return AuthorizationChecker
225
     */
226
    public static function getAuthorizationChecker()
227
    {
228
        return self::$container->get('security.authorization_checker');
229
    }
230
231
    /**
232
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
233
     */
234
    public static function getTokenStorage()
235
    {
236
        return self::$container->get('security.token_storage');
237
    }
238
239
    /**
240
     * @return TranslatorInterface
241
     */
242
    public static function getTranslator()
243
    {
244
        if (isset(self::$translator)) {
245
            return self::$translator;
246
        }
247
248
        if (self::$container) {
249
            return self::$container->get('translator');
250
        }
251
252
        return false;
253
    }
254
255
    public static function getMailer()
256
    {
257
        return self::$container->get('Symfony\Component\Mailer\Mailer');
258
    }
259
260
    /**
261
     * @return SettingsManager
262
     */
263
    public static function getSettingsManager()
264
    {
265
        return self::$container->get('chamilo.settings.manager');
266
    }
267
268
    /*public static function setSettingsManager($manager)
269
    {
270
        self::$settingsManager = $manager;
271
    }*/
272
273
    /**
274
     * @return \Chamilo\CourseBundle\Manager\SettingsManager
275
     */
276
    public static function getCourseSettingsManager()
277
    {
278
        return self::$container->get('Chamilo\CourseBundle\Manager\SettingsManager');
279
    }
280
281
    /**
282
     * @return \Doctrine\ORM\EntityManager
283
     */
284
    public static function getEntityManager()
285
    {
286
        return \Database::getManager();
287
    }
288
289
    public static function getUserManager()
290
    {
291
        return self::$container->get(UserRepository::class);
292
    }
293
294
    /**
295
     * @return CAttendanceRepository
296
     */
297
    public static function getAttendanceRepository()
298
    {
299
        return self::$container->get(CAttendanceRepository::class);
300
    }
301
302
    /**
303
     * @return CAnnouncementRepository
304
     */
305
    public static function getAnnouncementRepository()
306
    {
307
        return self::$container->get(CAnnouncementRepository::class);
308
    }
309
310
    /**
311
     * @return AccessUrlRepository
312
     */
313
    public static function getAccessUrlRepository()
314
    {
315
        return self::$container->get(AccessUrlRepository::class);
316
    }
317
318
    /**
319
     * @return CAnnouncementAttachmentRepository
320
     */
321
    public static function getAnnouncementAttachmentRepository()
322
    {
323
        return self::$container->get(CAnnouncementAttachmentRepository::class);
324
    }
325
326
    /**
327
     * @return CourseRepository
328
     */
329
    public static function getCourseRepository()
330
    {
331
        return self::$container->get(CourseRepository::class);
332
    }
333
334
    /**
335
     * @return SessionRepository
336
     */
337
    public static function getSessionRepository()
338
    {
339
        return self::$container->get(SessionRepository::class);
340
    }
341
342
    /**
343
     * @return CourseCategoryRepository|object|null
344
     */
345
    public static function getCourseCategoryRepository()
346
    {
347
        return self::$container->get(CourseCategoryRepository::class);
348
    }
349
350
    /**
351
     * @return CCalendarEventRepository
352
     */
353
    public static function getCalendarEventRepository()
354
    {
355
        return self::$container->get(CCalendarEventRepository::class);
356
    }
357
358
    /**
359
     * @return CCalendarEventAttachmentRepository
360
     */
361
    public static function getCalendarEventAttachmentRepository()
362
    {
363
        return self::$container->get(CCalendarEventAttachmentRepository::class);
364
    }
365
366
    /**
367
     * @return CDocumentRepository
368
     */
369
    public static function getDocumentRepository()
370
    {
371
        return self::$container->get(CDocumentRepository::class);
372
    }
373
374
    /**
375
     * @return CQuizRepository
376
     */
377
    public static function getExerciseRepository()
378
    {
379
        return self::$container->get(CQuizRepository::class);
380
    }
381
382
    /**
383
     * @return CExerciseCategoryRepository
384
     */
385
    public static function getExerciseCategoryRepository()
386
    {
387
        return self::$container->get(CExerciseCategoryRepository::class);
388
    }
389
390
    /**
391
     * @return CForumForumRepository
392
     */
393
    public static function getForumRepository()
394
    {
395
        return self::$container->get(CForumForumRepository::class);
396
    }
397
398
    /**
399
     * @return CForumCategoryRepository
400
     */
401
    public static function getForumCategoryRepository()
402
    {
403
        return self::$container->get(CForumCategoryRepository::class);
404
    }
405
406
    /**
407
     * @return CForumPostRepository
408
     */
409
    public static function getForumPostRepository()
410
    {
411
        return self::$container->get(CForumPostRepository::class);
412
    }
413
414
    /**
415
     * @return CForumAttachmentRepository
416
     */
417
    public static function getForumAttachmentRepository()
418
    {
419
        return self::$container->get(CForumAttachmentRepository::class);
420
    }
421
422
    /**
423
     * @return CForumThreadRepository
424
     */
425
    public static function getForumThreadRepository()
426
    {
427
        return self::$container->get(CForumThreadRepository::class);
428
    }
429
430
    /**
431
     * @return CGroupRepository
432
     */
433
    public static function getGroupRepository()
434
    {
435
        return self::$container->get(CGroupRepository::class);
436
    }
437
438
    /**
439
     * @return CGroupCategoryRepository
440
     */
441
    public static function getGroupCategoryRepository()
442
    {
443
        return self::$container->get(CGroupCategoryRepository::class);
444
    }
445
446
    /**
447
     * @return CQuizQuestionRepository
448
     */
449
    public static function getQuestionRepository()
450
    {
451
        return self::$container->get(CQuizQuestionRepository::class);
452
    }
453
454
    /**
455
     * @return CQuizQuestionCategoryRepository
456
     */
457
    public static function getQuestionCategoryRepository()
458
    {
459
        return self::$container->get(CQuizQuestionCategoryRepository::class);
460
    }
461
462
    /**
463
     * @return CLinkRepository
464
     */
465
    public static function getLinkRepository()
466
    {
467
        return self::$container->get(CLinkRepository::class);
468
    }
469
470
    /**
471
     * @return CLinkCategoryRepository
472
     */
473
    public static function getLinkCategoryRepository()
474
    {
475
        return self::$container->get(CLinkCategoryRepository::class);
476
    }
477
478
    /**
479
     * @return CLpRepository
480
     */
481
    public static function getLpRepository()
482
    {
483
        return self::$container->get(CLpRepository::class);
484
    }
485
486
    /**
487
     * @return CLpCategoryRepository
488
     */
489
    public static function getLpCategoryRepository()
490
    {
491
        return self::$container->get(CLpCategoryRepository::class);
492
    }
493
494
    /**
495
     * @return UserRepository
496
     */
497
    public static function getUserRepository()
498
    {
499
        return self::$container->get(UserRepository::class);
500
    }
501
502
    /**
503
     * @return IllustrationRepository
504
     */
505
    public static function getIllustrationRepository()
506
    {
507
        return self::$container->get(IllustrationRepository::class);
508
    }
509
510
    /**
511
     * @return CShortcutRepository
512
     */
513
    public static function getShortcutRepository()
514
    {
515
        return self::$container->get(CShortcutRepository::class);
516
    }
517
518
    /**
519
     * @return CStudentPublicationRepository
520
     */
521
    public static function getStudentPublicationRepository()
522
    {
523
        return self::$container->get(CStudentPublicationRepository::class);
524
    }
525
526
    /**
527
     * @return CStudentPublicationAssignmentRepository
528
     */
529
    public static function getStudentPublicationAssignmentRepository()
530
    {
531
        return self::$container->get(CStudentPublicationAssignmentRepository::class);
532
    }
533
534
    /**
535
     * @return CStudentPublicationCommentRepository
536
     */
537
    public static function getStudentPublicationCommentRepository()
538
    {
539
        return self::$container->get(CStudentPublicationCommentRepository::class);
540
    }
541
542
    /**
543
     * @return SequenceResourceRepository
544
     */
545
    public static function getSequenceResourceRepository()
546
    {
547
        return self::$container->get(SequenceResourceRepository::class);
548
    }
549
550
    /**
551
     * @return SequenceRepository
552
     */
553
    public static function getSequenceRepository()
554
    {
555
        return self::$container->get(SequenceRepository::class);
556
    }
557
558
    public static function getThematicRepository()
559
    {
560
        return self::$container->get(CThematicRepository::class);
561
    }
562
563
    public static function getThematicPlanRepository()
564
    {
565
        return self::$container->get(CThematicPlanRepository::class);
566
    }
567
568
    public static function getThematicAdvanceRepository()
569
    {
570
        return self::$container->get(CThematicAdvanceRepository::class);
571
    }
572
573
    /**
574
     * @return \Symfony\Component\Form\FormFactory
575
     */
576
    public static function getFormFactory()
577
    {
578
        return self::$container->get('form.factory');
579
    }
580
581
    /**
582
     * @param string $message
583
     * @param string $type    error|success|warning|danger
584
     */
585
    public static function addFlash($message, $type = 'success')
586
    {
587
        $session = self::getSession();
588
        $session->getFlashBag()->add($type, $message);
589
    }
590
591
    /**
592
     * @return Router
593
     */
594
    public static function getRouter()
595
    {
596
        return self::$container->get('router');
597
    }
598
599
    /**
600
     * @return ToolChain
601
     */
602
    public static function getToolChain()
603
    {
604
        return self::$container->get(ToolChain::class);
605
    }
606
607
    /**
608
     * @param ContainerInterface $container
609
     * @param bool               $setSession
610
     */
611
    public static function setLegacyServices($container, $setSession = true)
612
    {
613
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
614
        $em = $container->get('doctrine.orm.entity_manager');
615
        \Database::setManager($em);
616
        \CourseManager::setEntityManager($em);
617
        //self::setSettingsManager($container->get('chamilo.settings.manager'));
618
        //self::setUserManager($container->get('fos_user.user_manager'));
619
        \CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager'));
620
        // Setting course tool chain (in order to create tools to a course)
621
        \CourseManager::setToolList($container->get(ToolChain::class));
622
        if ($setSession) {
623
            self::$session = $container->get('session');
624
        }
625
    }
626
}
627