Skip to content

Commit 0e8fc2f

Browse files
committed
Merge branch 'release/v0.1'
2 parents fbfe7db + 87c043b commit 0e8fc2f

File tree

26 files changed

+853
-47
lines changed

26 files changed

+853
-47
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/nbproject/private/
2+
/vendor/
3+
4+
Engine/storage/views/
5+
6+
\.idea/

.htaccess

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
RewriteEngine on
2+
3+
4+
#RewriteCond %{REQUEST_URI} ^/i/
5+
#RewriteCond %{REQUEST_FILENAME} !-f
6+
#RewriteRule .* image.php [L]
7+
8+
9+
10+
RewriteCond %{REQUEST_URI} !/(public|Favicon)/
211
RewriteCond %{REQUEST_FILENAME} !-f
312
RewriteCond %{REQUEST_FILENAME} !-d
4-
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
13+
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
14+

Engine/App.php

100644100755
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Engine;
44

5+
use Engine\Error\ShiftError;
6+
use ReflectionClass;
7+
58
/**
69
* Class App
710
* @package Engine
@@ -22,28 +25,28 @@ final class App {
2225
public function __construct() {
2326
}
2427

28+
/**
29+
* @throws \ReflectionException
30+
*/
2531
public static function start(): void {
26-
spl_autoload_register(array('\Engine\App', 'autoload'));
27-
28-
2932
static $run;
3033
if ($run === TRUE) return;
3134

3235
Request::setup();
33-
echo Request::getController();
34-
echo APP_PATH;
35-
$controller = ucfirst(Request::getController()) . 'Controller';
36+
$controller = 'Controllers\\' . ucfirst(Request::getController()) . 'Controller';
3637

37-
self::$controller = new $controller();
38+
try {
39+
self::$controller = new $controller();
40+
} catch (\Throwable $exception) {
41+
throw new ShiftError($exception->getMessage(), $exception->getCode(), $exception->getPrevious());
42+
}
3843

3944
$class = new ReflectionClass(self::$controller);
4045

41-
$method = $class->getMethod(URI::getAction());
46+
$method = $class->getMethod(Request::getAction());
4247

43-
$method->invokeArgs(self::$controller, URI::getArguments());
48+
$method->invokeArgs(self::$controller, Request::getArguments());
4449
$run = TRUE;
45-
46-
4750
}
4851

4952

@@ -66,13 +69,14 @@ public static function autoload(string $class_name): void {
6669
];
6770

6871
foreach ($locations as $location) {
69-
echo $location . $class . '.php' . "<br>";
7072
if (file_exists($location . $class . '.php')) {
7173
require_once($location . $class . '.php');
7274
}
7375
}
7476

7577
}
7678

77-
78-
}
79+
public static function setHelpers(): void {
80+
require_once 'Utils/helpers.php';
81+
}
82+
}

Engine/Controller.php

100644100755
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
class Controller {
1010

1111
/**
12-
*
12+
* @param string $view
13+
* @param array $data
14+
* @param string $title
15+
* @param array $styles
16+
* @param array $scripts
17+
* @return void
1318
*/
14-
public function render(){
15-
19+
public function render(string $view, array $data = [], string $title = '', array $styles = [], array $scripts = []): void {
20+
(new View)->make($view, $data, $title, $styles, $scripts);
1621
}
17-
}
22+
}

Engine/Error/ShiftError.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawid
5+
* Date: 07.07.18
6+
* Time: 17:58
7+
*/
8+
9+
namespace Engine\Error;
10+
11+
12+
use Throwable;
13+
14+
/**
15+
* Class ShiftError
16+
* @package Engine\Error
17+
*/
18+
class ShiftError extends \Error {
19+
20+
/**
21+
* ShiftError constructor.
22+
* @param string $message
23+
* @param int $code
24+
* @param Throwable|null $previous
25+
*/
26+
public function __construct(string $message = "", int $code = 0, Throwable $previous = null) {
27+
parent::__construct($message, $code, $previous);
28+
29+
$line = (int)$this->getLine();
30+
$highlighted = $this->highlight_file_with_line_numbers($this->getFile(), $line);
31+
$stackTrace = $this->getBeautyStackTrace();
32+
33+
echo '
34+
<style>
35+
*{margin: 0;padding: 0;}
36+
#error-container{width: 100%;float: left;background: #135171;box-sizing: border-box;padding: 5px;color:#fff;font-family: Arial, sans-serif}
37+
#error-container #message{width: 100%;float: left;}
38+
#error-container .file-presentation,
39+
#error-container #stack-trace .trace-item .file-presentation{white-space: pre-wrap;background: white;position: relative}
40+
#error-container #stack-trace .trace-item .file-presentation{display: none;}
41+
#error-container .file-presentation #yellow-line,
42+
#error-container #stack-trace .trace-item .file-presentation #yellow-line{width: 100%;z-index: 1;position: absolute;height: 15px;left: 0;background: rgba(255,255,0,0.36)}
43+
#error-container .file-presentation code,
44+
#error-container #stack-trace .trace-item .file-presentation code{z-index: 2;}
45+
#error-container #stack-trace .trace-item{border-bottom:1px #ffffff solid;padding: 5px 2px}
46+
#error-container #stack-trace .trace-item:last-child{border:none;}
47+
#error-container #stack-trace .trace-item .filename{font-weight: bold}
48+
#error-container #stack-trace .trace-item .filepath{font-size: 12px;line-height: 15px;}
49+
#error-container #stack-trace .trace-item .filepath .show-hide{cursor: pointer;font-size: 15px;padding: 0 5px;font-weight: bold;}
50+
#error-container #stack-trace .trace-item .method{font-size: 14px;font-style: italic}
51+
#error-container #stack-trace .trace-item .method .array{border-bottom: 1px #fff dotted; cursor: pointer;position: relative}
52+
#error-container #stack-trace .trace-item .method .array .array-content{display: none;position: absolute;bottom: 15px;left: -70px;background: #f3f3f3;color: #000;min-width: 200px;padding: 4px;white-space: pre;border: 1px #a2a2a2 dotted;cursor: text;font-style: normal}
53+
#error-container #stack-trace .trace-item .method .array:hover .array-content{display: inline}
54+
</style>
55+
<div id="error-container">
56+
<div id="message">' . $this->getMessage() . '</div>
57+
<div id="file-info">in <b>' . $this->getFile() . '</b></div>
58+
<div class="file-presentation">' . $highlighted . '</div>
59+
<br><br>
60+
<div id="stack-trace-text">Stack trace:</div>
61+
<div id="stack-trace">' . $stackTrace . '</div>
62+
</div>
63+
<script>
64+
const buttons = document.getElementsByClassName("show-hide");
65+
66+
for(const button of buttons) {
67+
button.addEventListener("click", function(e) {
68+
const filePresentation = this.parentElement.parentElement.getElementsByClassName("file-presentation");
69+
70+
if(!filePresentation.length) {
71+
return;
72+
}
73+
74+
if (filePresentation[0].style.display !== "block") {
75+
filePresentation[0].style.display = "block";
76+
this.innerText = "-";
77+
} else {
78+
this.innerText = "+";
79+
filePresentation[0].style.display = "none";
80+
}
81+
});
82+
}
83+
</script>';
84+
}
85+
86+
/**
87+
* @param string $file
88+
* @param int $lineWithError
89+
* @return string
90+
*/
91+
private function highlight_file_with_line_numbers(string $file, int $lineWithError = null): string {
92+
$code = substr(highlight_file($file, true), 36, -15);
93+
$lines = explode('<br />', $code);
94+
$lineCount = count($lines);
95+
$padLength = strlen($lineCount);
96+
97+
$return = '';
98+
if ($lineWithError)
99+
$return .= '<div id="yellow-line" style="top: 75px"></div>';
100+
101+
$return .= "<code><span style=\"color: #000000\">";
102+
103+
$start = null;
104+
$limit = 10;
105+
if ($lineWithError) {
106+
$start = $lineWithError - 5;
107+
}
108+
109+
foreach ($lines as $i => $line) {
110+
if ($start) {
111+
if ($start > $i + 1) {
112+
continue;
113+
}
114+
if ($start + $limit < $i + 1) {
115+
continue;
116+
}
117+
}
118+
$lineNumber = str_pad($i + 1, $padLength, " ", STR_PAD_LEFT);
119+
$return .= '<span style="color: #999999">' . $lineNumber . ' | </span>' . $line . "\n";
120+
}
121+
122+
$return .= "</span></code>";
123+
124+
125+
return $return;
126+
}
127+
128+
/**
129+
* @return string
130+
*/
131+
private function getBeautyStackTrace(): string {
132+
$return = '';
133+
foreach ($this->getTrace() as $info) {
134+
$infoFile = isset($info['file']) ? $info['file'] : '';
135+
$pathAsArray = explode('/', $infoFile);
136+
$filename = $pathAsArray[count($pathAsArray) - 1];
137+
138+
$fargs = '';
139+
140+
foreach ($info['args'] as $arg) {
141+
$type = gettype($arg);
142+
143+
switch ($type) {
144+
case 'string':
145+
if (strlen($arg) === 0) {
146+
$fargs .= "''";
147+
} else {
148+
$fargs .= "'" . $arg . "'";
149+
}
150+
break;
151+
case 'array':
152+
$fargs .= '<span class="array">Array<span class="array-content">' . print_r($arg, true) . '</span></span>';
153+
break;
154+
case 'object':
155+
$fargs .= '<span class="array">' . get_class($arg) . ' Object<span class="array-content">' . print_r($arg, true) . '</span></span>';
156+
break;
157+
default:
158+
$fargs .= $arg;
159+
}
160+
$fargs .= ', ';
161+
}
162+
$fargs = trim($fargs, ', ');
163+
164+
$infoLine = isset($info['line']) ? $info['line'] : '';
165+
$filePresentation = strlen($infoFile) && stream_is_local($infoFile) ? $this->highlight_file_with_line_numbers($infoFile, (int)$infoLine) : '';
166+
$return .= '
167+
<div class="trace-item">
168+
<div class="filename">
169+
' . $filename . ':' . $infoLine . '
170+
</div>
171+
<div class="filepath">
172+
' . $info["file"] . ' <span class="show-hide">+</span>
173+
</div>
174+
<div class="method">
175+
' . $info["class"] . $info["type"] . $info["function"] . '(' . $fargs . ')
176+
</div >
177+
<div class="file-presentation">' . $filePresentation . '</div >
178+
</div >
179+
';
180+
}
181+
return $return;
182+
}
183+
}

Engine/Error/StorageError.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: dawid
5+
* Date: 07.07.18
6+
* Time: 17:58
7+
*/
8+
9+
namespace Engine\Error;
10+
11+
12+
use Throwable;
13+
14+
/**
15+
* Class StorageError
16+
* @package Engine\Error
17+
*/
18+
class StorageError extends ShiftError {
19+
20+
/**
21+
* StorageError constructor.
22+
* @param string $message
23+
* @param int $code
24+
* @param Throwable|null $previous
25+
*/
26+
public function __construct(string $message = "", int $code = 0, Throwable $previous = null) {
27+
parent::__construct('Storage Error: ' . $message, $code, $previous);
28+
}
29+
}

Engine/Request.php

100644100755
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

33
namespace Engine;
4-
use \Engine\Utils\Debug;
54

65
/**
76
* Class Request
@@ -27,7 +26,7 @@ class Request {
2726
/**
2827
* @var
2928
*/
30-
protected static $arguments;
29+
protected static $arguments = [];
3130

3231
/**
3332
* @var
@@ -51,16 +50,15 @@ public static function setup(): void {
5150

5251
self::$array = explode('/', trim(self::$path, '/'));
5352

54-
self::$controller = self::$array[0] ?? App::$defaultController;
55-
self::$action = self::$array[1] ?? App::$defaultAction;
56-
53+
self::$controller = (self::$array[0] ?? App::$defaultController) ?: App::$defaultController;
54+
self::$action = (self::$array[1] ?? App::$defaultAction) ?: App::$defaultAction;
5755

5856
if (count(self::$array) > 2) {
5957
$tmp = array_slice(self::$array, 2, count(self::$array));
6058
self::$arguments = $tmp;
6159
}
6260

63-
// Debug::dd(Request::getArray(), Request::getArguments(), $get, $post);
61+
// dd(Request::getArray(), Request::getArguments() , 'sdad', $get, $post);
6462
}
6563

6664
/**
@@ -132,4 +130,4 @@ public static function getPath(): string {
132130
public static function setPath($path): void {
133131
self::$path = $path;
134132
}
135-
}
133+
}

0 commit comments

Comments
 (0)