Skip to content

Commit 351bea6

Browse files
committed
🎉 created base project
0 parents  commit 351bea6

File tree

6 files changed

+462
-0
lines changed

6 files changed

+462
-0
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
open_collective: leaf

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test
2+
Experimental
3+
vendor
4+
composer.lock

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- markdownlint-disable no-inline-html -->
2+
<p align="center">
3+
<br><br>
4+
<img src="https://leaf-docs.netlify.app/images/logo.png" height="100"/>
5+
<h1 align="center">Leaf Session Module</h1>
6+
<br><br>
7+
</p>
8+
9+
# Leaf PHP
10+
11+
[![Latest Stable Version](https://poser.pugx.org/leafs/session/v/stable)](https://packagist.org/packages/leafs/session)
12+
[![Total Downloads](https://poser.pugx.org/leafs/session/downloads)](https://packagist.org/packages/leafs/session)
13+
[![License](https://poser.pugx.org/leafs/session/license)](https://packagist.org/packages/leafs/session)
14+
15+
Leaf's core http functionality packaged as a serve-yourself module. Although seperated from Leaf core, it is still part of the default initial installation and doesn't need to be installed manually (unless you want a particular version).
16+
17+
## Installation
18+
19+
You can easily install Leaf using [Composer](https://getcomposer.org/).
20+
21+
```bash
22+
composer require leafs/session
23+
```
24+
25+
## View Leaf's docs [here](https://leafphp.netlify.app/#/)
26+
27+
Built with ❤ by [**Mychi Darko**](https://mychi.netlify.app)

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "leafs/session",
3+
"description": "Leaf PHP session + flash modules",
4+
"keywords": [
5+
"session",
6+
"http",
7+
"flash",
8+
"leaf",
9+
"php",
10+
"framework"
11+
],
12+
"homepage": "https://leafphp.netlify.app/#/",
13+
"type": "library",
14+
"license": "MIT",
15+
"authors": [
16+
{
17+
"name": "Michael Darko",
18+
"email": "[email protected]",
19+
"homepage": "https://mychi.netlify.app",
20+
"role": "Developer"
21+
}
22+
],
23+
"autoload": {
24+
"psr-4": {
25+
"Leaf\\": "src"
26+
}
27+
},
28+
"minimum-stability": "stable",
29+
"require": {
30+
"leafs/anchor": "^1.0"
31+
}
32+
}

src/Flash.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Leaf;
4+
5+
/**
6+
* Leaf Flash
7+
* -----
8+
* Simple flash messages for your leaf apps
9+
*
10+
* @author Michael Darko <[email protected]>
11+
* @since 2.5.0
12+
*/
13+
class Flash
14+
{
15+
private static $config = [
16+
"key" => "leaf.flash",
17+
"default" => "message",
18+
"saved" => "leaf.flash.saved",
19+
];
20+
21+
/**
22+
* Configure leaf flash
23+
*
24+
* @param array $config Configuration for leaf flash
25+
*/
26+
public static function config(array $config)
27+
{
28+
static::$config = array_merge(static::$config, $config);
29+
}
30+
31+
/**
32+
* Set a new flash message
33+
*
34+
* @param string $message The flash message to set
35+
* @param string $key The key to save message
36+
*/
37+
public static function set(string $message, string $key = "default")
38+
{
39+
static::session();
40+
41+
if ($key === "default") {
42+
$key = static::$config["default"];
43+
}
44+
45+
$_SESSION[static::$config["key"]][$key] = $message;
46+
}
47+
48+
/**
49+
* Remove a flash message
50+
*
51+
* @param string|null $key The key of message to remove
52+
*/
53+
public static function unset(string $key = null)
54+
{
55+
static::session();
56+
57+
if (!$key) {
58+
Http\Session::unset(static::$config["key"]);
59+
} else {
60+
if ($key === "default") {
61+
$key = static::$config["default"];
62+
}
63+
64+
$_SESSION[static::$config["key"]][$key] = null;
65+
}
66+
}
67+
68+
/**
69+
* Get the flash array
70+
*
71+
* @param string|null $key The key of message to get
72+
* @return string|array
73+
*/
74+
private static function get(string $key = null)
75+
{
76+
static::session();
77+
78+
if (!$key) {
79+
return Http\Session::get(static::$config["key"]);
80+
}
81+
82+
if ($key === "default") {
83+
$key = static::$config["default"];
84+
}
85+
86+
$item = null;
87+
$items = Http\Session::get(static::$config["key"], false);
88+
89+
if (isset($items[$key])) {
90+
$item = $items[$key];
91+
}
92+
93+
if ($key) {
94+
static::unset($key);
95+
}
96+
97+
return $item;
98+
}
99+
100+
/**
101+
* Display a flash message
102+
*
103+
* @param string $key The key of message to display
104+
* @return string
105+
*/
106+
public static function display(string $key = "default")
107+
{
108+
static::session();
109+
110+
return static::get($key);
111+
}
112+
113+
/**
114+
* Save a flash message (won't delete after view).
115+
* You can save only one message at a time.
116+
*
117+
* @param string $message The flash message to save
118+
*/
119+
public static function save(string $message)
120+
{
121+
static::session();
122+
123+
Http\Session::set(static::$config["saved"], $message);
124+
}
125+
126+
/**
127+
* Clear the saved flash message
128+
*/
129+
public static function clearSaved()
130+
{
131+
static::session();
132+
133+
Http\Session::set(static::$config["saved"], null);
134+
}
135+
136+
/**
137+
* Display the saved flash message
138+
*/
139+
public static function displaySaved()
140+
{
141+
static::session();
142+
143+
return Http\Session::get(static::$config["saved"]);
144+
}
145+
146+
private static function session()
147+
{
148+
if (!session_id()) {
149+
session_start();
150+
}
151+
}
152+
}

0 commit comments

Comments
 (0)