psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / src / Core / Kernel.php
blobe4566780cbe304898f54c4a0809aa1f1dba3271a
1 <?php
3 /**
4 * OpenEMR <https://open-emr.org>.
6 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
7 */
9 namespace OpenEMR\Core;
11 require_once dirname(__FILE__) . '/../../interface/globals.php';
13 use Symfony\Component\DependencyInjection\Reference;
14 use Symfony\Component\DependencyInjection\Definition;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
17 use Symfony\Component\EventDispatcher\EventDispatcher;
18 use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
20 /**
21 * Class Kernel.
23 * This is the core of OpenEMR. It is a thin class enabling service containers,
24 * event dispatching for now.
26 * @package OpenEMR
27 * @subpackage Core
28 * @author Robert Down <robertdown@live.com>
29 * @copyright Copyright (c) 2017 Robert Down
31 class Kernel
33 /** @var ContainerBuilder */
34 private $container;
36 public function __construct()
38 $this->prepareContainer();
41 /**
42 * Setup the initial container
44 private function prepareContainer()
46 if (!$this->container) {
47 $builder = new ContainerBuilder(new ParameterBag());
48 $builder->addCompilerPass(new RegisterListenersPass());
49 $definition = new Definition(EventDispatcher::class, [new Reference('service_container')]);
50 $definition->setPublic(true);
51 $builder->setDefinition('event_dispatcher', $definition);
52 $builder->compile();
53 $this->container = $builder;
57 /**
58 * Return true if the environment variable OPENEMR__ENVIRONMENT is set to dev.
60 * @return bool
62 public function isDev()
64 return (($_ENV['OPENEMR__ENVIRONMENT'] ?? '') === 'dev') ? true : false;
67 /**
68 * Get the Service Container
70 * @return ContainerBuilder
72 public function getContainer()
74 if (!$this->container) {
75 $this->prepareContainer();
78 return $this->container;
81 /**
82 * Get the Event Dispatcher
84 * @return EventDispatcher
85 * @throws \Exception
87 public function getEventDispatcher()
89 if ($this->container) {
90 /** @var EventDispatcher $dispatcher */
91 return $this->container->get('event_dispatcher');
92 } else {
93 throw new \Exception('Container does not exist');