Skip to content

Commit ff846d2

Browse files
committed
moves logic from front controller to Client app setup
1 parent 74a6e10 commit ff846d2

File tree

2 files changed

+52
-33
lines changed

2 files changed

+52
-33
lines changed

src/OAuth2Demo/Client/Client.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,44 @@
44

55
use Silex\Application;
66
use Silex\ControllerProviderInterface;
7-
use Silex\ControllerCollection;
8-
use Symfony\Component\HttpFoundation\Response;
7+
use Silex\Provider\SessionServiceProvider;
98

109
class Client implements ControllerProviderInterface
1110
{
12-
public function connect(Application $app)
11+
/**
12+
* function to set up the container for the Client app
13+
*/
14+
public function setup(Application $app)
1315
{
14-
// creates a new controller based on the default route
15-
$routing = $app['controllers_factory'];
16+
// create session object and start it
17+
$app->register(new SessionServiceProvider());
1618

17-
/* Set the container */
18-
// ensures this runs on default port
19+
if (!$app['session']->isStarted()) {
20+
$app['session']->start();
21+
}
22+
23+
// create curl object and ensure it runs on default port
1924
$port = is_numeric($_SERVER['SERVER_PORT']) ? intval($_SERVER['SERVER_PORT']) : 80;
2025
$app['curl'] = new Http\Curl(array('http_port' => $port));
26+
2127
// add twig extension
2228
$app['twig']->addExtension(new Twig\JsonStringifyExtension());
2329

24-
/* Set corresponding endpoints on the controller classes */
30+
// load parameters configuration
31+
$this->loadParameters($app);
32+
}
33+
34+
/**
35+
* Connect the controller classes to the routes
36+
*/
37+
public function connect(Application $app)
38+
{
39+
// set up the service container
40+
$this->setup($app);
41+
42+
// Load routes from the controller classes
43+
$routing = $app['controllers_factory'];
44+
2545
Controllers\Homepage::addRoutes($routing);
2646
Controllers\ReceiveAuthorizationCode::addRoutes($routing);
2747
Controllers\RequestToken::addRoutes($routing);
@@ -30,4 +50,27 @@ public function connect(Application $app)
3050

3151
return $routing;
3252
}
53+
54+
/**
55+
* Load the parameters configuration
56+
*/
57+
private function loadParameters(Application $app)
58+
{
59+
$parameterFile = __DIR__.'/../../../data/parameters.json';
60+
if (!file_exists($parameterFile)) {
61+
// allows you to customize parameter file
62+
$parameterFile = $parameterFile.'.dist';
63+
}
64+
$app['environments'] = array();
65+
if (!$parameters = json_decode(file_get_contents($parameterFile), true)) {
66+
throw new \Exception('unable to parse parameters file: '.$parameterFile);
67+
}
68+
// we are using an array of configurations
69+
if (!isset($parameters['client_id'])) {
70+
$app['environments'] = array_keys($parameters);
71+
$env = $app['session']->get('config_environment');
72+
$parameters = isset($parameters[$env]) ? $parameters[$env] : array_shift($parameters);
73+
}
74+
$app['parameters'] = $parameters;
75+
}
3376
}

web/index.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,13 @@
88

99
/** set up the silex application object */
1010
$app = new Silex\Application();
11+
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
1112
$app->register(new Silex\Provider\TwigServiceProvider(), array(
1213
'twig.path' => __DIR__.'/../views',
1314
));
14-
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
15-
$app->register(new Silex\Provider\SessionServiceProvider());
1615

1716
$app['debug'] = true;
1817

19-
/** start the session */
20-
if (!$app['session']->isStarted()) {
21-
$app['session']->start();
22-
}
23-
24-
/** load the parameters configuration */
25-
$parameterFile = __DIR__.'/../data/parameters.json';
26-
if (!file_exists($parameterFile)) {
27-
// allows you to customize parameter file
28-
$parameterFile = $parameterFile.'.dist';
29-
}
30-
$app['environments'] = array();
31-
if (!$parameters = json_decode(file_get_contents($parameterFile), true)) {
32-
throw new Exception('unable to parse parameters file: '.$parameterFile);
33-
}
34-
// we are using an array of configurations
35-
if (!isset($parameters['client_id'])) {
36-
$app['environments'] = array_keys($parameters);
37-
$env = $app['session']->get('config_environment');
38-
$parameters = isset($parameters[$env]) ? $parameters[$env] : array_shift($parameters);
39-
}
40-
$app['parameters'] = $parameters;
41-
4218
/** set up routes / controllers */
4319
$app->mount('/', new OAuth2Demo\Client\Client());
4420
$app->mount('/lockdin', new OAuth2Demo\Server\Server());

0 commit comments

Comments
 (0)