Skip to content

Commit

Permalink
Merge pull request #7 from 8fold/working
Browse files Browse the repository at this point in the history
Going back to OOP
  • Loading branch information
joshbruce authored Oct 28, 2021
2 parents 41fd3f0 + e224d00 commit 959883e
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 146 deletions.
135 changes: 40 additions & 95 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');

require __DIR__ . '/../vendor/autoload.php';
$projectRoot = implode('/', array_slice(explode('/', __DIR__), 0, -1));

require $projectRoot . '/vendor/autoload.php';

/**
* Rergardless of what happens next, we'll need a basline markown converter.
Expand All @@ -15,34 +17,12 @@
->smartPunctuation();

// Inject environment variables to global $_SERVER array
Dotenv\Dotenv::createImmutable(__DIR__ . '/../')->load();
Dotenv\Dotenv::createImmutable($projectRoot)->load();

$projectRoot = implode('/', array_slice(explode('/', __DIR__), 0, -1));
$server = JoshBruce\Site\Server::init($_SERVER);

/**
* Verifying setup is valid.
*/
$requestRequiredServerGlobals = [
'APP_ENV',
'CONTENT_UP',
'CONTENT_FOLDER',
'REQUEST_SCHEME',
'HTTP_HOST',
'REQUEST_URI'
];

// TESTING
// unset($_SERVER['APP_ENV']);
$requestHasRequiredServerGlobals = true;
foreach ($requestRequiredServerGlobals as $key) {
if (! array_key_exists($key, $_SERVER)) {
$requestHasRequiredServerGlobals = false;
break;
}
}

if (! $requestHasRequiredServerGlobals) {
$content = JoshBruce\Site\Content::init($projectRoot, 0, '/500-errors')
if ($server->isMissingRequiredValues()) {
$content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors')
->for('/500.md');

JoshBruce\Site\Emitter::emitWithResponse(
Expand All @@ -62,26 +42,36 @@
exit;
}

if ($_SERVER['APP_ENV'] !== 'production') {
$erroHandler = new Whoops\Run;
$erroHandler->pushHandler(new Whoops\Handler\PrettyPageHandler);
$erroHandler->register();
}
if ($server->isUsingUnsupportedMethod()) {
$content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors')
->for('/405.md');

/**
* Verifying specified content area exists.
*/
JoshBruce\Site\Emitter::emitWithResponse(
405,
[
'Cache-Control' => [
'no-cache',
'must-revalidate'
],
'Allow' => $server->supportedMethods()
],
Eightfold\HTMLBuilder\Document::create(
$markdownConverter->getFrontMatter($content->markdown())['title']
)->body(
$markdownConverter->convert($content->markdown())
)->build()
);
exit;
}

// TESTING
// $_SERVER['CONTENT_FOLDER'] = '/does/not/exist';
$content = JoshBruce\Site\Content::init(
$projectRoot,
$_SERVER['CONTENT_UP'],
$_SERVER['CONTENT_FOLDER']
$server->contentUp(),
$server->contentFolder()
);

if (! $content->folderDoesExist()) {
$content = JoshBruce\Site\Content::init($projectRoot, 0, '/500-errors')
if ($content->folderIsMissing()) {
$content = JoshBruce\Site\Content::init($projectRoot, 0, '/setup-errors')
->for('/502.md');

JoshBruce\Site\Emitter::emitWithResponse(
Expand All @@ -101,50 +91,14 @@
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
//
// TESTING: Redirection
// 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'
];
// if ($server->requestUri() !== '/design-your-life') {
// $_SERVER['REQUEST_URI'] = '/self-improvement';
// $server = JoshBruce\Site\Server::init($_SERVER);
// }

$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 @@ -164,12 +118,7 @@
exit;
}

/**
* Target file exists: local response time 27ms
*
* Handle file
*/
if ($requestIsForFile) {
if ($server->isRequestingFile()) {
JoshBruce\Site\Emitter::emitWithResponseFile(
200,
[
Expand All @@ -181,18 +130,14 @@
exit;
}

/**
* Target file wants to redirect us: local response time 40ms
*/
$redirectPath = $content->redirectPath();
if (strlen($redirectPath) > 0) {
if ($content->hasMoved()) {
$scheme = $_SERVER['REQUEST_SCHEME'];
$serverName = $_SERVER['HTTP_HOST'];
$requestDomain = $scheme . '://' . $serverName;
JoshBruce\Site\Emitter::emitWithResponse(
301,
[
'Location' => $requestDomain . $redirectPath
'Location' => $requestDomain . $content->redirectPath()
]
);
exit;
Expand Down
12 changes: 12 additions & 0 deletions setup-errors/405.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Unsupported method
usage: The request is using an Unsupported method.
---

# 405: Unsupported method

We cannot support that type of request.

Please try again later.

If this error persists, please contact [Josh Bruce](https://github.com/joshbruce).
File renamed without changes.
File renamed without changes.
14 changes: 12 additions & 2 deletions src/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __construct(
) {
}

public function folderDoesExist(): bool
public function folderIsMissing(): bool
{
return file_exists($this->root()) and is_dir($this->root());
return ! $this->folderExists();
}

public function for(string $path): Content
Expand All @@ -61,6 +61,11 @@ public function notFound(): bool
return ! $this->exists();
}

public function hasMoved(): bool
{
return strlen($this->redirectPath()) > 0;
}

public function filePath(): string
{
return $this->root() . $this->path;
Expand Down Expand Up @@ -131,6 +136,11 @@ public function redirectPath(): string
return '';
}

private function folderExists(): bool
{
return file_exists($this->root()) and is_dir($this->root());
}

private function exists(): bool
{
return file_exists($this->filePath());
Expand Down
119 changes: 119 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

namespace JoshBruce\Site;

use Whoops\Run;
use Whoops\Handler\PrettyPageHandler;

class Server
{
/**
* @param array<string, string> $serverGlobals
*/
public static function init(array $serverGlobals): Server
{
return new Server(serverGlobals: $serverGlobals);
}

/**
* @param array<string, string> $serverGlobals
*/
public function __construct(private array $serverGlobals)
{
}

public function isMissingRequiredValues(): bool
{
$required = [
'APP_ENV',
'CONTENT_UP',
'CONTENT_FOLDER',
'REQUEST_SCHEME',
'HTTP_HOST',
'REQUEST_URI'
];

$hasRequired = true;
foreach ($required as $key) {
if (! array_key_exists($key, $this->serverGlobals)) {
return true;
}
}

if ($this->serverGlobals['APP_ENV'] !== 'production') {
$erroHandler = new Run();
$erroHandler->pushHandler(new PrettyPageHandler());
$erroHandler->register();
}

return false;
}

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

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

/**
* @return string[] [description]
*/
public function supportedMethods(): array
{
return ['GET'];
}

public function contentUp(): int
{
return intval($this->serverGlobals['CONTENT_UP']);
}

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';
}

public function requestUri(): string
{
if ($this->serverGlobals['REQUEST_URI'] === '/') {
return '';
}
return $this->serverGlobals['REQUEST_URI'];
}
}
2 changes: 1 addition & 1 deletion tests/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
'text/html'
);

expect($this->baseContent->folderDoesExist())->toBeTrue();
expect($this->baseContent->folderIsMissing())->toBeFalse();
})->group('content');
2 changes: 2 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ function serverGlobals(string $requestUri = '/'): array
$_SERVER['APP_ENV'] = 'test';
$_SERVER['CONTENT_UP'] = 0;
$_SERVER['CONTENT_FOLDER'] = '/tests/test-content';
$_SERVER['REQUEST_SCHEME'] = 'http';
$_SERVER['HTTP_HOST'] = 'testing.com';
$_SERVER['REQUEST_URI'] = $requestUri;
$_SERVER['REQUEST_METHOD'] = 'get';

Expand Down
Loading

0 comments on commit 959883e

Please sign in to comment.