Passed
Push — master ( 8cd85a...5627cd )
by Julito
09:53 queued 10s
created

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