2017 © Pedro Peláez
 

library silex-simple-annotations

Provide annotations for silex

image

nicolascharpentier/silex-simple-annotations

Provide annotations for silex

  • Thursday, July 14, 2016
  • by NicolasCharpentier
  • Repository
  • 1 Watchers
  • 10 Stars
  • 29 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

silex-simple-annotations

Warning

Not recommanded for production, as it has no cache, and the project is unfinished (but working)., (*1)

When is the last time you dreamed of annotations for every controller route?

Silex Simple Annotations allows you to get rid of controller providers and have it done your(our) way., (*2)

Prepend your controller function with annotations. We will deal with the rest., (*3)

    /**
     * @Path /login 
     * @Method POST
     * @Bind user.login
     */
    public function loginRegisterAction(Application $app, Request $request)
    {
        $user = SomeSuperStaticClass::loginOrRegisterThisGuy($request, $app);
        return $app->json();
    }

This examples links a POST on baseUrl/SomeControllerPrefixRouteYouGave/login to the loginRegisterAction action, (*4)

Why should I use this provider?

Currently, there seem to be only 2 providers after a quick Google search, (*5)

  • https://github.com/danadesrosiers/silex-annotation-provider
  • https://github.com/dcousineau/orlex

They both use Doctrine\Annotations to parse annotations and seem to work., (*6)

So why? * You hate Doctrine (strange) * You want a simple syntax * You are lazy and just want to provide your /src for controllers * You may need an automatic documentation. This will come in the future, (*7)

Why not? * These other providers are probably faster for parsing * I'm french, (*8)

What can i do? Is this FREE?

Registering

First, add this to your composer.json dependencies, (*9)

"require": {
    "other/packages...": "...",
    "nicolascharpentier/silex-simple-annotations": "dev-master"
  },

Then register the following service provider wherever you do so for your project:, (*10)

$app->register(new SilexSimpleAnnotations\AnnotationsServiceProvider(), array(
    'simpleAnnots.controllersPath' => __DIR__ . '/../src/Controller',
    'simpleAnnots.recursiv' => false, // Optional, default to false,
    'simpleAnnots.controllersAsApplicationAwareServices' => false // Optional, default to false. Will instanciate controllers with $app as services
));

Parameters start with simpleAnnots and end with: * controllersPath : String or Array of controller directory/ies * recursiv : Boolean, if true the search for controllers will be .. recursiv. Not recommanded for optimization., (*11)

Using

Loop the following for each controller you want to be annotation-equiped., (*12)

  1. Give a prefix for the controller (which will be prepend for every action route), (*13)

    /**
     * @Prefix /user
     */
    class UserController {
    
  2. Pimp your actions, (*14)

    /**
     * @Path /logout
     */
    public function logoutAction(Application $app)
    {
        AbstractUserManager::disconnectCurrentUser($app);
    
        return $app->json();
    }
    
    /**
     * @Path /stalk/{id}
     * @Bind user.stalkhim
     */
    public function stalkAction(Application $app, $id)
    {
        AbstractUserManager::disconnectCurrentUser($app);
    
        return $app->json();
    }
    

    This will mean, (*15)

    • a GET on /user/logout will call the logoutAction, which will be binded to user.logout
    • a GET on /user/stalk/1 will call the stalkAction, with $id === '1' and the bind on user.stalkhim
  3. Test and enjoy!!, (*16)

Available annotations

  • Controller Level, (*17)

    • Prefix REQUIRED
    • Sets the route prefix for every actions, also will be used for the default binding value.
    • eg. /user
  • Actions Level, (*18)

    • Path REQUIRED
    • Sets the Route suffix.
    • eg. /edit/{id}
    • Bind OPTIONAL
    • Sets the Route binding.
    • Default to Prefix + '.' + actionName (str_replace($function_name, 'Action', ''))
    • eg. user.edit
    • Method OPTIONAL
    • Sets the request method.
    • Default to GET
    • Value(s) accepted GET POST PUT OPTIONS DELETE
    • eg. POST PUT

TODOS

  • ~~Get why the ordered list on this README just displays '1' at every step.~~
  • ~~Support multiple methods for a Route~~
  • Modify the Rules class to be more self explanable
  • Generalize annotations usage when building Controllers
  • Implement a cache system
  • Start implementing the automated documentation

Ps: Not working on this at the moment, still checking for issues., (*19)

Feedback & Contribution

Any sort of feedback is strongly encouraged and will be listened., (*20)

Also feel free to contribute or just ask for change, even the smallest, (*21)

"No more controller provider needed. Got my wife back. Love this" - George, Alabama., (*22)

"This is awesome, my legal e-commerce website got 200% more purchases" - Ben, Thailandia., (*23)

The Versions

14/07 2016

dev-master

9999999-dev

Provide annotations for silex

  Sources   Download

MIT

The Requires

  • php >= 5.3

 

The Development Requires

  • php >= 5.3

by Nicolas Charpenter

03/01 2016

dev-hard_modifications

dev-hard_modifications

Provide annotations for silex

  Sources   Download

The Requires

  • php >= 5.3

 

The Development Requires

  • php >= 5.3

by Nicolas Charpenter