eole-io /
eole-api
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Eole\Silex; |
||
| 4 | |||
| 5 | use Eole\Sandstone\Application as BaseApplication; |
||
| 6 | |||
| 7 | class Application extends BaseApplication |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * {@InheritDoc} |
||
| 11 | */ |
||
| 12 | public function __construct(array $values = array()) |
||
| 13 | { |
||
| 14 | parent::__construct($values); |
||
| 15 | |||
| 16 | $this->logErrors(); |
||
| 17 | $this->checkConstants(); |
||
| 18 | $this->loadEnvironmentParameters(); |
||
| 19 | $this->registerSilexProviders(); |
||
| 20 | $this->registerSecurity(); |
||
| 21 | $this->registerServices(); |
||
| 22 | $this->registerListeners(); |
||
| 23 | $this->loadAllServices(); |
||
| 24 | $this->registerDoctrine(); |
||
| 25 | $this->registerOAuth2Security(); |
||
| 26 | |||
| 27 | if ($this['debug']) { |
||
| 28 | $this->enableProfiler(); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Check whether application constants are well defined. |
||
| 34 | */ |
||
| 35 | private function checkConstants() |
||
| 36 | { |
||
| 37 | if (!isset($this['project.root'])) { |
||
| 38 | throw new \LogicException('project.root must be defined.'); |
||
| 39 | } |
||
| 40 | |||
| 41 | if (!isset($this['env'])) { |
||
| 42 | throw new \LogicException('env must be defined.'); |
||
| 43 | } |
||
| 44 | |||
| 45 | $environments = array('dev', 'docker', 'test', 'prod'); |
||
| 46 | |||
| 47 | if (!in_array($this['env'], $environments)) { |
||
| 48 | throw new \DomainException('env must be one of: "'.implode('", "', $environments).'".'); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Load config/environment.yml or config/environment.yml.dist. |
||
| 54 | * |
||
| 55 | * @throws Exception if file not found. |
||
| 56 | */ |
||
| 57 | private function loadEnvironmentParameters() |
||
| 58 | { |
||
| 59 | $parser = new \Symfony\Component\Yaml\Parser(); |
||
| 60 | $environmentFile = $this['project.root'].'/config/environment.yml'; |
||
| 61 | $extEnvironmentFile = $this['project.root'].'/config/environment_'.$this['env'].'.yml'; |
||
| 62 | |||
| 63 | if (!file_exists($environmentFile)) { |
||
| 64 | throw new \LogicException($environmentFile.' not found, unable to load environment parameters.'); |
||
| 65 | } |
||
| 66 | |||
| 67 | $environment = $parser->parse(file_get_contents($environmentFile)); |
||
| 68 | |||
| 69 | if (file_exists($extEnvironmentFile)) { |
||
| 70 | $extEnvironment = $parser->parse(file_get_contents($extEnvironmentFile)); |
||
| 71 | $environment = array_replace_recursive($environment, $extEnvironment); |
||
| 72 | } |
||
| 73 | |||
| 74 | $this['environment'] = $environment; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Register default silex providers |
||
| 79 | */ |
||
| 80 | private function registerSilexProviders() |
||
| 81 | { |
||
| 82 | $this->register(new \Silex\Provider\ServiceControllerServiceProvider()); |
||
| 83 | $this->register(new \Silex\Provider\MonologServiceProvider(), array( |
||
| 84 | 'monolog.name' => 'eole', |
||
| 85 | 'monolog.logfile' => $this['project.root'].'/var/logs/monolog_'.$this['env'].'.log', |
||
| 86 | )); |
||
| 87 | } |
||
| 88 | |||
| 89 | /* |
||
| 90 | * Register Symfony security |
||
| 91 | */ |
||
| 92 | private function registerSecurity() |
||
| 93 | { |
||
| 94 | $userProvider = function () { |
||
| 95 | return new \Alcalyn\UserApi\Security\UserProvider($this['eole.player_api']); |
||
| 96 | }; |
||
| 97 | |||
| 98 | $this->register(new \Silex\Provider\SecurityServiceProvider(), array( |
||
| 99 | 'security.firewalls' => array( |
||
| 100 | 'api' => array( |
||
| 101 | 'pattern' => '^/api', |
||
| 102 | 'oauth' => true, |
||
| 103 | 'stateless' => true, |
||
| 104 | 'anonymous' => true, |
||
| 105 | 'users' => $userProvider, |
||
| 106 | ), |
||
| 107 | ), |
||
| 108 | )); |
||
| 109 | |||
| 110 | $this['security.default_encoder'] = function () { |
||
| 111 | return $this['security.encoder.digest']; |
||
| 112 | }; |
||
| 113 | |||
| 114 | $this['eole.user_provider'] = $userProvider; |
||
| 115 | } |
||
| 116 | |||
| 117 | /* |
||
| 118 | * Register OAuth2 Security |
||
| 119 | */ |
||
| 120 | private function registerOAuth2Security() |
||
| 121 | { |
||
| 122 | $this->register(new \Eole\Sandstone\OAuth2\Silex\OAuth2ServiceProvider(), array( |
||
| 123 | 'oauth.firewall_name' => 'api', |
||
| 124 | 'oauth.security.user_provider' => 'eole.user_provider', |
||
| 125 | 'oauth.tokens_dir' => $this['project.root'].'/var/oauth-tokens', |
||
| 126 | 'oauth.scope' => $this['environment']['oauth']['scope'], |
||
| 127 | 'oauth.clients' => $this['environment']['oauth']['clients'], |
||
| 128 | )); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Register doctrine DBAL and ORM |
||
| 133 | */ |
||
| 134 | private function registerDoctrine() |
||
| 135 | { |
||
| 136 | $this->registerDoctrineDBAL(); |
||
| 137 | $this->registerDoctrineORM(); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Register doctrine DBAL |
||
| 142 | */ |
||
| 143 | private function registerDoctrineDBAL() |
||
| 144 | { |
||
| 145 | $this->register(new \Silex\Provider\DoctrineServiceProvider(), array( |
||
| 146 | 'db.options' => $this['environment']['database']['connection'], |
||
| 147 | )); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Register and configure doctrine ORM |
||
| 152 | */ |
||
| 153 | private function registerDoctrineORM() |
||
| 154 | { |
||
| 155 | $this->register(new \Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array( |
||
| 156 | 'orm.proxies_dir' => $this['project.root'].'/var/cache/doctrine/proxies', |
||
| 157 | 'orm.auto_generate_proxies' => $this['environment']['database']['orm']['auto_generate_proxies'], |
||
| 158 | 'orm.em.options' => array( |
||
| 159 | 'mappings' => $this['eole.mappings'], |
||
| 160 | ), |
||
| 161 | )); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Enable profiler interface for debugging. |
||
| 166 | * |
||
| 167 | * @throws \LogicException If composer dev dependencies are not loaded. |
||
| 168 | */ |
||
| 169 | private function enableProfiler() |
||
| 170 | { |
||
| 171 | if (!class_exists('\Silex\Provider\HttpFragmentServiceProvider', true)) { |
||
| 172 | throw new \LogicException('You must load composer dev dependencies in order to use profiler.'); |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->register(new \Silex\Provider\HttpFragmentServiceProvider()); |
||
| 176 | $this->register(new \Silex\Provider\TwigServiceProvider()); |
||
| 177 | |||
| 178 | $this->register(new \Silex\Provider\WebProfilerServiceProvider(), array( |
||
| 179 | 'profiler.cache_dir' => $this['project.root'].'/var/cache/profiler', |
||
| 180 | 'profiler.mount_prefix' => '/_profiler', |
||
| 181 | )); |
||
| 182 | |||
| 183 | $this->register(new \Sorien\Provider\DoctrineProfilerServiceProvider()); |
||
| 184 | $this->register(new \Eole\Sandstone\Push\Debug\PushServerProfilerServiceProvider()); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Register Eole services |
||
| 189 | */ |
||
| 190 | private function registerServices() |
||
| 191 | { |
||
| 192 | $this->register(new \Eole\Sandstone\Serializer\ServiceProvider()); |
||
| 193 | |||
| 194 | $this['serializer.builder']->setCacheDir($this['project.root'].'/var/cache/serializer'); |
||
| 195 | |||
| 196 | $this->register(new \Eole\Sandstone\Websocket\ServiceProvider(), [ |
||
| 197 | 'sandstone.websocket.server' => [ |
||
| 198 | 'bind' => $this['environment']['websocket']['server']['bind'], |
||
| 199 | 'port' => $this['environment']['websocket']['server']['port'], |
||
| 200 | ], |
||
| 201 | ]); |
||
| 202 | |||
| 203 | $this->register(new \Eole\Sandstone\Push\ServiceProvider(), [ |
||
| 204 | 'sandstone.push.enabled' => $this['environment']['push']['enabled'], |
||
| 205 | ]); |
||
| 206 | |||
| 207 | $this->register(new \Eole\Sandstone\Push\Bridge\ZMQ\ServiceProvider(), [ |
||
| 208 | 'sandstone.push.server' => [ |
||
| 209 | 'bind' => $this['environment']['push']['server']['bind'], |
||
| 210 | 'host' => $this['environment']['push']['server']['host'], |
||
| 211 | 'port' => $this['environment']['push']['server']['port'], |
||
| 212 | ], |
||
| 213 | ]); |
||
| 214 | |||
| 215 | View Code Duplication | $this['eole.mappings'] = function () { |
|
|
0 ignored issues
–
show
|
|||
| 216 | $mappings = array(); |
||
| 217 | |||
| 218 | $mappings []= array( |
||
| 219 | 'type' => 'yml', |
||
| 220 | 'namespace' => 'Alcalyn\UserApi\Model', |
||
| 221 | 'path' => $this['project.root'].'/vendor/alcalyn/doctrine-user-api/Mapping', |
||
| 222 | ); |
||
| 223 | |||
| 224 | return $mappings; |
||
| 225 | }; |
||
| 226 | |||
| 227 | $this['eole.listener.authorization_header_fix'] = function () { |
||
| 228 | return new \Alcalyn\AuthorizationHeaderFix\AuthorizationHeaderFixListener(); |
||
| 229 | }; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Register events listeners. |
||
| 234 | */ |
||
| 235 | private function registerListeners() |
||
| 236 | { |
||
| 237 | $this->on( |
||
| 238 | \Symfony\Component\HttpKernel\KernelEvents::REQUEST, |
||
| 239 | array( |
||
| 240 | $this['eole.listener.authorization_header_fix'], |
||
| 241 | 'onKernelRequest' |
||
| 242 | ), |
||
| 243 | 10 |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Load Eole and games services. |
||
| 249 | */ |
||
| 250 | View Code Duplication | private function loadAllServices() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 251 | { |
||
| 252 | foreach ($this['environment']['mods'] as $modName => $modConfig) { |
||
| 253 | $modClass = $modConfig['provider']; |
||
| 254 | $mod = new $modClass(); |
||
| 255 | $provider = $mod->createServiceProvider(); |
||
| 256 | |||
| 257 | if ($provider instanceof \Pimple\ServiceProviderInterface) { |
||
| 258 | $this->register($provider); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Get all GameProviders which are in environment. |
||
| 265 | * |
||
| 266 | * @return GameProvider[] |
||
| 267 | */ |
||
| 268 | public function getGameProviders() |
||
| 269 | { |
||
| 270 | $gameProviders = array(); |
||
| 271 | |||
| 272 | foreach ($this['environment']['mods'] as $modName => $modConfig) { |
||
| 273 | $modClass = $modConfig['provider']; |
||
| 274 | $mod = new $modClass(); |
||
| 275 | |||
| 276 | if ($mod instanceof GameProvider) { |
||
| 277 | $gameProviders[$modName] = $mod; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | return $gameProviders; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Log errors. |
||
| 286 | */ |
||
| 287 | private function logErrors() |
||
| 288 | { |
||
| 289 | $this->error(function (\Exception $e) { |
||
| 290 | $logFile = $this['project.root'].'/var/logs/errors.txt'; |
||
| 291 | $message = get_class($e).' '.$e->getMessage().PHP_EOL.$e->getTraceAsString().PHP_EOL.PHP_EOL; |
||
| 292 | file_put_contents($logFile, $message, FILE_APPEND); |
||
| 293 | }); |
||
| 294 | } |
||
| 295 | } |
||
| 296 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.