1
1
<?php
2
2
namespace Appkita \SPARK ;
3
+ require_once __DIR__ .DIRECTORY_SEPARATOR .'config.php ' ;
4
+ require_once __DIR__ .DIRECTORY_SEPARATOR .'helps.php ' ;
5
+ require_once __DIR__ .DIRECTORY_SEPARATOR .'Request.php ' ;
6
+
3
7
use Exception ;
8
+ use \Appkita \SPARK \Helps ;
9
+ use \Appkita \SPARK \Request ;
10
+ use \Appkita \SPARK \Config ;
4
11
5
12
Class App {
6
- protected $ path = '' ;
7
- protected $ router = '' ;
8
- private $ indexFiles = ['index.html ' , 'index.php ' ];
13
+ use Config;
14
+ public $ request ;
9
15
10
16
function __construct ($ config = '' ) {
11
- $ this ->init ($ config );
12
- }
17
+ $ this ->initConfig ($ config );
13
18
14
- public function getListFiles () {
15
- return $ this ->listFiles ;
19
+ $ this ->request = new Request ([
20
+ 'host ' =>$ this ->getHost (),
21
+ 'port ' =>$ this ->getPort (),
22
+ 'path ' =>$ this ->getPath (),
23
+ 'router ' =>$ this ->getRouter (),
24
+ 'indexFiles ' =>$ this ->getIndexFiles ()
25
+ ]);
16
26
}
17
27
18
- public function init ($ config = '' ) {
19
- if (!empty ($ config )) {
20
- if (\is_string ($ config )) {
21
- $ this ->path = $ config ;
22
- } else if (\is_object ($ config )) {
23
- if (isset ($ config ->path )) $ this ->path = $ config ->path ;
24
- if (isset ($ config ->router )) $ this ->router = $ config ->router ;
25
- if (isset ($ config ->indexFiles )) $ this ->indexFiles = $ config ->indexFiles ;
26
- } else if (\is_array ($ config )) {
27
- $ config = (object ) $ config ;
28
- if (isset ($ config ->path )) $ this ->path = $ config ->path ;
29
- if (isset ($ config ->router )) $ this ->router = $ config ->router ;
30
- if (isset ($ config ->indexFiles )) $ this ->indexFiles = $ config ->indexFiles ;
31
- }
28
+ public function showError (int $ code = 404 ) {
29
+ $ page = $ this ->pageError ($ code );
30
+ if (\file_exists ($ page )) {
31
+ $ page = $ this ->pageError (404 );
32
32
}
33
+ include $ page ;
34
+ die ();
33
35
}
34
36
35
- public function getRouter ($ page ) {
36
- $ router = $ this ->router ;
37
- if (\is_array ($ router )){
38
- foreach ($ router as $ regex => $ fn )
39
- {
40
- if (preg_match ('% ' .$ regex .'% ' , $ page ))
41
- {
42
- return dirname (__FILE__ ) . $ fn ;
43
- break ;
44
- }
45
- }
37
+ public function pageError (int $ code =404 ) {
38
+ switch ($ code ) {
39
+ case 404 :
40
+ return 'Error/notfound.php ' ;
41
+ break ;
46
42
}
47
- return $ page ;
48
43
}
49
44
50
- public function getIndexPage ($ page ) {
51
- if (is_dir ($ page ))
52
- {
53
- foreach ($ this ->indexFiles as $ filename )
54
- {
55
-
56
- $ fn = $ page .DIRECTORY_SEPARATOR .$ filename ;
57
- if (is_file ($ fn )) {
58
- return $ fn ;
59
- break ;
45
+ public function _autoload () {
46
+ if (is_string ($ this ->autoload )) {
47
+ if (\file_exists ($ this ->autoload )) {
48
+ require_once $ this ->autoload ;
49
+ }
50
+ } else if (\is_array ($ this ->autoload )) {
51
+ for ($ i = 0 ; $ i < sizeof ($ this ->autoload ); $ i ++) {
52
+ if (\file_exists ($ this ->autoload [$ i ])) {
53
+ require_once $ this ->autoload [$ i ];
60
54
}
61
55
}
62
- return self ::pageError (404 );
63
56
}
64
- return $ page ;
65
57
}
66
58
67
- public function pageError (int $ code =404 ) {
68
- $ path = \dirname (__FILE__ );
69
- switch ($ code ) {
70
- case 404 :
71
- return 'Error/notfound.php ' ;
72
- break ;
59
+ public function run () {
60
+ $ page = rtrim ($ this ->request ->getPage (), '\\% ' );
61
+ $ ext = (new \SplFileInfo ($ page ))->getExtension ();
62
+ $ isClass = false ;
63
+ if (empty ($ ext )) {
64
+ if ($ this ->getRewrite () && !empty ($ this ->getFileClass ())) {
65
+ $ page = $ this ->getFileClass ();
66
+ $ isClass = true ;
67
+ } else {
68
+ $ page = $ this ->pageError (404 );
69
+ }
73
70
}
71
+ return $ this ->openPage ($ page , $ isClass );
74
72
}
75
73
76
- public function openPage ($ page = null ) {
77
- if (empty ($ page )) {
78
- $ page = '' ;
79
- }
80
- $ page = \ltrim (\rtrim (rtrim ($ page , '/ ' ), '\\' ));
81
- $ page = $ this ->path . $ page ;
82
- if (!\file_exists ($ page )) {
83
- include_once self ::pageError (404 );
84
- return true ;
85
- }
86
- $ page = self ::getRouter ($ page );
87
- $ page = self ::getIndexPage ($ page );
74
+ public function openPage ($ page , $ isClass = false ) {
88
75
$ ext = (new \SplFileInfo ($ page ))->getExtension ();
89
76
if (\strtolower ($ ext ) == 'php ' ) {
90
- include_once $ page ;
91
- return true ;
77
+ $ this ->_autoload ();
78
+ $ exp_page = explode ('\\' , $ page );
79
+ $ pagename = str_replace ('.php ' , '' , $ exp_page [(sizeof ($ exp_page ) - 1 )]);
80
+ $ class_file = strtolower (str_replace ('.php ' , '' , $ this ->file_class ));
81
+ if ($ pagename == $ class_file ) {
82
+ $ isClass = true ;
83
+ }
84
+ if ($ isClass && $ this ->getRewrite () && !empty ($ this ->getFileClass ())) {
85
+ if (file_exists ($ page )) {
86
+ $ page = $ page ;
87
+ } else if (file_exists ($ this ->getPath () . $ page )) {
88
+ $ page = $ this ->getPath ().$ page ;
89
+ } else {
90
+ return $ this ->showError (404 );
91
+ }
92
+ include_once $ page ;
93
+ $ initclass = Helps::getClassNameFile ($ page );
94
+ $ classname = !empty ($ initclass ->namespace ) ? '\\' . $ initclass ->namespace : '\\' ;
95
+ $ classname .= $ initclass ->class ;
96
+ $ class = new $ classname ();
97
+ $ uri = $ this ->request ->getUri ();
98
+ if (\is_callable (array ($ class , strtolower ($ uri )))) {
99
+ $ param = $ this ->request ->argsURL ($ uri );
100
+ return \call_user_func_array (array ($ class , $ uri ), $ param );
101
+ } else {
102
+ return $ this ->showError (404 );
103
+ }
104
+ } else {
105
+ if (file_exists ($ page )) {
106
+ include_once $ page ;
107
+ } else {
108
+ return $ this ->showError (404 );
109
+ }
110
+ }
111
+ die ();
92
112
} else {
93
113
if ($ ext == 'html ' || $ ext == 'htm ' ) {
94
114
$ mimi = 'text/html ' ;
@@ -100,11 +120,11 @@ public function openPage($page = null) {
100
120
}else {
101
121
$ mimi = mime_content_type ($ page );
102
122
}
103
- header ('Content-Type: ' . $ mimi );
123
+ header ('Content-Type: ' . $ mimi );
104
124
$ fh = fopen ($ page , 'r ' );
105
125
fpassthru ($ fh );
106
126
fclose ($ fh );
107
- return true ;
127
+ die () ;
108
128
}
109
129
}
110
130
}
@@ -124,6 +144,5 @@ function is_cli() : bool{
124
144
if (!is_cli ()) {
125
145
$ env = json_decode (\getenv ('CONFIG_ENV ' ));
126
146
$ app = new App ($ env );
127
- $ page = !empty ($ page ) ? $ page : $ _SERVER ['REQUEST_URI ' ];
128
- $ app ->openPage ($ page );
147
+ $ app ->run ();
129
148
}
0 commit comments