Skip to content

Commit

Permalink
Merge pull request #1841 from ushahidi/lumen-remove-kohana
Browse files Browse the repository at this point in the history
Remove the remains of kohana
  • Loading branch information
rjmackay authored Jan 25, 2018
2 parents c6948c9 + a7d8bb8 commit 77e0530
Show file tree
Hide file tree
Showing 61 changed files with 271 additions and 3,279 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/API/MigrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public function index(Request $request, $command = 'status')
$command = 'status';
}

$db = service('db.config');
$phinx_config = ['configuration' => realpath(APPPATH . '../phinx.php'),
$phinx_config = [
'configuration' => base_path('phinx.php'),
'parser' => 'php',
];

Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/MigrateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class MigrateController extends Controller

public function migrate()
{
$db = service('db.config');
$phinx_config = [
'configuration' => realpath(APPPATH . '../phinx.php'),
'configuration' => base_path('phinx.php'),
'parser' => 'php',
];

Expand Down
65 changes: 25 additions & 40 deletions app/Http/Controllers/RESTController.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ protected function getRouteParams(Request $request)
/**
* Execute the usecase that the controller prepared.
*
* @todo should this take Usecase as a param rather than use $this->usecase?
*
* @throws HTTP_Exception_400
* @throws HTTP_Exception_403
* @throws HTTP_Exception_404
Expand Down Expand Up @@ -243,9 +245,6 @@ protected function executeUsecase(Request $request)
*/
protected function prepResponse(array $responsePayload = null, Request $request)
{
// Add CORS headers to the response
// $this->add_cors_headers($this->response);

// Use JSON if the request method is OPTIONS
if ($request->method() === Request::METHOD_OPTIONS) {
$type = 'json';
Expand All @@ -255,45 +254,31 @@ protected function prepResponse(array $responsePayload = null, Request $request)
$type = strtolower($request->query('format')) ?: 'json';
}

try {
//$format = service("formatter.output.$type");

// $body = $format($this->_response_payload);
// $mime = $format->getMimeType();
// $this->response->headers('Content-Type', $mime);

if (empty($responsePayload)) {
// If the payload is empty, return a 204
// https://tools.ietf.org/html/rfc7231#section-6.3.5
$response = response('', 204);
} else {
$response = response()->json(
$responsePayload,
200,
[],
env('APP_DEBUG', false) ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES : null
);

if ($type === 'jsonp') {
$response->withCallback($request->input('callback'));
// Prevent Opera and Chrome from executing the response as anything
// other than JSONP, see T455.
$response->headers->set('X-Content-Type-Options', 'nosniff');
}
}

// Should we prevent this request from being cached?
if (! in_array($request->method(), $this->cacheableMethods)) {
$response->headers->set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate');
if (empty($responsePayload)) {
// If the payload is empty, return a 204
// https://tools.ietf.org/html/rfc7231#section-6.3.5
$response = response('', 204);
} else {
$response = response()->json(
$responsePayload,
200,
[],
env('APP_DEBUG', false) ? JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES : null
);

if ($type === 'jsonp') {
$response->withCallback($request->input('callback'));
// Prevent Opera and Chrome from executing the response as anything
// other than JSONP
$response->headers->set('X-Content-Type-Options', 'nosniff');
}
}

return $response;
} catch (\Aura\Di\Exception\ServiceNotFound $e) {
abort(400, 'Unknown response format:' . $type);
} catch (\InvalidArgumentException $e) {
abort(400, 'Bad formatting parameters:' . $e->getMessage());
} catch (\Ushahidi\Core\Exception\FormatterException $e) {
abort(500, 'Error while formatting response:' . $e->getMessage());
// Should we prevent this request from being cached?
if (! in_array($request->method(), $this->cacheableMethods)) {
$response->headers->set('Cache-Control', 'no-cache, no-store, max-age=0, must-revalidate');
}

return $response;
}
}
199 changes: 95 additions & 104 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ class AppServiceProvider extends ServiceProvider
* @return void
*/
public function register()
{
$this->app->configure('cdn');
$this->app->configure('media');
$this->app->configure('ratelimiter');
$this->app->configure('multisite');
$this->app->configure('ohanzee-db');

$this->registerServicesFromAura();

$this->registerFilesystem();
$this->registerMailer();
$this->registerDataSources();

$this->configureAuraDI();

// Hack, must construct it to register route :/
$this->app->make('datasources');
}

public function registerServicesFromAura()
{
$this->app->singleton(\Ushahidi\Factory\UsecaseFactory::class, function ($app) {
// Just return it from AuraDI
Expand All @@ -22,20 +42,10 @@ public function register()
// Just return it from AuraDI
return service('repository.message');
});
}

$this->app->configure('cdn');
$this->app->configure('ratelimiter');
$this->app->configure('multisite');

// Add filesystem
$this->app->singleton('filesystem', function ($app) {
return $app->loadComponent(
'filesystems',
\Illuminate\Filesystem\FilesystemServiceProvider::class,
'filesystem'
);
});

public function registerMailer()
{
// Add mailer
$this->app->singleton('mailer', function ($app) {
return $app->loadComponent(
Expand All @@ -44,103 +54,68 @@ public function register()
'mailer'
);
});
}

public function registerFilesystem()
{
// Add filesystem
$this->app->singleton('filesystem', function ($app) {
return $app->loadComponent(
'filesystems',
\Illuminate\Filesystem\FilesystemServiceProvider::class,
'filesystem'
);
});
}

public function registerDataSources()
{
$this->app->singleton('datasources', function () {
return $this->app->loadComponent(
'datasources',
\Ushahidi\App\DataSource\DataSourceServiceProvider::class,
'datasources'
);
});

$this->configureAuraDI();

// Hack, must construct it to register route :/
$this->app->make('datasources');
}

// @todo move most of this elsewhere
protected function configureAuraDI()
{
$di = service();

// Multisite db
$di->set('site', function () use ($di) {
$site = '';

// Is this a multisite install?
$multisite = config('multisite.enabled');
if ($multisite) {
$site = $di->get('multisite')->getSite();
}

return $site;
});

// Site config
$di->set('site.config', function () use ($di) {
return $di->get('repository.config')->get('site')->asArray();
});

// Client Url
$di->set('clienturl', function () use ($di) {
return $this->getClientUrl($di->get('site.config'));
});

// Feature config
$di->set('features', function () use ($di) {
return $di->get('repository.config')->get('features')->asArray();
});

// Roles config settings
$di->set('roles.enabled', function () use ($di) {
$config = $di->get('features');

return $config['roles']['enabled'];
});

// Feature config
$di->set('features.limits', function () use ($di) {
$config = $di->get('features');

return $config['limits'];
});

// Webhooks config settings
$di->set('webhooks.enabled', function () use ($di) {
$config = $di->get('features');

return $config['webhooks']['enabled'];
});

// Post Locking config settings
$di->set('post-locking.enabled', function () use ($di) {
$config = $di->get('features');

return $config['post-locking']['enabled'];
});
$this->configureAuraServices($di);
$this->injectAuraConfig($di);
}

// Redis config settings
$di->set('redis.enabled', function () use ($di) {
$config = $di->get('features');
protected function configureAuraServices(\Aura\Di\ContainerInterface $di)
{
// Configure mailer
$di->set('tool.mailer', $di->lazyNew('Ushahidi\App\Tools\LumenMailer', [
'mailer' => app('mailer'),
'siteConfig' => $di->lazyGet('site.config'),
'clientUrl' => $di->lazyGet('clienturl')
]));

return $config['redis']['enabled'];
});
// Setup user session service
$di->set('session', $di->lazyNew(\Ushahidi\App\Tools\LumenSession::class, [
'userRepo' => $di->lazyGet('repository.user')
]));

// Data import config settings
$di->set('data-import.enabled', function () use ($di) {
$config = $di->get('features');
// Multisite db
$di->set('kohana.db.multisite', function () use ($di) {
$config = config('ohanzee-db');

return $config['data-import']['enabled'];
return \Ohanzee\Database::instance('multisite', $config['default']);
});

$di->set('features.data-providers', function () use ($di) {
$config = $di->get('features');

return array_filter($config['data-providers']);
// Deployment db
$di->set('kohana.db', function () use ($di) {
return \Ohanzee\Database::instance('deployment', $this->getDbConfig($di));
});
}

protected function injectAuraConfig(\Aura\Di\ContainerInterface $di)
{
// CDN Config settings
$di->set('cdn.config', function () use ($di) {
return config('cdn');
Expand All @@ -151,15 +126,22 @@ protected function configureAuraDI()
return config('ratelimiter');
});

// Private deployment config settings
// @todo move to repo
$di->set('site.private', function () use ($di) {
$site = $di->get('site.config');
$features = $di->get('features');
return $site['private']
and $features['private']['enabled'];
// Multisite db
// Move multisite enabled check to class and move to src/App
$di->set('site', function () use ($di) {
// @todo default to using the current domain
$site = '';

// Is this a multisite install?
$multisite = config('multisite.enabled');
if ($multisite) {
$site = $di->get('multisite')->getSite();
}

return $site;
});

// Move multisite enabled check to class and move to src/App
$di->set('tool.uploader.prefix', function () use ($di) {
// Is this a multisite install?
$multisite = config('multisite.enabled');
Expand All @@ -170,17 +152,26 @@ protected function configureAuraDI()
return '';
});

// Configure mailer
$di->set('tool.mailer', $di->lazyNew('Ushahidi\App\Tools\LumenMailer', [
'mailer' => app('mailer'),
'siteConfig' => $di->lazyGet('site.config'),
'clientUrl' => $di->lazyGet('clienturl')
]));
// Client Url
$di->set('clienturl', function () use ($di) {
return $this->getClientUrl($di->get('site.config'));
});
}

// @todo move to auth provider?
$di->set('session', $di->lazyNew(\Ushahidi\App\Tools\LumenSession::class, [
'userRepo' => $di->lazyGet('repository.user')
]));
protected function getDbConfig(\Aura\Di\ContainerInterface $di)
{
// Kohana injection
// DB config
$config = config('ohanzee-db');
$config = $config['default'];

// Is this a multisite install?
$multisite = config('multisite.enabled');
if ($multisite) {
$config = $di->get('multisite')->getDbConfig();
}

return $config;
}

protected function getClientUrl($config)
Expand Down
Loading

0 comments on commit 77e0530

Please sign in to comment.