Completed
Push — master ( cbfb7e...6f5084 )
by Julito
20:33
created

Container::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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