Skip to content

Commit b3c1851

Browse files
authored
Merge pull request #17 from SproutPHP/dev_yanik
Dev yanik
2 parents d0230ad + 414124b commit b3c1851

File tree

2 files changed

+148
-44
lines changed

2 files changed

+148
-44
lines changed

DOCS.md

Lines changed: 126 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ class HomeController {
5656

5757
## Features & Improvements Coming Soon
5858

59-
| Feature/Improvement | Status | Notes/Suggestions |
60-
|------------------------------------|-------------|---------------------------------------------------|
61-
| [ ] Event System | In Consideration | Event/listener system |
62-
| [ ] Localization (i18n) | In Consideration | Translation files and helpers |
63-
| [ ] Caching (Redis, Memcached, etc.)| In Consideration | Cache abstraction, Redis/Memcached support |
64-
| [ ] Testing Utilities | In Consideration | PHPUnit integration and helpers |
65-
| [ ] File Uploads & Storage | In Consideration | File upload and storage abstraction |
66-
| [ ] Command Bus/CQRS | In Consideration | Command handler system |
67-
| [ ] Form Builder | In Consideration | Dynamic form generation and validation |
68-
| [ ] API Support (JWT, rate limiting, etc.) | In Consideration | API middleware, JWT, transformers |
69-
| [ ] ORM/Query Builder | In Consideration | Query builder/ORM for easier DB access |
70-
| [ ] Model Relationships | In Consideration | hasOne, hasMany, belongsTo, etc. |
71-
| [ ] Package Installation System | In Consideration | Install and manage reusable packages/plugins |
59+
| Feature/Improvement | Status | Notes/Suggestions |
60+
| ------------------------------------------ | ---------------- | -------------------------------------------- |
61+
| [ ] Event System | In Consideration | Event/listener system |
62+
| [ ] Localization (i18n) | In Consideration | Translation files and helpers |
63+
| [ ] Caching (Redis, Memcached, etc.) | In Consideration | Cache abstraction, Redis/Memcached support |
64+
| [ ] Testing Utilities | In Consideration | PHPUnit integration and helpers |
65+
| [ ] File Uploads & Storage | In Consideration | File upload and storage abstraction |
66+
| [ ] Command Bus/CQRS | In Consideration | Command handler system |
67+
| [ ] Form Builder | In Consideration | Dynamic form generation and validation |
68+
| [ ] API Support (JWT, rate limiting, etc.) | In Consideration | API middleware, JWT, transformers |
69+
| [ ] ORM/Query Builder | In Consideration | Query builder/ORM for easier DB access |
70+
| [ ] Model Relationships | In Consideration | hasOne, hasMany, belongsTo, etc. |
71+
| [ ] Package Installation System | In Consideration | Install and manage reusable packages/plugins |
7272

7373
## Optional Packages
7474

@@ -129,13 +129,15 @@ See `CONFIGURATION.md` for complete documentation.
129129
SproutPHP now includes a minimal, extensible validation system for validating user input in controllers and forms.
130130

131131
### Overview
132-
- Validate associative arrays (e.g., $_POST, custom data)
132+
133+
- Validate associative arrays (e.g., $\_POST, custom data)
133134
- Supports common rules: required, email, min, max, numeric, string, in, etc.
134135
- Collects and returns error messages
135136
- Easily display errors in Twig views
136137
- Extensible: add your own rules as needed
137138

138139
### Example Usage in a Controller
140+
139141
```php
140142
use Core\Support\Validator;
141143

@@ -145,6 +147,8 @@ public function handleForm()
145147
$validator = new Validator($data, [
146148
'email' => 'required|email',
147149
'name' => 'required|min:3',
150+
'age' => 'required|numeric|min:18',
151+
'role' => 'in:admin,user,guest',
148152
]);
149153

150154
if ($validator->fails()) {
@@ -160,17 +164,40 @@ public function handleForm()
160164
```
161165

162166
### Available Rules
167+
163168
- `required` — Field must not be empty
164169
- `email` — Must be a valid email address
165170
- `min:N` — Minimum length N
166171
- `max:N` — Maximum length N
167172
- `numeric` — Must be a number
173+
- `integer` — Must be an integer
168174
- `string` — Must be a string
175+
- `boolean` — Must be a boolean value
176+
- `array` — Must be an array
169177
- `in:val1,val2,...` — Value must be one of the listed options
178+
- `not_in:val1,val2,...` — Value must NOT be one of the listed options
179+
- `same:field` — Must match another field
180+
- `different:field` — Must be different from another field
181+
- `confirmed` — Must have a matching {field}\_confirmation value
182+
- `regex:pattern` — Must match a regex pattern
183+
- `url` — Must be a valid URL
184+
- `ip` — Must be a valid IP address
185+
- `date` — Must be a valid date
186+
- `before:date` — Must be a date before the given date
187+
- `after:date` — Must be a date after the given date
188+
- `nullable` — Field is allowed to be null (affects other rules)
189+
- `present` — Field must be present in the input (even if empty)
190+
- `digits:N` — Must be exactly N digits
191+
- `digits_between:min,max` — Must be between min and max digits
192+
- `size:N` — Must be exactly N characters (for strings) or N value (for numbers/arrays)
193+
- `starts_with:val1,val2,...` — Must start with one of the given values
194+
- `ends_with:val1,val2,...` — Must end with one of the given values
195+
- `uuid` — Must be a valid UUID
170196

171197
You can add more rules by extending the Validator class.
172198

173199
### Displaying Errors in Twig
200+
174201
```twig
175202
<form method="POST">
176203
<input type="text" name="name" value="{{ old.name|e }}">
@@ -186,9 +213,70 @@ You can add more rules by extending the Validator class.
186213
```
187214

188215
### Notes
216+
189217
- Use the `validate()` helper for a shortcut: `validate($data, $rules)`
190218
- See the Validator class in `core/Support/Validator.php` for more details and to add custom rules.
191219

220+
## Dark/Light Mode Support
221+
222+
SproutPHP supports instant dark and light mode switching using PicoCSS's built-in color schemes. The framework provides an optional sun/moon icon button in the navbar to toggle the theme.
223+
224+
- PicoCSS automatically applies dark or light styles based on the `data-theme` attribute on `<html>`.
225+
- The toggle button updates `data-theme` to `dark` or `light` and saves the preference in localStorage.
226+
- No extra CSS is needed for the color scheme itself—PicoCSS handles all color changes.
227+
228+
### Example Usage
229+
230+
Add this to your navbar:
231+
232+
```html
233+
<button
234+
id="theme-toggle-btn"
235+
aria-label="Toggle dark/light mode"
236+
style="background:none;border:none;cursor:pointer;font-size:1.5rem;"
237+
>
238+
<span id="theme-icon">☀️</span>
239+
</button>
240+
```
241+
242+
Add this script (in your layout or navbar):
243+
244+
```html
245+
<script>
246+
(function () {
247+
const themeBtn = document.getElementById("theme-toggle-btn");
248+
const themeIcon = document.getElementById("theme-icon");
249+
const html = document.documentElement;
250+
if (!themeBtn || !themeIcon) return;
251+
function setInitialTheme() {
252+
let theme = localStorage.getItem("theme");
253+
if (!theme) {
254+
theme = window.matchMedia("(prefers-color-scheme: dark)").matches
255+
? "dark"
256+
: "light";
257+
}
258+
html.setAttribute("data-theme", theme);
259+
themeIcon.textContent = theme === "dark" ? "🌙" : "☀️";
260+
}
261+
setInitialTheme();
262+
themeBtn.addEventListener("click", function () {
263+
const currentTheme = html.getAttribute("data-theme");
264+
const newTheme = currentTheme === "dark" ? "light" : "dark";
265+
html.setAttribute("data-theme", newTheme);
266+
localStorage.setItem("theme", newTheme);
267+
themeIcon.textContent = newTheme === "dark" ? "🌙" : "☀️";
268+
});
269+
})();
270+
</script>
271+
```
272+
273+
**Result:**
274+
275+
- The icon (☀️ or 🌙) is shown in the navbar.
276+
- Clicking the icon toggles the theme and updates the icon instantly.
277+
- The theme preference is saved in `localStorage`.
278+
- PicoCSS automatically applies the correct color scheme.
279+
192280
## Using HTMX and PicoCSS
193281

194282
You do **not** need to install or include HTMX or PicoCSS yourself—they are already downloaded and loaded in your base template:
@@ -209,6 +297,7 @@ SproutPHP uses a **hybrid system** for making PHP helper functions available in
209297
### Usage
210298

211299
1. **Add a helper to `helpers.php`:**
300+
212301
```php
213302
// core/Support/helpers.php
214303
if (!function_exists('my_custom_helper')) {
@@ -217,7 +306,9 @@ SproutPHP uses a **hybrid system** for making PHP helper functions available in
217306
}
218307
}
219308
```
309+
220310
Now you can use it in Twig:
311+
221312
```twig
222313
{{ my_custom_helper('hello') }}
223314
```
@@ -231,13 +322,15 @@ SproutPHP uses a **hybrid system** for making PHP helper functions available in
231322
```
232323

233324
### How it works
325+
234326
- All helpers in `helpers.php` are auto-registered.
235327
- Any helpers listed in `twig_helpers` are also registered (even if not in `helpers.php`).
236328
- If a helper exists in both, it is only registered once.
237329

238330
**This means most of the time, you just add your helper to `helpers.php` and it works in Twig!**
239331

240332
### Note on the `view()` Helper
333+
241334
- The `view()` helper now supports a third parameter `$return` (default: `false`).
242335
- If `$return` is `true`, it returns the rendered string instead of echoing it. This is used by the generic fragment helper to inject fragments into layouts.
243336

@@ -257,6 +350,7 @@ Route::get('/my-fragment', function () {
257350
render_fragment_or_full('partials/my-fragment', $data); // uses layouts/base.twig by default
258351
});
259352
```
353+
260354
- **Best for most use cases.**
261355
- Keeps your code DRY and consistent.
262356
- Ensures direct URL access always returns a full page.
@@ -277,6 +371,7 @@ Route::get('/my-fragment', function () {
277371
}
278372
});
279373
```
374+
280375
- **Use when you need custom logic per route.**
281376
- Useful for advanced scenarios or when you want to handle fragments differently.
282377

@@ -300,6 +395,7 @@ Sometimes you want a link to always trigger a full page reload (for example, you
300395
```
301396

302397
**Best Practice:**
398+
303399
- Use these attributes for any link that should always reload the full page, such as your site home or links to external sites.
304400
- This prevents issues where the page loses its CSS/JS context due to HTMX fragment swaps.
305401

@@ -313,6 +409,7 @@ SproutPHP includes a built-in CORS middleware, registered globally by default bu
313409
- By default, CORS is **disabled** for security. Enable only if you need cross-origin requests (e.g., for APIs or frontend apps).
314410

315411
**Example config/security.php:**
412+
316413
```php
317414
'cors' => [
318415
'enabled' => env('CORS_ENABLED', false),
@@ -323,6 +420,7 @@ SproutPHP includes a built-in CORS middleware, registered globally by default bu
323420
```
324421

325422
**Security Note:**
423+
326424
- Only enable CORS for trusted origins in production. Use `*` for development only.
327425

328426
## CLI Reference
@@ -350,8 +448,8 @@ SproutPHP is a living, growing project—just like its name! Contributions, idea
350448
SproutPHP includes a post-install script that lets you choose your preferred PicoCSS build right after running `composer install`.
351449

352450
### How it Works
353-
- After installing dependencies, you'll be prompted to select a PicoCSS build:
354-
0. Default Sprout Layout (Minimal PicoCSS) — just press Enter or choose 0 for the default
451+
452+
- After installing dependencies, you'll be prompted to select a PicoCSS build: 0. Default Sprout Layout (Minimal PicoCSS) — just press Enter or choose 0 for the default
355453
1. Minimal (Standard)
356454
2. Classless
357455
3. Conditional
@@ -366,17 +464,19 @@ SproutPHP includes a post-install script that lets you choose your preferred Pic
366464
- The script will download the latest PicoCSS file from the CDN and save it as `public/assets/css/sprout.min.css`.
367465

368466
### Use Cases
369-
| Use Case | Choose This Option |
370-
|----------|-------------------|
371-
| Default Sprout layout, minimal PicoCSS | 0 (or press Enter) |
372-
| Simple blog, no layout classes | Classless |
373-
| Full control, grid, utilities | Minimal (Standard) |
374-
| Themed look + classless | Classless + Color Theme |
375-
| Toggle light/dark with JS | Conditional or Conditional + Color Theme |
376-
| Full-width layout, no classes | Fluid Classless |
377-
| Define your own classes | Color Palette Only |
467+
468+
| Use Case | Choose This Option |
469+
| -------------------------------------- | ---------------------------------------- |
470+
| Default Sprout layout, minimal PicoCSS | 0 (or press Enter) |
471+
| Simple blog, no layout classes | Classless |
472+
| Full control, grid, utilities | Minimal (Standard) |
473+
| Themed look + classless | Classless + Color Theme |
474+
| Toggle light/dark with JS | Conditional or Conditional + Color Theme |
475+
| Full-width layout, no classes | Fluid Classless |
476+
| Define your own classes | Color Palette Only |
378477

379478
### Changing PicoCSS Later
479+
380480
- You can re-run the post-install script at any time:
381481
```bash
382482
php core/Console/PostInstall.php
@@ -388,6 +488,7 @@ SproutPHP includes a post-install script that lets you choose your preferred Pic
388488
- Or, manually download your preferred PicoCSS file from [jsdelivr PicoCSS CDN](https://cdn.jsdelivr.net/npm/@picocss/pico@latest/css/) and place it in `public/assets/css/sprout.min.css`.
389489

390490
### Advanced
491+
391492
- All PicoCSS builds and color themes are available. See the [PicoCSS documentation](https://picocss.com/docs/) for more details on each build type and theme.
392493

393494
## Production Build (bloom Command)
@@ -402,4 +503,3 @@ This will run the production build process (minifies, strips dev code, precompil
402503

403504
- The old `build` command is now replaced by `bloom` for clarity and branding.
404505
- Use this command before deploying your app to production.
405-

RELEASE_NOTES.md

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,42 @@
1-
# SproutPHP v0.1.6-alpha.1 Release Notes
1+
# SproutPHP v0.1.7-alpha.1 Release Notes
22

33
## 🎉 New Features & Improvements
44

55
### Validation System
6-
- **New Validator class**: Minimal, extensible validation for forms and data.
7-
- **Validation helper**: Easy to use in controllers and views.
8-
- **Twig integration**: Display validation errors in templates.
96

10-
### Session Handling
11-
- **Configurable session name**: Set session cookie name via config/app.php and .env.
12-
- **Session start improvements**: Session is started after Composer autoload, ensuring config() is available.
7+
- **Expanded validation rules**: Now supports numeric, integer, string, boolean, array, in, not_in, same, different, confirmed, regex, url, ip, date, before, after, nullable, present, digits, digits_between, size, starts_with, ends_with, uuid, and more.
8+
- **Improved documentation**: DOCS.md now lists all available rules and usage examples.
9+
- **Better error clearing**: Validation errors are cleared on input focus for a smoother UX.
10+
11+
### Dark/Light Mode Support
12+
13+
- **PicoCSS dark/light mode toggle**: Optional sun/moon icon button in the navbar for instant theme switching.
14+
- **Theme preference**: Saved in localStorage and applied instantly.
15+
- **Post-install option**: Users can choose to auto-include the toggle during installation.
1316

1417
### Other Improvements
15-
- **CSRF fixes**: More robust CSRF token handling for HTMX and forms.
16-
- **Bug fixes**: Typos and namespace fixes in Validator and helpers.
17-
- **Documentation updates**: Improved setup and usage instructions for new features.
18+
19+
- **Post-install script**: Now prompts for dark/light mode toggle inclusion.
20+
- **Code cleanup and bug fixes**: Heredoc indentation, improved scripts, and UI polish.
21+
- **Documentation updates**: More examples and clearer instructions for new features.
1822

1923
## 🛠️ Upgrade Guide
2024

21-
- Update your config/app.php and .env for the new session name option if desired.
22-
- Use the new Validator in your controllers for form validation.
23-
- See DOCS.md for usage examples.
25+
- Use the new validation rules in your controllers and forms.
26+
- To add the dark/light mode toggle, re-run the post-install script or add the button and script as shown in DOCS.md.
27+
- See DOCS.md for updated usage examples.
2428

2529
## 📅 Release Date
2630

27-
June 2024
31+
July 2024
2832

2933
## 📦 Framework Version
3034

31-
v0.1.6-alpha.1
35+
v0.1.7-alpha.1
3236

3337
---
3438

35-
**Release Date**: June 2024
36-
**Framework Version**: v0.1.6-alpha.1
39+
**Release Date**: July 2024
40+
**Framework Version**: v0.1.7-alpha.1
3741
**PHP Version**: 8.1+
38-
**Composer**: 2.0+
42+
**Composer**: 2.0+

0 commit comments

Comments
 (0)