Passed
Push — master ( ff7940...243d80 )
by Julito
16:14
created

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