Completed
Push — master ( a8dd9d...76a038 )
by Julito
11:51
created

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