Skip to content

Instantly share code, notes, and snippets.

@HeahDude
Last active January 27, 2016 16:14
Show Gist options
  • Save HeahDude/de32f548607913778cad to your computer and use it in GitHub Desktop.
Save HeahDude/de32f548607913778cad to your computer and use it in GitHub Desktop.
Silex skeleton
<?php
/* /app/bootstrap.php */
use Silex\Application;
use DevTeam\Providers\CustomServiceProvider;
$app = new Application(array('debug' => $debug));
$app->register(new CustomServiceProvider(), array(
'param_1' => 'a string !',
'param_2' => 128,
'cache' => __DIR__ . '/../var/cache'
));
// ... register some other providers
return $app;
<?php
/* /src/DevTeam/Providers/CustomServiceProvider.php */
namespace DevTeam/Providers;
use DevTeam\Debug\SomeDebugClass;
use DevTeam\Listeners\MyListenerClass;
use DevTeam\Listeners\MyDebugListenerClass;
use DevTeam\Utils\SomeServiceClass;
use DevTeam\Utils\CallableService;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
use Silex\Api\BootableProviderInterface;
use Silex\Api\ControllerProviderInterface;
use Silex\Api\EventListenerProviderInterface;
use Silex\Application;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CustomServiceProvider implements ServiceProviderInterface, BootableProviderInterface, EventListenerProviderInterface
{
/*
* Cette fonction permet d'ajouter des paramètres ou des service au container $app.
*
* Attention
* =========
* Elle peut passer des services par référence mais ne doit pas en faire l'usage.
* Si un service doit être instancié implémenter le code dans $this->boot()
*/
public function register(Container $app)
{
$app['my_service.options'] = function ($c) { // $c = $app
return array(
'param_1' => $c['param_1'] ?: 'default string',
'param_2' => isset($c['param_2']) ? $c['param_2'] : 0,
// or with php7 : 'param_2' => $c['param_2'] ?? 0,
);
}
$app['my_service'] = function ($c) {
return new SomeServiceClass($c['request_stack'], $c['my_service.options'], $c['debug']);
}
$app['my_callable_service'] = $app->protect(function ($arg1, $arg2) use ($app) {
if ($app['debug']) {
$service = new SomeDebugClass($app['dispatcher']);
} else {
$service = new CallableService();
}
return $service->someMethod($arg1, $arg2);
});
$app['my_listener'] = function ($app) {
return $app['debug'] ? new MyDebugListenerClass($app['cache']) : new MyListenerClass();
}
}
/*
* Le code sera exécuté une fois tous les providers registered ($provider->register())
* et l'application bootée ($app->boot())
*/
public function boot(Application $app)
{
$app->mount('/some_prefix', $this->connect($app));
}
/*
* permet de souscrire des listeners ou des subscribers
* voir doc \Symfony\Component\EventDispatcher\EventListenerInterface et EventSubscriberInterface
*/
public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['my_listener']);
}
/*
* Implémentation des routes
*/
private function connect(Application $app)
{
$controllers = $app['controllers_factory'];
$controllers->get('/some/path/{var}/{param}', function (Application $app, $var) {
$service = $app['my_service'];
$object = $service->doSomething($var);
if ($data = $app['my_callable_service']($object, $param)) {
$response = array(
'code' => '200',
'content' => $data
);
} else {
$response = array('code' => '404', 'content' => 'What the fuck happened.');
}
return $app->json($response);
// équivalent à
// $response = new Response(json_encode($response));
// $response->headers->set('Content-Type', 'application/json');
//
// return $response ;
})
->bind('some_route_name')
->assert('var', 'regex')
->value('param', 'default')
;
return $controllers;
}
}
<?php
/* /web/index.php */
require_once '../vendor/autoload.php';
$debug = '127.0.0.1' === $_SERVER['REMOTE_ADDR']; // or any logic
$app = require_once '../app/bootstrap.php';
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment