nyholm / symfony-runtime
Enables decoupling PHP apps from global state
Installs: 9
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 4
Forks: 0
Open Issues: 0
Type:composer-plugin
Requires
- php: >=7.2.5
- composer-plugin-api: ^1.0|^2.0
- symfony/polyfill-php80: ^1.15
Requires (Dev)
- composer/composer: ^1.0.2|^2.0
- symfony/console: ^4.4|^5
- symfony/dotenv: ^5.1
- symfony/http-foundation: ^4.4|^5
- symfony/http-kernel: ^4.4|^5
Conflicts
- symfony/dotenv: <5.1
This package is auto-updated.
Last update: 2024-11-23 01:36:30 UTC
README
This repository is a mirror of symfony/symfony#36652
Symfony Runtime enables decoupling apps from global state.
Getting Started
$ composer require nyholm/symfony-runtime
RuntimeInterface
The core of this component is the RuntimeInterface
which describes a high-order
runtime logic.
It is designed to be totally generic and able to run any application outside of the global state in 6 steps:
- your front-controller returns a closure that wraps your app;
- the arguments of this closure are resolved by
RuntimeInterface::resolve()
which returns aResolvedAppInterface
. This is an invokable with zero arguments that returns whatever object of yours represents your app (e.g a Symfony kernel or response, a console application or command); - this invokable is called and returns this object that represents your app;
- your app object is passed to
RuntimeInterface::start()
, which returns aStartedAppInterface
: an invokable that knows how to "run" your app; - that invokable is called and returns the exit status code as int;
- the PHP engine is exited with this status code.
This process is extremely flexible as it allows implementations of
RuntimeInterface
to hook into any critical steps.
Autoloading
This package registers itself as a Composer plugin to generate a
vendor/autoload_runtime.php
file. You need to require it instead of the usual
vendor/autoload.php
in front-controllers that leverage this component and
return a closure.
Before requiring the vendor/autoload_runtime.php
file, you can set the
$_SERVER['APP_RUNTIME']
variable to a class that implements RuntimeInterface
and that should be used to run the app.
A SymfonyRuntime
is used by default. It knows the conventions to run
Symfony and native PHP apps.
Examples
This public/index.php
is a "Hello World" that handles a "name" query parameter:
<?php require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; return function (array $request, array $context): string { // $request holds keys "query", "data", "files" and "session", // which map to $_GET, $_POST, $_FILES and &$_SESSION respectively // $context maps to $_SERVER $name = $request['query']['name'] ?? 'World'; $time = $context['REQUEST_TIME']; return sprintf('Hello %s, the current Unix timestamp is %s.', $name, $time); };
This bin/console.php
is a single-command "Hello World" app
(run composer require symfony/console
before launching it):
<?php use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; return function (Command $command) { $command->addArgument('name', null, 'Who should I greet?', 'World'); return function (InputInterface $input, OutputInterface $output) { $name = $input->getArgument('name'); $output->writeln(sprintf('Hello <comment>%s</>', $name)); }; };
The SymfonyRuntime
can resolve and handle many types related to the
symfony/http-foundation
and symfony/console
components.
Check its source code for more information.