Completed
Push — master ( 079342...737906 )
by Julito
09:35
created

Container::getParameter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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