Hook up the templating system and router better.

This commit is contained in:
flash 2018-01-03 16:49:37 +01:00
parent 886de03762
commit c8593b7414
11 changed files with 62 additions and 16 deletions

View file

@ -8,7 +8,7 @@
}, },
"require": { "require": {
"php": ">=7.2", "php": ">=7.2",
"flashwave/aitemu": "dev-master#3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace", "flashwave/aitemu": "dev-master#7997543717d996b56938d5050ea497d62eb71f2e",
"twig/twig": "~2.4", "twig/twig": "~2.4",
"nesbot/carbon": "~1.22", "nesbot/carbon": "~1.22",
"illuminate/database": "~5.5", "illuminate/database": "~5.5",

10
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "4c9cc87ad91ef38f4333d4e9e80a0f39", "content-hash": "fd3ae2504a3c120f29942565ef7cd403",
"packages": [ "packages": [
{ {
"name": "doctrine/annotations", "name": "doctrine/annotations",
@ -590,12 +590,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/flashwave/aitemu.git", "url": "https://github.com/flashwave/aitemu.git",
"reference": "3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace" "reference": "7997543717d996b56938d5050ea497d62eb71f2e"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/flashwave/aitemu/zipball/3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace", "url": "https://api.github.com/repos/flashwave/aitemu/zipball/7997543717d996b56938d5050ea497d62eb71f2e",
"reference": "3ddb8147ed809cfba59c46f6e3d3af9fb2a86ace", "reference": "7997543717d996b56938d5050ea497d62eb71f2e",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -616,7 +616,7 @@
], ],
"description": "PHP router library, mainly for personal use.", "description": "PHP router library, mainly for personal use.",
"homepage": "https://github.com/flashwave/aitemu", "homepage": "https://github.com/flashwave/aitemu",
"time": "2018-01-01T22:36:19+00:00" "time": "2018-01-03T15:36:23+00:00"
}, },
{ {
"name": "illuminate/container", "name": "illuminate/container",

View file

@ -3,4 +3,6 @@ namespace Misuzu;
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
Application::start()->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit')); $app = Application::start();
$app->debug(IO\Directory::exists(__DIR__ . '/vendor/phpunit/phpunit'));
$app->router->add(include_once __DIR__ . '/routes.php');

7
routes.php Normal file
View file

@ -0,0 +1,7 @@
<?php
use Aitemu\Route;
use Misuzu\Controllers\HomeController;
return [
Route::get('/', 'index', HomeController::class),
];

13
server.php Normal file
View file

@ -0,0 +1,13 @@
<?php
// php -S localhost:8000 -t public/ server.php
// Decode and parse the request uri
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
// Check if the file exist in the public directory and if it does serve it.
if ($uri !== '/' && file_exists(__DIR__ . '/public' . $uri)) {
return false;
}
// Otherwise include the router
require_once __DIR__ . '/public/index.php';

View file

@ -66,19 +66,13 @@ class Application
$this->addModule('templating', new TemplateEngine); $this->addModule('templating', new TemplateEngine);
$this->addModule('config', new ConfigManager($config)); $this->addModule('config', new ConfigManager($config));
$this->debug(true);
$this->templating->addFilter('json_decode'); $this->templating->addFilter('json_decode');
$this->templating->addFilter('byte_symbol'); $this->templating->addFilter('byte_symbol');
$this->templating->addFunction('byte_symbol'); $this->templating->addFunction('byte_symbol');
$this->templating->addFunction('session_id'); $this->templating->addFunction('session_id');
$this->templating->addFunction('config', [$this->config, 'get']); $this->templating->addFunction('config', [$this->config, 'get']);
$this->templating->addFunction('route', [$this->router, 'url']); $this->templating->addFunction('route', [$this->router, 'url']);
$this->templating->addFunction('git_hash', [Application::class, 'gitCommitHash']);
$this->templating->addFunction('git_branch', [Application::class, 'gitBranch']);
$this->templating->addPath('nova', __DIR__ . '/../views/nova'); $this->templating->addPath('nova', __DIR__ . '/../views/nova');
echo $this->templating->render('home.landing');
} }
public function __destruct() public function __destruct()

View file

@ -0,0 +1,8 @@
<?php
namespace Misuzu\Controllers;
use Aitemu\BaseController;
abstract class Controller extends BaseController
{
}

View file

@ -0,0 +1,17 @@
<?php
namespace Misuzu\Controllers;
use Misuzu\Application;
class HomeController extends Controller
{
public function index(): string
{
$twig = Application::getInstance()->templating;
$twig->addFunction('git_hash', [Application::class, 'gitCommitHash']);
$twig->addFunction('git_branch', [Application::class, 'gitBranch']);
return $twig->render('home.landing');
}
}

View file

@ -19,11 +19,15 @@ class TemplateEngine
private const FILE_EXTENSION = '.twig'; private const FILE_EXTENSION = '.twig';
/** /**
* Container for twig. * Instance of the Twig Environment.
* @var Twig_Environment * @var Twig_Environment
*/ */
private $twig; private $twig;
/**
* Instance a Twig loader, probably only compatible with the Filesystem type.
* @var Twig_Loader_Filesystem
*/
private $loader; private $loader;
/** /**
@ -135,7 +139,7 @@ class TemplateEngine
* @param array $vars * @param array $vars
* @return string * @return string
*/ */
public function render(string $path, array $vars = null): string public function render(string $path, ?array $vars = null): string
{ {
$path = self::fixPath($path); $path = self::fixPath($path);

View file

@ -1,4 +1,4 @@
{% extends '@nova/master.twig' %} {% extends '@nova/home/master.twig' %}
{% block content %} {% block content %}
test test

View file

@ -0,0 +1 @@
{% extends '@nova/master.twig' %}