草庐IT

php - Drupal 8 覆盖 session 管理

coder 2024-04-30 原文

我想覆盖 drupals 核心 session 管理以支持我自己的,而不是将 session 保存到 Redis 而不是数据库。

在谷歌搜索之后,除了这个之外没什么可做的: https://www.drupal.org/project/session_proxy

唯一的问题是它与 Drupal 8 不兼容,我只想保存到 Redis,不需要任何其他处理程序。

在 Symfony 中,我创建了一个 session 处理程序服务,但在 Drupal 8 中似乎更加棘手。

关于我应该如何进行的任何建议?

谢谢

最佳答案

我认为在不依赖第 3 方模块或任何其他插件的情况下解决此问题的最简单方法是覆盖 Drupals 核心 SessionHandler 类。

首先,在我的模块中,我创建了一个 ServiceProvider 类,它指示容器用我自己的定义重新定义核心 SessionHandler 类定义。我不需要数据库连接服务,所以我确保只有请求堆栈被传递给构造函数。

<?php

namespace Drupal\my_module;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;

class OoAuthServiceProvider extends ServiceProviderBase
{
    /**
     * {@inheritdoc}
     */
    public function alter(ContainerBuilder $container)
    {
        $container->getDefinition('session_handler.storage')
            ->setClass('Drupal\my_module\SessionHandler')
            ->setArguments([
                new Reference('request_stack')
            ]);
    }
}

然后我开始创建自己的 Redis SessionHandler:

<?php

namespace Drupal\my_module;

use Drupal\Component\Utility\Crypt;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Utility\Error;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;

/**
 * Default session handler.
 */
class SessionHandler extends AbstractProxy implements \SessionHandlerInterface {

    use DependencySerializationTrait;

    /**
     * The request stack.
     *
     * @var RequestStack
     */
    protected $requestStack;

    /**
     * @var \Redis
     */
    protected $redis;

    /**
     * SessionHandler constructor.
     *
     * @param RequestStack $requestStack
     */
    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
        // TODO: Store redis connection details in config.
        $this->redis = (new PhpRedis())->getClient('redis-host', 6379);
    }

    /**
     * {@inheritdoc}
     */
    public function open($savePath, $name)
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function read($sid)
    {
        $data = '';

        if (!empty($sid)) {
            $query = $this->redis->get(Crypt::hashBase64($sid));
            $data = unserialize($query);
        }

        return (string) $data['session'];
    }

    /**
     * {@inheritdoc}
     */
    public function write($sid, $value)
    {
        // The exception handler is not active at this point, so we need to do it
        // manually.

        var_dump(['Value', $value]);
        try {
            $request = $this->requestStack->getCurrentRequest();
            $fields = [
                'uid' => $request->getSession()->get('uid', 0),
                'hostname' => $request->getClientIP(),
                'session' => $value,
                'timestamp' => REQUEST_TIME,
            ];

            $this->redis->set(
              Crypt::hashBase64($sid),
              serialize($fields),
              (int) ini_get("session.gc_maxlifetime")
            );

            return true;
        }
        catch (\Exception $exception) {
            require_once DRUPAL_ROOT . '/core/includes/errors.inc';
            // If we are displaying errors, then do so with no possibility of a
            // further uncaught exception being thrown.
            if (error_displayable()) {
                print '<h1>Uncaught exception thrown in session handler.</h1>';
                print '<p>' . Error::renderExceptionSafe($exception) . '</p><hr />';
            }

            return true;
        }
    }

    /**
     * {@inheritdoc}
     */
    public function close()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function destroy($sid)
    {
        // Delete session data.
        $this->redis->delete(Crypt::hashBase64($sid));

        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function gc($lifetime)
    {
        // Redundant method when using Redis. You no longer have to check the session
        // timestamp as the session.gc_maxlifetime is set as TTL on write.
        return true;
    }

}

在我自己的 SessionHandler 实现中使用的 PhpRedis 只是一个用于处理连接到 Redis 的小实用程序类。

<?php

namespace Drupal\my_module;

/**
 * Class PhpRedis
 * @package Drupal\oo_auth
 */
class PhpRedis implements ClientInterface
{
  /**
   * {@inheritdoc}
   */
    public function getClient($host = null, $port = null, $base = null, $password = null)
    {
        $client = new \Redis();
        $client->connect($host, $port);

        if (isset($password)) {
            $client->auth($password);
        }

        if (isset($base)) {
            $client->select($base);
        }

        // Do not allow PhpRedis serialize itself data, we are going to do it
        // oneself. This will ensure less memory footprint on Redis size when
        // we will attempt to store small values.
        $client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);

        return $client;
    }

  /**
   * {@inheritdoc}
   */
    public function getName() {
        return 'PhpRedis';
    }
}

<?php

namespace Drupal\my_module;

/**
 * Interface ClientInterface
 * @package Drupal\oo_auth
 */
interface ClientInterface
{
    /**
     * Get the connected client instance.
     *
     * @param null $host
     * @param null $port
     * @param null $base
     *
     * @return mixed
     */
    public function getClient($host = NULL, $port = NULL, $base = NULL);

    /**
    * Get underlying library name used.
    *
    * This can be useful for contribution code that may work with only some of
    * the provided clients.
    *
    * @return string
    */
    public function getName();
}

没有(我能找到的)建议文档向您提供如何使用 Redis(这实际上适用于任何数据存储)作为 Drupal 安装的 session 存储的示例。有一些关于如何启动它并与其他第 3 方模块一起运行的帖子,这很好,但我不想要多余的内容。

关于php - Drupal 8 覆盖 session 管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486546/

有关php - Drupal 8 覆盖 session 管理的更多相关文章

  1. ruby - i18n Assets 管理/翻译 UI - 2

    我正在使用i18n从头开始​​构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在ruby​​onrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi

  2. ruby-on-rails - 在混合/模块中覆盖模型的属性访问器 - 2

    我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah

  3. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  4. ruby - 覆盖相似的方法,更短的语法 - 2

    在Ruby类中,我重写了三个方法,并且在每个方法中,我基本上做同样的事情:classExampleClassdefconfirmation_required?is_allowed&&superenddefpostpone_email_change?is_allowed&&superenddefreconfirmation_required?is_allowed&&superendend有更简洁的语法吗?如何缩短代码? 最佳答案 如何使用别名?classExampleClassdefconfirmation_required?is_a

  5. ruby - 是否可以覆盖 gemfile 进行本地开发? - 2

    我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI

  6. ruby-on-rails - 获取 inf-ruby 以使用 ruby​​ 版本管理器 (rvm) - 2

    我安装了ruby​​版本管理器,并将RVM安装的ruby​​实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby​​。有没有办法让emacs像shell一样尊重ruby​​的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el

  7. ruby-on-rails - 事件管理员日期过滤器日期格式自定义 - 2

    是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s

  8. ruby-on-rails - Rails 优雅地处理超时 session ? - 2

    使用rails4,ruby2。我在rails配置中为我的cookiesession设置了30分钟的超时时间。问题是,如果我转到表单,让session超时,然后提交表单,我会收到此ActionController::InvalidAuthenticityToken错误。如何在Rails中优雅地处理这个错误?比如说,重定向到登录屏幕? 最佳答案 在您的ApplicationController:rescue_fromActionController::InvalidAuthenticityTokendoredirect_tosome_p

  9. ruby-on-rails - 为什么在 Rails 5.1.1 中删除了 session 存储初始化程序 - 2

    我去了这个website查看Rails5.0.0和Rails5.1.1之间的区别为什么5.1.1不再包含:config/initializers/session_store.rb?谢谢 最佳答案 这是删除它的提交:Setupdefaultsessionstoreinternally,nolongerthroughanapplicationinitializer总而言之,新应用没有该初始化器,session存储默认设置为cookie存储。即与在该初始值设定项的生成版本中指定的值相同。 关于

  10. ruby - (Ruby || Python) 窗口管理器 - 2

    我想用这两种语言中的任何一种(最好是ruby​​)制作一个窗口管理器。老实说,除了我需要加载某种X模块外,我不知道从哪里开始。因此,如果有人有线索,如果您能指出正确的方向,那就太好了。谢谢 最佳答案 XCB,X的下一代API使用XML格式定义X协议(protocol),并使用脚本生成特定语言绑定(bind)。它在概念上与SWIG类似,只是它描述的不是CAPI,而是X协议(protocol)。目前,C和Python存在绑定(bind)。理论上,Ruby端口只是编写一个从XML协议(protocol)定义语言到Ruby的翻译器的问题。生

随机推荐