55namespace Codefy \Framework \Helpers ;
66
77use Codefy \Framework \Application ;
8+ use Qubus \Exception \Data \TypeException ;
9+ use Qubus \Exception \Exception ;
810
11+ use function implode ;
912use function ltrim ;
1013use function Qubus \Security \Helpers \__observer ;
11- use function Qubus \Support \Helpers \is_null__ ;
1214use function str_replace ;
1315
1416/**
1517 * Get the path to the application base directory.
1618 *
1719 * @param string|null $path
1820 * @return string
21+ * @throws TypeException
1922 */
2023function base_path (?string $ path = null ): string
2124{
22- return app (name: 'dir.path ' )->base . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
25+ return join_paths ( app (name: 'dir.path ' )->base , $ path );
2326}
2427
2528/**
2629 * Get the path to the application "src" directory.
2730 *
2831 * @param string|null $path
2932 * @return string
33+ * @throws TypeException
3034 */
3135function src_path (?string $ path = null ): string
3236{
33- return app (name: 'dir.path ' )->path . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
37+ return join_paths ( app (name: 'dir.path ' )->base , $ path );
3438}
3539
3640/**
3741 * Get the path to the application "bootstrap" directory.
3842 *
3943 * @param string|null $path
4044 * @return string
45+ * @throws TypeException
4146 */
4247function bootstrap_path (?string $ path = null ): string
4348{
44- return app (name: 'dir.path ' )->bootstrap . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
49+ return join_paths ( app (name: 'dir.path ' )->bootstrap , $ path );
4550}
4651
4752/**
4853 * Get the path to the application "config" directory.
4954 *
5055 * @param string|null $path
5156 * @return string
57+ * @throws TypeException
5258 */
5359function config_path (?string $ path = null ): string
5460{
55- return app (name: 'dir.path ' )->config . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
61+ return join_paths ( app (name: 'dir.path ' )->config , $ path );
5662}
5763
5864/**
5965 * Get the path to the application "database" directory.
6066 *
6167 * @param string|null $path
6268 * @return string
69+ * @throws TypeException
6370 */
6471function database_path (?string $ path = null ): string
6572{
66- return app (name: 'dir.path ' )->database . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
73+ return join_paths ( app (name: 'dir.path ' )->database , $ path );
6774}
6875
6976/**
7077 * Get the path to the application "locale" directory.
7178 *
7279 * @param string|null $path
7380 * @return string
81+ * @throws TypeException
7482 */
7583function locale_path (?string $ path = null ): string
7684{
77- return app (name: 'dir.path ' )->locale . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
85+ return join_paths ( app (name: 'dir.path ' )->locale , $ path );
7886}
7987
8088/**
8189 * Get the path to the application "public" directory.
8290 *
8391 * @param string|null $path
8492 * @return string
93+ * @throws TypeException
8594 */
8695function public_path (?string $ path = null ): string
8796{
88- return app (name: 'dir.path ' )->public . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
97+ return join_paths ( app (name: 'dir.path ' )->public , $ path );
8998}
9099
91100/**
92101 * Get the path to the application "storage" directory.
93102 *
94103 * @param string|null $path
95104 * @return string
105+ * @throws TypeException
96106 */
97107function storage_path (?string $ path = null ): string
98108{
99- return app (name: 'dir.path ' )->storage . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
109+ return join_paths ( app (name: 'dir.path ' )->storage , $ path );
100110}
101111
102112/**
103113 * Get the path to the application "resource" directory.
104114 *
105115 * @param string|null $path
106116 * @return string
117+ * @throws TypeException
107118 */
108119function resource_path (?string $ path = null ): string
109120{
110- return app (name: 'dir.path ' )->resource . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
121+ return join_paths ( app (name: 'dir.path ' )->resource , $ path );
111122}
112123
113124/**
114125 * Get the path to the application "view" directory.
115126 *
116127 * @param string|null $path
117128 * @return string
129+ * @throws TypeException
118130 */
119131function view_path (?string $ path = null ): string
120132{
121- return app (name: 'dir.path ' )->view . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
133+ return join_paths ( app (name: 'dir.path ' )->view , $ path );
122134}
123135
124136/**
125137 * Get the path to the application "vendor" directory.
126138 *
127139 * @param string|null $path
128140 * @return string
141+ * @throws TypeException
129142 */
130143function vendor_path (?string $ path = null ): string
131144{
132- return app (name: 'dir.path ' )->vendor . (! is_null__ (var: $ path) ? Application:: DS . $ path : '' );
145+ return join_paths ( app (name: 'dir.path ' )->vendor , $ path );
133146}
134147
148+ /**
149+ * @throws Exception
150+ */
135151function router_basepath (string $ path ): string
136152{
137153 $ fullPath = str_replace (search: $ _SERVER ['DOCUMENT_ROOT ' ], replace: '' , subject: $ path );
@@ -140,3 +156,23 @@ function router_basepath(string $path): string
140156
141157 return ltrim (string: $ filteredPath , characters: '/ ' ) . '/ ' ;
142158}
159+
160+ /**
161+ * Join the given paths together.
162+ *
163+ * @param string|null $basePath
164+ * @param string ...$paths
165+ * @return string
166+ */
167+ function join_paths (?string $ basePath = null , ...$ paths ): string
168+ {
169+ foreach ($ paths as $ index => $ path ) {
170+ if (empty ($ path ) && $ path !== '0 ' ) {
171+ unset($ paths [$ index ]);
172+ } else {
173+ $ paths [$ index ] = Application::DS . ltrim (string: $ path , characters: Application::DS );
174+ }
175+ }
176+
177+ return $ basePath . implode (separator: '' , array: $ paths );
178+ }
0 commit comments