2017 © Pedro Peláez
 

library tinder

Clean code enabler for silex

image

davedevelopment/tinder

Clean code enabler for silex

  • Wednesday, June 19, 2013
  • by davedevelopment
  • Repository
  • 3 Watchers
  • 23 Stars
  • 134 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Tinder

:fire: :fire: With Silex and Tinder, your apps will be on fire :fire: :fire:, (*1)

Build Status, (*2)

A few helpers for Silex, that help you move boundaries outside your controllers where possible. It's a little hard to explain why you might want that, but eventually I'd like to have an example application that can show some of the benefits., (*3)

At this stage, Tinder is completely backwards-compatible with Silex, so you should be able to instantiate or extend Tinder\Application, rather than Silex\Application and your existing functionality will still work., (*4)

Table Of Contents

Controllers as Services

UPDATE - This is now available as the ServiceControllerServiceProvider the Silex Core, Tinder enables it by default., (*5)

Argument Injection

Tinder extends the default controller resolver to allow injecting HTTP GET or POST vars directly as controller arguments., (*6)

``` php <?php, (*7)

// an array called query will be GET vars $app->get("/search", function (array $query) { });, (*8)

// an array called params will be POST vars $app->post("/blog/posts", function (array $params) { });, (*9)


*Why?* This means your controllers can be dependent on an arbitrary array of query vars or parameters, rather than a `Symfony\Component\HttpFoundation\Request`. Want to share controller code between the CLI and Web interfaces? ## Template Rendering ## Tinder adds a simple route helper that allows you to return an array of context variables from your controllers and have them rendered by a twig template defined on the route. An optional second argument allows you to manipulate the returned data before being passed to the template, allowing for presenters or ViewModels and the like. ``` php <?php $app->get("/dave", function() { return array( "rating" => rand(1,10), ); }) ->template("dave.html.twig", function($data) { $data['rating'] = 10; return $data; });

Why? Why should your controller be responsible for rendering a template?, (*10)

Redirects

Redirecting to a URL after a successful action (e.g. POST/REDIRECT/GET) doesn't have to be the concern of your controller/use case/interactor, tinder provides a simple DSL to tell it to send a redirect response if the controller returns null., (*11)

``` php, (*12)

$app->post("/users/{id}", function() { return; }) ->redirect("/users");, (*13)


If you're using the `UrlGeneratorServiceProvider`, you send a string id and a callable, which must return the params for the url generator. The callable can use the same name and type hinting semantics as a regular controller. ``` php $app->get("/users/{id}", function($id) { }) ->bind("get_user"); $app->post("/users/{id}", function($id) { return; }) ->redirect("get_user", function($id) { return ["id" => $id]; });

Something similar can also be achieved if you're not using the generator, by having a callable generate the url., (*14)

``` php, (*15)

$app->post("/users/{id}", function($id) { return; }) ->redirect(function($id) { return "/users/$id"; });, (*16)



Event Listeners as Services =========================== Much like the controllers, Tinder includes a `PimpleAwareEventDispatcher`, allowing you to configure services as event listeners or subscribers, so that they are lazy loaded. ``` php <?php $app['dispatcher']->addListenerService('some.event', array('some_service_name', 'someMethod')); $app['dispatcher']->addSubscriberService('some_other_service', 'The\Fully\Qualified\ClassName\Of\That\Service');

Why? Performance. If you fully adopt the event dispatcher, your listeners are likely to be many, which in turn are likely to have many dependencies. This method saves loading them until they're actually needed., (*17)

Readme Driven Development

Most of this isn't implemented yet, but here's the kind of thing I'd like to get to, if it's even possible, (*18)

``` php <?php, (*19)

require "vendor/autoload.php", (*20)

$app = new Tinder\Application;, (*21)

$app->post("/user/{user}", "my_controller_as_service:post") ->convert("user", $someKindOfUserConverter) ->enforce("ROLE_ADMIN") // only admins can do this, could be a closure with true/false return or throw ->redirect("get_user", function(User $user) { // on success, go to the get_user url (no idea how I'll do the params yet) // params can be string as url or route name and/or closure for // pre-processing, returning an url if a string param is not present }) ->template("user_edit.html.twig", function(array $data) { // use this template and do some optional data conversion beforehand $data['user'] = new MyApp\Presenter\User($user); return $data; });, (*22)

$app->get("/some-page", function() {}) ->template("some_page.html.twig") ->cache(["expires" => "tomorrow"]); // http cache until tomorrow, (*23)

```, (*24)

Contributing

Get in touch before hacking on anything, I've no idea what I might be doing, the whole library may have changed since my last push :), (*25)

Copyright (c) 2012 Dave Marshall. See LICENCE for further details, (*26)

The Versions

19/06 2013

dev-master

9999999-dev

Clean code enabler for silex

  Sources   Download

The Requires

 

The Development Requires

13/05 2013

1.0.x-dev

1.0.9999999.9999999-dev

Clean code enabler for silex

  Sources   Download

The Requires

 

The Development Requires

13/05 2013

v1.0.0

1.0.0.0

Clean code enabler for silex

  Sources   Download

The Requires

 

The Development Requires