Skip to content

Commit 4bcff6d

Browse files
committed
v1.0.0-beta.7
1 parent b2884a2 commit 4bcff6d

File tree

10 files changed

+286
-44
lines changed

10 files changed

+286
-44
lines changed

app/App.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
declare(strict_types = 1);
3+
namespace Alddesign\EzMvc;
4+
5+
use Alddesign\EzMvc\System\Route;
6+
use Alddesign\EzMvc\System\Router;
7+
8+
9+
abstract class App
10+
{
11+
12+
/**
13+
* Gets call before user defined routes get applied to the request.
14+
* Here you can add custom routers to the Router class
15+
*
16+
* @see \Alddesign\EzMvc\System\Router The Router class also has infos about the current request
17+
* @return void
18+
*/
19+
public static function onRequestPreRoutes()
20+
{
21+
//Here you can reroute the request (this is not a redirect)
22+
Router::addRoute(Route::newStartsWith('/Main/products', '/Product/list'));
23+
Router::addRoute(Route::newRegex('/\/item.*/i', '/Product/list'));
24+
}
25+
26+
/**
27+
* Gets called after the user defined routes get applied to the request.
28+
*
29+
* @see \Alddesign\EzMvc\System\Router The Router class has infos about the current request
30+
* @return void
31+
*/
32+
public static function onRequestPostRoutes()
33+
{
34+
35+
}
36+
37+
/**
38+
* Gets called after the routing and validation of the request url. At this point the request is valid (has controller and action)
39+
*
40+
* @see \Alddesign\EzMvc\System\Router The Router class has infos about the current request
41+
* @return void
42+
*/
43+
public static function onRequestPostValidate()
44+
{
45+
46+
}
47+
}

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.

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
define('EZ_PHP_DISPLAY_ERRORS', 'On'); //'On' or 'Off'. Specify if PHP shows runtime errors at all. See: https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
2121
define('EZ_PHP_DISPLAY_STARTUP_ERRORS', 'On'); //'On' or 'Off'. Specify if PHP shows startup errors. See: https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors
2222
define('EZ_PHP_ERROR_REPORTING', -1); //PHP error reporting level. See: https://www.php.net/manual/en/function.error-reporting.php
23-
define('EZ_VERSION', 'v1.0.0-beta.5'); //Version of ez-mvc (just in case u wanna know)
23+
define('EZ_VERSION', 'v1.0.0-beta.7'); //Version of ez-mvc (just in case u wanna know)

system/Helper.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
abstract class Helper
88
{
9+
private static $starts = [];
910
/**
1011
* A better implementation of PHP function var_dump();
1112
*
@@ -197,6 +198,15 @@ public static function addStartingSlash(string $value)
197198
return mb_substr($value, 0, 1) === '/' ? $value : '/'. $value;
198199
}
199200

201+
/**
202+
* Adds a starting slash to an url, path or whatever if it doesnt exist
203+
* @return string
204+
*/
205+
public static function addStartingAndTrailingSlash(string $value)
206+
{
207+
return self::addStartingSlash(self::addTrailingSlash($value));
208+
}
209+
200210
/**
201211
* Removes the starting slash of an url, path or whatever if there is one
202212
* @return string
@@ -305,7 +315,33 @@ public static function redirect(string $url, bool $literalUrl = false)
305315
$url = $literalUrl ? $url : self::url($url);
306316

307317
header('Location: ' . $url);
308-
die();
318+
die;
319+
}
320+
321+
/**
322+
* Short for htmlspecialchars()
323+
*/
324+
public static function h($text)
325+
{
326+
return htmlspecialchars((string)$text);
327+
}
328+
329+
public static function start(int $i = 0)
330+
{
331+
self::$starts[$i] = microtime(true);
332+
}
333+
334+
public static function end(int $i = 0, int $decimals = 3)
335+
{
336+
$e = microtime(true);
337+
if(!isset(self::$starts[$i]))
338+
{
339+
return -1.0;
340+
}
341+
342+
$s = self::$starts[$i];
343+
unset(self::$starts[$i]);
344+
return round($s - $e, $decimals);
309345
}
310346
}
311347

system/HelperGlobal.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ function u(string $url)
1313
{
1414
return \Alddesign\EzMvc\System\Helper::url($url);
1515
}
16+
17+
/**
18+
* Shorthand for \Alddesign\EzMvc\System\Helper::h($text)
19+
* @return string
20+
*/
21+
function h($text)
22+
{
23+
return \Alddesign\EzMvc\System\Helper::h($text);
24+
}

system/Route.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
declare(strict_types = 1);
3+
namespace Alddesign\EzMvc\System;
4+
5+
class Route
6+
{
7+
public string $path = '';
8+
public string $to = '';
9+
public bool $isDefault = false;
10+
public bool $isRegex = false;
11+
public bool $isStartsWith = false;
12+
13+
14+
private function __construct()
15+
{
16+
17+
}
18+
19+
/** @return Route */
20+
public static function newDefault(string $to)
21+
{
22+
$r = new Route();
23+
$r->isDefault = true;
24+
$r->to = $to;
25+
return $r;
26+
}
27+
28+
/** @return Route */
29+
public static function newRegex(string $pathRegex, string $to)
30+
{
31+
$r = new Route();
32+
$r->isRegex = true;
33+
$r->path = $pathRegex;
34+
$r->to = $to;
35+
return $r;
36+
}
37+
38+
/** @return Route */
39+
public static function newStartsWith(string $pathStartsWith, string $to)
40+
{
41+
$r = new Route();
42+
$r->isStartsWith = true;
43+
$r->path = $pathStartsWith;
44+
$r->to = $to;
45+
return $r;
46+
}
47+
48+
/** @return Route */
49+
public static function new(string $path, string $to)
50+
{
51+
$r = new Route();
52+
$r->path = $path;
53+
$r->to = $to;
54+
return $r;
55+
}
56+
57+
public function applies($path)
58+
{
59+
if($this->isDefault)
60+
{
61+
return true;
62+
}
63+
elseif($this->isRegex)
64+
{
65+
return preg_match($this->path, $path) === 1;
66+
}
67+
elseif($this->isStartsWith)
68+
{
69+
return str_starts_with($path, $this->path);
70+
}
71+
72+
return $path === $this->path || Helper::addTrailingSlash($path) === Helper::addTrailingSlash($this->path);
73+
}
74+
}

0 commit comments

Comments
 (0)