Serverless Functions are great, but unfortunately Google Cloud Platform (and other cloud vendors) don't support PHP. Let's fill that gap.
Concis [kΙΜsi] is a PHP Serverless Functions Framework that allows you to register functions which will respond to a certain trigger. Triggers have an external origin and are, for example, an incoming HTTP Request or a Cloud Pub/Sub Message Event.
Concis comes with several Providers which provide you with a handful of EventHandlers (and accompanying abstractions / date types) that allow you to easily respond and work with these different types of triggers.
A Concis EventHandler can run on any webserver with PHP, or through one of the Runtime Containers.
Concis uses symfony/http-foundation
internally, utilizing its Symfony\Component\HttpFoundation\Request
and Symfony\Component\HttpFoundation\Response
classes.
A Concis EventHandler is expected to take a Symfony\Component\HttpFoundation\Request
instance as its input, and return a Symfony\Component\HttpFoundation\Response
instance which will be sent to the client.
However:
-
Providers can, and should, pre-process the
Symfony\Component\HttpFoundation\Request
instance and pass their extracted info into their registered Callbacks.For example: The
CloudStorage
Concis EventHandler from theGCPCloud
Provider will parse the POSTed JSON-message and build aCloudStorageObject
instance from that JSON. It is not theRequest
object but theCloudStorageObject
object that will then be passed into the registered function. -
If no
Response
instance is returned from a Concis EventHandler, Concis will try and cast it to aResponse
:- If a
string
is returned from a Concis EventHandler, Concis will create a newResponse
instance with the returnedstring
set as its content and send that. - If no response at all is returned from a Concis EventHandler, Concis will create a new β empty β
Response
instance and send that.
Note: By default
Response
instances have a HTTP Status Code of 200, so that will be used in the two scenarios listed above. - If a
-
An EventHandler that responds to a simple HTTP Request:
$eventHandler = new \Concis\Provider\Generic\Http\EventHandler(function(\Symfony\Component\HttpFoundation\Request $request) { $name = $request->query->get('name', 'Concis'); return new \Symfony\Component\HttpFoundation\Response("Hello, " . htmlspecialchars($name, ENT_QUOTES)); });
-
An EventHandler that responds to a Message being published on Google Cloub Pub/Sub:
$eventHandler = new \Concis\Provider\GCPCloud\Pubsub\EventHandler(function(string $topic, \Concis\Provider\GCPCloud\Pubsub\Datatype\PubsubMessage $message) { $data = $message->getJsonData(true); // β¦ });
To run one of these sample EventHandlers we'll use a simple index.php
file which we'll upload to a webserver.
$concis = new \Concis\Concis();
$eventHandler = /* β¦ */
$request = Request::createFromGlobals();
$response = $concis->handle(
$request,
$eventHandler
);
$response->send();
Alternatively we can use one of the Concis Runtimes
Out of the box Concis comes with these providers:
Please refer to each provider's documentation on how to use them.
(Submit a PR with your own Provider to have Concis support more platforms/triggers)
Concis allows EventHandlers to be accessed using a shorthand. That way your code becomes even more concise.
// Without shorthand:
$eventHandler = new \Concis\Provider\Generic\Http\EventHandler(function(\Symfony\Component\HttpFoundation\Request $request) {
// β¦
});
// With shorthand:
$eventHandler = \Concis\EventHandler::http(function(\Symfony\Component\HttpFoundation\Request $request) {
// β¦
});
Before you can use any of these shorthands, you'll need to register the provider with Concis. For example:
\Concis\Concis::registerProviderShorthands(new \Concis\Provider\Generic\Provider());
\Concis\Concis::registerProviderShorthands(new \Concis\Provider\GCPCloud\Provider());
@TODO
No you don't. You can run EventHandlers on any infrastructure.
Yes, you can use the generic HTTP Trigger.
There's a popular PHP Functions Framework for AWS that is named Bref. Concis is a synonym of Bref.
@TODO π
Concis is released under the MIT License (MIT). See the enclosed LICENSE
for details.