Skip to content

Commit

Permalink
Merge pull request #206 from agungaprianto/add-settings-interface
Browse files Browse the repository at this point in the history
add settings interface
  • Loading branch information
l0gicgate authored Jan 9, 2021
2 parents 46a4a70 + 138cc82 commit 0aee592
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 20 deletions.
5 changes: 3 additions & 2 deletions app/dependencies.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
declare(strict_types=1);

use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
Expand All @@ -11,9 +12,9 @@
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
LoggerInterface::class => function (ContainerInterface $c) {
$settings = $c->get('settings');
$settings = $c->get(SettingsInterface::class);

$loggerSettings = $settings['logger'];
$loggerSettings = $settings->get('logger');
$logger = new Logger($loggerSettings['name']);

$processor = new UidProcessor();
Expand Down
21 changes: 13 additions & 8 deletions app/settings.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
<?php
declare(strict_types=1);

use App\Application\Settings\Settings;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder) {

// Global Settings Object
$containerBuilder->addDefinitions([
'settings' => [
'displayErrorDetails' => true, // Should be set to false in production
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
],
],
SettingsInterface::class => function () {
return new Settings([
'displayErrorDetails' => true, // Should be set to false in production
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
],
]);
}
]);
};
3 changes: 2 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use App\Application\Handlers\HttpErrorHandler;
use App\Application\Handlers\ShutdownHandler;
use App\Application\ResponseEmitter\ResponseEmitter;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;
Expand Down Expand Up @@ -46,7 +47,7 @@
$routes($app);

/** @var bool $displayErrorDetails */
$displayErrorDetails = $container->get('settings')['displayErrorDetails'];
$displayErrorDetails = $container->get(SettingsInterface::class)->get('displayErrorDetails');

// Create Request object from globals
$serverRequestCreator = ServerRequestCreatorFactory::create();
Expand Down
9 changes: 5 additions & 4 deletions src/Application/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public function __construct(LoggerInterface $logger)
}

/**
* @param Request $request
* @param Request $request
* @param Response $response
* @param array $args
* @param array $args
* @return Response
* @throws HttpNotFoundException
* @throws HttpBadRequestException
*/
public function __invoke(Request $request, Response $response, $args): Response
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->request = $request;
$this->response = $response;
Expand Down Expand Up @@ -98,7 +98,8 @@ protected function resolveArg(string $name)
}

/**
* @param array|object|null $data
* @param array|object|null $data
* @param int $statusCode
* @return Response
*/
protected function respondWithData($data = null, int $statusCode = 200): Response
Expand Down
7 changes: 4 additions & 3 deletions src/Application/Actions/User/UserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ abstract class UserAction extends Action

/**
* @param LoggerInterface $logger
* @param UserRepository $userRepository
* @param UserRepository $userRepository
*/
public function __construct(LoggerInterface $logger, UserRepository $userRepository)
{
public function __construct(LoggerInterface $logger,
UserRepository $userRepository
) {
parent::__construct($logger);
$this->userRepository = $userRepository;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Application/Settings/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace App\Application\Settings;

class Settings implements SettingsInterface
{
/**
* @var array
*/
private $settings;

/**
* Settings constructor.
* @param array $settings
*/
public function __construct(array $settings)
{
$this->settings = $settings;
}

/**
* @param string $key
* @return mixed
*/
public function get(string $key = '')
{
return (empty($key)) ? $this->settings : $this->settings[$key];
}
}
13 changes: 13 additions & 0 deletions src/Application/Settings/SettingsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace App\Application\Settings;

interface SettingsInterface
{
/**
* @param string $key
* @return mixed
*/
public function get(string $key = '');
}
4 changes: 2 additions & 2 deletions tests/Application/Actions/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
parent::__construct($loggerInterface);
}

public function action() :Response
public function action(): Response
{
return $this->respond(
new ActionPayload(
Expand Down Expand Up @@ -58,7 +58,7 @@ public function __construct(
parent::__construct($loggerInterface);
}

public function action() :Response
public function action(): Response
{
return $this->respondWithData(
[
Expand Down

0 comments on commit 0aee592

Please sign in to comment.