Skip to content

Commit 2a30a33

Browse files
committed
v1.0.0-beta.4
-Code cleanup -Improved config handling
1 parent a8955fd commit 2a30a33

File tree

12 files changed

+188
-123
lines changed

12 files changed

+188
-123
lines changed

Readme.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@
1212
- PHP 7.1 or higher
1313
- [PHP Composer](https://getcomposer.org/)
1414
- Install via Composer:
15-
- Run `composer create-project alddesign/ez-mvc=dev-master` in the directory where you want ez-mvc to be installed.
15+
- Run `composer create-project alddesign/ez-mvc=dev-master` in the directory where you want ez-mvc to be installed. (Or download from github and extract manually).
1616
- Run `composer update` in the newly created directory.
1717
- This is your **ez-mvc root** directory from now on.
1818
- Change RewriteBase in the `.htaccess` with your `/path-to-ez-mvc/`. This is the path to your ez-mvc root directory (relative to your webserver root directory).
1919
- Change the `base-url` in `/system/system.config.php` to the url that points to your ez-mvc root directory. For example: `http://your-host/path-to-ez-mvc`
2020
- Open http://your-host/path-to-ez-mvc/ in your webbrowser
2121

22-
If you install ez-mvc in the root directory of your webserver and do not rename any folders or files, your can leave the above config files as they are.
23-
2422
Now you should see the ez-mvc sample application. A little application which should demonstrate how to use ez-mvc.
2523

2624
**PLEASE: Do not delete the sample application right away. It has plenty of documentation und examples. This document is just a guidline.**
2725

2826
## Basics
29-
ez-mvc is a simple PHP Model View Controller framework. It provides the absolute basic functionalities to build MVC webapps. Its much more simple than [Laravel](#https://laravel.com/) or even [CodeIgniter](https://codeigniter.com/). But if follows the same approach.
27+
Ez-mvc is a simple PHP Model View Controller framework. It provides the absolute basic functionalities to build MVC webapps. Its much more simple than [Laravel](https://laravel.com/) or even [CodeIgniter](https://codeigniter.com/). But if follows the same approach.
3028

31-
- ez-mvc comes without any third party software, only requires composer for quick installation
32-
- ez-mvc has no frontend capabilities design built-in (CSS/JS).
29+
- Ez-mvc comes without any third party software, only requires composer for quick installation
30+
- Ez-mvc has no frontend capabilities built-in (CSS/JS).
3331
- When talking about the ez-mvc root directory: its the directory with the `index.php` file. All paths are realtive to it.
3432
- Create your app (models, views, controllers, config) only in the `/app` folder.
35-
- Assets line js, css, images, can be placed in the `/assets` folder
33+
- Assets like JS, CSS, images, can be placed in the `/assets` folder
3634
- The only system file you need to edit is `/system/system.config.php`. This file is the base configuration for ez-mvc
37-
- Of course you can customize or extend ez-mvc´s core if you really wnat to. Its located in the `/system` folder
35+
- Of course you can customize or extend ez-mvc´s core if you really want to. Its located in the `/system` folder
3836

3937
## Overview
4038
Ez-mvc provides the following features:
@@ -62,7 +60,7 @@ Ez-mvc provides the following features:
6260
- *See: `/app/config/app.config.php`*
6361

6462
**Routing**
65-
- ez-mvc has build-in routing, and the best thing: you dont need to care about it (i know sometimes you want to, but this is ez-mvc)
63+
- Ez-mvc has build-in routing, and the best thing: you dont need to care about it (i know sometimes you want to, but this is ez-mvc)
6664
- Routing happens based on the URL.
6765
- The format is always the same: http://your-host/path-to-ez-mvc/Controller/Action/parameter1/parameter2/...
6866
- Example: http://your-host/path-to-ez-mvc/Product/list. Controller is "Product" (/app/controllers/Product.php). Action is "list" (Method list() in /app/controllers/Product.php).
@@ -79,5 +77,4 @@ The Helper class offers functionality which makes your life ez-er.
7977

8078
## Trivia
8179
- Checkout ez-mvc on Github: [alddesign/ez-mvc](https://github.com/alddesign/ez-mvc)
82-
- Written in [Visual Studio Code](https://code.visualstudio.com/download)
83-
- Some desing patterns of the framework itself are considert bad practice (Singletons for example). And yes, i dont care.
80+
- Some desing patterns used in the framework are considert bad practice (Singletons, static classes, lazy stuff...). And yes, i dont care.

app/controllers/Main.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
declare(strict_types = 1);
33
namespace Alddesign\EzMvc\Controllers;
44

5+
use Alddesign\EzMvc\Models\DefaultModel;
56
use Alddesign\EzMvc\System\View;
67
use Alddesign\EzMvc\System\Controller;
8+
use Alddesign\EzMvc\System\Helper;
9+
use Alddesign\EzMvc\System\Model;
710

811
/**
912
* This is a Controller.
@@ -12,7 +15,7 @@
1215
abstract class Main extends Controller
1316
{
1417
/**
15-
* This is an Controller Action.
18+
* This is a Controller Action.
1619
* The Controller Action name is the second segment of the url: http://localhost/Main/index/...
1720
*/
1821
public static function index()

app/models/DefaultModel.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Alddesign\EzMvc\System\Helper;
66
use Alddesign\EzMvc\System\Model;
7-
use Exception;
87
use \PDO;
98

109
abstract class DefaultModel extends Model

app/sample-database.sqlite

0 Bytes
Binary file not shown.

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
<?php
2-
$autoloader = __DIR__.'/vendor/autoload.php';
3-
if(!file_exists($autoloader))
2+
//Set Error defaults to catch early configuration errors:
3+
ini_set('display_errors', 'On');
4+
ini_set('display_startup_errors', 'On');
5+
ini_set('error_reporting', -1);
6+
7+
//Check for autoloder and embed it
8+
if(!file_exists(__DIR__.'/vendor/autoload.php'))
49
{
5-
die('Damn, run <b>composer update</b> before using EZ-MVC.<br/>Please, <a href="https://getcomposer.org">get composer.</a> and thank me later.');
10+
http_response_code(500);
11+
echo 'vendor/autoloader.php not found. Forgot "composer update"?';
12+
die;
613
}
7-
require ($autoloader); //using composers autoloader file
14+
require(__DIR__.'/vendor/autoload.php');
815

916
session_start();
1017

1118
//Loads the config
1219
Alddesign\EzMvc\System\Config::load();
1320

14-
//Error reporting & display
21+
//Set Error reporting as configured
1522
ini_set('display_errors', Alddesign\EzMvc\System\Config::system('php-display-errors', 'On'));
1623
ini_set('display_startup_errors', Alddesign\EzMvc\System\Config::system('php-display-startup-errors', 'On'));
1724
ini_set('error_reporting', Alddesign\EzMvc\System\Config::system('php-error-reporting', E_ALL));

system/Config.php

Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@
44

55
abstract class Config
66
{
7-
private static $loaded = false;
7+
private static bool $loaded = false;
8+
private static bool $found = false;
89

910
/**
10-
* Loads a value from the app.config.php. You can access the config via the dot syntax (separating keys with a '.')
11+
* Loads a value from the app config (app.config.php).
12+
*
13+
* @param string $key The key for the config value. Supports dot syntax - see example. If $key is an empty string '' the whole app config will be returned.
14+
* @param string $default Retruns this default value, if no config value for $key was found.
15+
*
16+
* @return mixed The config value for $key or $default if no config value for $key was found.
1117
*
1218
* ```php
13-
* //Example
14-
* Config::get('captions.price');
19+
* //Example of the dot syntax: these two call do the same.
20+
* $priceCaption = Config::get('captions.price');
1521
* //Is the same as
16-
* Config::get('captions')['price'];
22+
* $priceCaption = Config::get('captions')['price'];
1723
* ```
18-
* @param string $key
19-
* @param string $default Default value if key is not found
20-
*
21-
* @return mixed
2224
*/
2325
public static function get(string $key = '', $default = '')
2426
{
27+
self::$found = true;
2528
global $_EZMVC_APP_CONFIG;
2629

2730
if($key === '')
@@ -42,6 +45,7 @@ public static function get(string $key = '', $default = '')
4245
{
4346
if(!is_array($value) || !array_key_exists($dotkey, $value))
4447
{
48+
self::$found = false;
4549
return $default;
4650
}
4751

@@ -51,16 +55,65 @@ public static function get(string $key = '', $default = '')
5155
return $value;
5256
}
5357

58+
5459
/**
55-
* Loads a value from the ez-mvc system.config.php. Works the same way as Config::get()
60+
* Works the same way as **Config::get()**, but stores the config value in $outVar. Returns if a config value for $key was found.
5661
*
57-
* @param string $key
58-
* @param string $default Default value if key is not found
62+
* @param mixed &$outVar The config value will be stored in this variable (pass by reference);
63+
* @param string $default Default value for $outVar if no config value for $key was found.
64+
*
65+
* @return bool TRUE if a config value for $key was found, otherwise FALSE.
66+
*/
67+
public static function getVar(string $key, &$outVar, $default = '')
68+
{
69+
$outVar = self::get($key, $default);
70+
71+
return self::$found;
72+
}
73+
74+
/**
75+
* Works the same way as **Config::get()**, but throws an exception if no config value for $key was found.
76+
*
77+
* @param bool $throwErrorIfEmpty Also throw an error if the config value is empty. 0 is not empty.
78+
* @throws Exception if no config value for $key was found.
5979
*
6080
* @return mixed
6181
*/
82+
public static function need(string $key, bool $throwErrorIfEmpty = false)
83+
{
84+
$value = self::get($key, '');
85+
86+
if(!self::$found)
87+
{
88+
Helper::ex('Missing config value. Key "%s".', $key);
89+
}
90+
91+
if($throwErrorIfEmpty && Helper::e($value))
92+
{
93+
Helper::ex('The config value must not be empty. Key "%s".', $key);
94+
}
95+
96+
return $value;
97+
}
98+
99+
/**
100+
* Checks if a config value for $key exists.
101+
*
102+
* @return bool
103+
*/
104+
public static function has(string $key)
105+
{
106+
self::get($key, '');
107+
108+
return self::$found;
109+
}
110+
111+
/**
112+
* Works the same way as **Config::get()**, but for the ez-mvc system config (system/system.config.php)
113+
*/
62114
public static function system(string $key = '', $default = '')
63115
{
116+
self::$found = true;
64117
global $_EZMVC_SYS_CONFIG;
65118

66119
if($key === '')
@@ -81,6 +134,7 @@ public static function system(string $key = '', $default = '')
81134
{
82135
if(!is_array($value) || !array_key_exists($dotkey, $value))
83136
{
137+
self::$found = false;
84138
return $default;
85139
}
86140
$value = $value[$dotkey];
@@ -89,6 +143,48 @@ public static function system(string $key = '', $default = '')
89143
return $value;
90144
}
91145

146+
/**
147+
* Works the same way as **Config::getVar()**, but for the ez-mvc system config (system/system.config.php)
148+
*/
149+
public static function systemVar(string $key, &$outVar, $default = '')
150+
{
151+
$outVar = self::system($key, $default);
152+
153+
return self::$found;
154+
}
155+
156+
/**
157+
* Works the same way as **Config::need()**, but for the ez-mvc system config (system/system.config.php)
158+
*/
159+
public static function systemNeed(string $key, bool $throwErrorIfEmpty = false)
160+
{
161+
$value = self::system($key, '');
162+
163+
if(!self::$found)
164+
{
165+
Helper::ex('Missing system-config value. Key "%s".', $key);
166+
}
167+
168+
if($throwErrorIfEmpty && Helper::e($value))
169+
{
170+
Helper::ex('The system-config value must not be empty. Key "%s".', $key);
171+
}
172+
173+
return $value;
174+
}
175+
176+
/**
177+
* Works the same way as **Config::has()**, but for the ez-mvc system config (system/system.config.php)
178+
*
179+
* @return bool
180+
*/
181+
public static function systemHas(string $key)
182+
{
183+
self::system($key, '');
184+
185+
return self::$found;
186+
}
187+
92188
/**
93189
* Loads the config files into the global vars
94190
*

system/Helper.php

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -118,26 +118,16 @@ public static function xout($value, bool $dontDie = false, bool $initCall = true
118118
}
119119

120120
/**
121-
* Shorthand call of throw new Exception(); with up to 8 placeholders
121+
* Shorthand call of **throw new Exception();** with placeholders.
122122
*
123123
* ```
124124
* //Example usage:
125125
* Helper::ex("%d errors while trying to delete user '%s'.", 4, "admin");
126126
* ```
127-
*
128-
* @param string $message The Exception message to show
129-
* @param mixed $p1 (optional) placeholder
130-
* @param mixed $p2 (optional) placeholder
131-
* @param mixed $p3 (optional) placeholder
132-
* @param mixed $p4 (optional) placeholder
133-
* @param mixed $p5 (optional) placeholder
134-
* @param mixed $p6 (optional) placeholder
135-
* @param mixed $p7 (optional) placeholder
136-
* @param mixed $p8 (optional) placeholder
137127
*/
138-
public static function ex(string $message, $p1 = '', $p2 = '', $p3 = '', $p4 = '', $p5 = '', $p6 = '', $p7 = '', $p8 = '')
128+
public static function ex(string $message, ...$params)
139129
{
140-
throw new \Exception(sprintf($message, $p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8));
130+
throw new \Exception(vsprintf($message, $params));
141131
}
142132

143133
/**
@@ -149,19 +139,15 @@ public static function ex(string $message, $p1 = '', $p2 = '', $p3 = '', $p4 = '
149139
*/
150140
public static function e($var, bool $zeroIsEmpty = false)
151141
{
152-
$type = gettype($var);
153-
switch($type)
154-
{
155-
case 'boolean' : return false;
156-
case 'integer' : return ($zeroIsEmpty && $var === 0) ? true : false;
157-
case 'double' : return ($zeroIsEmpty && $var === 0.0) ? true : false;
158-
case 'string' : return ($var === '');
159-
case 'array' : return ($var === []);
160-
case 'object' : return ($var == (object)[]);
161-
case 'resource' : return false;
162-
case 'NULL' : return true;
163-
default : return false;
164-
}
142+
return
143+
(
144+
($zeroIsEmpty && $var === 0) ||
145+
($zeroIsEmpty && $var === 0.0) ||
146+
$var === '' ||
147+
$var === [] ||
148+
$var === (object)[] ||
149+
$var === null
150+
);
165151
}
166152

167153
/**
@@ -257,9 +243,9 @@ public static function echoUrl(string $url)
257243

258244
/**
259245
* Checks if a $_GET request variable exists and stores its value in $outVar.
260-
* @return bool FALSE if variable exists, otherwise FALSE
246+
* @return bool TRUE if variable exists, otherwise FALSE
261247
*/
262-
public static function get(string $name, &$outVar, $default = "")
248+
public static function get(string $name, &$outVar, $default = '')
263249
{
264250
if(!isset($_GET[$name]) || Helper::e($_GET[$name]))
265251
{
@@ -273,9 +259,9 @@ public static function get(string $name, &$outVar, $default = "")
273259

274260
/**
275261
* Checks if a $_POST request variable exists and stores its value in $outVar.
276-
* @return bool FALSE if variable exists, otherwise FALSE
262+
* @return bool TRUE if variable exists, otherwise FALSE
277263
*/
278-
public static function post(string $name, &$outVar, $default = "")
264+
public static function post(string $name, &$outVar, $default = '')
279265
{
280266
if(!isset($_POST[$name]) || Helper::e($_POST[$name]))
281267
{
@@ -289,9 +275,9 @@ public static function post(string $name, &$outVar, $default = "")
289275

290276
/**
291277
* Checks if a session variable exists and stores its value in $outVar.
292-
* @return bool FALSE if variable exists, otherwise FALSE
278+
* @return bool TRUE if variable exists, otherwise FALSE
293279
*/
294-
public static function session(string $name, &$outVar, $default = "")
280+
public static function session(string $name, &$outVar, $default = '')
295281
{
296282
if(!isset($_SESSION[$name]) || Helper::e($_SESSION[$name]))
297283
{
@@ -305,7 +291,7 @@ public static function session(string $name, &$outVar, $default = "")
305291

306292
/**
307293
* Returns the value of a session variable.
308-
* @return mixed|bool FALSE if the session variable doesnt exist
294+
* @return mixed The session variable or $default
309295
*/
310296
public static function sessionVal(string $name, $default = false)
311297
{

0 commit comments

Comments
 (0)