1
0
mirror of https://github.com/nekudo/shiny_blog.git synced 2025-09-16 07:42:02 +02:00

Initial commit

This commit is contained in:
Simon Samtleben
2016-02-17 19:58:15 +01:00
commit 00862d12e0
9 changed files with 229 additions and 0 deletions

17
.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
# Eclipse/PDT
/.settings/
/.buildpath
/.project
# PhpStorm
.idea
# Netbeans
/nbproject/
# OSX
**.DS_Store
# Composer
vendor
composer.lock

10
composer.json Normal file
View File

@@ -0,0 +1,10 @@
{
"require": {
"nikic/fast-route": "^0.7.0"
},
"autoload": {
"psr-4": {
"Nekudo\\ShinyBlog\\": "src/"
}
}
}

4
index.php Normal file
View File

@@ -0,0 +1,4 @@
<?php
declare(strict_types=1);
require 'src/bootstrap.php';

11
src/Action/Article.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Nekudo\ShinyBlog\Action;
class Article
{
public function __invoke(array $arguments)
{
var_dump($arguments);
}
}

11
src/Action/Home.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Nekudo\ShinyBlog\Action;
class Home
{
public function __invoke(array $arguments)
{
echo "home";
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Nekudo\ShinyBlog\Responder;
class HttpResponder
{
protected $statusCode = 200;
protected $statusMessages = [
200 => 'OK',
404 => 'Not Found',
405 => 'Method Not Allowed',
];
public function found()
{
$this->statusCode = 200;
$this->respond();
}
public function notFound()
{
$this->statusCode = 404;
$this->respond();
}
public function methodNotAllowed()
{
$this->statusCode = 405;
$this->respond();
}
protected function respond()
{
$statusMessage = $this->statusMessages[$this->statusCode];
$header = sprintf('HTTP/1.1 %d %s', $this->statusCode, $statusMessage);
header($header);
}
}

111
src/ShinyBlog.php Normal file
View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace Nekudo\ShinyBlog;
use Exception;
use Nekudo\ShinyBlog\Action\Article;
use Nekudo\ShinyBlog\Action\Home;
use RuntimeException;
use FastRoute;
use FastRoute\RouteCollector;
use Nekudo\ShinyBlog\Responder\HttpResponder;
class ShinyBlog
{
/** @var array $config */
protected $config;
/** @var FastRoute\Dispatcher $dispatcher */
protected $dispatcher;
public function __construct(array $config)
{
$this->config = $config;
}
/**
* ShinyBlog main method.
* Dispatches and handles requests.
*/
public function run()
{
try {
$this->setRoutes();
$this->dispatch();
} catch(Exception $e) {
// @todo throw some kind of error 500...
}
}
/**
* Defines blog and page routes.
*
* @throws RuntimeException
*/
protected function setRoutes()
{
if (empty($this->config['routes'])) {
throw new RuntimeException('No routes defined in configuration file.');
}
$this->dispatcher = FastRoute\simpleDispatcher(function(RouteCollector $r) {
foreach ($this->config['routes'] as $routeName => $route) {
if (empty($route['method']) || empty($route['route']) || empty($route['action'])) {
throw new RuntimeException('Invalid route in configuration.');
}
$r->addRoute($route['method'], $route['route'], $route['action']);
}
});
}
/**
* Tries to find a route matching the current request. If found the defined action is called.
*/
protected function dispatch()
{
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$routeInfo = $this->dispatcher->dispatch($httpMethod, $uri);
if (!isset($routeInfo[0])) {
throw new RuntimeException('Could not dispatch request.');
}
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
$responder = new HttpResponder;
$responder->notFound();
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$responder = new HttpResponder;
$responder->methodNotAllowed();
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$arguments = $routeInfo[2];
$this->runAction($handler, $arguments);
break;
default:
throw new RuntimeException('Could not dispatch request.');
}
}
/**
* Calls an action.
*
* @param string $actionName
* @param array $arguments
*/
protected function runAction(string $actionName, array $arguments = [])
{
switch ($actionName) {
case 'home':
$action = new Home;
break;
case 'article':
$action = new Article;
break;
default:
throw new RuntimeException('Invalid action.');
break;
}
$action->__invoke($arguments);
}
}

9
src/bootstrap.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Nekudo\ShinyBlog;
require_once __DIR__ . '/../vendor/autoload.php';
$config = require 'config.php';
$blog = new ShinyBlog($config);
$blog->run();

17
src/config.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);
return [
'routes' => [
'home' => [
'method' => 'GET',
'route' => '/',
'action' => 'home'
],
'article' => [
'method' => 'GET',
'route' => '/blog/{alias:[a-z0-9-]+}',
'action' => 'article'
],
]
];