2017 © Pedro Peláez
 

library silex-annotation-provider

A silex service provider that allows the use of annotations in ServiceControllers.

image

ddesrosiers/silex-annotation-provider

A silex service provider that allows the use of annotations in ServiceControllers.

  • Thursday, May 17, 2018
  • by danadesrosiers
  • Repository
  • 5 Watchers
  • 27 Stars
  • 65,386 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 12 Forks
  • 1 Open issues
  • 14 Versions
  • 7 % Grown

The README.md

Latest Stable Version Build Status Total Downloads License Scrutinizer Code Quality, (*1)

silex-annotation-provider

A Silex ServiceProvider that defines annotations that can be used in a Silex controller. Define your controllers in a class and use annotations to setup routes and define modifiers., (*2)

Changes in v3

  • Redesigned annotation format. No more Doctrine Annotations.
  • Uses PSR-16 cache instead of Doctrine Cache.
  • Simplified feature set. Removes support for ServiceProviders, custom Service Controller registration, and other obscure customization options in favor of simplicity.
  • Minimum PHP version is now 7.1. No official support for HHVM.

Installation

Install the silex-annotation-provider using composer., (*3)

{
    "require": {
        "ddesrosiers/silex-annotation-provider": "~3.0"
    }
}

Registration

$app->register(new DDesrosiers\SilexAnnotations\AnnotationServiceProvider(), array(
    "annot.cache" => new MyPsr16Cache(),
    "annot.controllerDir" => "$srcDir/Controller",
    "annot.controllers" => [
        MyClass1::class,
        MyClass2::class
    ]
));

Parameters

annot.controllerDir

Specify the directory in which to search for controllers. This directory will be searched recursively for classes with the @Controller annotation. Found controller classes will be processed for route annotations. Either this or annot.controllers is required to locate controllers., (*4)

annot.controllers

An array of fully qualified controller names. If set, the provider will automatically register each controller as a ServiceController and set up routes and modifiers based on annotations found., (*5)

annot.cache

An instance of a class that implements Psr\SimpleCache\CacheInterface. This cache is used to cache annotation and the controller list to improve performance., (*6)

Faster Controller Registration

Silex has to register every endpoint in your app on every request. If you have a lot of endpoints, that could be a significant overhead on each and every request. Silex Annotations can improve this by filtering the controllers that need to be registered using the prefix on the Controller annotation. We only need to register the endpoints in the Controller if the prefix matches the URI. In this way, Silex Annotations allows FASTER routing than pure Silex., (*7)

Annotate Controllers

Create your controller. The following is an example demonstrating the use of annotations to register an endpoint., (*8)

namespace DDesrosiers\Controller;

use Symfony\Component\HttpFoundation\Response;

/**
 * @Controller(
 *     prefix => test
 *     after => \DDesrosiers\Controller\TestController::converter
 *     host => www.test.com
 *     requireHttp
 *     secure => ADMIN
 * )
 */
class TestController
{
    /**
     * @Route(
     *     uri => GET test/{var}
     *     assert => var, \d+
     *     convert => var, \DDesrosiers\Controller\TestController::converter
     *     after => \DDesrosiers\Controller\TestController::converter
     *     host => www.test.com
     *     requireHttps
     *     secure => DEV
     *     value => var, default
     * )
     */
    public function testMethod($var)
    {
        return new Response("test Method: $var");
    }

    public static function converter($var)
    {
        return $var;
    }
}

The annotations in our TestController are interpreted as follows:, (*9)

$controllerCollection = $app['controller_factory']
    ->after('\DDesrosiers\Controller\TestController::converter');
    ->host('www.test.com');
    ->requireHttp();
    ->secure('ADMIN');
$controllerCollection->get("test/{var}", "\\DDesrosiers\\Controller\\TestController:testMethod")
    ->assert('var', '\d+')
    ->convert('var', "\\DDesrosiers\\Controller\\TestController::converter");
    ->host('www.test.com')
    ->requireHttps()
    ->secure('DEV')
    ->value('var', 'default');
$app->mount('/prefix', $controllerCollection);

Annotations

Controller, (*10)

The @Controller annotation marks a class as a controller. The 'prefix' option defines the mount point for the controller collection. The prefix must be the first option., (*11)

@Route, (*12)

The @Route annotation defines an endpoint. 'uri' is required and must be the first option defined., (*13)

Short Annotation Notation

In the Controller annotation, if prefix is the only option needed, the 'prefix' key can be omitted., (*14)

In the Route annotation, if uri is the only option needed, the 'uri' key can be omitted., (*15)

   namespace DDesrosiers\Controller;

   use Symfony\Component\HttpFoundation\Response;

   /**
    * @Controller(test)
    */
   class TestController
   {
       /**
        * @Route(GET test/{var})
        */
       public function testMethod($var)
       {
           return new Response("test Method: $var");
       }
   }

The Versions

17/05 2018

dev-master

9999999-dev

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

23/04 2018

dev-fast-register

dev-fast-register

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

20/03 2018

v2.0.1

2.0.1.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

28/01 2018

dev-support-php7

dev-support-php7

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

28/01 2018

dev-issue22

dev-issue22

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

25/01 2017

v2.0

2.0.0.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

08/04 2015

1.1.x-dev

1.1.9999999.9999999-dev

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

28/11 2014

v1.1.0

1.1.0.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

16/06 2014

v1.0.0

1.0.0.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Dana Desrosiers

service silex provider annotation

04/05 2014

v0.2.1

0.2.1.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

by Dana Desrosiers

service silex provider annotation

05/01 2014

v0.2.0

0.2.0.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

by Dana Desrosiers

service silex provider annotation

03/11 2013

v0.1.2

0.1.2.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

by Dana Desrosiers

service silex provider annotation

22/09 2013

v0.1.1

0.1.1.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

by Dana Desrosiers

service silex provider annotation

16/09 2013

v0.1.0

0.1.0.0

A silex service provider that allows the use of annotations in ServiceControllers.

  Sources   Download

MIT

The Requires

 

by Dana Desrosiers

service silex provider annotation