Completed
Push — master ( a3ace3...0ae69c )
by Julito
11:10
created

Container::addFlash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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\CourseBundle\Repository\CDocumentRepository;
8
use Chamilo\CourseBundle\Repository\CExerciseCategoryRepository;
9
use Chamilo\CourseBundle\Repository\CQuizCategoryRepository;
10
use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository;
11
use Chamilo\CourseBundle\Repository\CQuizRepository;
12
use Chamilo\PageBundle\Entity\Page;
13
use Chamilo\SettingsBundle\Manager\SettingsManager;
14
use Sonata\PageBundle\Entity\SiteManager;
15
use Sonata\UserBundle\Entity\UserManager;
16
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
17
use Symfony\Component\DependencyInjection\ContainerInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Session\Session;
20
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
21
use Symfony\Component\Security\Core\Role\RoleHierarchy;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
24
/**
25
 * Class Container
26
 * This class is a way to access Symfony2 services in legacy Chamilo code.
27
 *
28
 * @package Chamilo\CoreBundle\Framework
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
    public static function setRequest($request)
216
    {
217
        self::$request = $request;
218
    }
219
220
    /**
221
     * @return Session|false
222
     */
223
    public static function getSession()
224
    {
225
        if (self::$container && self::$container->has('session')) {
226
            return self::$container->get('session');
227
        }
228
229
        return false;
230
    }
231
232
    /**
233
     * @return AuthorizationChecker
234
     */
235
    public static function getAuthorizationChecker()
236
    {
237
        return self::$container->get('security.authorization_checker');
238
    }
239
240
    /**
241
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
242
     */
243
    public static function getTokenStorage()
244
    {
245
        return self::$container->get('security.token_storage');
246
    }
247
248
    /**
249
     * @return TranslatorInterface
250
     */
251
    public static function getTranslator()
252
    {
253
        if (isset(self::$translator)) {
254
            return self::$translator;
255
        }
256
257
        if (self::$container) {
258
            return self::$container->get('translator');
259
        }
260
261
        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...
262
    }
263
264
    /**
265
     * @return AssetsHelper
266
     */
267
    public static function getAsset()
268
    {
269
        return self::$container->get('templating.helper.assets');
270
    }
271
272
    /**
273
     * @return \Swift_Mailer
274
     */
275
    public static function getMailer()
276
    {
277
        return self::$container->get('mailer');
278
    }
279
280
    /**
281
     * @return \Elao\WebProfilerExtraBundle\TwigProfilerEngine
282
     */
283
    public static function getTemplate()
284
    {
285
        return self::$container->get('templating');
286
    }
287
288
    /**
289
     * @return SettingsManager
290
     */
291
    public static function getSettingsManager()
292
    {
293
        return self::$settingsManager;
294
    }
295
296
    /**
297
     * @param SettingsManager $manager
298
     */
299
    public static function setSettingsManager($manager)
300
    {
301
        self::$settingsManager = $manager;
302
    }
303
304
    /**
305
     * @return \Chamilo\CourseBundle\Manager\SettingsManager
306
     */
307
    public static function getCourseSettingsManager()
308
    {
309
        return self::$container->get('chamilo_course.settings.manager');
310
    }
311
312
    /**
313
     * @return \Doctrine\ORM\EntityManager
314
     */
315
    public static function getEntityManager()
316
    {
317
        return \Database::getManager();
318
    }
319
320
    /**
321
     * @return UserManager
322
     */
323
    public static function getUserManager()
324
    {
325
        return self::$userManager;
326
    }
327
328
    /**
329
     * @return CDocumentRepository
330
     */
331
    public static function getDocumentRepository()
332
    {
333
        return self::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository');
334
    }
335
336
    /**
337
     * @return CQuizRepository
338
     */
339
    public static function getExerciseRepository()
340
    {
341
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizRepository');
342
    }
343
344
    /**
345
     * @return CExerciseCategoryRepository
346
     */
347
    public static function getExerciseCategoryRepository()
348
    {
349
        return self::$container->get('Chamilo\CourseBundle\Repository\CExerciseCategoryRepository');
350
    }
351
352
    /**
353
     * @return CQuizQuestionCategoryRepository
354
     */
355
    public static function getQuestionCategoryRepository()
356
    {
357
        return self::$container->get('Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository');
358
    }
359
360
    /**
361
     * @param $manager UserManager
362
     */
363
    public static function setUserManager($manager)
364
    {
365
        self::$userManager = $manager;
366
    }
367
368
    /**
369
     * @return SiteManager
370
     */
371
    public static function getSiteManager()
372
    {
373
        return self::$siteManager;
374
    }
375
376
    /**
377
     * @param $manager UserManager
378
     */
379
    public static function setSiteManager($manager)
380
    {
381
        self::$siteManager = $manager;
382
    }
383
384
    /**
385
     * @return \Sonata\UserBundle\Entity\GroupManager
386
     */
387
    public static function getGroupManager()
388
    {
389
        return self::$container->get('fos_user.group_manager');
390
    }
391
392
    /**
393
     * @return \Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
394
     */
395
    public static function getEventDispatcher()
396
    {
397
        return self::$container->get('event_dispatcher');
398
    }
399
400
    /**
401
     * @return \Symfony\Component\Form\FormFactory
402
     */
403
    public static function getFormFactory()
404
    {
405
        return self::$container->get('form.factory');
406
    }
407
408
    /**
409
     * @param string $message
410
     * @param string $type    error|success|warning|danger
411
     */
412
    public static function addFlash($message, $type = 'success')
413
    {
414
        $session = self::getSession();
415
        $session->getFlashBag()->add($type, $message);
416
    }
417
418
    /**
419
     * @return object|\Symfony\Cmf\Component\Routing\ChainRouter
420
     */
421
    public static function getRouter()
422
    {
423
        return self::$container->get('router');
424
    }
425
426
    /**
427
     * @return \Chamilo\CourseBundle\ToolChain
428
     */
429
    public static function getToolChain()
430
    {
431
        return self::$container->get('chamilo_course.tool_chain');
432
    }
433
434
    /**
435
     * @param ContainerInterface $container
436
     * @param bool               $setSession
437
     */
438
    public static function setLegacyServices($container, $setSession = true)
439
    {
440
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
441
        $em = $container->get('doctrine.orm.entity_manager');
442
        \Database::setManager($em);
443
        \CourseManager::setEntityManager($em);
444
445
        self::setSettingsManager($container->get('chamilo.settings.manager'));
446
        self::setUserManager($container->get('fos_user.user_manager'));
447
        self::setSiteManager($container->get('sonata.page.manager.site'));
448
449
        \CourseManager::setCourseSettingsManager($container->get('chamilo_course.settings.manager'));
450
        \CourseManager::setCourseManager($container->get('chamilo_core.entity.manager.course_manager'));
451
452
        // Setting course tool chain (in order to create tools to a course)
453
        \CourseManager::setToolList($container->get('chamilo_course.tool_chain'));
454
455
        if ($setSession) {
456
            self::$session = $container->get('session');
457
        }
458
        // Setting legacy properties.
459
        self::$dataDir = $container->get('kernel')->getDataDir();
460
        self::$courseDir = $container->get('kernel')->getDataDir();
461
    }
462
463
    /**
464
     * Gets a sonata page.
465
     *
466
     * @param string $slug
467
     *
468
     * @return Page
469
     */
470
    public static function getPage($slug)
471
    {
472
        $container = self::$container;
473
        /*$siteSelector = $container->get('sonata.page.site.selector');
474
        $site = $siteSelector->retrieve();*/
475
        $siteManager = $container->get('sonata.page.manager.site');
476
        $request = Container::getRequest();
477
        $page = null;
478
        if ($request) {
479
            $host = $request->getHost();
480
            $criteria = [
481
                'locale' => $request->getLocale(),
482
                'host' => $host,
483
            ];
484
            $site = $siteManager->findOneBy($criteria);
485
486
            $pageManager = $container->get('sonata.page.manager.page');
487
            // Parents only of homepage
488
            $criteria = ['site' => $site, 'enabled' => true, 'slug' => $slug];
489
            /** @var Page $page */
490
            $page = $pageManager->findOneBy($criteria);
491
492
            return $page;
493
        }
494
495
        return $page;
496
    }
497
}
498