2
2
namespace App \Rooter ;
3
3
4
4
class Rooter {
5
- private $ uri = '/ ' ;
6
-
5
+
6
+ private string $ uri = '/ ' ;
7
+
8
+ private string $ siteName = '' ;
9
+
10
+ /**
11
+ * Rooter constructor.
12
+ * @param string $uri Uri (ex: '/', '/news', '/video?id=16a', etc)
13
+ */
14
+ public function __construct (string $ uri )
15
+ {
16
+ $ this ->uri = $ uri ;
17
+ }
18
+
7
19
/**
8
20
* root
9
- *
10
- * Root vers la bonne page
11
21
*
12
- * @param string $uri get the url like /
13
- * @return string the page
22
+ * Root vers la bonne page
23
+ * @return string La page
14
24
*/
15
- public function root (string $ uri ): string
25
+ public function root (): string
16
26
{
17
- $ this -> uri = $ uri ;
27
+ $ uri = $ this -> uri ;
18
28
19
29
if ($ uri === '/ ' ) {
20
- ob_start (); // init du transfère du fichier dans la variable $content
30
+ ob_start ();
21
31
require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . '/home.php ' ;
22
- return $ content = ob_get_clean (); // transfère dans la variable
23
- } else {
24
- if (file_exists ( dirname ( dirname ( __DIR__ )) . DIRECTORY_SEPARATOR . ' elements ' . ( string ) $ uri ) ) {
32
+ return $ content = ob_get_clean ();
33
+ } else if ( $ this -> doesItExist ( $ uri )) {
34
+ if (strpos ( $ uri , ' ? ' ) !== false ) {
25
35
ob_start ();
26
- require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . ( string ) $ uri ;
36
+ require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . $ this -> detectorGetForm ( $ uri) ;
27
37
return $ content = ob_get_clean ();
28
38
} else {
29
-
30
- $ position = strpos ((string )$ uri , "? " );
31
-
32
- if (!$ position ) {
33
- ob_start ();
34
- require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements/404.php ' ;
35
- return $ content = ob_get_clean ();
36
- } else {
37
-
38
- $ uri_TEMP = $ position ;
39
- print $ uri_TEMP ;
40
-
41
- $ uri_TEMP2 = substr ((string )$ uri , 0 , $ uri_TEMP );
42
- print ' ' . $ uri_TEMP2 ;
43
-
44
- if (file_exists (dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . $ uri_TEMP )) {
45
- ob_start ();
46
- require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . $ uri_TEMP ;
47
- return $ content = ob_get_clean ();
48
- } else {
49
- if ($ uri_TEMP === '/ ' ) {
50
- ob_start (); // init du transfère du fichier dans la variable $content
51
- require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . '/home.php ' ;
52
- return $ content = ob_get_clean (); // transfère dans la variable
53
- } else {
54
- ob_start ();
55
- require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements/404.php ' ;
56
- return $ content = ob_get_clean ();
57
- }
58
- }
59
- }
60
-
61
-
39
+ ob_start ();
40
+ require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . (string )$ uri . '.php ' ;
41
+ return $ content = ob_get_clean ();
62
42
}
63
-
43
+ } else {
44
+ ob_start ();
45
+ require dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements/404.php ' ;
46
+ return $ content = ob_get_clean ();
64
47
}
48
+
65
49
}
66
50
/**
67
- * getPageTitle -> Récupère le titre de la page
51
+ * getPageTitle Récupère le titre de la page
68
52
*
69
- * @return string -> Titre de la page
53
+ * @return string Titre de la page
70
54
*/
71
55
public function getPageTitle (): string
72
56
{
73
57
$ uri = $ this ->uri ;
74
58
75
- function createTitle (string $ uri ): string {
76
- $ lenght = strlen ($ uri ) - 5 ;
77
-
78
- $ preTitle = substr ($ uri , 1 , $ lenght );
79
- $ firstLetter = strtoupper (substr ($ preTitle , 0 , 1 ));
80
-
81
- $ title = $ firstLetter . substr ($ preTitle , 1 );
82
- return $ title ;
59
+ if ($ uri === '/ ' ) { // si on est dans la racine du site
60
+ return $ this ->transformEnd ('Accueil ' );
61
+ } else if ($ this ->doesItExist ($ uri )) {
62
+ if (strpos ($ uri , '? ' ) !== false ) {
63
+ $ title = strtoupper (substr ($ uri , 1 , 1 )) . substr ($ uri , 2 , (strpos ($ uri , '? ' ) - 2 ));
64
+ return $ this ->transformEnd ($ title );
65
+ } else {
66
+ $ title = strtoupper (substr ($ uri , 1 , 1 )) . substr ($ uri , 2 , strlen ($ uri ));
67
+ return $ this ->transformEnd ($ title );
68
+ }
69
+ } else {
70
+ return '404 ' ;
83
71
}
84
72
85
- if ($ uri === '/ ' ) { // si on est dans la racine du site
86
- return 'Accueil ' ;
73
+ }
74
+
75
+ /**
76
+ * getPageDesc Récupère la description de la page
77
+ *
78
+ * @return string Description de la page
79
+ */
80
+ public function getPageDesc (): string
81
+ {
82
+ $ uri = $ this ->uri ;
83
+ if ($ uri === '/ ' ) {
84
+ return 'Description ' ;
85
+ } else if ($ this ->doesItExist ($ uri )) {
86
+ return '' ;
87
87
} else {
88
+ return '404 error ' ;
89
+ }
90
+ }
88
91
89
- if (file_exists (dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . (string )$ uri )) {
90
- return createTitle ($ uri );
92
+
93
+ public function setSiteName (string $ siteName ): void
94
+ {
95
+ $ this ->siteName = $ siteName ;
96
+ }
97
+
98
+
99
+ /**
100
+ * doesItExist Informe si le fichier portant ce nom existe
101
+ *
102
+ * @param mixed $uri Uri (ex: '/', '/hey', etc)
103
+ * @return bool True = fichier existe ; False = n'existe pas
104
+ */
105
+ private function doesItExist (string $ uri ): bool
106
+ {
107
+ if ($ uri === '/html ' ) {
108
+ return false ;
109
+ } else if (file_exists (dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . (string )$ uri . '.php ' )) {
110
+ return true ;
111
+ } else if (strpos ($ uri , '? ' ) !== false ) {
112
+ if (file_exists (dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . $ this ->detectorGetForm ($ uri ))) {
113
+ return true ;
91
114
} else {
92
- $ position = strpos ((string )$ uri , "? " );
93
-
94
- if (!$ position ) {
95
- return '404 ' ;
96
- } else {
97
-
98
- $ uri_TEMP = $ position - 1 ;
99
-
100
- $ uri_TEMP2 = substr ((string )$ uri , 0 , $ uri_TEMP );
101
-
102
- if (file_exists (dirname (dirname (__DIR__ )) . DIRECTORY_SEPARATOR . 'elements ' . $ uri_TEMP2 )) {
103
- return createTitle ($ uri_TEMP2 );
104
- } else {
105
- if ($ uri_TEMP === '/ ' ) {
106
- return 'Accueil ' ;
107
- } else {
108
- return '404 ' ;
109
- }
110
- }
111
- }
115
+ return false ;
112
116
}
117
+ } else {
118
+ return false ;
119
+ }
120
+ }
121
+
122
+ /**
123
+ * transformEnd Transforme la fin du fichier
124
+ *
125
+ * @param mixed $base Base à transformer
126
+ * @return string Base transformée
127
+ */
128
+ private function transformEnd (string $ base ): string
129
+ {
130
+ return $ base . ' | ' . $ this ->siteName ;
131
+ }
113
132
133
+ /**
134
+ * detectorGetForm Détecte et renvoie le nom du fichier originel lors de l'utilisation de GET dans un formulaire
135
+ *
136
+ * @param mixed $uri Uri (ex: '/', '/hey?lul=yes', etc)
137
+ * @return string Nom du fichier ; False = fichier inexistant
138
+ */
139
+ private function detectorGetForm (string $ uri ): string
140
+ {
141
+ if (strpos ($ uri , '? ' ) !== false ) {
142
+ return substr ($ uri , 0 , strpos ($ uri , '? ' )) . '.php ' ;
143
+ } else {
144
+ return false ;
114
145
}
115
146
}
116
147
}
0 commit comments