Skip to content

Commit

Permalink
[FIRST COMMIT]
Browse files Browse the repository at this point in the history
  • Loading branch information
SparSio committed Mar 2, 2015
1 parent 3b2e386 commit 5fa2eca
Show file tree
Hide file tree
Showing 10 changed files with 765 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Emacs
*~

# OS X
.DS_Store
._*
.Spotlight-V100
.Trashes

# test
tmp/

#composer
vendor/

# phpstorm
.idea

# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 ETNA SARL

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "etna/config-provider",
"description": "ETNA Config Provider",
"keywords": ["utils", "config", "provider", "silex"],
"license": "proprietary",
"authors": [
{
"name": "ETNA",
"email": "dev@etna-alternance.net",
"homepage": "http://etna-alternance.net"
}
],
"require": {
"php": ">=5.5",

"silex/silex": "1.x",

"doctrine/orm": "2.x",
"dflydev/doctrine-orm-service-provider": "1.0.*@dev",
"beberlei/DoctrineExtensions": "dev-master",

"monolog/monolog": "~1.10",
"Ibsciss/supermonolog-service-provider": "dev-master",
"dzunke/slack-bundle" : "dev-master"
},
"autoload": {
"psr-4": {
"ETNA\\Silex\\Provider\\Config\\": "src"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
},
"repositories": [
{
"type": "composer",
"url": "http://blu-composer.herokuapp.com"
}
]
}
36 changes: 36 additions & 0 deletions src/AbstractETNAIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ETNA\Silex\Provider\Config;

use Silex\Application;

abstract class AbstractETNAIndexer
{
protected $app;

/**
* @param Application $app
*/
public function __construct(Application $app)
{
$this->app = $app;
$this->app['elasticsearch.reindex'] = [$this, 'reindex'];
$this->app['elasticsearch.put_document'] = [$this, 'putDocument'];
$this->app['elasticsearch.remove_document'] = [$this, 'removeDocument'];
}

/**
* @return void
*/
abstract public function reindex();

/**
* @return array
*/
abstract public function putDocument();

/**
* @return void
*/
abstract public function removeDocument();
}
95 changes: 95 additions & 0 deletions src/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace ETNA\Silex\Provider\Config;

use Silex\Application;
use Silex\ServiceProviderInterface;

use Symfony\Component\HttpFoundation\Request;

use \ETNA\Silex\Provider\Auth\AuthServiceProvider;

class Auth implements ServiceProviderInterface
{
private $auth_config;
private $app;

public function __construct($auth_config = null)
{
$auth_config = $auth_config ?: [
"auth.api_path" => "^/?",
"auth.force_guest" => true,
"auth.cookie_expiration" => false,
"auth.before_function" => [$this, 'authBeforeFunction']
];
$this->auth_config = $auth_config;

$auth_url = getenv("AUTH_URL");
$auth_cookie_expiration = getenv("AUTH_COOKIE_EXPIRATION");

if (false === $auth_url) {
throw new \Exception("AUTH_URL doesn't exist");
}

$this->auth_config["auth.authenticator_url"] = $auth_url;

if (false !== $auth_cookie_expiration) {
// transforme la chaine 'false' reçu de l'env en booleen.
$auth_cookie_expiration = ($auth_cookie_expiration === 'false') ? false : $auth_cookie_expiration;

$this->auth_config["auth.cookie_expiration"] = $auth_cookie_expiration;
}
}

/**
*
* @{inherit doc}
*/
public function register(Application $app)
{
$this->app = $app;

if (true !== isset($app["application_env"])) {
throw new \Exception('$app["application_env"] is not set');
}

if (true !== isset($app["application_name"])) {
throw new \Exception('$app["application_name"] is not set');
}

if (true !== isset($app["application_path"])) {
throw new \Exception('$app["application_path"] is not set');
}

$this->auth_config["auth.app_name"] = $app["application_name"];

foreach ($this->auth_config as $conf_name => $conf_value) {
$app[$conf_name] = $conf_value;
}

$app["auth.public_key.tmp_path"] = "{$app['application_path']}/tmp/public-{$app['application_env']}.key";

$app->register(new AuthServiceProvider());
}

public function authBeforeFunction(Request $req)
{
// On autorise les OPTIONS sans auth
if ('OPTIONS' === $req->getMethod()) {
return;
}

if (!isset($req->user)) {
return $this->app->json("Authorization Required", 401);
}
}

/**
*
* @{inherit doc}
*/
public function boot(Application $app)
{
$app->before($app['auth.before_function']);
}
}
54 changes: 54 additions & 0 deletions src/Doctrine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace ETNA\Silex\Provider\Config;

use Silex\Provider\DoctrineServiceProvider;

use Silex\Application;
use Silex\ServiceProviderInterface;

class Doctrine implements ServiceProviderInterface
{
private $dbs_options;

/**
* @param null|string[] $dbs_options
*/
public function __construct(array $dbs_options = null)
{
$dbs_options = $dbs_options ?: [
"default",
];
$this->dbs_options = [];
foreach ($dbs_options as $db_name) {
$database_env = getenv(strtoupper("{$db_name}_DATABASE_URL"));
if (false === $database_env) {
throw new \Exception(strtoupper($db_name) . "_DATABASE_URL doesn't exist");

}
$this->dbs_options[$db_name] = [
"url" => $database_env,
"charset" => "utf8",
];
}
}

/**
*
* @{inherit doc}
*/
public function register(Application $app)
{
$app["dbs.options"] = $this->dbs_options;
$app->register(new DoctrineServiceProvider());
}

/**
*
* @{inherit doc}
*/
public function boot(Application $app)
{
return $app;
}
}
46 changes: 46 additions & 0 deletions src/DoctrineDbProfiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace ETNA\Silex\Provider\Config;

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

use Doctrine\DBAL\Logging\DebugStack;

class DoctrineDbProfiler implements ServiceProviderInterface
{
/**
*
* @{inherit doc}
*/
public function register(Application $app)
{
if (!isset($app["orm.em"])) {
throw new \Exception('$app["orm.em"] is not set');
}

if ($app["debug"]) {
$app["orm.profiler"] = new DebugStack();
$app["orm.em"]->getConnection()->getConfiguration()->setSQLLogger($app["orm.profiler"]);

$app->after(
function (Request $request, Response $response) use ($app) {
$response->headers->set("X-ORM-Profiler-Route", $request->getPathInfo());
$response->headers->set("X-ORM-Profiler-Count", count($app["orm.profiler"]->queries));
$response->headers->set("X-ORM-Profiler-Queries", json_encode($app["orm.profiler"]->queries));
}
);
}
}

/**
*
* @{inherit doc}
*/
public function boot(Application $app)
{
return $app;
}
}
Loading

0 comments on commit 5fa2eca

Please sign in to comment.