-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
293 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
C:37:"PHPUnit\Runner\DefaultTestResultCache":109:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:1:{s:49:"Tests\WebSocketComponentTest::one_and_one_are_two";d:0.001;}}} | ||
C:37:"PHPUnit\Runner\DefaultTestResultCache":701:{a:2:{s:7:"defects";a:3:{s:65:"Tests\ConfigTest::it_allows_to_retrieve_a_value_with_dot_notation";i:4;s:77:"Tests\ConfigTest::it_allows_to_retrieve_a_value_from_custom_or_default_config";i:4;s:49:"Tests\WebSocketComponentTest::one_and_one_are_two";i:3;}s:5:"times";a:5:{s:49:"Tests\WebSocketComponentTest::one_and_one_are_two";d:0.003;s:65:"Tests\ConfigTest::it_allows_to_retrieve_a_value_with_dot_notation";d:0.001;s:77:"Tests\ConfigTest::it_allows_to_retrieve_a_value_from_custom_or_default_config";d:0.001;s:74:"Tests\ConfigTest::it_allows_to_retrieve_a_value_from_default_or_alt_values";d:0;s:89:"Tests\WebSocketComponentTest::a_websocket_message_is_sent_to_connect_clients_on_new_email";d:0.003;}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
return [ | ||
'version' => '1.0.0', | ||
|
||
'smtp' => [ | ||
'host' => '127.0.0.1:8025', | ||
], | ||
|
||
'web' => [ | ||
'host' => '127.0.0.1:8080', | ||
], | ||
|
||
'websocket' => [ | ||
'host' => '127.0.0.1:1338', | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Mailamie; | ||
|
||
use Exception; | ||
|
||
class Config | ||
{ | ||
private array $params; | ||
private ?array $altParams; | ||
|
||
public function __construct(array $params, array $altParams = null) | ||
{ | ||
$this->params = $params; | ||
$this->altParams = $altParams; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @return array|mixed|null | ||
* @throws Exception | ||
*/ | ||
public function get(string $key) | ||
{ | ||
if ($this->altParams) { | ||
try { | ||
return static::dotGet($key, $this->altParams); | ||
} catch (Exception $e) { | ||
} | ||
} | ||
|
||
return static::dotGet($key, $this->params); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param array $data | ||
* @return array|mixed|null | ||
* @throws Exception | ||
*/ | ||
private static function dotGet(string $key, array $data) | ||
{ | ||
$keys = explode('.', $key); | ||
|
||
foreach ($keys as $innerKey) { | ||
if (!array_key_exists($innerKey, $data)) { | ||
throw new Exception('Cannot find the given key in config.'); | ||
} | ||
|
||
$data = $data[$innerKey]; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace Mailamie; | ||
|
||
use Mailamie\Emails\Store; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use React\Http\Message\Response; | ||
use Throwable; | ||
|
||
class WebController | ||
{ | ||
private Store $store; | ||
|
||
public function __construct(Store $store) | ||
{ | ||
$this->store = $store; | ||
} | ||
|
||
public function route(ServerRequestInterface $request): Response | ||
{ | ||
try { | ||
$path = $request->getUri()->getPath(); | ||
|
||
if (preg_match('/^\/api\//i', $path)) { | ||
return $this->handleApiCall($request); | ||
} | ||
|
||
$path = $this->convertToPublicPath($path); | ||
|
||
if (file_exists($path)) { | ||
if (static::endsWith($path, '.php')) { | ||
return $this->handlePhpFile($path); | ||
} | ||
|
||
return $this->handleStaticFile($path); | ||
} | ||
} catch (Throwable $e) { | ||
$this->serverError($e); | ||
} | ||
|
||
return $this->fileNotFoundError(); | ||
} | ||
|
||
private function convertToPublicPath(string $originalPath): string | ||
{ | ||
$path = $originalPath === '/' ? '/index.php' : $originalPath; | ||
return "public" . DIRECTORY_SEPARATOR . $path; | ||
} | ||
|
||
private function handleApiCall(ServerRequestInterface $request): Response | ||
{ | ||
$path = $request->getUri()->getPath(); | ||
|
||
try { | ||
if (preg_match('/^\/api\/messages\/?$/i', $path)) { | ||
return $this->json($this->store->all()); | ||
} | ||
|
||
if (preg_match('/^\/api\/messages\/(.*)$/i', $path, $matches)) { | ||
$id = (string)$matches[1]; | ||
$message = $this->store->get($id); | ||
return $this->json($message->toArray()); | ||
} | ||
} catch (Throwable $e) { | ||
return $this->serverError($e); | ||
} | ||
} | ||
|
||
private function handleStaticFile(string $path) | ||
{ | ||
return new Response( | ||
200, | ||
['Content-Type' => mime_content_type($path)], | ||
file_get_contents($path) | ||
); | ||
} | ||
|
||
private function handlePhpFile(string $path) | ||
{ | ||
ob_start(); | ||
require($path); | ||
$page = ob_get_contents(); | ||
ob_end_clean(); | ||
|
||
return new Response( | ||
200, | ||
['Content-Type' => 'text/html'], | ||
$page | ||
); | ||
} | ||
|
||
private function fileNotFoundError(): Response | ||
{ | ||
return new Response( | ||
404, | ||
['Content-Type' => 'text/html'], | ||
"<h2>404 - Page or content not found</h2>" | ||
); | ||
} | ||
|
||
private function serverError(Throwable $e): Response | ||
{ | ||
return new Response( | ||
500, | ||
['Content-Type' => 'text/html'], | ||
"<h2>{$e->getMessage()}</h2> <pre>{$e->getTraceAsString()}</pre>" | ||
); | ||
} | ||
|
||
/** | ||
* @param array|object $data | ||
* @return Response | ||
*/ | ||
private function json($data): Response | ||
{ | ||
return new Response( | ||
200, | ||
['Content-Type' => 'application/json'], | ||
json_encode($data) | ||
); | ||
} | ||
|
||
private static function endsWith(string $haystack, string $needle): bool | ||
{ | ||
$length = strlen($needle); | ||
if (!$length) { | ||
return true; | ||
} | ||
return substr($haystack, -$length) === $needle; | ||
|
||
} | ||
} |
Oops, something went wrong.