44
55use Silex \Application ;
66use Silex \ControllerProviderInterface ;
7- use Silex \ControllerCollection ;
8- use Symfony \Component \HttpFoundation \Response ;
7+ use Silex \Provider \SessionServiceProvider ;
98
109class 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}
0 commit comments