Skip to content

Commit

Permalink
Fixed internal resource setting
Browse files Browse the repository at this point in the history
  • Loading branch information
eldadfux committed Oct 21, 2020
1 parent dcc536e commit 03d9022
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ class App
*/
public function __construct($timezone)
{
$this->resources['utopia'] = $this;
self::setResource('utopia', function() {
return $this;
});

\date_default_timezone_set($timezone);
}

Expand Down Expand Up @@ -541,14 +544,18 @@ public function execute(Route $route, array $args = []): self
foreach ($groups as $group) {
if(isset(self::$errors[$group])) {
foreach (self::$errors[$group] as $error) { // Group shutdown hooks
$this->resources['error'] = $e;
self::setResource('error', function() use ($e) {
return $e;
});
\call_user_func_array($error['callback'], $this->getResources($error['resources']));
}
}
}

foreach (self::$errors['*'] as $error) { // Global error hooks
$this->resources['error'] = $e;
self::setResource('error', function() use ($e) {
return $e;
});
\call_user_func_array($error['callback'], $this->getResources($error['resources']));
}
}
Expand All @@ -568,8 +575,13 @@ public function execute(Route $route, array $args = []): self
*/
public function run(Request $request, Response $response): self
{
$this->resources['request'] = $request;
$this->resources['response'] = $response;
self::setResource('request', function() use ($request) {
return $request;
});

self::setResource('response', function() use ($response) {
return $response;
});

/*
* Re-order array
Expand Down Expand Up @@ -616,13 +628,17 @@ public function run(Request $request, Response $response): self
}
} catch (\Throwable $e) {
foreach (self::$errors['*'] as $error) { // Global error hooks
$this->resources['error'] = $e;
self::setResource('error', function() use ($e) {
return $e;
});
\call_user_func_array($error['callback'], $this->getResources($error['resources']));
}
}
} else {
foreach (self::$errors['*'] as $error) { // Global error hooks
$this->resources['error'] = new Exception('Not Found', 404);
self::setResource('error', function() {
return new Exception('Not Found', 404);
});
\call_user_func_array($error['callback'], $this->getResources($error['resources']));
}
}
Expand Down

0 comments on commit 03d9022

Please sign in to comment.