Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

container_builder.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 9.22 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb\di;
 015   
 016  use Symfony\Component\DependencyInjection\ContainerBuilder;
 017  use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
 018  use Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass;
 019   
 020  class container_builder
 021  {
 022      /** @var string phpBB Root Path */
 023      protected $phpbb_root_path;
 024   
 025      /** @var string php file extension  */
 026      protected $php_ext;
 027   
 028      /**
 029      * The container under construction
 030      *
 031      * @var ContainerBuilder
 032      */
 033      protected $container;
 034   
 035      /**
 036      * @var \phpbb\db\driver\driver_interface
 037      */
 038      protected $dbal_connection = null;
 039   
 040      /**
 041      * @var array the installed extensions
 042      */
 043      protected $installed_exts = null;
 044   
 045      /**
 046      * Indicates whether the php config file should be injected into the container (default to true).
 047      *
 048      * @var bool
 049      */
 050      protected $inject_config = true;
 051   
 052      /**
 053      * Indicates whether extensions should be used (default to true).
 054      *
 055      * @var bool
 056      */
 057      protected $use_extensions = true;
 058   
 059      /**
 060      * Defines a custom path to find the configuration of the container (default to $this->phpbb_root_path . 'config')
 061      *
 062      * @var string
 063      */
 064      protected $config_path = null;
 065   
 066      /**
 067      * Indicates whether the phpBB compile pass should be used (default to true).
 068      *
 069      * @var bool
 070      */
 071      protected $use_custom_pass = true;
 072   
 073      /**
 074      * Indicates whether the kernel compile pass should be used (default to true).
 075      *
 076      * @var bool
 077      */
 078      protected $use_kernel_pass = true;
 079   
 080      /**
 081      * Indicates whether the container should be dumped to the filesystem (default to true).
 082      *
 083      * If DEBUG_CONTAINER is set this option is ignored and a new container is build.
 084      *
 085      * @var bool
 086      */
 087      protected $dump_container = true;
 088   
 089      /**
 090      * Indicates if the container should be compiled automatically (default to true).
 091      *
 092      * @var bool
 093      */
 094      protected $compile_container = true;
 095   
 096      /**
 097      * Custom parameters to inject into the container.
 098      *
 099      * 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   
 189          if ($this->compile_container)
 190          {
 191              $this->inject_dbal();
 192          }
 193   
 194          return $this->container;
 195      }
 196   
 197      /**
 198      * Set if the extensions should be used.
 199      *
 200      * @param bool $use_extensions
 201      */
 202      public function set_use_extensions($use_extensions)
 203      {
 204          $this->use_extensions = $use_extensions;
 205      }
 206   
 207      /**
 208      * Set if the phpBB compile pass have to be used.
 209      *
 210      * @param bool $use_custom_pass
 211      */
 212      public function set_use_custom_pass($use_custom_pass)
 213      {
 214          $this->use_custom_pass = $use_custom_pass;
 215      }
 216   
 217      /**
 218      * Set if the kernel compile pass have to be used.
 219      *
 220      * @param bool $use_kernel_pass
 221      */
 222      public function set_use_kernel_pass($use_kernel_pass)
 223      {
 224          $this->use_kernel_pass = $use_kernel_pass;
 225      }
 226   
 227      /**
 228      * Set if the php config file should be injecting into the container.
 229      *
 230      * @param bool $inject_config
 231      */
 232      public function set_inject_config($inject_config)
 233      {
 234          $this->inject_config = $inject_config;
 235      }
 236   
 237      /**
 238      * Set if a dump container should be used.
 239      *
 240      * If DEBUG_CONTAINER is set this option is ignored and a new container is build.
 241      *
 242      * @var bool $dump_container
 243      */
 244      public function set_dump_container($dump_container)
 245      {
 246          $this->dump_container = $dump_container;
 247      }
 248   
 249      /**
 250      * Set if the container should be compiled automatically (default to true).
 251      *
 252      * @var bool $dump_container
 253      */
 254      public function set_compile_container($compile_container)
 255      {
 256          $this->compile_container = $compile_container;
 257      }
 258   
 259      /**
 260      * Set a custom path to find the configuration of the container
 261      *
 262      * @param string $config_path
 263      */
 264      public function set_config_path($config_path)
 265      {
 266          $this->config_path = $config_path;
 267      }
 268   
 269      /**
 270      * Set custom parameters to inject into the container.
 271      *
 272      * @param array $custom_parameters
 273      */
 274      public function set_custom_parameters($custom_parameters)
 275      {
 276          $this->custom_parameters = $custom_parameters;
 277      }
 278   
 279      /**
 280      * Dump the container to the disk.
 281      *
 282      * @param string $container_filename The name of the file.
 283      */
 284      protected function dump_container($container_filename)
 285      {
 286          $dumper = new PhpDumper($this->container);
 287          $cached_container_dump = $dumper->dump(array(
 288              'class'         => 'phpbb_cache_container',
 289              'base_class'    => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
 290          ));
 291   
 292          file_put_contents($container_filename, $cached_container_dump);
 293      }
 294   
 295      /**
 296      * Inject the connection into the container if one was opened.
 297      */
 298      protected function inject_dbal()
 299      {
 300          if ($this->dbal_connection !== null)
 301          {
 302              $this->container->get('dbal.conn')->set_driver($this->dbal_connection);
 303          }
 304      }
 305   
 306      /**
 307      * Get DB connection.
 308      *
 309      * @return \phpbb\db\driver\driver_interface
 310      */
 311      protected function get_dbal_connection()
 312      {
 313          if ($this->dbal_connection === null)
 314          {
 315              $dbal_driver_class = $this->config_php_file->convert_30_dbms_to_31($this->config_php_file->get('dbms'));
 316              $this->dbal_connection = new $dbal_driver_class();
 317              $this->dbal_connection->sql_connect(
 318                  $this->config_php_file->get('dbhost'),
 319                  $this->config_php_file->get('dbuser'),
 320                  $this->config_php_file->get('dbpasswd'),
 321                  $this->config_php_file->get('dbname'),
 322                  $this->config_php_file->get('dbport'),
 323                  defined('PHPBB_DB_NEW_LINK') && PHPBB_DB_NEW_LINK
 324              );
 325          }
 326   
 327          return $this->dbal_connection;
 328      }
 329   
 330      /**
 331      * Get enabled extensions.
 332      *
 333      * @return array enabled extensions
 334      */
 335      protected function get_installed_extensions()
 336      {
 337          $db = $this->get_dbal_connection();
 338          $extension_table = $this->config_php_file->get('table_prefix') . 'ext';
 339   
 340          $sql = 'SELECT *
 341              FROM ' . $extension_table . '
 342              WHERE ext_active = 1';
 343   
 344          $result = $db->sql_query($sql);
 345          $rows = $db->sql_fetchrowset($result);
 346          $db->sql_freeresult($result);
 347   
 348          $exts = array();
 349          foreach ($rows as $row)
 350          {
 351              $exts[$row['ext_name']] = $this->phpbb_root_path . 'ext/' . $row['ext_name'] . '/';
 352          }
 353   
 354          return $exts;
 355      }
 356   
 357      /**
 358      * Create the ContainerBuilder object
 359      *
 360      * @param array $extensions Array of Container extension objects
 361      * @return ContainerBuilder object
 362      */
 363      protected function create_container(array $extensions)
 364      {
 365          $container = new ContainerBuilder();
 366   
 367          foreach ($extensions as $extension)
 368          {
 369              $container->registerExtension($extension);
 370              $container->loadFromExtension($extension->getAlias());
 371          }
 372   
 373          return $container;
 374      }
 375   
 376      /**
 377      * Inject the customs parameters into the container
 378      */
 379      protected function inject_custom_parameters()
 380      {
 381          if ($this->custom_parameters === null)
 382          {
 383              $this->custom_parameters = array(
 384                  'core.root_path' => $this->phpbb_root_path,
 385                  'core.php_ext' => $this->php_ext,
 386              );
 387          }
 388   
 389          foreach ($this->custom_parameters as $key => $value)
 390          {
 391              $this->container->setParameter($key, $value);
 392          }
 393      }
 394   
 395      /**
 396      * Get the filename under which the dumped container will be stored.
 397      *
 398      * @return string Path for dumped container
 399      */
 400      protected function get_container_filename()
 401      {
 402          $filename = str_replace(array('/', '.'), array('slash', 'dot'), $this->phpbb_root_path);
 403          return $this->phpbb_root_path . 'cache/container_' . $filename . '.' . $this->php_ext;
 404      }
 405  }
 406