Skip to content

Commit

Permalink
Merge pull request #6 from fatkulnurk/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fatkulnurk authored Jan 5, 2020
2 parents e946577 + 5ca53fd commit 861f47c
Show file tree
Hide file tree
Showing 55 changed files with 1,564 additions and 95 deletions.
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@
],
"autoload": {
"classmap": ["system/"],
"psr-4": {"App\\": "src"}
"psr-4": {"App\\": "src"},
"files": ["system/helpers.php"]
},
"require": {
"php": "^7.2",
"twig/twig":"2.*",
"twig/twig":"3.*",
"pyrocms/lex": "2.2.*",
"ext-json": "*",
"monolog/monolog": "^2.0",
"psr/http-message": "^1.0",
"myclabs/php-enum": "^1.7"
"myclabs/php-enum": "^1.7",
"spatie/array-to-xml": "^2.11",
"narrowspark/http-emitter": "^1.0",
"nyholm/psr7": "^1.2",
"zendframework/zend-httphandlerrunner": "^1.1",
"salernolabs/php-to-xml": "^1.0",
"filp/whoops": "^2.5"
},
"require-dev": {
"squizlabs/php_codesniffer": "3.*",
Expand Down
5 changes: 4 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once 'vendor/autoload.php';
require_once '../vendor/autoload.php';

use Fatkulnurk\Microframework\App;

Expand All @@ -10,5 +10,8 @@
$routes = require __DIR__ . '/../src/route.php';
$app->routing($routes);

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

// menjalankan aplikasi & proses dispatch
$app->dispatch();
85 changes: 71 additions & 14 deletions src/route.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,92 @@
<?php

use Fatkulnurk\Microframework\Http\Message\Response;
use Fatkulnurk\Microframework\Routing\RouteCollector;

return function (RouteCollector $r) {

/**
* Example GET route
*
* @param array $args Route parameters
*
* @return \Psr\Http\Message\ResponseInterface
*/
$r->addRoute('GET', '/', function ($args) {
// echo "hello";
// echo "aaaaaaa";

var_dump(\Fatkulnurk\Microframework\Http\Message\ServerRequest::getInstance()
->make('GET', $_SERVER['REQUEST_URI'])->getUri());

die();
return Response::getInstance()
->withView('index', [
'name' => 'fatkul nur koirudin',
'birthday' => '18 januari 1999'
], new \Fatkulnurk\Microframework\Http\Data\View\Page());

// /* Contoh return data view tanpa template factory */
// return Response::getInstance()
// ->withView('index', [
// 'name' => 'fatkul nur k',
// 'birthday' => '18 januari 1999'
// , new \Fatkulnurk\Microframework\Http\Data\View\LexTemplateFactory()]);
});

$r->addRoute('GET', '/tes-json', function ($args) {
$data = [
'biodata' => [
'nama' => 'fatkul nur koirudin',
'ttl' => 'Lamongan, 18 Januari 1999',
'alamat' => [
'desa' => 'Desa Ngambeg',
'kecamatan' => 'Kecamatan Pucuk',
'kabupaten' => 'Kabupaten Lamongan'
],
'email' => 'fatkulnurk@gmail.com',
'hoby' => [
'memancing',
'belajar hal baru'
]
]
];

return Response::getInstance()
->withJson($data);
});

$r->addRoute('GET', '/tes-redirect', function ($args) {
return Response::getInstance()
->withRedirect('http://google.com');
});

$r->addRoute('GET', '/biodata', function ($args) {
echo json_encode([
'a' => 'b'
'biodata' => [
'nama' => 'fatkul nur koirudin',
'ttl' => 'Lamongan, 18 Januari 1999',
'alamat' => [
'desa' => 'Desa Ngambeg',
'kecamatan' => 'Kecamatan Pucuk',
'kabupaten' => 'Kabupaten Lamongan'
],
'email' => 'fatkulnurk@gmail.com',
'hoby' => [
'memancing',
'belajar hal baru'
]
]
]);
header('Content-Type: application/json');
});

$r->addRoute('GET', '/home', function ($args, $request, $response) {
$r->addRoute('GET', '/home', function ($args) {
echo "aa";
});

$r->addRoute('GET', '/user/{id:\d+}[/{name}]', function ($args) {
var_dump($args);
});

$r->addRoute('GET', '/test/{name}', 'Coba::index');
};

// testing handler
class Coba {
public function index() {
class Coba
{
public function index()
{
echo "Aaaa";
}
}
14 changes: 14 additions & 0 deletions src/views/index.twig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Halaman Awal</title>
</head>
<body>
<div>
Halo {{ name }}!
<br>
Tanggal lahir kamu {{ birthday }}
</div>
</body>
</html>
40 changes: 38 additions & 2 deletions system/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
namespace Fatkulnurk\Microframework;

use Fatkulnurk\Microframework\Core\Singleton;
use Fatkulnurk\Microframework\Http\Message\Response;
use Fatkulnurk\Microframework\Routing\Dispatcher;
use Fatkulnurk\Microframework\Routing\RouteParser;
use Fatkulnurk\Microframework\Routing\DataGenerator;
use Fatkulnurk\Microframework\Routing\RouteCollector;
use Narrowspark\HttpEmitter\SapiEmitter;
use Whoops\Handler\HandlerInterface;

class App
{
Expand Down Expand Up @@ -128,17 +131,50 @@ public function dispatch() : void
*
* Dokumentasi selengkapnya: https://www.php.net/manual/en/language.types.callable.php
* */
// if (is_callable($handler, true)) {
// call_user_func($handler, $vars);
// } else {
// list($class, $method) = explode("/", $handler, 2);
// call_user_func_array(array(new $class, $method), $vars);
// }

if (is_callable($handler, true)) {
call_user_func($handler, $vars);
$response = call_user_func($handler, $vars);
} else {
list($class, $method) = explode("/", $handler, 2);
call_user_func_array(array(new $class, $method), $vars);
$response = call_user_func_array(array(new $class, $method), $vars);
}

// if ($response instanceof \Nyholm\Psr7\Response) {
if ($response instanceof Response) {
(new \Zend\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);
// $emitter = new SapiEmitter();
// $emitter->emit($response);
} else {
if (is_callable($handler, true)) {
call_user_func($handler, $vars);
} else {
list($class, $method) = explode("/", $handler, 2);
call_user_func_array(array(new $class, $method), $vars);
}
}
break;

default:
new \ErrorException('Handler Error');
}
}

public function errorView(HandlerInterface $handler = null)
{
if ($handler instanceof HandlerInterface) {
$whoops = new \Whoops\Run;
$whoops->prependHandler($handler);
$whoops->register();
} else {
$whoops = new \Whoops\Run;
$whoops->prependHandler(new \Whoops\Handler\PrettyPageHandler);
$whoops->register();
}
}
}
9 changes: 9 additions & 0 deletions system/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
declare(strict_types=1);

namespace Mifa\Core;
//use Noodlehaus\Config;
//use Noodlehaus\Parser\Json;

//https://github.com/hassankhan/config
class Config
{
private $patConfig;

public function read()
{
$config = new Noodlehaus\Config($this->patConfig);
return $config;
}
}
3 changes: 2 additions & 1 deletion system/Enum/Enum.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
/**
* Modifikasi dari Myclabs
* @link http://github.com/myclabs/php-enum
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
declare(strict_types=1);

namespace Mifa\Enum;
namespace Fatkulnurk\Microframework\Enum;

/**
* Base Enum class
Expand Down
13 changes: 13 additions & 0 deletions system/Http/Data/Document/BaseDocumentArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

abstract class BaseDocumentArray implements DocumentArray
{
protected $data = [];
protected $result = '';

public function __construct(array $data = [])
{
$this->data = $data;
}
}
14 changes: 14 additions & 0 deletions system/Http/Data/Document/BaseDocumentObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

class BaseDocumentObject
{
protected $data;
protected $dataArray = [];
protected $result = '';

public function __construct(object $data)
{
$this->data = $data;
}
}
39 changes: 39 additions & 0 deletions system/Http/Data/Document/Document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

class Document
{
protected $data;

public function __construct($data)
{
$this->data = $data;
}

public function result(DocumentFactory $documentFactory)
{
if (is_object($this->data)) {
$result = $documentFactory->createFromObject($this->data);
} else {
$result = $documentFactory->createFromArray($this->data);
}

return $result->result();
}
}

// CONTOH PENGGUNAAN
//$data = [
// 'nama' => 'fatkul nur k'
//];
//
//$dataObj = new Std();
//$dataObj->name = 'fatkul nur k';
//$dataObj->birthday = '18 januari 1999';
//
//
//$document = new Document($data);
//$result = $document->result(new JsonFactory());
//
//$document = new Document($dataObj);
//$result = $document->result(new JsonFactory());
8 changes: 8 additions & 0 deletions system/Http/Data/Document/DocumentArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

interface DocumentArray
{
public function proses();
public function result();
}
8 changes: 8 additions & 0 deletions system/Http/Data/Document/DocumentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

interface DocumentFactory
{
public function createFromArray(array $data);
public function createFromObject(object $data);
}
9 changes: 9 additions & 0 deletions system/Http/Data/Document/DocumentObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

interface DocumentObject
{
public function convert();
public function proses();
public function result();
}
21 changes: 21 additions & 0 deletions system/Http/Data/Document/JsonDocumentArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\Document;

class JsonDocumentArray extends BaseDocumentArray implements DocumentArray
{
public function proses(): void
{
if (is_array($this->data)) {
$this->result = json_encode($this->data);
}

$this->result = json_encode((array) $this->data);
}

public function result()
{
$this->proses();

return $this->result;
}
}
Loading

0 comments on commit 861f47c

Please sign in to comment.