3

I am just starting a new Silex project. I am using the Cartalyst Sentry Authentication package and I wish to inject into my controller Service Controllers. Here is my attempt at using Silex's built in dependency container which extends Pimple. I would just like some feedback on whether I am going about things the right way and what I can improve.

$app['sentry'] = $app->share(function() use ($app) {
    $hasher = new Cartalyst\Sentry\Hashing\NativeHasher;
    $userProvider = new Cartalyst\Sentry\Users\Eloquent\Provider($hasher);
    $groupProvider = new Cartalyst\Sentry\Groups\Eloquent\Provider;
    $throttleProvider = new Cartalyst\Sentry\Throttling\Eloquent\Provider($userProvider);
    $session = new Cartalyst\Sentry\Sessions\NativeSession;
    $cookie = new Cartalyst\Sentry\Cookies\NativeCookie(array());

    $sentry = new Cartalyst\Sentry\Sentry(
        $userProvider,
        $groupProvider,
        $throttleProvider,
        $session,
        $cookie
    );

    Cartalyst\Sentry\Facades\Native\Sentry::setupDatabaseResolver(new PDO(
        $app['db.dsn'], 
        $app['db.options']['user'], 
        $app['db.options']['password']
    ));

    return $sentry;
});

Defining my controller:

// General Service Provder for Controllers
$app->register(new Silex\Provider\ServiceControllerServiceProvider());

$app['user.controller'] = $app->share(function() use ($app) {
    return new MyNS\UserController($app);
});

$app->get('/user', "user.controller:indexAction");

Here is my controller, note that app['sentry'] is available to my controller by injecting it into the constructor.

class UserController
{
    private $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function indexAction()
    {
            // just testing various things here....
        $user = $this->app['sentry']->getUserProvider()->findById(1);
        $sql = "SELECT * FROM genes";
        $gene = $this->app['db']->fetchAssoc($sql);
        $this->app['monolog']->addDebug(print_r($gene,true));
        return new JsonResponse($user);
    }

}
Hines Bourne
  • 600
  • 1
  • 7
  • 20
  • 2
    looks great , you can define extra shared services for every object you instanciante , it might give you more flexibility. – mpm May 07 '13 at 15:18
  • I believe you are on the right track, the code above should work. However, if you aim for modular design later on, I would advise to write a service provider that will expose the Sentry to pimple as a service. Here's the documentation for writing such a provider: http://silex.sensiolabs.org/doc/providers.html#creating-a-provider This solution will allow you to inject parameters to your service in a more elegant and reusable way, not to mention benefits for testing. – gbtekkie Sep 17 '13 at 10:06

1 Answers1

0

This is the process I go through after finding a vendor package I'd like to use. I'm using a simpler library in order to focus on setting up the service provider.

Install the new package via composer.

~$ composer require ramsey/uuid

Create the service provider.

<?php

namespace My\Namespaced\Provider;

use Silex\Application;
use Silex\ServiceProviderInterface;
use Rhumsaa\Uuid\Uuid;

class UuidServiceProvider implements ServiceProviderInterface
{
    public function register(Application $app)
    {
        $app['uuid1'] = $app->share(function () use ($app) {
            $uuid1 = Uuid::uuid1();
            return $uuid1;
        });        

        $app['uuid4'] = $app->share(function () use ($app) {
            $uuid4 = Uuid::uuid4();
            return $uuid4;
        });
    }

    public function boot(Application $app)
    {
    }
}

Register the service provider.

$app->register(new My\Namespaced\Provider\UuidServiceProvider());

Use the new service in a controller.

<?php

namespace My\Namespaced\Controller;

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ExampleController
{
    public function indexAction(Application $app, Request $request)
    {
        $uuid = $app['uuid4']->toString();
        return new Response('<h2>'.$uuid.'</h2>');
    }
}
ooXei1sh
  • 3,459
  • 2
  • 27
  • 47