Completed
Push — master ( eedd1b...b75c6e )
by Julito
10:22
created

Container::getTemplate()   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
26
/**
27
 * Class Container
28
 * This class is a way to access Symfony2 services in legacy Chamilo code.
29
 */
30
class Container
31
{
32
    /**
33
     * @var ContainerInterface
34
     */
35
    public static $container;
36
    public static $session;
37
    public static $request;
38
    public static $configuration;
39
    public static $environment;
40
    public static $urlGenerator;
41
    public static $checker;
42
    /** @var TranslatorInterface */
43
    public static $translator;
44
    public static $mailer;
45
    public static $template;
46
47
    public static $rootDir;
48
    public static $logDir;
49
    public static $tempDir;
50
    public static $dataDir;
51
    public static $courseDir;
52
    public static $configDir;
53
    public static $assets;
54
    public static $htmlEditor;
55
    public static $twig;
56
    public static $roles;
57
    /** @var string */
58
    public static $legacyTemplate = '@ChamiloTheme/Layout/layout_one_col.html.twig';
59
    private static $settingsManager;
60
    private static $userManager;
61
    private static $siteManager;
62
63
    /**
64
     * @param ContainerInterface $container
65
     */
66
    public static function setContainer($container)
67
    {
68
        self::$container = $container;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public static function getConfigDir()
75
    {
76
        return self::$configDir;
77
    }
78
79
    /**
80
     * @param string $parameter
81
     *
82
     * @return mixed
83
     */
84
    public static function getParameter($parameter)
85
    {
86
        if (self::$container->hasParameter($parameter)) {
87
            return self::$container->getParameter($parameter);
88
        }
89
90
        return false;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public static function getEnvironment()
97
    {
98
        return self::$container->get('kernel')->getEnvironment();
99
    }
100
101
    /**
102
     * @return RoleHierarchy
103
     */
104
    public static function getRoles()
105
    {
106
        return self::$container->get('security.role_hierarchy');
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public static function getLogDir()
113
    {
114
        return self::$container->get('kernel')->getLogDir();
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public static function getTempDir()
121
    {
122
        return self::$container->get('kernel')->getCacheDir().'/';
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public static function getRootDir()
129
    {
130
        if (isset(self::$container)) {
131
            return self::$container->get('kernel')->getRealRootDir();
132
        }
133
134
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public static function getUrlAppend()
141
    {
142
        return self::$container->get('kernel')->getUrlAppend();
143
    }
144
145
    /**
146
     * @return string
147
     */
148
    public static function isInstalled()
149
    {
150
        return self::$container->get('kernel')->isInstalled();
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public static function getDataDir()
157
    {
158
        return self::$dataDir;
159
    }
160
161
    /**
162
     * @return string
163
     */
164
    public static function getCourseDir()
165
    {
166
        return self::$courseDir;
167
    }
168
169
    /**
170
     * @return \Twig_Environment
171
     */
172
    public static function getTwig()
173
    {
174
        return self::$container->get('twig');
175
    }
176
177
    /**
178
     * @return \Symfony\Bundle\TwigBundle\TwigEngine
179
     */
180
    public static function getTemplating()
181
    {
182
        return self::$container->get('templating');
183
    }
184
185
    /**
186
     * @return Editor
187
     */
188
    public static function getHtmlEditor()
189
    {
190
        return self::$container->get('chamilo_core.html_editor');
191
    }
192
193
    /**
194
     * @deprecated
195
     *
196
     * @return \Symfony\Bundle\FrameworkBundle\Routing\Router
197
     */
198
    public static function getUrlGenerator()
199
    {
200
        return self::$container->get('router.default');
201
    }
202
203
    /**
204
     * @return object|Request
205
     */
206
    public static function getRequest()
207
    {
208
        if (!empty(self::$request)) {
209
            return self::$request;
210
        }
211
212
        return self::$container->get('request_stack');
213
    }
214
215
    /**
216
     * @param $request
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
     * @return AccessUrlRepository
373
     */
374
    public static function getCourseRepository()
375
    {
376
        return self::$container->get('Chamilo\CoreBundle\Repository\CourseRepository');
377
    }
378
379
    /**
380
     * @param $manager UserManager
381
     */
382
    public static function setUserManager($manager)
383
    {
384
        self::$userManager = $manager;
385
    }
386
387
    /**
388
     * @return SiteManager
389
     */
390
    public static function getSiteManager()
391
    {
392
        return self::$siteManager;
393
    }
394
395
    /**
396
     * @param $manager UserManager
397
     */
398
    public static function setSiteManager($manager)
399
    {
400
        self::$siteManager = $manager;
401
    }
402
403
    /**
404
     * @return \Sonata\UserBundle\Entity\GroupManager
405
     */
406
    public static function getGroupManager()
407
    {
408
        return self::$container->get('fos_user.group_manager');
409
    }
410
411
    /**
412
     * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
413
     */
414
    public static function getEventDispatcher()
415
    {
416
        return self::$container->get('event_dispatcher');
417
    }
418
419
    /**
420
     * @return \Symfony\Component\Form\FormFactory
421
     */
422
    public static function getFormFactory()
423
    {
424
        return self::$container->get('form.factory');
425
    }
426
427
    /**
428
     * @param string $message
429
     * @param string $type    error|success|warning|danger
430
     */
431
    public static function addFlash($message, $type = 'success')
432
    {
433
        $session = self::getSession();
434
        $session->getFlashBag()->add($type, $message);
435
    }
436
437
    /**
438
     * @return object|\Symfony\Cmf\Component\Routing\ChainRouter
439
     */
440
    public static function getRouter()
441
    {
442
        return self::$container->get('router');
443
    }
444
445
    /**
446
     * @return ToolChain
447
     */
448
    public static function getToolChain()
449
    {
450
        return self::$container->get('chamilo_core.tool_chain');
451
    }
452
453
    /**
454
     * @param ContainerInterface $container
455
     * @param bool               $setSession
456
     */
457
    public static function setLegacyServices($container, $setSession = true)
458
    {
459
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
460
        $em = $container->get('doctrine.orm.entity_manager');
461
        \Database::setManager($em);
462
        \CourseManager::setEntityManager($em);
463
464
        self::setSettingsManager($container->get('chamilo.settings.manager'));
465
        self::setUserManager($container->get('fos_user.user_manager'));
466
        self::setSiteManager($container->get('sonata.page.manager.site'));
467
468
        \CourseManager::setCourseSettingsManager($container->get('chamilo_course.settings.manager'));
469
        // Setting course tool chain (in order to create tools to a course)
470
        \CourseManager::setToolList($container->get('chamilo_core.tool_chain'));
471
472
        if ($setSession) {
473
            self::$session = $container->get('session');
474
        }
475
        // Setting legacy properties.
476
        self::$dataDir = $container->get('kernel')->getDataDir();
477
        self::$courseDir = $container->get('kernel')->getDataDir();
478
    }
479
480
    /**
481
     * Gets a sonata page.
482
     *
483
     * @param string $slug
484
     *
485
     * @return Page
486
     */
487
    public static function getPage($slug)
488
    {
489
        $container = self::$container;
490
        /*$siteSelector = $container->get('sonata.page.site.selector');
491
        $site = $siteSelector->retrieve();*/
492
        $siteManager = $container->get('sonata.page.manager.site');
493
        $request = Container::getRequest();
494
        $page = null;
495
        if ($request) {
496
            $host = $request->getHost();
497
            $criteria = [
498
                'locale' => $request->getLocale(),
499
                'host' => $host,
500
            ];
501
            $site = $siteManager->findOneBy($criteria);
502
503
            $pageManager = $container->get('sonata.page.manager.page');
504
            // Parents only of homepage
505
            $criteria = ['site' => $site, 'enabled' => true, 'slug' => $slug];
506
            /** @var Page $page */
507
            $page = $pageManager->findOneBy($criteria);
508
509
            return $page;
510
        }
511
512
        return $page;
513
    }
514
515
    /**
516
     * @param string $class
517
     *
518
     * @throws \Exception
519
     *
520
     * @return HookEventInterface
521
     */
522
    public static function instantiateHook(string $class): HookEventInterface
523
    {
524
        return self::$container->get('chamilo_core.hook_factory')->build($class);
525
    }
526
}
527