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:
- Testing Difficulties: Static methods are hard to mock in unit tests
- Tight Coupling: Code using static calls is tightly coupled to the implementation
- 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.
The bundle follows a consistent pattern for each static class it wraps:
- Contract Interfaces: Define the methods that will be available (e.g.,
DbResolverContractInterface
) - Contract Implementations: Implement the interfaces by wrapping static calls (e.g.,
DbResolverContract
) - Bundle-specific Interfaces: Extend the contract interfaces and are marked as internal (e.g.,
DbResolverInterface
) - 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.
-
Install via Composer:
composer require pimcore/static-resolver-bundle
-
Enable the bundle in
config/bundles.php
:use Pimcore\Bundle\StaticResolverBundle\PimcoreStaticResolverBundle; // ... return [ // ... PimcoreStaticResolverBundle::class => ['all' => true], // ... ];
Instead of using static calls directly in your code, inject the appropriate resolver service and use its methods:
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;
}
}
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;
}
}
The bundle provides resolver services for various Pimcore components:
-
Database Operations:
Pimcore\Bundle\StaticResolverBundle\Contract\Db\DbResolverContractInterface
-
Model Operations:
- Various resolvers for DataObject, Asset, Document, etc.
-
Library Tools:
- Authentication, Console, Email, etc.
- Improved Testability: Services can be easily mocked in unit tests
- Explicit Dependencies: Dependencies are clearly visible in constructor parameters
- Loose Coupling: Code depends on interfaces rather than concrete implementations
- Consistent API: The resolver services provide a consistent API for static functionality
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');
}
}
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();
}
}
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.
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');
}
}
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();
}
}
- 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