AppKernel.php 5.92 KB
Newer Older
1
<?php
2
/**
3
 * 2007-2020 PrestaShop and Contributors
4
5
6
7
8
9
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
10
 * https://opensource.org/licenses/OSL-3.0
11
12
13
14
15
16
17
18
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
Thomas Nabord's avatar
Thomas Nabord committed
19
 * needs please refer to https://www.prestashop.com for more information.
20
21
 *
 * @author    PrestaShop SA <contact@prestashop.com>
22
 * @copyright 2007-2020 PrestaShop SA and Contributors
23
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
24
25
26
 * International Registered Trademark & Property of PrestaShop SA
 */

27
use PrestaShopBundle\Kernel\ModuleRepositoryFactory;
28
use Symfony\Component\Config\Loader\LoaderInterface;
Mickaël Andrieu's avatar
Mickaël Andrieu committed
29
use Symfony\Component\DependencyInjection\ContainerBuilder;
MathiasReker's avatar
MathiasReker committed
30
use Symfony\Component\HttpKernel\Kernel;
31
32
33

class AppKernel extends Kernel
{
Thomas Baccelli's avatar
Thomas Baccelli committed
34
    const VERSION = '1.7.6.9';
35
36
    const MAJOR_VERSION_STRING = '1.7';
    const MAJOR_VERSION = 17;
Pablo Borowicz's avatar
Pablo Borowicz committed
37
    const MINOR_VERSION = 6;
Thomas Baccelli's avatar
Thomas Baccelli committed
38
    const RELEASE_VERSION = 9;
39

40
41
42
    /**
     * @{inheritdoc}
     */
43
44
45
46
47
48
49
50
51
52
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
Xavier Gouley's avatar
Xavier Gouley committed
53
54
            // PrestaShop Core bundle
            new PrestaShopBundle\PrestaShopBundle(),
55
56
            // PrestaShop Translation parser
            new PrestaShop\TranslationToolsBundle\TranslationToolsBundle(),
Mickaël Andrieu's avatar
Mickaël Andrieu committed
57
            // REST API consumer
58
            new Csa\Bundle\GuzzleBundle\CsaGuzzleBundle(),
Šarūnas Jonušas's avatar
Šarūnas Jonušas committed
59
            new League\Tactician\Bundle\TacticianBundle(),
60
61
        );

Mickaël Andrieu's avatar
Mickaël Andrieu committed
62
        if (in_array($this->getEnvironment(), array('dev', 'test'), true)) {
63
64
65
66
67
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        }

68
69
70
71
        if ('dev' === $this->getEnvironment()) {
            $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
        }

72
        /* Will not work until PrestaShop is installed */
73
74
        $activeModules = $this->getActiveModules();
        if (!empty($activeModules)) {
75
            try {
76
                $this->enableComposerAutoloaderOnModules($activeModules);
77
78
            } catch (\Exception $e) {
            }
79
80
        }

81
82
83
        return $bundles;
    }

84
85
86
87
88
89
90
    /**
     * @{inheritdoc}
     */
    protected function getKernelParameters()
    {
        $kernelParameters = parent::getKernelParameters();

91
92
        return array_merge(
            $kernelParameters,
93
            array('kernel.active_modules' => $this->getActiveModules())
94
95
96
        );
    }

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    /**
     * @{inheritdoc}
     */
    public function getRootDir()
    {
        return __DIR__;
    }

    /**
     * @{inheritdoc}
     */
    public function getCacheDir()
    {
        return dirname(__DIR__).'/var/cache/'.$this->getEnvironment();
    }

    /**
     * @{inheritdoc}
     */
    public function getLogDir()
    {
        return dirname(__DIR__).'/var/logs';
    }

121
122
    /**
     * @{inheritdoc}
123
     * @throws \Exception
124
     */
125
126
    public function registerContainerConfiguration(LoaderInterface $loader)
    {
Mickaël Andrieu's avatar
Mickaël Andrieu committed
127
128
129
130
131
132
        $loader->load(function (ContainerBuilder $container) {
            $container->setParameter('container.autowiring.strict_mode', true);
            $container->setParameter('container.dumper.inline_class_loader', false);
            $container->addObjectResource($this);
        });

133
134
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
135
136
137
138
139
140
141
142

    /**
     * Return all active modules.
     *
     * @return array list of modules names.
     */
    private function getActiveModules()
    {
143
144
145
146
147
148
        $activeModules = [];
        try {
            if ($modulesRepository = ModuleRepositoryFactory::getInstance()->getRepository()) {
                $activeModules = $modulesRepository->getActiveModules();
            }
        } catch (\Exception $e) {
149
150
            //Do nothing because the modules retrieval must not block the kernel, and it won't work
            //during the installation process
151
152
153
        }


154
        return $activeModules;
155
    }
156
157
158
159
160

    /**
     * Enable auto loading of module Composer autoloader if needed.
     * Need to be done as earlier as possible in application lifecycle.
     *
161
162
163
     * Note: this feature is also manage in PrestaShop\PrestaShop\Adapter\ContainerBuilder
     * for non Symfony environments.
     *
164
165
166
167
168
169
170
171
172
173
174
175
     * @param array $modules the list of modules
     */
    private function enableComposerAutoloaderOnModules($modules)
    {
        foreach ($modules as $module) {
            $autoloader = __DIR__.'/../modules/'.$module.'/vendor/autoload.php';

            if (file_exists($autoloader)) {
                include_once $autoloader;
            }
        }
    }
176
177
178
179
180
181
182
183
184
185
186
187
188

    /**
     * Gets the application root dir.
     * Override Kernel due to the fact that we remove the composer.json in
     * downloaded package. More we are not a framework and the root directory
     * should always be the parent of this file.
     *
     * @return string The project root dir
     */
    public function getProjectDir()
    {
        return realpath(__DIR__ . '/..');
    }
189
}