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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cloud-run-function-lib

Google Cloud Run Function framework

CI Coverage Packagist License PHP

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.

: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

PHP Build Version
Package Version
Requires php Version ^8.5
christianjbrown/user-friendly-exception Version ^1.0
guzzlehttp/guzzle Version ^7.15
psr/clock Version ^1.0
symfony/clock Version ^8.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package christianjbrown/cloud-run-function-lib contains the following files

Loading the files please wait ...