[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/phpbb/di/ -> container_builder.php (source)

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1