1 | <?php |
||
12 | class BasePath |
||
13 | { |
||
14 | const KEY = 'BASE_PATH'; |
||
15 | const KEY_GENERATOR = 'BASE_PATH_GENERATOR'; |
||
16 | |||
17 | use Utils\BasePathTrait; |
||
18 | use Utils\AttributeTrait; |
||
19 | |||
20 | /** |
||
21 | * @var bool |
||
22 | */ |
||
23 | private $autodetect = false; |
||
24 | |||
25 | /** |
||
26 | * Returns the basePath. |
||
27 | * |
||
28 | * @param ServerRequestInterface $request |
||
29 | * |
||
30 | * @return string|null |
||
31 | */ |
||
32 | public static function getBasePath(ServerRequestInterface $request) |
||
33 | { |
||
34 | return self::getAttribute($request, self::KEY); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Returns a callable to build a full path. |
||
39 | * |
||
40 | * @param ServerRequestInterface $request |
||
41 | * |
||
42 | * @return callable|null |
||
43 | */ |
||
44 | public static function getGenerator(ServerRequestInterface $request) |
||
45 | { |
||
46 | return self::getAttribute($request, self::KEY_GENERATOR); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Constructor. Set the path prefix. |
||
51 | * |
||
52 | * @param string|null $basePath |
||
53 | */ |
||
54 | public function __construct($basePath = null) |
||
60 | |||
61 | /** |
||
62 | * Autodetect the basePath. |
||
63 | * |
||
64 | * @param bool $autodetect |
||
65 | * |
||
66 | * @return self |
||
67 | */ |
||
68 | public function autodetect($autodetect = true) |
||
74 | |||
75 | /** |
||
76 | * Execute the middleware. |
||
77 | * |
||
78 | * @param ServerRequestInterface $request |
||
79 | * @param ResponseInterface $response |
||
80 | * @param callable $next |
||
81 | * |
||
82 | * @return ResponseInterface |
||
83 | */ |
||
84 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
85 | { |
||
86 | if ($this->autodetect) { |
||
87 | $this->basePath(Utils\Helpers::joinPath(self::detectBasePath($request), $this->basePath)); |
||
88 | } |
||
89 | |||
90 | $uri = $request->getUri(); |
||
91 | $path = $this->getPath($uri->getPath()); |
||
92 | $request = $request->withUri($uri->withPath($path)); |
||
93 | |||
94 | $generator = function ($path) { |
||
95 | return Utils\Helpers::joinPath($this->basePath, $path); |
||
96 | }; |
||
97 | |||
98 | $request = self::setAttribute($request, self::KEY, $this->basePath); |
||
99 | $request = self::setAttribute($request, self::KEY_GENERATOR, $generator); |
||
100 | |||
101 | return $next($request, $response); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Auto-detect the base path from the request environment. |
||
106 | * |
||
107 | * Uses a variety of criteria in order to detect the base URL of the request |
||
108 | * (i.e., anything additional to the document root). |
||
109 | * |
||
110 | * This code has been adapted from the Zend implementation: |
||
111 | * https://github.com/zendframework/zend-http/blob/master/src/PhpEnvironment/Request.php |
||
112 | * |
||
113 | * @param ServerRequestInterface $request |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | private static function detectBasePath(ServerRequestInterface $request) |
||
190 | } |
||
191 |