Completed
Push — master ( 5627cd...c7e907 )
by Julito
15:36
created

Container::setLegacyServices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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