Skip to content

Commit

Permalink
Merge pull request #10 from fatkulnurk/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fatkulnurk authored Apr 17, 2020
2 parents df9f81c + 473d9a4 commit feda6a4
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 43 deletions.
16 changes: 12 additions & 4 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@

use Fatkulnurk\Microframework\App;

// membuat instance app
// create instance
$app = App::getInstance();

// set Base Dir
$app->setPath(__DIR__);

// pendaftaran routes
// set config
$app->setConfig([
'path_template' => __DIR__ . "./../src/views",
'path_public' => __DIR__ . "./../public/"
]);

// registration route
$routes = require __DIR__ . '/../src/route.php';
$app->routing($routes);

// menjalankan custom errornya, saya pakai whoops
// error handling (with whoops)
$app->errorView();

// menjalankan aplikasi & proses dispatch
// run app
$app->dispatch();
2 changes: 1 addition & 1 deletion src/controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class HomeController
{
public function welcome()
{
echo "hello bro";
echo "welcome";
}
}
7 changes: 5 additions & 2 deletions src/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
use Fatkulnurk\Microframework\Routing\RouteCollector;

return function (RouteCollector $r) {
$r->addRoute('GET', '/', function ($args) {
$r->addRoute(['GET', 'POST'], '/', function ($args) {
return Response::getInstance()
->withView('index');
->withView('index.twig', [
'title' => 'Microframework',
'message' => 'Welcome To Microframework',
]);
});

$r->addRoute('GET', '/name/{name}', function ($args) {
Expand Down
1 change: 0 additions & 1 deletion src/views/header.html

This file was deleted.

4 changes: 4 additions & 0 deletions src/views/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% include 'layouts/header.twig' %}
<h1>{{ title }}</h1>
<h2>{{ message }}</h2>
{% include 'layouts/footer.twig' %}
4 changes: 4 additions & 0 deletions src/views/layouts/footer.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

</div>
</body>
</html>
8 changes: 8 additions & 0 deletions src/views/layouts/header.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<div>
7 changes: 2 additions & 5 deletions system/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ class App

public $path = '';

protected $config = [
'path_template' => __DIR__ . "./../src/views",
'path_public' => __DIR__ . "./../public/"
];
protected $config = [];

/**
* Method ini untuk interaksi dengan routing
Expand Down Expand Up @@ -182,7 +179,7 @@ public function errorView(HandlerInterface $handler = null)
* Bagian ini berisi informasi mengenai
* Setter Getter untuk konfigurasi
* */
public function setConfig($config)
public function setConfig(array $config)
{
$this->config = $config;
}
Expand Down
12 changes: 8 additions & 4 deletions system/Http/Data/View/BasePageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ abstract class BasePageTemplate implements PageTemplate
public function __construct(string $location = '')
{
try {
$this->baseLocation = App::getInstance()->getConfig('path_template');
$this->baseLocation = App::getInstance()->getPath();
} catch (\Exception $exception) {
throw new \Exception('config with key path_template not found');
throw new \Exception('Path not found');
}

if (empty($location)) {
$this->location = $this->baseLocation;
} else {
$this->location = $location;
}
// $this->baseLocation = $_SERVER['DOCUMENT_ROOT'] . "./../src/views";
$this->location = $location;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions system/Http/Data/View/LexPageTemplate.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\View;

use Fatkulnurk\Microframework\App;

class LexPageTemplate extends BasePageTemplate implements PageTemplate
{
public function openFileTemplate()
{
$this->templateString = file_get_contents($this->getLocation(). '.lex');
// $this->templateString = file_get_contents($this->getLocation(). '.lex');
$this->templateString = file_get_contents(
App::getInstance()->getPath() . "./../src/views/" . $this->getLocation()
);
}

public function getTemplateString()
{
return $this->templateString;
}
}
}
12 changes: 9 additions & 3 deletions system/Http/Data/View/LexRenderer.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\View;

use Fatkulnurk\Microframework\System\Core\Exception\TemplateEngineNotFoundException;

class LexRenderer implements TemplateRenderer
{
public function render(string $templateString, array $arguments = []): string
{
$parser = new \Lex\Parser();
$template = $parser->parse($templateString, $arguments);
if (class_exists(\Lex\Parser::class)) {
$parser = new \Lex\Parser();
$template = $parser->parse($templateString, $arguments);

return $template;
}

return $template;
throw new TemplateEngineNotFoundException('Lex not installed');
}
}
3 changes: 2 additions & 1 deletion system/Http/Data/View/TwigPageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class TwigPageTemplate extends BasePageTemplate implements PageTemplate
{
public function openFileTemplate()
{
$this->templateString = file_get_contents($this->getBaseLocation() . '/' .$this->getLocation(). '.twig.php');
//$this->templateString = file_get_contents($this->getBaseLocation() . '/' .$this->getLocation(). '.twig.php');
$this->templateString = $this->getLocation();
}

public function getTemplateString()
Expand Down
37 changes: 24 additions & 13 deletions system/Http/Data/View/TwigRenderer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\View;

use Fatkulnurk\Microframework\System\Core\Exception\TemplateEngineNotFoundException;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
Expand All @@ -9,19 +10,29 @@ class TwigRenderer implements TemplateRenderer
{
public function render(string $templateString, array $arguments = []): string
{
$loader = new \Twig\Loader\ArrayLoader([
'theme' => $templateString,
]);
$twig = new \Twig\Environment($loader);
// $loader = new \Twig\Loader\ArrayLoader([
// 'theme' => $templateString,
// ]);
// $twig = new \Twig\Environment($loader);

try {
return $twig->render('theme', $arguments);
} catch (LoaderError $e) {
die($e);
} catch (RuntimeError $e) {
die($e);
} catch (SyntaxError $e) {
die($e);

if (class_exists(\Twig\Loader\FilesystemLoader::class)) {
$loader = new \Twig\Loader\FilesystemLoader(\Fatkulnurk\Microframework\App::getInstance()
->getPath() . "./../src/views");
$twig = new \Twig\Environment($loader);

try {
return $twig->render($templateString, $arguments);
// return $twig->render('theme', $arguments); // if use array loader
} catch (LoaderError $e) {
die($e);
} catch (RuntimeError $e) {
die($e);
} catch (SyntaxError $e) {
die($e);
}
}
throw new TemplateEngineNotFoundException('Twig not installed');

}
}
}
44 changes: 37 additions & 7 deletions system/Http/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace Fatkulnurk\Microframework\Http\Message;

use Adbar\Dot;
use Fatkulnurk\Microframework\Core\Singleton;
use phpDocumentor\Reflection\Types\This;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
Expand Down Expand Up @@ -118,17 +118,17 @@ public function fromGlobal(): ServerRequest
$this->stream = Stream::create($body);
}

$cookies = empty($_COOKIE) ? array($_COOKIE) : [];
// $cookies = empty($_COOKIE) ? array($_COOKIE) : [];
// $this->withCookieParams($cookies);
$this->cookieParams = array($_COOKIE);
$this->cookieParams = $_COOKIE;

$gets = empty($_GET) ? array($_GET) : [];
// $gets = empty($_GET) ? array($_GET) : [];
// $this->withQueryParams($gets);
$this->queryParams = array($_GET);
$this->queryParams = $_GET;

$posts = empty($_POST) ? array($_POST) : [];
// $posts = empty($_POST) ? array($_POST) : [];
// $this->withParsedBody($posts);
$this->parsedBody = array($_POST);
$this->parsedBody = $_POST;

$this->uploadedFiles = $this->normalizeFiles();

Expand Down Expand Up @@ -464,4 +464,34 @@ public function createUploadedFile(
}
return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
}


// need DOTKey
public function query(string $key, $default = '')
{
$dotKey = new Dot($this->getQueryParams());

return $dotKey->get($key, $default);
}

public function hasQuery(string $key)
{
$dotKey = new Dot($this->queryParams);

return $dotKey->has($key);
}

public function input(string $key, $default = '')
{
$dotkey = new Dot($this->parsedBody);

return $dotkey->get($key, $default);
}

public function hasInput(string $key)
{
$dotkey = new Dot($this->parsedBody);

return $dotkey->has($key);
}
}

0 comments on commit feda6a4

Please sign in to comment.