Skip to content

Commit

Permalink
Update config options for Autoloader, Router, Response and View services
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfelles committed Dec 30, 2021
1 parent 635e0a5 commit 0e169c8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ public static function autoloader(string $instance = 'default') : Autoloader
if ($service) {
return $service;
}
$service = new Autoloader();
$config = static::config()->get('autoloader', $instance);
$service = new Autoloader($config['register'] ?? true, $config['extensions'] ?? '.php');
if (isset($config['namespaces'])) {
$service->setNamespaces($config['namespaces']);
}
Expand Down Expand Up @@ -529,10 +529,19 @@ public static function router(string $instance = 'default') : Router
return $service;
}
$config = static::config()->get('router', $instance);
$router = static::setService('router', new Router(
$service = static::setService('router', new Router(
static::response($config['response_instance'] ?? 'default'),
static::language($config['language_instance'] ?? 'default')
), $instance);
if (isset($config['auto_options']) && $config['auto_options'] === true) {
$service->setAutoOptions();
}
if (isset($config['auto_methods']) && $config['auto_methods'] === true) {
$service->setAutoMethods();
}
if ( ! empty($config['placeholders'])) {
$service->addPlaceholder($config['placeholders']);
}
if (isset($config['files'])) {
foreach ($config['files'] as $file) {
if ( ! \is_file($file)) {
Expand All @@ -541,7 +550,7 @@ public static function router(string $instance = 'default') : Router
Isolation::require($file);
}
}
return $router;
return $service;
}

/**
Expand Down Expand Up @@ -579,11 +588,22 @@ public static function response(string $instance = 'default') : Response
return $service;
}
$config = static::config()->get('response', $instance);
return static::setService(
'response',
new Response(static::request($config['request_instance'] ?? 'default')),
$instance
);
$service = new Response(static::request($config['request_instance'] ?? 'default'));
if ( ! empty($config['headers'])) {
$service->setHeaders($config['headers']);
}
if (isset($config['auto_etag']) && $config['auto_etag'] === true) {
$service->setAutoEtag();
}
if (isset($config['auto_language']) && $config['auto_language'] === true) {
$service->setContentLanguage(static::language()->getCurrentLocale());
}
if (isset($config['cache'])) {
$config['cache'] === false
? $service->setNoCache()
: $service->setCache($config['cache']['seconds'], $config['cache']['public'] ?? false);
}
return static::setService('response', $service, $instance);
}

/**
Expand Down Expand Up @@ -654,14 +674,8 @@ public static function view(string $instance = 'default') : View
if ($service) {
return $service;
}
$service = new View();
$config = static::config()->get('view', $instance);
if (isset($config['base_dir'])) {
$service->setBaseDir($config['base_dir']);
}
if (isset($config['extension'])) {
$service->setExtension($config['extension']);
}
$service = new View($config['base_dir'] ?? null, $config['extension'] ?? '.php');
if (isset($config['layout_prefix'])) {
$service->setLayoutPrefix($config['layout_prefix']);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,10 @@ public function testNegotiateLanguage() : void
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'jp,es;q=0.8,en;q=0.5,en-US;q=0.3';
self::assertSame('es', App::negotiateLanguage($language));
}

public function testResponse() : void
{
App::config()->add('response', ['cache' => false]);
self::assertSame(0, App::response()->getCacheSeconds());
}
}
2 changes: 2 additions & 0 deletions tests/configs/autoloader.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
return [
'default' => [
'register' => true,
'extensions' => '.php',
'classes' => [],
'namespaces' => [
'Tests\\MVC' => dirname(__DIR__),
Expand Down
9 changes: 9 additions & 0 deletions tests/configs/response.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
*/
return [
'default' => [
'headers' => [
'X-Version' => '3.6.9',
],
'auto_etag' => true,
'auto_language' => true,
'cache' => [
'seconds' => 60,
'public' => true,
],
'request_instance' => 'default',
],
];
5 changes: 5 additions & 0 deletions tests/configs/router.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
return [
'default' => [
'auto_options' => true,
'auto_methods' => true,
'placeholders' => [
'foo' => '(.*)',
],
'files' => [
__DIR__ . '/routes.php',
],
Expand Down

0 comments on commit 0e169c8

Please sign in to comment.