[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Session/ -> SessionFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Session;
  11  
  12  use InvalidArgumentException;
  13  use Joomla\Database\DatabaseInterface;
  14  use Joomla\DI\ContainerAwareInterface;
  15  use Joomla\DI\ContainerAwareTrait;
  16  use Joomla\Registry\Registry;
  17  use Joomla\Session\Handler;
  18  use Joomla\Session\HandlerInterface;
  19  use Memcached;
  20  use Redis;
  21  use RuntimeException;
  22  use Symfony\Component\OptionsResolver\OptionsResolver;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('JPATH_PLATFORM') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Factory for creating session API objects
  30   *
  31   * @since  4.0.0
  32   */
  33  class SessionFactory implements ContainerAwareInterface
  34  {
  35      use ContainerAwareTrait;
  36  
  37      /**
  38       * Create a session handler based on the application configuration.
  39       *
  40       * @param   array  $options  The options used to instantiate the SessionInterface instance.
  41       *
  42       * @return  HandlerInterface
  43       *
  44       * @since   4.0.0
  45       */
  46      public function createSessionHandler(array $options): HandlerInterface
  47      {
  48          $resolver = new OptionsResolver();
  49          $this->configureSessionHandlerOptions($resolver);
  50  
  51          $options = $resolver->resolve($options);
  52  
  53          /** @var Registry $config */
  54          $config = $this->getContainer()->get('config');
  55  
  56          $handlerType = $config->get('session_handler', 'filesystem');
  57  
  58          switch ($handlerType) {
  59              case 'apcu':
  60                  if (!Handler\ApcuHandler::isSupported()) {
  61                      throw new RuntimeException('APCu is not supported on this system.');
  62                  }
  63  
  64                  return new Handler\ApcuHandler();
  65  
  66              case 'database':
  67                  return new Handler\DatabaseHandler($this->getContainer()->get(DatabaseInterface::class));
  68  
  69              case 'filesystem':
  70              case 'none':
  71                  // Try to use a custom configured path, fall back to the path in the PHP runtime configuration
  72                  $path = $config->get('session_filesystem_path', ini_get('session.save_path'));
  73  
  74                  // If we still have no path, as a last resort fall back to the system's temporary directory
  75                  if (empty($path)) {
  76                      $path = sys_get_temp_dir();
  77                  }
  78  
  79                  return new Handler\FilesystemHandler($path);
  80  
  81              case 'memcached':
  82                  if (!Handler\MemcachedHandler::isSupported()) {
  83                      throw new RuntimeException('Memcached is not supported on this system.');
  84                  }
  85  
  86                  $host = $config->get('session_memcached_server_host', 'localhost');
  87                  $port = $config->get('session_memcached_server_port', 11211);
  88  
  89                  $memcached = new Memcached($config->get('session_memcached_server_id', 'joomla_cms'));
  90                  $memcached->addServer($host, $port);
  91  
  92                  ini_set('session.save_path', "$host:$port");
  93                  ini_set('session.save_handler', 'memcached');
  94  
  95                  return new Handler\MemcachedHandler($memcached, ['ttl' => $options['expire']]);
  96  
  97              case 'redis':
  98                  if (!Handler\RedisHandler::isSupported()) {
  99                      throw new RuntimeException('Redis is not supported on this system.');
 100                  }
 101  
 102                  $redis = new Redis();
 103                  $host  = $config->get('session_redis_server_host', '127.0.0.1');
 104  
 105                  // Use default port if connecting over a socket whatever the config value
 106                  $port = $host[0] === '/' ? 0 : $config->get('session_redis_server_port', 6379);
 107  
 108                  if ($config->get('session_redis_persist', true)) {
 109                      $redis->pconnect(
 110                          $host,
 111                          $port
 112                      );
 113                  } else {
 114                      $redis->connect(
 115                          $host,
 116                          $port
 117                      );
 118                  }
 119  
 120                  if (!empty($config->get('session_redis_server_auth', ''))) {
 121                      $redis->auth($config->get('session_redis_server_auth', null));
 122                  }
 123  
 124                  $db = (int) $config->get('session_redis_server_db', 0);
 125  
 126                  if ($db !== 0) {
 127                      $redis->select($db);
 128                  }
 129  
 130                  return new Handler\RedisHandler($redis, ['ttl' => $options['expire']]);
 131  
 132              case 'wincache':
 133                  // @TODO Remove WinCache with Joomla 5.0
 134                  if (!Handler\WincacheHandler::isSupported()) {
 135                      throw new RuntimeException('Wincache is not supported on this system.');
 136                  }
 137  
 138                  return new Handler\WincacheHandler();
 139  
 140              default:
 141                  throw new InvalidArgumentException(sprintf('The "%s" session handler is not recognised.', $handlerType));
 142          }
 143      }
 144  
 145      /**
 146       * Resolve the options for the session handler.
 147       *
 148       * @param   OptionsResolver  $resolver  The options resolver.
 149       *
 150       * @return  void
 151       *
 152       * @since   4.0.0
 153       */
 154      protected function configureSessionHandlerOptions(OptionsResolver $resolver)
 155      {
 156          $resolver->setDefaults(
 157              [
 158                  'force_ssl' => false,
 159              ]
 160          );
 161  
 162          $resolver->setRequired(['name', 'expire']);
 163  
 164          $resolver->setAllowedTypes('name', ['string']);
 165          $resolver->setAllowedTypes('expire', ['int']);
 166          $resolver->setAllowedTypes('force_ssl', ['bool']);
 167      }
 168  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer