PageRenderTime 108ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/app/AppKernel.php

https://gitlab.com/jankube/mautic
PHP | 436 lines | 261 code | 64 blank | 111 comment | 46 complexity | 626793d7859cc882fa0850e513e19a24 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2014 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. use Symfony\Component\Config\Loader\LoaderInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\HttpKernel\Kernel;
  14. /**
  15. * Mautic Application Kernel
  16. */
  17. class AppKernel extends Kernel
  18. {
  19. /**
  20. * Major version number
  21. *
  22. * @const integer
  23. */
  24. const MAJOR_VERSION = 1;
  25. /**
  26. * Minor version number
  27. *
  28. * @const integer
  29. */
  30. const MINOR_VERSION = 1;
  31. /**
  32. * Patch version number
  33. *
  34. * @const integer
  35. */
  36. const PATCH_VERSION = 4;
  37. /**
  38. * Extra version identifier
  39. *
  40. * This constant is used to define additional version segments such as development
  41. * or beta status.
  42. *
  43. * @const string
  44. */
  45. const EXTRA_VERSION = '-dev';
  46. /**
  47. * @var array
  48. */
  49. private $pluginBundles = array();
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  54. {
  55. if (strpos($request->getRequestUri(), 'installer') !== false || !$this->isInstalled()) {
  56. define('MAUTIC_INSTALLER', 1);
  57. }
  58. if (false === $this->booted) {
  59. $this->boot();
  60. }
  61. //the context is not populated at this point so have to do it manually
  62. $router = $this->getContainer()->get('router');
  63. $requestContext = new \Symfony\Component\Routing\RequestContext();
  64. $requestContext->fromRequest($request);
  65. $router->setContext($requestContext);
  66. if (strpos($request->getRequestUri(), 'installer') === false && !$this->isInstalled()) {
  67. //the context is not populated at this point so have to do it manually
  68. $router = $this->getContainer()->get('router');
  69. $requestContext = new \Symfony\Component\Routing\RequestContext();
  70. $requestContext->fromRequest($request);
  71. $router->setContext($requestContext);
  72. $base = $requestContext->getBaseUrl();
  73. //check to see if the .htaccess file exists or if not running under apache
  74. if ((strpos(strtolower($_SERVER["SERVER_SOFTWARE"]), 'apache') === false || !file_exists(__DIR__ .'../.htaccess') && strpos($base, 'index') === false)) {
  75. $base .= '/index.php';
  76. }
  77. //return new RedirectResponse();
  78. return new RedirectResponse($base . '/installer');
  79. }
  80. // Check for an an active db connection and die with error if unable to connect
  81. if (!defined('MAUTIC_INSTALLER')) {
  82. $db = $this->getContainer()->get('database_connection');
  83. try {
  84. $db->connect();
  85. } catch (\Exception $e) {
  86. error_log($e);
  87. throw new \Mautic\CoreBundle\Exception\DatabaseConnectionException(
  88. $this->getContainer()->get('translator')->trans('mautic.core.db.connection.error', array(
  89. '%code%' => $e->getCode()
  90. )
  91. ));
  92. }
  93. }
  94. return parent::handle($request, $type, $catch);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function registerBundles()
  100. {
  101. $bundles = array(
  102. new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
  103. new Symfony\Bundle\SecurityBundle\SecurityBundle(),
  104. new Symfony\Bundle\MonologBundle\MonologBundle(),
  105. new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
  106. new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
  107. new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
  108. new Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle(),
  109. new Knp\Bundle\MenuBundle\KnpMenuBundle(),
  110. new FOS\OAuthServerBundle\FOSOAuthServerBundle(),
  111. new Bazinga\OAuthServerBundle\BazingaOAuthServerBundle(),
  112. new FOS\RestBundle\FOSRestBundle(),
  113. new JMS\SerializerBundle\JMSSerializerBundle(),
  114. new Oneup\UploaderBundle\OneupUploaderBundle(),
  115. );
  116. //dynamically register Mautic Bundles
  117. $searchPath = __DIR__ . '/bundles';
  118. $finder = new \Symfony\Component\Finder\Finder();
  119. $finder->files()
  120. ->followLinks()
  121. ->in($searchPath)
  122. ->depth('1')
  123. ->name('*Bundle.php');
  124. foreach ($finder as $file) {
  125. $dirname = basename($file->getRelativePath());
  126. $filename = substr($file->getFilename(), 0, -4);
  127. $class = '\\Mautic' . '\\' . $dirname . '\\' . $filename;
  128. if (class_exists($class)) {
  129. $bundleInstance = new $class();
  130. $bundles[] = $bundleInstance;
  131. unset($bundleInstance);
  132. }
  133. }
  134. //dynamically register Mautic Plugin Bundles
  135. $searchPath = dirname(__DIR__) . '/plugins';
  136. $finder = new \Symfony\Component\Finder\Finder();
  137. $finder->files()
  138. ->followLinks()
  139. ->depth('1')
  140. ->in($searchPath)
  141. ->name('*Bundle.php');
  142. foreach ($finder as $file) {
  143. $dirname = basename($file->getRelativePath());
  144. $filename = substr($file->getFilename(), 0, -4);
  145. $class = '\\MauticPlugin' . '\\' . $dirname . '\\' . $filename;
  146. if (class_exists($class)) {
  147. $plugin = new $class();
  148. if ($plugin instanceof \Symfony\Component\HttpKernel\Bundle\Bundle) {
  149. $bundles[] = $plugin;
  150. }
  151. unset($plugin);
  152. }
  153. }
  154. // @deprecated 1.1.4; bc support for MauticAddon namespace; to be removed in 2.0
  155. $searchPath = dirname(__DIR__) . '/addons';
  156. $finder = new \Symfony\Component\Finder\Finder();
  157. $finder->files()
  158. ->followLinks()
  159. ->depth('1')
  160. ->in($searchPath)
  161. ->name('*Bundle.php');
  162. foreach ($finder as $file) {
  163. $dirname = basename($file->getRelativePath());
  164. $filename = substr($file->getFilename(), 0, -4);
  165. $class = '\\MauticAddon' . '\\' . $dirname . '\\' . $filename;
  166. if (class_exists($class)) {
  167. $addon = new $class();
  168. if ($addon instanceof \Symfony\Component\HttpKernel\Bundle\Bundle) {
  169. $bundles[] = $addon;
  170. }
  171. unset($addon);
  172. }
  173. }
  174. if (in_array($this->getEnvironment(), array('dev', 'test'))) {
  175. $bundles[] = new Symfony\Bundle\TwigBundle\TwigBundle();
  176. $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
  177. $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
  178. $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
  179. $bundles[] = new Webfactory\Bundle\ExceptionsBundle\WebfactoryExceptionsBundle();
  180. }
  181. if (in_array($this->getEnvironment(), array('test'))) {
  182. $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
  183. }
  184. // Check for local bundle inclusion
  185. if (file_exists(__DIR__ .'/config/bundles_local.php')) {
  186. include __DIR__ . '/config/bundles_local.php';
  187. }
  188. return $bundles;
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function boot()
  194. {
  195. if (true === $this->booted) {
  196. return;
  197. }
  198. if (!defined('MAUTIC_INSTALLER') && !defined('MAUTIC_TABLE_PREFIX')) {
  199. //set the table prefix before boot
  200. $localParams = $this->getLocalParams();
  201. $prefix = isset($localParams['db_table_prefix']) ? $localParams['db_table_prefix'] : '';
  202. define('MAUTIC_TABLE_PREFIX', $prefix);
  203. }
  204. if ($this->loadClassCache) {
  205. $this->doLoadClassCache($this->loadClassCache[0], $this->loadClassCache[1]);
  206. }
  207. // init bundles
  208. $this->initializeBundles();
  209. // init container
  210. $this->initializeContainer();
  211. // If in console, set the table prefix since handle() is not executed
  212. if (defined('IN_MAUTIC_CONSOLE') && !defined('MAUTIC_TABLE_PREFIX')) {
  213. $localParams = $this->getLocalParams();
  214. $prefix = isset($localParams['db_table_prefix']) ? $localParams['db_table_prefix'] : '';
  215. define('MAUTIC_TABLE_PREFIX', $prefix);
  216. }
  217. $registeredPluginBundles = $this->container->getParameter('mautic.plugin.bundles');
  218. foreach ($this->getBundles() as $name => $bundle) {
  219. $bundle->setContainer($this->container);
  220. $bundle->boot();
  221. }
  222. $this->pluginBundles = $registeredPluginBundles;
  223. $this->booted = true;
  224. }
  225. /**
  226. * Returns a list of addon bundles that are enabled
  227. *
  228. * @return array
  229. */
  230. public function getPluginBundles()
  231. {
  232. return $this->pluginBundles;
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public function registerContainerConfiguration(LoaderInterface $loader)
  238. {
  239. $loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.php');
  240. }
  241. /**
  242. * Retrieves the application's version number
  243. *
  244. * @return string
  245. */
  246. public function getVersion()
  247. {
  248. return self::MAJOR_VERSION . '.' . self::MINOR_VERSION . '.' . self::PATCH_VERSION . self::EXTRA_VERSION;
  249. }
  250. /**
  251. * Checks if the application has been installed
  252. *
  253. * @return bool
  254. */
  255. private function isInstalled()
  256. {
  257. static $isInstalled = null;
  258. if ($isInstalled === null) {
  259. $params = $this->getLocalParams();
  260. $isInstalled = (is_array($params) && !empty($params['db_driver']) && !empty($params['mailer_from_name']));
  261. }
  262. return $isInstalled;
  263. }
  264. /**
  265. * @param array $params
  266. *
  267. * @return \Doctrine\DBAL\Connection
  268. * @throws Exception
  269. * @throws \Doctrine\DBAL\DBALException
  270. */
  271. public function getDatabaseConnection($params = array())
  272. {
  273. if (empty($params)) {
  274. $params = $this->getLocalParams();
  275. }
  276. if (!empty($params) && !empty($params['db_driver'])) {
  277. $testParams = array('driver', 'host', 'port', 'name', 'user', 'password', 'path');
  278. $dbParams = array();
  279. foreach ($testParams as &$p) {
  280. $param = (isset($params["db_{$p}"])) ? $params["db_{$p}"] : '';
  281. if ($p == 'port') {
  282. $param = (int) $param;
  283. }
  284. $name = ($p == 'name') ? 'dbname' : $p;
  285. $dbParams[$name] = $param;
  286. }
  287. // Test a database connection and existence of a user
  288. $db = \Doctrine\DBAL\DriverManager::getConnection($dbParams);
  289. $db->connect();
  290. return $db;
  291. } else {
  292. throw new \Exception('not configured');
  293. }
  294. }
  295. /**
  296. * {@inheritdoc}
  297. *
  298. * @api
  299. */
  300. public function getCacheDir()
  301. {
  302. $parameters = $this->getLocalParams();
  303. if (isset($parameters['cache_path'])) {
  304. $envFolder = (strpos($parameters['cache_path'], -1) != '/') ? '/' . $this->environment : $this->environment;
  305. return str_replace('%kernel.root_dir%', $this->getRootDir(), $parameters['cache_path'] . $envFolder);
  306. } else {
  307. return parent::getCacheDir();
  308. }
  309. }
  310. /**
  311. * {@inheritdoc}
  312. *
  313. * @api
  314. */
  315. public function getLogDir()
  316. {
  317. $parameters = $this->getLocalParams();
  318. if (isset($parameters['log_path'])) {
  319. return str_replace('%kernel.root_dir%', $this->getRootDir(), $parameters['log_path']);
  320. } else {
  321. return parent::getLogDir();
  322. }
  323. }
  324. /**
  325. * Get Mautic's local configuration file
  326. *
  327. * @return array
  328. */
  329. public function getLocalParams()
  330. {
  331. static $localParameters;
  332. if (!is_array($localParameters)) {
  333. /** @var $paths */
  334. $root = $this->getRootDir();
  335. include $root . '/config/paths.php';
  336. if ($configFile = $this->getLocalConfigFile()) {
  337. /** @var $parameters */
  338. include $configFile;
  339. $localParameters = (isset($parameters) && is_array($parameters)) ? $parameters : array();
  340. } else {
  341. $localParameters = array();
  342. }
  343. //check for parameter overrides
  344. if (file_exists($root . '/config/parameters_local.php')) {
  345. /** @var $parameters */
  346. include $root . '/config/parameters_local.php';
  347. $localParameters = array_merge($localParameters, $parameters);
  348. }
  349. }
  350. return $localParameters;
  351. }
  352. /**
  353. * Get local config file
  354. *
  355. * @param bool $checkExists If true, then return false if the file doesn't exist
  356. *
  357. * @return bool
  358. */
  359. public function getLocalConfigFile($checkExists = true)
  360. {
  361. /** @var $paths */
  362. $root = $this->getRootDir();
  363. include $root . '/config/paths.php';
  364. if (isset($paths['local_config'])) {
  365. $paths['local_config'] = str_replace('%kernel.root_dir%', $root, $paths['local_config']);
  366. if (!$checkExists || file_exists($paths['local_config'])) {
  367. return $paths['local_config'];
  368. }
  369. }
  370. return false;
  371. }
  372. }