Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/symlex/symlex-core
Minimalistic Kernel and Router based on Symfony Components
https://github.com/symlex/symlex-core
dependency-injection kernel micro-framework php router symfony
Last synced: about 1 month ago
JSON representation
Minimalistic Kernel and Router based on Symfony Components
- Host: GitHub
- URL: https://github.com/symlex/symlex-core
- Owner: symlex
- License: mit
- Created: 2014-08-24T18:09:07.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2022-07-05T07:24:18.000Z (over 2 years ago)
- Last Synced: 2024-09-30T16:21:53.516Z (about 2 months ago)
- Topics: dependency-injection, kernel, micro-framework, php, router, symfony
- Language: PHP
- Homepage: https://docs.symlex.org
- Size: 129 KB
- Stars: 11
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Minimalistic Kernel and Routers based on Symfony Components
===========================================================[![Latest Stable Version](https://poser.pugx.org/symlex/symlex-core/v/stable.svg)](https://packagist.org/packages/symlex/symlex-core)
[![License](https://poser.pugx.org/symlex/symlex-core/license.svg)](https://packagist.org/packages/symlex/symlex-core)
[![Test Coverage](https://codecov.io/gh/symlex/symlex-core/branch/master/graph/badge.svg)](https://codecov.io/gh/symlex/symlex-core)
[![Build Status](https://travis-ci.org/symlex/symlex-core.png?branch=master)](https://travis-ci.org/symlex/symlex-core)
[![Documentation](https://readthedocs.org/projects/symlex-docs/badge/?version=latest&style=flat)](https://docs.symlex.org/en/latest/)
[![Community Chat](https://badges.gitter.im/symlex/community.png)](https://gitter.im/symlex/community)*Note: This repository contains the kernel and routers as reusable components. For more information and a
complete framework based on symlex-core please see https://github.com/symlex/symlex*As published by [phpbenchmarks.com](http://www.phpbenchmarks.com/en/benchmark/apache-bench/php-7.3/symlex-4.1.html),
Symlex adds [significantly less overhead](https://github.com/symlex/symlex/blob/master/README.md#performance) to REST
requests than other common PHP frameworks:Our complete framework documentation can be found on [docs.symlex.org](https://docs.symlex.org/en/latest/).
[Tuzi Liu](https://github.com/tuzimoe) maintains a [Chinese translation](https://docs.symlex.org/zh/latest/) for us.## Kernel ##
The light-weight Symlex kernel can bootstrap almost any application. It is based on our
[di-microkernel](https://github.com/symlex/di-microkernel) library. The kernel itself is just a few lines
to set environment parameters, initialize the Symfony service container and then start the app by calling `run()`.YAML files located in `config/` configure the application and all of it's dependencies as a service. The filename matches
the application's environment name (e.g. `config/console.yml`). The configuration can additionally be modified
for sub environments such as local or production by providing a matching config file like `config/console.local.yml`
(see `app.sub_environment` parameter). These files are in the same [well documented](https://symfony.com/doc/current/components/dependency_injection.html)
format you might know from Symfony:```yaml
parameters:
app.name: 'My App'
app.version: '1.0'services:
doctrine.migrations.migrate:
class: Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand
app:
class: Symfony\Component\Console\Application
arguments: [%app.name%, %app.version%]
public: true
calls:
- [ add, [ "@doctrine.migrations.migrate" ] ]
```This provides a uniform approach for bootstrapping Web applications such as `Symlex\Application\Web` or command-line
applications like `Symfony\Component\Console\Application` (wrapped in `Symlex\Application\Console`) using the same kernel.
The result is much cleaner and leaner than the usual bootstrap and configuration madness you know from many frameworks.### Disable Caching ###
If debug mode is turned off, the service container configuration is cached by the kernel in the directory set as cache path.
You have to delete all cache files after updating the configuration. To disable caching completely, add
`container.cache: false` to your config parameters:```yaml
parameters:
container.cache: false
```## Routers ##
There are three router classes included in this library. They configure the Symfony router component to perform the actual routing, so you can expect the same high performance.
After routing a request to the appropriate controller action, the router subsequently renders the response to ease controller testing (actions never directly return JSON or HTML):- `Symlex\Router\Web\RestRouter` handles REST requests (JSON)
- `Symlex\Router\Web\ErrorRouter` renders exceptions as error messages (HTML or JSON)
- `Symlex\Router\Web\TwigRouter` renders regular Web pages via Twig (HTML)
- `Symlex\Router\Web\TwigDefaultRouter` is like TwigRouter but sends all requests to a default controller action (required for client-side routing e.g. with Vue.js)It's easy to create your own custom routing/rendering based on the existing examples.
The application's HTTP kernel class initializes the routers that were configured in the service container:
```php
debug) {
ini_set('display_errors', 1);
}
}public function getUrlPrefix($urlPrefixPostfix = ''): string
{
return $this->urlPrefix . $urlPrefixPostfix;
}public function setUrlPrefix(string $urlPrefix)
{
$this->urlPrefix = $urlPrefix;
}protected function setUp()
{
$container = $this->getContainer();// The error router catches errors and displays them as error pages
$container->get('router.error')->route();// Routing for REST API calls
$container->get('router.rest')->route($this->getUrlPrefix('/api'), 'controller.rest.');// All other requests are routed to matching controller actions
$container->get('router.twig')->route($this->getUrlPrefix(), 'controller.web.');
}
}
```The REST and Twig routers accept optional URL (e.g. `/api`) and service name prefixes (e.g. `controller.rest.`).
Routing examples for the default HTTP kernel (`Symlex\Kernel\WebApp`):
- `GET /` will be routed to `controller.web.index` service's `indexAction(Request $request)`
- `POST /session/login` will be routed to `controller.web.session` service's `postLoginAction(Request $request)`
- `GET /api/users` will be routed to `controller.rest.users` service's `cgetAction(Request $request)`
- `POST /api/users` will be routed to `controller.rest.users` service's `postAction(Request $request)`
- `OPTIONS /api/users` will be routed to `controller.rest.users` service's `coptionsAction(Request $request)`
- `GET /api/users/123` will be routed to `controller.rest.users` service's `getAction($id, Request $request)`
- `OPTIONS /api/users/123` will be routed to `controller.rest.users` service's `optionsAction($id, Request $request)`
- `GET /api/users/123/comments` will be routed to `controller.rest.users` service's `cgetCommentsAction($id, Request $request)`
- `GET /api/users/123/comments/5` will be routed to `controller.rest.users` service's `getCommentsAction($id, $commendId, Request $request)`
- `PUT /api/users/123/comments/5` will be routed to `controller.rest.users` service's `putCommentsAction($id, $commendId, Request $request)`The routers pass on the request instance to each matched controller action as last argument. It contains request parameters and headers: http://symfony.com/doc/current/book/http_fundamentals.html#requests-and-responses-in-symfony
Controller actions invoked by **TwigRouter** can either return nothing (the matching Twig template will be rendered), an array (the Twig template can access the values as variables) or a string (redirect URL).
REST controller actions (invoked by **RestRouter**) always return arrays, which are automatically converted to valid JSON. Delete actions can return null ("204 No Content").
## Interceptors ##
HTTP interceptors can be used to perform HTTP authentication or other actions (e.g. blocking certain IP ranges) **before** routing a request:
```php
getContainer();/*
* In app/config/web.yml:
*
* services:
* http.interceptor:
* class: Symlex\Router\HttpInterceptor
*/
$interceptor = $container->get('http.interceptor');
$interceptor->digestAuth('Realm', array('foouser' => 'somepassword'));$container->get('router.error')->route();
$container->get('router.rest')->route('/api', 'controller.rest.');
$container->get('router.twig')->route('', 'controller.web.');
}
}
```## Run multiple kernels via `Symlex\Kernel\WebApps` ##
*Note: This is an experimental proof-of-concept. Feedback welcome.*
As an alternative to Symfony bundles, `Symlex\Kernel\WebApps` is capable of running multiple apps based on `Symlex\Kernel\App` on the same Symlex installation:
```php
$app = new WebApps('web', __DIR__ . '/../app', false);
$app->run();
```It's bootstrapped like a regular WebApp and subsequently bootstaps other Symlex apps according to the configuration in `app/config/web.guests.yml` (path, debug, prefix and domain are optional; bootstrap and config are required):
```yaml
example:
prefix: /example
domain: www.example.com
bootstrap: \Symlex\Kernel\WebApp
config: web.yml
debug: true
path: vendors/foo/bar/appdefault:
bootstrap: \Symlex\Kernel\WebApp
config: web.default.yml
```*Note: Assets in web/ like images, CSS or JavaScript in are not automatically shared in a way Assetic does this with Symfony bundles. If your apps not only provide Web services, you might have to create symbolic links or modify your HTML templates.*
## About ##
Symlex is maintained by [Michael Mayer](https://blog.liquidbytes.net/about) and
aims to simplify agile Web development by providing a working system that promotes best practices by example.
Michael released his [first PHP framework](http://freshmeat.sourceforge.net/projects/awf) in 2001 and
has previously worked with major framework vendors.
Building this would not have been possible without a lot of prior work by other developers.
Thank you to those and everyone who contributed!Feel free to send an e-mail to [[email protected]](mailto:[email protected]) if you have any questions,
need [commercial support](https://blog.liquidbytes.net/contact/) or just want to say hello.
Contributions are welcome, even if it's just a tiny pull-request or bug report.## Donations ##
Please leave a star if you like this project, it provides enough motivation to keep going.
Thank you very much! <3