55namespace MaplePHP \Http ;
66
77use MaplePHP \Http \Interfaces \DirInterface ;
8+ use MaplePHP \Http \Interfaces \DirHandlerInterface ;
89
910class Dir implements DirInterface
1011{
1112 private $ dir ;
12- private $ publicDirPath ;
13+ private $ handler ;
14+
1315
1416 public function __construct ($ dir )
1517 {
1618 $ this ->dir = $ dir ;
17-
18- // Will move this
19- $ envDir = getenv ("APP_PUBLIC_DIR " );
20- $ this ->publicDirPath = "public/ " ;
21- if (is_string ($ envDir ) && $ this ->validateDir ($ envDir )) {
22- $ this ->publicDirPath = ltrim (rtrim ($ envDir , "/ " ), "/ " )."/ " ;
23- }
2419 }
2520
2621 /**
27- * Get root dir
28- * @param string $path
29- * @return string
22+ * Not required but recommended. You can pass on Directory shortcuts to the class
23+ * E.g. getPublic, getCss
24+ * @param DirHandlerInterface $handler
3025 */
31- public function getDir ( string $ path = "" ): string
26+ public function setHandler ( DirHandlerInterface $ handler ): void
3227 {
33- return $ this ->dir . $ path ;
28+ $ this ->handler = $ handler ;
3429 }
3530
3631 /**
3732 * Get root dir
3833 * @param string $path
3934 * @return string
4035 */
41- public function getRoot (string $ path = "" ): string
42- {
43- return $ this ->getDir ($ path );
44- }
45-
46- /**
47- * Get resource dir
48- * @param string $path
49- * @return string
50- */
51- public function getResources (string $ path = "" ): string
52- {
53- return $ this ->getDir ("resources/ {$ path }" );
54- }
55-
56- /**
57- * Get resource dir
58- * @param string $path
59- * @return string
60- */
61- public function getPublic (string $ path = "" ): string
36+ public function getDir (string $ path = "" ): string
6237 {
63- return $ this ->getDir ( "{ $ this -> publicDirPath }{ $ path}" ) ;
38+ return $ this ->dir . $ path ;
6439 }
6540
6641 /**
67- * Get storage dir
42+ * Get root dir
6843 * @param string $path
6944 * @return string
7045 */
71- public function getStorage (string $ path = "" ): string
46+ public function getRoot (string $ path = "" ): string
7247 {
73- return $ this ->getDir (" storage/ { $ path}" );
48+ return $ this ->getDir ($ path );
7449 }
7550
7651 /**
@@ -80,22 +55,26 @@ public function getStorage(string $path = ""): string
8055 */
8156 public function getLogs (string $ path = "" ): string
8257 {
83- return $ this ->getStorage ("logs/ {$ path }" );
58+ if (!is_null ($ this ->handler )) {
59+ return $ this ->handler ->getLogs ($ path );
60+
61+ }
62+ return "" ;
8463 }
8564
8665 /**
87- * Get cache dir
88- * @param string $path
89- * @return string
66+ * Access handler
67+ * @param string $method
68+ * @param array $args
69+ * @return mixed
70+ * @psalm-taint-sink
9071 */
91- public function getCaches ( string $ path = "" ): string
72+ public function __call ( $ method , $ args ): mixed
9273 {
93- return $ this ->getStorage ("caches/ {$ path }" );
94- }
95-
96- public function validateDir (string $ path ): bool
97- {
98- $ fullPath = realpath ($ _ENV ['APP_DIR ' ].$ path );
99- return (is_string ($ fullPath ) && strpos ($ fullPath , $ _ENV ['APP_DIR ' ]) === 0 );
74+ if (!is_null ($ this ->handler ) && method_exists ($ this ->handler , $ method )) {
75+ return call_user_func_array ([$ this ->handler , $ method ], $ args );
76+ } else {
77+ throw new \BadMethodCallException ("The method ( {$ method }) does not exist in \"" .__CLASS__ ."\" (DirInterface or DirHandlerInterface). " , 1 );
78+ }
10079 }
10180}
0 commit comments