Download the PHP package christianjbrown/cloud-run-function-lib without Composer
On this page you can find all versions of the php package christianjbrown/cloud-run-function-lib. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download christianjbrown/cloud-run-function-lib
More information about christianjbrown/cloud-run-function-lib
Files in christianjbrown/cloud-run-function-lib
Package cloud-run-function-lib
Short Description A strongly-typed PHP 8.5+ framework for building Google Cloud Run functions HTTP endpoints that return a consistent JSON envelope with built-in header auth, CORS, and CDN cache-control headers.
License MIT
Informations about the package cloud-run-function-lib
Google Cloud Run Function framework
A strongly-typed PHP framework for building Google Cloud Run function HTTP endpoints that return a consistent JSON envelope. You write the business logic; the library handles header-based authorization, CORS, CDN cache-control headers, and uniform success/error responses.
It is built around PSR-7: you hand it a ServerRequestInterface and it returns a ResponseInterface. Configuration is read straight from your Cloud Run environment variables.
- Uniform envelope — every response carries
success,timestamp_unix, andtimestamp_iso8601, plusdata,version(the Cloud Run revision), orerroras appropriate. - Header authorization — optionally require a header key/value before running your handler.
- CORS + caching —
Access-Control-*,Vary,Cache-Control, andSurrogate-Controlheaders derived from config. - Safe error handling — user-friendly exceptions surface their message; anything else returns a generic error unless
DEBUGis on, and is logged to stderr either way.
:heavy_check_mark: Prerequisites
:bulb: If you're on MacOS and have Homebrew, PHP and Composer will install with brew install composer.
:building_construction: Installation
For your composer-enabled project:
:computer: Usage
Implement DataProviderInterface with your endpoint's logic — it receives the PSR-7 request and returns an array that becomes the response data:
Build the function with CloudRunFunctionFactory, which wires every default, and run the request:
If you need the config yourself (for example to wrap it in your own config object), build it with
the factory's transformer and pass it to create():
Every collaborator (RequestAuthorizerInterface, JsonResponseFactoryInterface, the config appliers)
is an interface injected through a constructor, so any of them can be replaced by building
CloudRunFunction yourself. A new environment variable is a new FunctionConfigApplierInterface
class registered in the transformer's list.
$response is a PSR-7 response ready to emit (e.g. with guzzlehttp/psr7's HTTP factories or your Cloud Run function's runtime).
Environment variables
FunctionConfigTransformer::transform() (via the factory) reads these keys (only K_REVISION is required — Cloud Run sets it automatically):
| Variable | Purpose |
|---|---|
K_REVISION |
Required. The revision id, surfaced as version in the response. |
DEBUG |
"true" to return raw exception messages instead of a generic error. |
REQUIRED_HEADER_KEY / REQUIRED_HEADER_VALUE |
Require this header on the request, else 401. |
REQUIRED_ORIGIN |
Value for Access-Control-Allow-Origin (enables the Vary header). |
USE_CACHE_TTL |
s-maxage / max-age seconds for successful responses. |
USE_BROWSER_CACHE_TTL |
max-age seconds for browsers only, when that should differ from the CDN's. See below. |
USE_CACHE_BUT_REQUEST_TTL |
stale-while-revalidate seconds. |
USE_CACHE_IF_ERROR_TTL |
stale-if-error seconds. |
Letting a purge reach visitors
By default the browser and the CDN are given the same TTL, so USE_CACHE_TTL=3600
produces:
That max-age is what makes a surrogate-key purge look like it did nothing: the
CDN drops its copy, but a visitor who loaded the page in the last hour keeps
theirs. Set USE_BROWSER_CACHE_TTL to split the two. With 0:
The browser now revalidates on every page load, which the CDN answers from its
own cache, so a purge is visible immediately. Fastly reads Surrogate-Control
in preference to Cache-Control and strips it before the response reaches the
client, so the CDN's own TTL and its stale-while-revalidate / stale-if-error
resilience are untouched.
Note what is not in that Cache-Control: the stale directives carry no s-
prefix, so leaving them there would let a browser serve a body days old of its
own accord and undo the point of revalidating. They are emitted on
Surrogate-Control only whenever USE_BROWSER_CACHE_TTL is set.
Leave the variable unset and the headers are exactly as they were before it existed.
Response shape
A successful response:
An error response omits data and adds error:
:rotating_light: Error handling
Inside your DataProviderInterface::getData(), throwing an exception that implements christianjbrown/user-friendly-exception's UserFriendlyExceptionInterface returns its message to the client (HTTP 500). Any other Throwable returns a generic "An unhandled error occurred" message — unless DEBUG is enabled, in which case the raw message is returned to aid debugging. In both cases the Throwable is written to stderr with error_log(), so the cause is kept in Cloud Logging against the failing request rather than discarded with the response. A failed authorization check short-circuits with "Not authorized" (HTTP 401) before your handler runs.
:arrow_up: Upgrading to 2.0
CloudRunFunction no longer builds its own collaborators, FunctionConfig is immutable, and the
response classes are gone. Everything is built by CloudRunFunctionFactory.
Building the function:
Building a config by hand (tests, for example):
Responses:
new FunctionConfigTransformer() with no arguments no longer works: it needs its list of appliers,
which CloudRunFunctionFactory::createConfigTransformer() supplies.
:memo: Changelog
Notable changes in each release are listed in CHANGELOG.md.
:page_facing_up: License
Released under the MIT License.
All versions of cloud-run-function-lib with dependencies
christianjbrown/user-friendly-exception Version ^1.0
guzzlehttp/guzzle Version ^7.15
psr/clock Version ^1.0
symfony/clock Version ^8.0