Skip to content

Instantly share code, notes, and snippets.

@davedevelopment
Created January 17, 2013 08:58
Show Gist options
  • Select an option

  • Save davedevelopment/4554678 to your computer and use it in GitHub Desktop.

Select an option

Save davedevelopment/4554678 to your computer and use it in GitHub Desktop.

Revisions

  1. davedevelopment created this gist Jan 17, 2013.
    61 changes: 61 additions & 0 deletions test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?php

    /**
    * Ideas for parameter converters
    *
    * I've used the word argument here rather than convert or converter,
    * because there may not be any converting as such. It may be that things are
    * plucked out of the request etc
    *
    * The idea being that some of the controller arguments come from the url,
    * some from the query or attributes and we're trying to make mapping routes to services easier and more explicit
    * /
    /**
    * Extend request with setArgument(or some aptly named method), allowing total flexibility
    */
    $app->post("/user/{user}/team", "user.controller.update_team:execute")
    ->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
    ->before(function(Request $request) use ($app) {
    $request->setArgument('team', $app['team.repository']->getUser($request->attributes->get('team_id')));
    });

    /**
    * Extend route with a flexible arguments(or some aptly named method),
    * that can return a key value map of items to be used as arguments
    */
    $app->post("/user/{user}/team", "user.controller.update_team:execute")
    ->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
    ->arguments(function(Request $request) use ($app) {
    return array(
    'team' => $app['team.repository']->getUser($request->attributes->get('team_id')),
    );
    });

    /**
    * Individual converters
    */
    $app->post("/user/{user}/team", "user.controller.update_team:execute")
    ->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
    ->argument('team', function(Request $request) use ($app) {
    return $app['team.repository']->getUser($request->attributes->get('team_id'));
    });


    /**
    * Nothing special, we just manually wrap the controller for these circumstances
    */
    $app->post("/user/{user}/team", function(User $user, Request $request, Application $app) {
    $team = $app['team.repository']->getUser($request->attributes->get('team_id'));
    return $app["user.controller.update_team"]->execute($user, $team);
    })
    ->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); });

    /**
    * Individual converters, specific to fields.
    */
    $app->post("/user/{user}/team", "user.controller.update_team:execute")
    ->convert('user', function($user) use ($app) { return $app['user.repository']->getUser($request->attributes->get('user_id')); })
    ->convertPost('team_id', "team", function($id) use ($app) {
    return $app['team.repository']->getUser($id));
    });