Skip to content

Commit

Permalink
Merge pull request #7 from fatkulnurk/dev
Browse files Browse the repository at this point in the history
Merge From Dev
  • Loading branch information
fatkulnurk authored Jan 8, 2020
2 parents 861f47c + c31ce41 commit 9392596
Show file tree
Hide file tree
Showing 21 changed files with 542 additions and 242 deletions.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,97 @@ PHP Microframework

Baca dokumentasi selengkapnya [link belum ada].

---

## Cara Install
gunakan composer, lalu jalankan perintah dibawah ini (pastikan sudah install composer)

````
composer create-project --prefer-dist fatkulnurk/microframework nama_aplikasi
````

---
**Mendaftarkan Routing**

Pendaftaran Routing

contoh pendaftaran routing beserta implementasinya, contoh dibawah ini untuk return berupa xml.

````
use Fatkulnurk\Microframework\Routing\RouteCollector;
return function (RouteCollector $r) {
$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' => '[email protected]',
'hoby' => [
'memancing',
'belajar hal baru'
]
]
];
return Response::getInstance()
->withXml($data);
});
};
````

Routing Handler Berupa Callback

dibawah ini contoh routing dengan callback.
````
$r->addRoute('GET', '/', function ($args) {
return Response::getInstance()
->withView('index', [
'name' => 'fatkul nur k',
'birthday' => '18 januari 1999'
]);
});
````



Routing Dengan handler berupa class

dibawah ini contoh routing dengan handler berupa method dari suatu class.
perlu di ingat, untuk pemanggilan adalah NamaClass::NamaMethod
````
$r->addRoute('GET', '/test/{name}', 'Coba::index');
````

Buat class sebagai handlernya, contohnya dibawah ini.
````
class Coba {
public function index()
{
echo "Hello World";
}
}
````

## Response
method yang bisa digunakan untuk response sama dengan yang ada pada aturan PSR 7.

untuk method tambahan adalah sebagai berikut:
- withView()
- withJson()
- withXml()
- withRedirect()
- withDownload()

## Request
semua sama seperti PSR 7. Gunakan seperti di framework lain.

---
**Cek CodeStyle**

Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"psr/http-message": "^1.0",
"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",
Expand Down
9 changes: 0 additions & 9 deletions src/Test.php

This file was deleted.

87 changes: 2 additions & 85 deletions src/route.php
Original file line number Diff line number Diff line change
@@ -1,92 +1,9 @@
<?php

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

return function (RouteCollector $r) {
$r->addRoute('GET', '/', function ($args) {

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' => '[email protected]',
'hoby' => [
'memancing',
'belajar hal baru'
]
]
];

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

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

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

$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()
{
echo "Aaaa";
}
}
};
47 changes: 0 additions & 47 deletions src/server.php

This file was deleted.

44 changes: 44 additions & 0 deletions system/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class App
private $response = null;


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

/**
* Method ini untuk interaksi dengan routing
* Pada bagian ini terdapat parser, data degerator, dispatcher dan routecollector
Expand Down Expand Up @@ -145,8 +150,27 @@ public function dispatch() : void
$response = call_user_func_array(array(new $class, $method), $vars);
}

/*
* Untuk redirect
* source: https://stackoverflow.com/questions/768431/how-do-i-make-a-redirect-in-php
* */
// if ($response->hasHeader('location')) {
// $result = $response->getHeader('location');
// // die($result[0]);
// header("Location: ". (string) $result[0], true, 301);
// die();
// }

// if ($response instanceof \Nyholm\Psr7\Response) {
if ($response instanceof Response) {
if ($response->hasHeader('location')) {
$result = $response->getHeader('location');
// die($result[0]);
header("Location: ". (string) $result[0], true, 301);
die();
}


(new \Zend\HttpHandlerRunner\Emitter\SapiEmitter())->emit($response);
// $emitter = new SapiEmitter();
// $emitter->emit($response);
Expand Down Expand Up @@ -177,4 +201,24 @@ public function errorView(HandlerInterface $handler = null)
$whoops->register();
}
}

/*
* CONFIG
* Bagian ini berisi informasi mengenai
* Setter Getter untuk konfigurasi
* */
public function setConfig($config)
{
$this->config = $config;
}

public function getConfig($key)
{
$keyToLower = \strtolower($key);
if (isset($this->config[$keyToLower])) {
return $this->config[$key];
}

throw new \Exception("Key Not Found In Config");
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 11 additions & 2 deletions system/Http/Data/View/BasePageTemplate.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Fatkulnurk\Microframework\Http\Data\View;

use Fatkulnurk\Microframework\App;

/**
* Class BasePageTemplate
*/
Expand All @@ -9,8 +11,8 @@ abstract class BasePageTemplate implements PageTemplate
/**
* @var string
*/
protected $baseLocation = '\xampp\htdocs\microframework\src\views';
// protected $baseLocation = '../../../src/views';
protected $baseLocation = '';

/**
* @var string
*/
Expand All @@ -23,9 +25,16 @@ abstract class BasePageTemplate implements PageTemplate
/**
* BasePageTemplate constructor.
* @param string $location
* @throws \Exception
*/
public function __construct(string $location = '')
{
try {
$this->baseLocation = App::getInstance()->getConfig('path_template');
} catch (\Exception $exception) {
throw new \Exception('config with key path_template not found');
}
// $this->baseLocation = $_SERVER['DOCUMENT_ROOT'] . "./../src/views";
$this->location = $location;
}

Expand Down
Loading

0 comments on commit 9392596

Please sign in to comment.