@@ -49,6 +49,13 @@ class WaterPipe
4949 */
5050 private $ _isRunning ;
5151
52+ /**
53+ * The base URI of the pipe.
54+ *
55+ * @var string
56+ */
57+ private $ _baseUri = "" ;
58+
5259 /**
5360 * The array of registered middleware.
5461 *
@@ -65,6 +72,8 @@ class WaterPipe
6572
6673 private $ _errorsRegistry ;
6774
75+ private $ _pipesRegistry ;
76+
6877 public function __construct ()
6978 {
7079 $ this ->_isRunning = false ;
@@ -75,6 +84,7 @@ public function __construct()
7584 $ this ->_deleteRequestRegistry = array ();
7685 $ this ->_requestRegistry = array ();
7786 $ this ->_errorsRegistry = array ();
87+ $ this ->_pipesRegistry = array ();
7888 }
7989
8090 public function use ($ plugin )
@@ -126,6 +136,11 @@ public function delete(string $uri, $action)
126136 $ this ->_deleteRequestRegistry [$ uri ] = $ action ;
127137 }
128138
139+ public function pipe (string $ baseUri , WaterPipe $ pipe )
140+ {
141+ $ this ->_pipesRegistry [$ baseUri ] = $ pipe ;
142+ }
143+
129144 /**
130145 * Run the water pipe.
131146 *
@@ -141,7 +156,30 @@ public function run()
141156
142157 Request::capture ();
143158
144- $ this ->_executeRequest ();
159+ $ pipe = $ this ->_findSubPipe ();
160+
161+ if ($ pipe === null ) {
162+ $ this ->_executeRequest ();
163+ } else {
164+ $ pipe [1 ]->_runBase ($ pipe [0 ]);
165+ }
166+ }
167+
168+ private function _runBase (string $ baseUri )
169+ {
170+ $ this ->_baseUri = $ baseUri ;
171+ $ this ->run ();
172+ }
173+
174+ private function _findSubPipe ()
175+ {
176+ foreach ($ this ->_pipesRegistry as $ baseUri => $ pipe ) {
177+ if (preg_match ("#^ " . RequestUri::pattern2regex ($ baseUri ) . "# " , Request::getInstance ()->uri ->getUri ())) {
178+ return array ($ baseUri , $ pipe );
179+ }
180+ }
181+
182+ return null ;
145183 }
146184
147185 private function _executeRequest ()
@@ -208,7 +246,7 @@ private function _getActionForRoutes(array $routes)
208246 $ runner = null ;
209247
210248 foreach ($ routes as $ pattern => $ action ) {
211- if (RequestUri::isMatch ($ pattern , Request::getInstance ()->uri ->getUri ())) {
249+ if (RequestUri::isMatch ($ pattern = " / " . trim ( $ this -> _baseUri . $ pattern , " / " ) , Request::getInstance ()->uri ->getUri ())) {
212250 Request::getInstance ()->uri ->setPattern ($ pattern )->build ();
213251 $ runner = $ action ;
214252 break ;
0 commit comments