Completed
Push — master ( 6f5084...9b811e )
by Julito
14:55
created

Container::getProjectDir()   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 0
dl 0
loc 7
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\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\CStudentPublicationCommentRepository;
22
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
23
use Chamilo\PageBundle\Entity\Page;
24
use Chamilo\SettingsBundle\Manager\SettingsManager;
25
use Chamilo\UserBundle\Repository\UserRepository;
26
use Sonata\PageBundle\Entity\SiteManager;
27
use Sonata\UserBundle\Entity\UserManager;
28
use Symfony\Component\DependencyInjection\ContainerInterface;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Session\Session;
31
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
32
use Symfony\Component\Security\Core\Role\RoleHierarchy;
33
use Symfony\Contracts\Translation\TranslatorInterface;
34
35
/**
36
 * Class Container
37
 * This class is a way to access Symfony2 services in legacy Chamilo code.
38
 */
39
class Container
40
{
41
    /**
42
     * @var ContainerInterface
43
     */
44
    public static $container;
45
    public static $session;
46
    public static $request;
47
    public static $configuration;
48
    public static $environment;
49
    public static $urlGenerator;
50
    public static $checker;
51
    /** @var TranslatorInterface */
52
    public static $translator;
53
    public static $mailer;
54
    public static $template;
55
56
    public static $rootDir;
57
    public static $logDir;
58
    public static $tempDir;
59
    public static $dataDir;
60
    public static $courseDir;
61
    public static $assets;
62
    public static $htmlEditor;
63
    public static $twig;
64
    public static $roles;
65
    /** @var string */
66
    public static $legacyTemplate = '@ChamiloTheme/Layout/layout_one_col.html.twig';
67
    private static $settingsManager;
68
    private static $userManager;
69
    private static $siteManager;
70
71
    /**
72
     * @param ContainerInterface $container
73
     */
74
    public static function setContainer($container)
75
    {
76
        self::$container = $container;
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 getCacheDir()
121
    {
122
        return self::$container->get('kernel')->getCacheDir().'/';
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public static function getProjectDir()
129
    {
130
        if (isset(self::$container)) {
131
            return self::$container->get('kernel')->getProjectDir().'/';
132
        }
133
134
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public static function isInstalled()
141
    {
142
        return self::$container->get('kernel')->isInstalled();
143
    }
144
145
    /**
146
     * @return \Twig_Environment
147
     */
148
    public static function getTwig()
149
    {
150
        return self::$container->get('twig');
151
    }
152
153
    /**
154
     * @return \Symfony\Bundle\TwigBundle\TwigEngine
155
     */
156
    public static function getTemplating()
157
    {
158
        return self::$container->get('templating');
159
    }
160
161
    /**
162
     * @return Editor
163
     */
164
    public static function getHtmlEditor()
165
    {
166
        return self::$container->get('chamilo_core.html_editor');
167
    }
168
169
    /**
170
     * @return object|Request
171
     */
172
    public static function getRequest()
173
    {
174
        if (self::$container === null) {
175
            return null;
176
        }
177
178
        if (!empty(self::$request)) {
179
            return self::$request;
180
        }
181
182
        return self::$container->get('request_stack');
183
    }
184
185
    /**
186
     * @param Request $request
187
     */
188
    public static function setRequest($request)
189
    {
190
        self::$request = $request;
191
    }
192
193
    /**
194
     * @return Session|false
195
     */
196
    public static function getSession()
197
    {
198
        if (self::$container && self::$container->has('session')) {
199
            return self::$container->get('session');
200
        }
201
202
        return false;
203
    }
204
205
    /**
206
     * @return AuthorizationChecker
207
     */
208
    public static function getAuthorizationChecker()
209
    {
210
        return self::$container->get('security.authorization_checker');
211
    }
212
213
    /**
214
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
215
     */
216
    public static function getTokenStorage()
217
    {
218
        return self::$container->get('security.token_storage');
219
    }
220
221
    /**
222
     * @return TranslatorInterface
223
     */
224
    public static function getTranslator()
225
    {
226
        if (isset(self::$translator)) {
227
            return self::$translator;
228
        }
229
230
        if (self::$container) {
231
            return self::$container->get('translator');
232
        }
233
234
        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...
235
    }
236
237
    public static function getMailer()
238
    {
239
        return self::$container->get('Symfony\Component\Mailer\Mailer');
240
    }
241
242
    /**
243
     * @return \Elao\WebProfilerExtraBundle\TwigProfilerEngine
244
     */
245
    public static function getTemplate()
246
    {
247
        return self::$container->get('templating');
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 CQuizQuestionRepository
316
     */
317
    public static function getQuestionRepository()
318
    {
319
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionRepository');
320
    }
321
322
    /**
323
     * @return CQuizQuestionCategoryRepository
324
     */
325
    public static function getQuestionCategoryRepository()
326
    {
327
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository');
328
    }
329
330
    /**
331
     * @return CLpRepository
332
     */
333
    public static function getLpRepository()
334
    {
335
        return self::$container->get('Chamilo\CourseBundle\Repository\CLpRepository');
336
    }
337
338
    /**
339
     * @return CLpCategoryRepository
340
     */
341
    public static function getLpCategoryRepository()
342
    {
343
        return self::$container->get('Chamilo\CourseBundle\Repository\CLpCategoryRepository');
344
    }
345
346
    /**
347
     * @return AccessUrlRepository
348
     */
349
    public static function getAccessUrlRepository()
350
    {
351
        return self::$container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
352
    }
353
354
    /**
355
     * @return CourseRepository
356
     */
357
    public static function getCourseRepository()
358
    {
359
        return self::$container->get('Chamilo\CoreBundle\Repository\CourseRepository');
360
    }
361
362
    /**
363
     * @return UserRepository
364
     */
365
    public static function getUserRepository()
366
    {
367
        return self::$container->get('Chamilo\UserBundle\Repository\UserRepository');
368
    }
369
370
    /**
371
     * @return CourseCategoryRepository|object|null
372
     */
373
    public static function getCourseCategoryRepository()
374
    {
375
        return self::$container->get('Chamilo\CoreBundle\Repository\CourseCategoryRepository');
376
    }
377
378
    /**
379
     * @return IllustrationRepository
380
     */
381
    public static function getIllustrationRepository()
382
    {
383
        return self::$container->get('Chamilo\CoreBundle\Repository\IllustrationRepository');
384
    }
385
386
    /**
387
     * @return CStudentPublicationRepository
388
     */
389
    public static function getStudentPublicationRepository()
390
    {
391
        return self::$container->get('Chamilo\CourseBundle\Repository\CStudentPublicationRepository');
392
    }
393
394
    /**
395
     * @return CStudentPublicationAssignmentRepository
396
     */
397
    public static function getStudentPublicationAssignmentRepository()
398
    {
399
        return self::$container->get('Chamilo\CourseBundle\Repository\CStudentPublicationAssignmentRepository');
400
    }
401
402
    /**
403
     * @return CStudentPublicationCommentRepository
404
     */
405
    public static function getStudentPublicationCommentRepository()
406
    {
407
        return self::$container->get('Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository');
408
    }
409
410
    /**
411
     * @param $manager UserManager
412
     */
413
    public static function setUserManager($manager)
414
    {
415
        self::$userManager = $manager;
416
    }
417
418
    /**
419
     * @return SiteManager
420
     */
421
    public static function getSiteManager()
422
    {
423
        return self::$siteManager;
424
    }
425
426
    /**
427
     * @param $manager UserManager
428
     */
429
    public static function setSiteManager($manager)
430
    {
431
        self::$siteManager = $manager;
432
    }
433
434
    /**
435
     * @return \Sonata\UserBundle\Entity\GroupManager
436
     */
437
    public static function getGroupManager()
438
    {
439
        return self::$container->get('fos_user.group_manager');
440
    }
441
442
    /**
443
     * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
444
     */
445
    public static function getEventDispatcher()
446
    {
447
        return self::$container->get('event_dispatcher');
448
    }
449
450
    /**
451
     * @return \Symfony\Component\Form\FormFactory
452
     */
453
    public static function getFormFactory()
454
    {
455
        return self::$container->get('form.factory');
456
    }
457
458
    /**
459
     * @param string $message
460
     * @param string $type    error|success|warning|danger
461
     */
462
    public static function addFlash($message, $type = 'success')
463
    {
464
        $session = self::getSession();
465
        $session->getFlashBag()->add($type, $message);
466
    }
467
468
    /**
469
     * @return object|\Symfony\Cmf\Component\Routing\ChainRouter
470
     */
471
    public static function getRouter()
472
    {
473
        return self::$container->get('router');
474
    }
475
476
    /**
477
     * @return ToolChain
478
     */
479
    public static function getToolChain()
480
    {
481
        return self::$container->get(ToolChain::class);
482
    }
483
484
    /**
485
     * @param ContainerInterface $container
486
     * @param bool               $setSession
487
     */
488
    public static function setLegacyServices($container, $setSession = true)
489
    {
490
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
491
        $em = $container->get('doctrine.orm.entity_manager');
492
        \Database::setManager($em);
493
        \CourseManager::setEntityManager($em);
494
495
        self::setSettingsManager($container->get('chamilo.settings.manager'));
496
        self::setUserManager($container->get('fos_user.user_manager'));
497
        self::setSiteManager($container->get('sonata.page.manager.site'));
498
499
        \CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager'));
500
        // Setting course tool chain (in order to create tools to a course)
501
        \CourseManager::setToolList($container->get(ToolChain::class));
502
503
        if ($setSession) {
504
            self::$session = $container->get('session');
505
        }
506
    }
507
508
    /**
509
     * Gets a sonata page.
510
     *
511
     * @param string $slug
512
     *
513
     * @return Page
514
     */
515
    public static function getPage($slug)
516
    {
517
        $container = self::$container;
518
        /*$siteSelector = $container->get('sonata.page.site.selector');
519
        $site = $siteSelector->retrieve();*/
520
        $siteManager = $container->get('sonata.page.manager.site');
521
        $request = Container::getRequest();
522
        $page = null;
523
        if ($request) {
524
            $host = $request->getHost();
525
            $criteria = [
526
                'locale' => $request->getLocale(),
527
                'host' => $host,
528
            ];
529
            $site = $siteManager->findOneBy($criteria);
530
531
            $pageManager = $container->get('sonata.page.manager.page');
532
            // Parents only of homepage
533
            $criteria = ['site' => $site, 'enabled' => true, 'slug' => $slug];
534
            /** @var Page $page */
535
            $page = $pageManager->findOneBy($criteria);
536
537
            return $page;
538
        }
539
540
        return $page;
541
    }
542
543
    /**
544
     * @throws \Exception
545
     */
546
    public static function instantiateHook(string $class): HookEventInterface
547
    {
548
        return self::$container->get('chamilo_core.hook_factory')->build($class);
549
    }
550
}
551