PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/phpbb/di/container_builder.php

https://github.com/AJenbo/ubuntudanmark.dk
PHP | 404 lines | 189 code | 53 blank | 162 comment | 16 complexity | f58aa82c9fb6e42a8d96e5ed41f97f9e MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\di;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  16. use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass;
  17. class container_builder
  18. {
  19. /** @var string phpBB Root Path */
  20. protected $phpbb_root_path;
  21. /** @var string php file extension */
  22. protected $php_ext;
  23. /**
  24. * The container under construction
  25. *
  26. * @var ContainerBuilder
  27. */
  28. protected $container;
  29. /**
  30. * @var \phpbb\db\driver\driver_interface
  31. */
  32. protected $dbal_connection = null;
  33. /**
  34. * @var array the installed extensions
  35. */
  36. protected $installed_exts = null;
  37. /**
  38. * Indicates whether the php config file should be injected into the container (default to true).
  39. *
  40. * @var bool
  41. */
  42. protected $inject_config = true;
  43. /**
  44. * Indicates whether extensions should be used (default to true).
  45. *
  46. * @var bool
  47. */
  48. protected $use_extensions = true;
  49. /**
  50. * Defines a custom path to find the configuration of the container (default to $this->phpbb_root_path . 'config')
  51. *
  52. * @var string
  53. */
  54. protected $config_path = null;
  55. /**
  56. * Indicates whether the phpBB compile pass should be used (default to true).
  57. *
  58. * @var bool
  59. */
  60. protected $use_custom_pass = true;
  61. /**
  62. * Indicates whether the kernel compile pass should be used (default to true).
  63. *
  64. * @var bool
  65. */
  66. protected $use_kernel_pass = true;
  67. /**
  68. * Indicates whether the container should be dumped to the filesystem (default to true).
  69. *
  70. * If DEBUG_CONTAINER is set this option is ignored and a new container is build.
  71. *
  72. * @var bool
  73. */
  74. protected $dump_container = true;
  75. /**
  76. * Indicates if the container should be compiled automatically (default to true).
  77. *
  78. * @var bool
  79. */
  80. protected $compile_container = true;
  81. /**
  82. * Custom parameters to inject into the container.
  83. *
  84. * Default to true:
  85. * array(
  86. * 'core.root_path', $this->phpbb_root_path,
  87. * 'core.php_ext', $this->php_ext,
  88. * );
  89. *
  90. * @var array
  91. */
  92. protected $custom_parameters = null;
  93. /**
  94. * @var \phpbb\config_php_file
  95. */
  96. protected $config_php_file;
  97. /**
  98. * Constructor
  99. *
  100. * @param \phpbb\config_php_file $config_php_file
  101. * @param string $phpbb_root_path Path to the phpbb includes directory.
  102. * @param string $php_ext php file extension
  103. */
  104. function __construct(\phpbb\config_php_file $config_php_file, $phpbb_root_path, $php_ext)
  105. {
  106. $this->config_php_file = $config_php_file;
  107. $this->phpbb_root_path = $phpbb_root_path;
  108. $this->php_ext = $php_ext;
  109. }
  110. /**
  111. * Build and return a new Container respecting the current configuration
  112. *
  113. * @return \phpbb_cache_container|ContainerBuilder
  114. */
  115. public function get_container()
  116. {
  117. $container_filename = $this->get_container_filename();
  118. if (!defined('DEBUG_CONTAINER') && $this->dump_container && file_exists($container_filename))
  119. {
  120. require($container_filename);
  121. $this->container = new \phpbb_cache_container();
  122. }
  123. else
  124. {
  125. if ($this->config_path === null)
  126. {
  127. $this->config_path = $this->phpbb_root_path . 'config';
  128. }
  129. $container_extensions = array(new \phpbb\di\extension\core($this->config_path));
  130. if ($this->use_extensions)
  131. {
  132. $installed_exts = $this->get_installed_extensions();
  133. $container_extensions[] = new \phpbb\di\extension\ext($installed_exts);
  134. }
  135. if ($this->inject_config)
  136. {
  137. $container_extensions[] = new \phpbb\di\extension\config($this->config_php_file);
  138. }
  139. $this->container = $this->create_container($container_extensions);
  140. if ($this->use_custom_pass)
  141. {
  142. // Symfony Kernel Listeners
  143. $this->container->addCompilerPass(new \phpbb\di\pass\collection_pass());
  144. $this->container->addCompilerPass(new RegisterListenersPass('dispatcher', 'event.listener_listener', 'event.listener'));
  145. if ($this->use_kernel_pass)
  146. {
  147. $this->container->addCompilerPass(new RegisterListenersPass('dispatcher'));
  148. }
  149. }
  150. $this->inject_custom_parameters();
  151. if ($this->compile_container)
  152. {
  153. $this->container->compile();
  154. }
  155. if ($this->dump_container && !defined('DEBUG_CONTAINER'))
  156. {
  157. $this->dump_container($container_filename);
  158. }
  159. }
  160. $this->container->set('config.php', $this->config_php_file);
  161. if ($this->compile_container)
  162. {
  163. $this->inject_dbal();
  164. }
  165. return $this->container;
  166. }
  167. /**
  168. * Set if the extensions should be used.
  169. *
  170. * @param bool $use_extensions
  171. */
  172. public function set_use_extensions($use_extensions)
  173. {
  174. $this->use_extensions = $use_extensions;
  175. }
  176. /**
  177. * Set if the phpBB compile pass have to be used.
  178. *
  179. * @param bool $use_custom_pass
  180. */
  181. public function set_use_custom_pass($use_custom_pass)
  182. {
  183. $this->use_custom_pass = $use_custom_pass;
  184. }
  185. /**
  186. * Set if the kernel compile pass have to be used.
  187. *
  188. * @param bool $use_kernel_pass
  189. */
  190. public function set_use_kernel_pass($use_kernel_pass)
  191. {
  192. $this->use_kernel_pass = $use_kernel_pass;
  193. }
  194. /**
  195. * Set if the php config file should be injecting into the container.
  196. *
  197. * @param bool $inject_config
  198. */
  199. public function set_inject_config($inject_config)
  200. {
  201. $this->inject_config = $inject_config;
  202. }
  203. /**
  204. * Set if a dump container should be used.
  205. *
  206. * If DEBUG_CONTAINER is set this option is ignored and a new container is build.
  207. *
  208. * @var bool $dump_container
  209. */
  210. public function set_dump_container($dump_container)
  211. {
  212. $this->dump_container = $dump_container;
  213. }
  214. /**
  215. * Set if the container should be compiled automatically (default to true).
  216. *
  217. * @var bool $dump_container
  218. */
  219. public function set_compile_container($compile_container)
  220. {
  221. $this->compile_container = $compile_container;
  222. }
  223. /**
  224. * Set a custom path to find the configuration of the container
  225. *
  226. * @param string $config_path
  227. */
  228. public function set_config_path($config_path)
  229. {
  230. $this->config_path = $config_path;
  231. }
  232. /**
  233. * Set custom parameters to inject into the container.
  234. *
  235. * @param array $custom_parameters
  236. */
  237. public function set_custom_parameters($custom_parameters)
  238. {
  239. $this->custom_parameters = $custom_parameters;
  240. }
  241. /**
  242. * Dump the container to the disk.
  243. *
  244. * @param string $container_filename The name of the file.
  245. */
  246. protected function dump_container($container_filename)
  247. {
  248. $dumper = new PhpDumper($this->container);
  249. $cached_container_dump = $dumper->dump(array(
  250. 'class' => 'phpbb_cache_container',
  251. 'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
  252. ));
  253. file_put_contents($container_filename, $cached_container_dump);
  254. }
  255. /**
  256. * Inject the connection into the container if one was opened.
  257. */
  258. protected function inject_dbal()
  259. {
  260. if ($this->dbal_connection !== null)
  261. {
  262. $this->container->get('dbal.conn')->set_driver($this->dbal_connection);
  263. }
  264. }
  265. /**
  266. * Get DB connection.
  267. *
  268. * @return \phpbb\db\driver\driver_interface
  269. */
  270. protected function get_dbal_connection()
  271. {
  272. if ($this->dbal_connection === null)
  273. {
  274. $dbal_driver_class = $this->config_php_file->convert_30_dbms_to_31($this->config_php_file->get('dbms'));
  275. $this->dbal_connection = new $dbal_driver_class();
  276. $this->dbal_connection->sql_connect(
  277. $this->config_php_file->get('dbhost'),
  278. $this->config_php_file->get('dbuser'),
  279. $this->config_php_file->get('dbpasswd'),
  280. $this->config_php_file->get('dbname'),
  281. $this->config_php_file->get('dbport'),
  282. defined('PHPBB_DB_NEW_LINK') && PHPBB_DB_NEW_LINK
  283. );
  284. }
  285. return $this->dbal_connection;
  286. }
  287. /**
  288. * Get enabled extensions.
  289. *
  290. * @return array enabled extensions
  291. */
  292. protected function get_installed_extensions()
  293. {
  294. $db = $this->get_dbal_connection();
  295. $extension_table = $this->config_php_file->get('table_prefix') . 'ext';
  296. $sql = 'SELECT *
  297. FROM ' . $extension_table . '
  298. WHERE ext_active = 1';
  299. $result = $db->sql_query($sql);
  300. $rows = $db->sql_fetchrowset($result);
  301. $db->sql_freeresult($result);
  302. $exts = array();
  303. foreach ($rows as $row)
  304. {
  305. $exts[$row['ext_name']] = $this->phpbb_root_path . 'ext/' . $row['ext_name'] . '/';
  306. }
  307. return $exts;
  308. }
  309. /**
  310. * Create the ContainerBuilder object
  311. *
  312. * @param array $extensions Array of Container extension objects
  313. * @return ContainerBuilder object
  314. */
  315. protected function create_container(array $extensions)
  316. {
  317. $container = new ContainerBuilder();
  318. foreach ($extensions as $extension)
  319. {
  320. $container->registerExtension($extension);
  321. $container->loadFromExtension($extension->getAlias());
  322. }
  323. return $container;
  324. }
  325. /**
  326. * Inject the customs parameters into the container
  327. */
  328. protected function inject_custom_parameters()
  329. {
  330. if ($this->custom_parameters === null)
  331. {
  332. $this->custom_parameters = array(
  333. 'core.root_path' => $this->phpbb_root_path,
  334. 'core.php_ext' => $this->php_ext,
  335. );
  336. }
  337. foreach ($this->custom_parameters as $key => $value)
  338. {
  339. $this->container->setParameter($key, $value);
  340. }
  341. }
  342. /**
  343. * Get the filename under which the dumped container will be stored.
  344. *
  345. * @return string Path for dumped container
  346. */
  347. protected function get_container_filename()
  348. {
  349. return $this->phpbb_root_path . 'cache/container_' . md5($this->phpbb_root_path) . '.' . $this->php_ext;
  350. }
  351. }