Skip to content

Commit

Permalink
local file path creation added to Server
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbruce committed Oct 28, 2021
1 parent 511d995 commit 34ab10d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 59 deletions.
60 changes: 2 additions & 58 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
exit;
}

/**
* Verifying the method is supported by the app
*/
if ($server->isUsingUnsupportedMethod()) {
$content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors')
->for('/405.md');
Expand All @@ -67,10 +64,6 @@
exit;
}


/**
* Verifying specified content area exists.
*/
$content = JoshBruce\Site\Content::init(
$projectRoot,
$server->contentUp(),
Expand Down Expand Up @@ -98,51 +91,7 @@
exit;
}

/**
* Bootsrap is complete: local response time 19ms
*
* Does the requested content exist?
*/
$requestUri = $_SERVER['REQUEST_URI'];
if ($requestUri === '/') {
$requestUri = '';
}

// TESTING
// $requestUri = '/does/not/exist'; // 404
//
// $requestUri = '/assets/favicons/favicon-16x16.png'; // file
//
// Check browser address becomes /design-your-life
// if ($requestUri !== '/design-your-life') { // redirecting
// $requestUri = '/self-improvement'; // redirecting
// } // redirecting

$requestIsForFile = strpos($requestUri, '.') > 0;

$localFilePath = $requestUri . '/content.md';

if ($requestIsForFile) {
$folderMap = [
'/css' => '/.assets/styles',
'/js' => '/.assets/scripts',
'/assets' => '/.assets'
];

$parts = explode('/', $requestUri);
$parts = array_filter($parts);
$first = array_shift($parts);

$folderMapKey = '/' . $first;

if (array_key_exists($folderMapKey, $folderMap)) {
$replace = $folderMap[$folderMapKey];

$localFilePath = str_replace($folderMapKey, $replace, $requestUri);
}
}

$content = $content->for($localFilePath);
$content = $content->for($server->filePathForRequest());
if ($content->notFound()) {
$content = $content->for(path: '/.errors/404.md');
JoshBruce\Site\Emitter::emitWithResponse(
Expand All @@ -162,12 +111,7 @@
exit;
}

/**
* Target file exists: local response time 27ms
*
* Handle file
*/
if ($requestIsForFile) {
if ($server->isRequestingFile()) {
JoshBruce\Site\Emitter::emitWithResponseFile(
200,
[
Expand Down
44 changes: 43 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ public function isMissingRequiredValues(): bool

public function isUsingUnsupportedMethod(): bool
{
$requestMethod = $this->serverGlobals['REQUEST_METHOD'];
$requestMethod = strtoupper($this->serverGlobals['REQUEST_METHOD']);
return ! in_array($requestMethod, $this->supportedMethods());
}

public function isRequestingFile(): bool
{
return strpos($this->requestUri(), '.') > 0;
}

/**
* @return string[] [description]
*/
Expand All @@ -74,4 +79,41 @@ public function contentFolder(): string
{
return strval($this->serverGlobals['CONTENT_FOLDER']);
}

public function filePathForRequest(): string
{
if ($this->isRequestingFile()) {
$folderMap = [
'/css' => '/.assets/styles',
'/js' => '/.assets/scripts',
'/assets' => '/.assets'
];

$parts = explode('/', $this->requestUri());
$parts = array_filter($parts);
$first = array_shift($parts);

$folderMapKey = '/' . $first;

if (array_key_exists($folderMapKey, $folderMap)) {
$replace = $folderMap[$folderMapKey];

return str_replace(
$folderMapKey,
$replace,
$this->requestUri()
);
}
return $this->requestUri();
}
return $this->requestUri() . '/content.md';
}

private function requestUri(): string
{
if ($this->serverGlobals['REQUEST_URI'] === '/') {
return '';
}
return $this->serverGlobals['REQUEST_URI'];
}
}
30 changes: 30 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@

use JoshBruce\Site\Server;

it('expected local file path', function() {
expect(
Server::init(serverGlobals())->filePathForRequest()
)->toBe(
'/content.md'
);

$serverGlobals = serverGlobals();
$serverGlobals['REQUEST_URI'] = '/some.file';

expect(
Server::init($serverGlobals)->filePathForRequest()
)->toBe(
'/some.file'
);
})->group('server');

it('can determine if request is for a file', function() {
expect(
Server::init(serverGlobals())->isRequestingFile()
)->toBeFalse();

$serverGlobals = serverGlobals();
$serverGlobals['REQUEST_URI'] = '/some.file';

expect(
Server::init($serverGlobals)->isRequestingFile()
)->toBeTrue();
})->group('server');

it('limits request methods', function() {
expect(
Server::init(serverGlobals())->isUsingUnsupportedMethod()
Expand Down

0 comments on commit 34ab10d

Please sign in to comment.