VOOZH about

URL: https://deepwiki.com/hypervel/object-pool/3.3-haspoolproxy-trait

⇱ HasPoolProxy Trait | hypervel/object-pool | DeepWiki


Loading...
Menu

HasPoolProxy Trait

Purpose and Scope

The HasPoolProxy trait provides a standardized way for host classes to manage multiple pool proxies with per-driver configuration and cleanup logic. This trait is designed to be used by classes that need to maintain multiple pooled resource connections (e.g., different database drivers, cache backends, or custom services).

For information about the PoolProxy class itself and how method interception works, see Pool Proxy. For information about creating and registering pools, see Pool Manager.

Sources: src/Traits/HasPoolProxy.php1-104


Overview

The HasPoolProxy trait enables a single host class to manage multiple object pools transparently through proxy objects. It provides a declarative approach to pool management by maintaining:

  1. Release Callbacks: Driver-specific cleanup logic executed before objects return to pools
  2. Poolables List: A registry of which drivers should use pooling
  3. Proxy Factory: A consistent method to create configured PoolProxy instances

This trait is particularly useful for manager classes that need to support multiple backends (e.g., a cache manager supporting Redis, Memcached, and file-based caching).

Sources: src/Traits/HasPoolProxy.php1-104 src/PoolProxy.php1-67


Trait Structure

Class Diagram


Sources: src/Traits/HasPoolProxy.php11-104 src/PoolProxy.php12-66


Core Functionality

Pool Proxy Creation

The createPoolProxy() method serves as a factory for creating PoolProxy instances with appropriate configuration:


Key Implementation Details:

AspectImplementationLocation
Pool Name Generationstatic::class . ':' . $driversrc/Traits/HasPoolProxy.php35
Proxy Class FlexibilitySupports custom proxy classes via poolProxyClass propertysrc/Traits/HasPoolProxy.php25-27
ValidationEnsures proxy class extends PoolProxy::classsrc/Traits/HasPoolProxy.php30-32
Release Callback IntegrationAutomatically retrieves and passes driver-specific callbacksrc/Traits/HasPoolProxy.php38

Sources: src/Traits/HasPoolProxy.php19-40


Release Callback Management

Release callbacks enable driver-specific cleanup logic before objects return to the pool. This is essential for resetting connection state, clearing buffers, or performing other cleanup operations.


Methods:

MethodPurposeReturn TypeFluent
setReleaseCallback(string $driver, Closure $callback)Register cleanup callback for driverstaticYes
getReleaseCallback(string $driver)Retrieve callback for driver?ClosureNo

Sources: src/Traits/HasPoolProxy.php42-58 src/PoolProxy.php60-62


Poolable Driver Management

The trait provides methods to manage a poolables array property, which typically declares which drivers should use object pooling. This allows selective pooling configuration.


Methods:

MethodBehaviorLocation
addPoolable(string $driver)Adds driver to poolables if not present; prevents duplicatessrc/Traits/HasPoolProxy.php63-70
removePoolable(string $driver)Removes driver from poolables; no-op if not foundsrc/Traits/HasPoolProxy.php74-85
getPoolables()Returns the current poolables arraysrc/Traits/HasPoolProxy.php89-93
setPoolables(array $poolables)Replaces the entire poolables arraysrc/Traits/HasPoolProxy.php97-103

Sources: src/Traits/HasPoolProxy.php60-104


Integration with Host Classes

Required Properties

The host class using this trait must define certain properties:

























PropertyRequiredPurposeDefault
poolablesYesArray of driver names that should use poolingN/A
poolProxyClassNoCustom PoolProxy subclass for specialized behaviorPoolProxy::class

Sources: src/Traits/HasPoolProxy.php25-27 src/Traits/HasPoolProxy.php65 src/Traits/HasPoolProxy.php92


Typical Usage Pattern


Sources: src/Traits/HasPoolProxy.php21-40


Method Reference

createPoolProxy()

Signature:


Parameters:

ParameterTypeDescription
$driverstringDriver identifier (e.g., "mysql", "redis")
$resolverClosureFactory function to create new pooled objects
$configarrayPool configuration (see Pool Configuration)
$proxyClass?stringOptional custom proxy class (must extend PoolProxy)

Returns: An instance of PoolProxy (or custom subclass)

Throws: InvalidArgumentException if $proxyClass does not extend PoolProxy::class

Pool Naming Convention: Pools are named as {HostClassName}:{driver} to ensure uniqueness across different host classes using the same driver identifier.

Sources: src/Traits/HasPoolProxy.php19-40


setReleaseCallback()

Signature:


Purpose: Registers a cleanup callback that will be executed after a method call completes but before the object is returned to the pool.

Fluent Interface: Returns $this for method chaining.

Sources: src/Traits/HasPoolProxy.php44-50


getReleaseCallback()

Signature:


Returns: The registered callback for the driver, or null if none is registered.

Sources: src/Traits/HasPoolProxy.php54-58


addPoolable()

Signature:


Purpose: Adds a driver to the poolables list if not already present. Uses in_array() check to prevent duplicates.

Fluent Interface: Returns $this for method chaining.

Sources: src/Traits/HasPoolProxy.php63-70


removePoolable()

Signature:


Purpose: Removes a driver from the poolables list. If the driver is not in the list, this is a no-op.

Fluent Interface: Returns $this for method chaining.

Sources: src/Traits/HasPoolProxy.php74-85


getPoolables()

Signature:


Returns: The current array of poolable driver names.

Sources: src/Traits/HasPoolProxy.php89-93


setPoolables()

Signature:


Purpose: Replaces the entire poolables array with a new set of driver names.

Fluent Interface: Returns $this for method chaining.

Sources: src/Traits/HasPoolProxy.php97-103


Implementation Considerations

Pool Name Uniqueness

The trait generates pool names using the pattern static::class . ':' . $driver. This ensures that:

  1. Different host classes can use the same driver identifier without conflicts
  2. Pool names are predictable and debuggable
  3. Multiple instances of the same host class share the same pool

Example:

  • App\Database\ConnectionManager:mysql
  • App\Database\ConnectionManager:postgres
  • App\Cache\CacheManager:redis

Sources: src/Traits/HasPoolProxy.php35


Extensibility Through Custom Proxy Classes

The trait supports custom proxy classes by checking for a poolProxyClass property on the host class. This allows specialized behavior while maintaining the trait's convenience:


Sources: src/Traits/HasPoolProxy.php23-32


Release Callback Execution Flow

Release callbacks integrate seamlessly with the PoolProxy.__call() mechanism:


Sources: src/PoolProxy.php53-65 src/Traits/HasPoolProxy.php38