2017 © Pedro Peláez
 

library symlex-core

A micro-kernel and REST/Twig router components for bootstrapping Silex with the Symfony service container

image

symlex/symlex-core

A micro-kernel and REST/Twig router components for bootstrapping Silex with the Symfony service container

  • Tuesday, June 12, 2018
  • by lastzero
  • Repository
  • 1 Watchers
  • 6 Stars
  • 147 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 31 Versions
  • 2 % Grown

The README.md

Minimalistic Kernel and Routers based on Symfony Components

Latest Stable Version License Test Coverage Build Status Documentation Community Chat, (*1)

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, (*2)

As published by phpbenchmarks.com, Symlex adds significantly less overhead to REST requests than other common PHP frameworks:, (*3)

, (*4)

Our complete framework documentation can be found on docs.symlex.org. Tuzi Liu maintains a Chinese translation for us., (*5)

Kernel

The light-weight Symlex kernel can bootstrap almost any application. It is based on our 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()., (*6)

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 format you might know from Symfony:, (*7)

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., (*8)

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:, (*9)

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):, (*10)

  • 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., (*11)

The application's HTTP kernel class initializes the routers that were configured in the service container:, (*12)

<?php

namespace Symlex\Kernel;

class WebApp extends App
{
    protected $urlPrefix = '';

    public function __construct($appPath, $debug = false)
    {
        parent::__construct('web', $appPath, $debug);
    }

    public function init()
    {
        if ($this->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.)., (*13)

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), (*14)

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, (*15)

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)., (*16)

REST controller actions (invoked by RestRouter) always return arrays, which are automatically converted to valid JSON. Delete actions can return null ("204 No Content")., (*17)

Interceptors

HTTP interceptors can be used to perform HTTP authentication or other actions (e.g. blocking certain IP ranges) before routing a request:, (*18)

<?php

use Symlex\Kernel\App;

class WebApp extends App
{
    public function __construct($appPath, $debug = false)
    {
        parent::__construct('web', $appPath, $debug);
    }

    public function boot () {
        parent::boot();

        $container = $this->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., (*19)

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:, (*20)

$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):, (*21)

example:
    prefix: /example
    domain: www.example.com
    bootstrap: \Symlex\Kernel\WebApp
    config: web.yml
    debug: true
    path: vendors/foo/bar/app

default:
    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., (*22)

About

Symlex is maintained by Michael Mayer and aims to simplify agile Web development by providing a working system that promotes best practices by example. Michael released his first PHP framework 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!, (*23)

Feel free to send an e-mail to hello@symlex.org if you have any questions, need commercial support or just want to say hello. Contributions are welcome, even if it's just a tiny pull-request or bug report., (*24)

Donations

Please leave a star if you like this project, it provides enough motivation to keep going. Thank you very much! <3, (*25)

The Versions

12/06 2018

dev-master

9999999-dev https://github.com/symlex/symlex-core

A micro-kernel and REST/Twig router components for bootstrapping Silex with the Symfony service container

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

12/06 2018

v3.4.0

3.4.0.0 https://github.com/symlex/symlex-core

A micro-kernel and REST/Twig router components for bootstrapping Silex with the Symfony service container

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

06/09 2017

v3.3.0

3.3.0.0 https://github.com/symlex/symlex-core

A micro-kernel and REST/Twig router components for bootstrapping Silex with the Symfony service container

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

13/04 2017

v3.2.1

3.2.1.0 https://github.com/symlex/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

12/04 2017

v3.2.0

3.2.0.0 https://github.com/symlex/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

12/04 2017

v3.1.0

3.1.0.0 https://github.com/symlex/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

11/04 2017

v3.0.0

3.0.0.0 https://github.com/symlex/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

07/04 2017

v2.3.1

2.3.1.0 https://github.com/symlex/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

05/04 2017

v2.3.0

2.3.0.0 https://github.com/lastzero/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

05/04 2017

v2.2.0

2.2.0.0 https://github.com/lastzero/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

03/03 2017

v2.1.2

2.1.2.0 https://github.com/lastzero/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

24/02 2017

v2.1.1

2.1.1.0 https://github.com/lastzero/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

22/02 2017

v2.1.0

2.1.0.0 https://github.com/lastzero/symlex-core

Silex + Symfony: A micro-kernel and REST/Twig router components for Silex 2

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

11/02 2017

v2.0.3

2.0.3.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

07/02 2017

v2.0.2

2.0.2.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

07/02 2017

v2.0.1

2.0.1.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

07/02 2017

v2.0.0

2.0.0.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

07/02 2017

v1.1.2

1.1.2.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

06/02 2017

v1.1.1

1.1.1.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

05/02 2017

v1.1.0

1.1.0.0 https://github.com/lastzero/symlex-core

Silex with Symfony DI Container: Microkernel and REST/Twig router components

  Sources   Download

MIT

The Requires

 

The Development Requires

dependency injection rest silex symfony performance micro-framework

30/09 2016

v1.0.1

1.0.1.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

The Development Requires

silex symfony

04/12 2015

v1.0.0

1.0.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

The Development Requires

silex symfony

02/10 2014

v0.9

0.9.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

The Development Requires

silex symfony

17/09 2014

v0.8

0.8.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

The Development Requires

silex symfony

11/09 2014

v0.7

0.7.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

silex symfony

02/09 2014

v0.6

0.6.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

silex symfony

02/09 2014

v0.5

0.5.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

silex symfony

25/08 2014

v0.4

0.4.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

silex symfony

25/08 2014

v0.3

0.3.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

silex symfony

24/08 2014

v0.2

0.2.0.0 https://github.com/lastzero/symlex-core

Symlex core components (Silex with Symfony DI Container)

  Sources   Download

MIT

The Requires

 

silex symfony

24/08 2014