pimcore/static-resolver-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pimcore/static-resolver-bundle

Composer 安装命令:

composer require pimcore/static-resolver-bundle

包简介

Pimcore Static Resolver Bundle

README 文档

README

What is the Static Resolver Bundle?

The Static Resolver Bundle is a Pimcore bundle designed to encapsulate static method calls within a service-oriented architecture. It provides a straightforward mechanism to transform static methods into injectable services, promoting a cleaner, more testable, and well-organized codebase.

In many PHP applications, including Pimcore, static methods are commonly used for utility functions and global operations. While convenient, static methods have several drawbacks:

  1. Testing Difficulties: Static methods are hard to mock in unit tests
  2. Tight Coupling: Code using static calls is tightly coupled to the implementation
  3. Hidden Dependencies: Static dependencies aren't explicit in class constructors

The Static Resolver Bundle addresses these issues by wrapping static calls in service classes that can be properly injected and mocked.

Architecture

The bundle follows a consistent pattern for each static class it wraps:

  1. Contract Interfaces: Define the methods that will be available (e.g., DbResolverContractInterface)
  2. Contract Implementations: Implement the interfaces by wrapping static calls (e.g., DbResolverContract)
  3. Bundle-specific Interfaces: Extend the contract interfaces and are marked as internal (e.g., DbResolverInterface)
  4. Bundle-specific Implementations: Extend the contract implementations and implement the bundle-specific interfaces (e.g., DbResolver)

This layered approach allows for flexibility and maintainability while keeping the public API clean.

How to Use the Static Resolver Bundle

Installation

  1. Install via Composer:

    composer require pimcore/static-resolver-bundle
  2. Enable the bundle in config/bundles.php:

    use Pimcore\Bundle\StaticResolverBundle\PimcoreStaticResolverBundle;
    // ...
    
    return [
        // ...
        PimcoreStaticResolverBundle::class => ['all' => true],
        // ...
    ];

Using Resolver Services

Instead of using static calls directly in your code, inject the appropriate resolver service and use its methods:

Before (with static calls):

use Pimcore\Tool\Authentication;

final class SecurityService implements SecurityServiceInterface
{
    public function getPimcoreUser(): User
    {
        $pimcoreUser = Authentication::authenticateSession();
        if (!$pimcoreUser instanceof User) {
            throw new Exception('No pimcore user found');
        }

        return $pimcoreUser;
    }
}

After (with resolver service):

use Pimcore\Bundle\StaticResolverBundle\Contract\Lib\Tools\Authentication\AuthenticationResolverContractInterface;

final class SecurityService implements SecurityServiceInterface
{
     public function __construct(private readonly AuthenticationResolverContractInterface $authenticationResolver
    ) {
    }

    public function getPimcoreUser(): User
    {
        $pimcoreUser = $this->authenticationResolver->authenticateSession();
        if (!$pimcoreUser instanceof User) {
            throw new Exception('No pimcore user found');
        }

        return $pimcoreUser;
    }
}

Available Resolver Services

The bundle provides resolver services for various Pimcore components:

  1. Database Operations:

    • Pimcore\Bundle\StaticResolverBundle\Contract\Db\DbResolverContractInterface
  2. Model Operations:

    • Various resolvers for DataObject, Asset, Document, etc.
  3. Library Tools:

    • Authentication, Console, Email, etc.

Benefits of Using Resolver Services

  1. Improved Testability: Services can be easily mocked in unit tests
  2. Explicit Dependencies: Dependencies are clearly visible in constructor parameters
  3. Loose Coupling: Code depends on interfaces rather than concrete implementations
  4. Consistent API: The resolver services provide a consistent API for static functionality

Example Use Cases

Database Operations

Instead of using Pimcore\Db static methods:

use Pimcore\Db;

$connection = Db::getConnection();
$data = $connection->fetchAllAssociative('SELECT * FROM users');

Use the DbResolver service:

use Pimcore\Bundle\StaticResolverBundle\Contract\Db\DbResolverContractInterface;

class UserRepository
{
    public function __construct(private readonly DbResolverContractInterface $dbResolver)
    {
    }

    public function getAllUsers(): array
    {
        $connection = $this->dbResolver->getConnection();
        return $connection->fetchAllAssociative('SELECT * FROM users');
    }
}

Authentication

Instead of using Pimcore\Tool\Authentication static methods:

use Pimcore\Tool\Authentication;

$user = Authentication::authenticateSession();

Use the AuthenticationResolver service:

use Pimcore\Bundle\StaticResolverBundle\Contract\Lib\Tools\Authentication\AuthenticationResolverContractInterface;

class AuthService
{
    public function __construct(private readonly AuthenticationResolverContractInterface $authenticationResolver)
    {
    }

    public function getCurrentUser()
    {
        return $this->authenticationResolver->authenticateSession();
    }
}

Internal Use Examples

For internal Pimcore use, you can use the Bundle-specific Interfaces (marked as @internal). These interfaces extend the Contract Interfaces and may provide additional functionality specific to Pimcore's internal needs.

Database Operations (Internal Use)

use Pimcore\Bundle\StaticResolverBundle\Db\DbResolverInterface;

/**
 * @internal This class is for internal Pimcore use only
 */
class InternalPimcoreService
{
    public function __construct(private readonly DbResolverInterface $dbResolver)
    {
    }

    public function executeInternalQuery(): array
    {
        $connection = $this->dbResolver->getConnection();
        return $connection->fetchAllAssociative('SELECT * FROM internal_table');
    }
}

Authentication (Internal Use)

use Pimcore\Bundle\StaticResolverBundle\Lib\Tools\Authentication\AuthenticationResolverInterface;

/**
 * @internal This class is for internal Pimcore use only
 */
class InternalAuthService
{
    public function __construct(private readonly AuthenticationResolverInterface $authenticationResolver)
    {
    }

    public function validateInternalUser()
    {
        return $this->authenticationResolver->authenticateSession();
    }
}

Important Notes

  • All Contract Interfaces in this bundle are designed for third-party developers to use
  • Bundle-specific Interfaces (marked as @internal) are intended for internal Pimcore use only
  • The bundle provides a transition path from static methods to a more service-oriented architecture

Documentation Overview

pimcore/static-resolver-bundle 适用场景与选型建议

pimcore/static-resolver-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 439.18k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 06 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pimcore/static-resolver-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pimcore/static-resolver-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 pimcore/static-resolver-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 439.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 13
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 8
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2023-06-15