Oihana PHP System

helpers

Table of Contents

Functions

cacheCollection()  : SimpleCache|null
Creates a namespaced cache collection from a Key/Value store definition registered in the dependency injection container.
documentUrl()  : string
Generates a full document URL based on the project's base URL.

Functions

cacheCollection()

Creates a namespaced cache collection from a Key/Value store definition registered in the dependency injection container.

cacheCollection(Container $container, string $collection, string $definition) : SimpleCache|null

A cache collection is an isolated namespace within the same backend, allowing logical separation of cached values (e.g. per feature, domain, or module). This helper function retrieves a KeyValueStore from the container, and wraps its collection in a PSR-16 SimpleCache implementation.

Example usage:

// Retrieve a cache collection named "users"
$userCache = cacheCollection( $container , "users" , 'cache:memory' );

// Store and retrieve values
$userCache->set("id:42", ["name" => "Alice"]);
$data = $userCache->get("id:42");
Parameters
$container : Container

The DI container used to resolve the cache store definition.

$collection : string

The collection name (namespace) to create inside the cache store.

$definition : string

The container entry identifier of the base key/value store in the DI container.

Tags
throws
DependencyException

If the container fails to resolve the cache definition.

throws
NotFoundException

If the cache definition is not registered in the container.

see
https://www.scrapbook.cash
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
SimpleCache|null

A PSR-16 cache instance scoped to the given collection, or null if the definition is not found or not compatible.

documentUrl()

Generates a full document URL based on the project's base URL.

documentUrl([string $path = Char::EMPTY ][, ContainerInterface|null $container = null ][, string|null $definition = 'baseUrl' ][, bool $trailingSlash = false ]) : string

This helper function is commonly used in IoC container definitions of models to generate the accessible URL of a document or resource.

The function:

  1. Retrieves the base URL from the DI container using the provided definition key (default 'baseUrl').
  2. Joins the base URL with the provided relative path.
  3. Optionally appends a trailing slash.

Example usage:

use Psr\Container\ContainerInterface;

$url = documentUrl('uploads/image.png', $container);
// returns something like 'https://example.com/uploads/image.png'

$urlWithSlash = documentUrl('uploads', $container, 'baseUrl', true);
// returns 'https://example.com/uploads/'
Parameters
$path : string = Char::EMPTY

Relative path of the document (default: empty string).

$container : ContainerInterface|null = null

Optional DI container to fetch the base URL from.

$definition : string|null = 'baseUrl'

Key used to fetch the base URL from the container (default: 'baseUrl').

$trailingSlash : bool = false

Whether to append a trailing slash to the resulting URL (default: false).

Tags
throws
ContainerExceptionInterface

If an error occurs while retrieving the base URL from the container.

throws
NotFoundExceptionInterface

If the base URL definition is not found in the container.

author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

The fully resolved document URL.


        
On this page

Search results