Skip to content

Commit d94fd7b

Browse files
committed
FIRST COMMIT
0 parents  commit d94fd7b

File tree

10 files changed

+270
-0
lines changed

10 files changed

+270
-0
lines changed

Autoloader.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
if(!isset($_SESSION)){
3+
session_start();
4+
}
5+
class Autoloader {
6+
static public function loader($className) {
7+
$filename = str_replace("\\", '/', $className) . ".php";
8+
if (file_exists($filename)) {
9+
include($filename);
10+
if (class_exists($className)) {
11+
return TRUE;
12+
}
13+
}
14+
return FALSE;
15+
}
16+
}
17+
spl_autoload_register('Autoloader::loader');

Irfa/Flashmsg/Flash.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/* Flash message
3+
Author:Irfa Ardiansyah <irfaardiansyah95@gmail.com>
4+
*/
5+
namespace Irfa\Flashmsg;
6+
7+
8+
use stdClass;
9+
use Irfa\Flashmsg\FlashGenerator;
10+
11+
class Flash extends FlashGenerator{
12+
13+
function __construct(){
14+
15+
}
16+
public static function set($key,$msg,$type='success'){
17+
self::flashset($key,$msg,$type);
18+
return true;
19+
20+
}
21+
public static function get($session_name){
22+
$ret = self::flashget($session_name);
23+
return $ret;
24+
25+
}
26+
27+
28+
}

Irfa/Flashmsg/FlashGenerator.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/* Flash message
3+
Author:Irfa Ardiansyah <irfaardiansyah95@gmail.com>
4+
*/
5+
namespace Irfa\Flashmsg;
6+
use stdClass;
7+
use Irfa\Flashmsg\ViewGenerator;
8+
9+
class FlashGenerator extends ViewGenerator {
10+
11+
private static $params = [];
12+
13+
protected static function flashset($key,$msg,$type){
14+
$_SESSION['arr'] = $arr;
15+
$_SESSION[$key.'-type'] = $type;
16+
$_SESSION[$key] = $msg;
17+
18+
return true;
19+
20+
}
21+
22+
protected static function flashget($session_name){
23+
$str = isset($_SESSION[$session_name])?$_SESSION[$session_name]:null;
24+
$type = isset($_SESSION[$session_name.'-type'])?$_SESSION[$session_name.'-type']:null;
25+
self::forget($session_name);
26+
if($str!=null){
27+
return self::viewMessage($str,$type);
28+
} else{
29+
return null;
30+
}
31+
32+
33+
}
34+
35+
protected static function forget($str){
36+
unset($_SESSION[$str]);
37+
unset($_SESSION[$str.'-type']);
38+
}
39+
40+
41+
42+
43+
}

Irfa/Flashmsg/ViewGenerator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/* Flash message
3+
Author:Irfa Ardiansyah <irfaardiansyah95@gmail.com>
4+
*/
5+
namespace Irfa\Flashmsg;
6+
use stdClass;
7+
8+
class ViewGenerator {
9+
10+
protected static function viewMessage($str,$type){
11+
return self::loadView(['message' => $str,'class' => self::cssClass($type)]);
12+
}
13+
14+
private static function cssClass($type){
15+
include('config.php');
16+
return $config[$type];
17+
}
18+
private static function loadView($var){
19+
if($var != null){
20+
foreach($var as $key => $val){
21+
22+
${$key} = $val;
23+
24+
}
25+
}
26+
include('config.php');
27+
$file=$config['view'];
28+
if(file_exists($file)){
29+
30+
include($file);
31+
} else{//Jika File tidak ditemukan
32+
echo "<b>Error: File ".$file." not found. Please check configuration</b>";
33+
}
34+
}
35+
36+
37+
38+
39+
}

Irfa/Flashmsg/config.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
$config = [
3+
4+
/*
5+
|--------------------------------------------------------------------------
6+
|Messages Classes
7+
|--------------------------------------------------------------------------
8+
| Here you can change css class message.
9+
*/
10+
//for success message
11+
// Default value : alert alert-success
12+
'success' => 'alert alert-success',
13+
//For fail Message
14+
// Default value : alert alert-danger
15+
'fail' => 'alert alert-danger',
16+
//For warning Message
17+
// Default value : alert alert-warning
18+
'warning' => 'alert alert-warning',
19+
//For info message
20+
// Default value : alert alert-info
21+
'info' => 'alert alert-info',
22+
//For general Message
23+
// Default value : alert alert-primary
24+
'general' => 'alert alert-primary',
25+
//For other Message
26+
// Default value : alert alert-secondary
27+
'other' => 'alert alert-secondary',
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
|View Message
32+
|--------------------------------------------------------------------------
33+
| Here you can change the location of view.
34+
| Default value : Irfa/Flashmsg/view/message.php
35+
*/
36+
'view' => 'Irfa/Flashmsg/view/message.php',
37+
38+
39+
40+
];

Irfa/Flashmsg/view/message.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="<?= $class ?>">
2+
<?= $message ?>
3+
</div>

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
# Flash Message
3+
4+
> **Configuration File**
5+
> Irfa/Flashmsg/config.php
6+
7+
8+
**<h2>Basic Usage</h2>**
9+
10+
11+
<?php
12+
require_once 'Autoloader.php';
13+
use Irfa\Flashmsg\Flash;
14+
...
15+
16+
<h2>Set Flash Message</h2>
17+
18+
> You are free to set this message type, change css class or message
19+
> view in config file.
20+
>
21+
> Flash::set(key , message, type);
22+
23+
24+
25+
...
26+
///SET FLASH Fail
27+
Flash::set('fail-msg' , 'Description is required','fail');
28+
///SET FLASH Success
29+
Flash::set('success-msg' , 'Update Success','success');
30+
///SET FLASH Info
31+
Flash::set('info-msg' , 'Please wait 1 minutes','success');
32+
///SET FLASH Warning
33+
Flash::set('warning-msg' , 'Username or Password Invalid','warning');
34+
///SET FLASH general
35+
Flash::set('general-msg' , 'Hello, i'm Message','general');
36+
///SET FLASH Other
37+
Flash::set('other-msg' , 'Just Other Message','other');
38+
...
39+
40+
<h2>Get Flash Message</h2>
41+
42+
> Flash::get(key);
43+
44+
...
45+
///GET FLASH MESSAGE
46+
Flash::get('msg');
47+
...
48+
49+

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "irfa/php-flash-message",
3+
"description": "\"Flash Message for PHP\"",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Irfa A",
8+
"email": "irfa.backend@protonmail.com"
9+
}
10+
],
11+
"require": {
12+
"php": ">=5.4.0"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"Irfa\\FlashMessage\\": "irfa/"
17+
}
18+
}
19+
}

example.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
require_once 'Autoloader.php';
3+
use Irfa\Flashmsg\Flash;
4+
?>
5+
<!DOCTYPE html>
6+
<html>
7+
<head>
8+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
9+
<title>Example Flash Message</title>
10+
</head>
11+
<body>
12+
<div class="container">
13+
14+
<?= Flash::get('msg'); ?>
15+
16+
<form action="examplepost.php" method="POST">
17+
<label> Name</label>
18+
<input type="text" class="form-control" placeholder="Please enter your name..." required="" name="name">
19+
<hr>
20+
<button class="btn btn-success">Send</button>
21+
</form>
22+
</div>
23+
</body>
24+
</html>

examplepost.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
require_once 'Autoloader.php';
3+
use Irfa\Flashmsg\Flash;
4+
5+
///SET FLASH MESSAGE
6+
/// Flash::set(key,message,type)
7+
Flash::set('msg' , 'Hello '.$_POST['name'],'success');
8+
header('location:'.$_SERVER['HTTP_REFERER']);

0 commit comments

Comments
 (0)