@@ -12,6 +12,8 @@ final class AppBuilder {
1212 private static $ appContext ;
1313
1414 /**
15+ * The config of the AppBuilder.
16+ *
1517 * @var array
1618 */
1719 private static $ config = array ();
@@ -110,6 +112,37 @@ public static function use($callback) {
110112 self ::$ config ['customHandlers ' ] = $ callback ;
111113 }
112114
115+ /**
116+ * Adds a custom setting.
117+ *
118+ * @param string $key The unique key.
119+ * @param mixed $value The value.
120+ *
121+ * @return void
122+ */
123+ public static function set ($ key , $ value ) {
124+ if (!isset (self ::$ config ['settings ' ])) {
125+ self ::$ config ['settings ' ] = array ();
126+ }
127+
128+ self ::$ config ['settings ' ][$ key ] = $ value ;
129+ }
130+
131+ /**
132+ * Gets a custom setting.
133+ *
134+ * @param string $key The name of the parameter to get.
135+ *
136+ * @return mixed
137+ */
138+ public static function get ($ key ) {
139+ if (!isset (self ::$ config ['settings ' ]) || !isset (self ::$ config ['settings ' ][$ key ])) {
140+ return null ;
141+ }
142+
143+ return self ::$ config ['settings ' ][$ key ];
144+ }
145+
113146 /**
114147 * Registers routes.
115148 *
@@ -140,7 +173,7 @@ public static function build() {
140173 if ($ route !== false ) {
141174 $ actionContext = self ::actionContext ($ route );
142175
143- if (!self ::cached ($ actionContext )) {
176+ if ($ actionContext !== null && !self ::cached ($ actionContext )) {
144177 self ::filters ($ actionContext );
145178 self ::headers ();
146179 self ::validation ();
@@ -260,6 +293,7 @@ private static function init() {
260293 $ info ->cacheProvider = $ config ['cacheProvider ' ];
261294 $ info ->request = new HttpRequest ();
262295 $ info ->response = new HttpResponse ();
296+ $ info ->session = $ _SESSION ;
263297
264298 $ config ['httpContext ' ] = new HttpContext ($ info );
265299 }
@@ -277,6 +311,18 @@ private static function init() {
277311 }
278312 }
279313
314+ // default response handlers
315+ $ response = $ config ['httpContext ' ]->getResponse ();
316+
317+ $ response ->setEndHandler (function () use ($ config ) {
318+ self ::invokeAll (self ::$ appContext ->getEnd (), array (new ActionContext ($ config ['httpContext ' ])));
319+ });
320+
321+ $ response ->setPreSendHandler (function () use ($ config ) {
322+ self ::invokeAll (self ::$ appContext ->getPreSend (), array (new ActionContext ($ config ['httpContext ' ])));
323+ });
324+
325+ // set context and config, and invoke init handlers
280326 InternalHelper::setStaticPropertyValue ('\\PhpMvc \\HttpContext ' , 'current ' , $ config ['httpContext ' ]);
281327
282328 self ::$ appContext ->setConfig ($ config );
@@ -329,6 +375,7 @@ private static function canRoute() {
329375 */
330376 private static function actionContext ($ route ) {
331377 $ httpContext = self ::$ config ['httpContext ' ];
378+ $ response = $ httpContext ->getResponse ();
332379
333380 // TODO: kill constants
334381 if (!defined ('PHPMVC_CONTROLLER ' )) { define ('PHPMVC_CONTROLLER ' , ucfirst ($ route ->getValueOrDefault ('controller ' , 'Home ' ))); }
@@ -337,13 +384,21 @@ private static function actionContext($route) {
337384 if (!defined ('PHPMVC_CURRENT_CONTROLLER_PATH ' )) { define ('PHPMVC_CURRENT_CONTROLLER_PATH ' , PHPMVC_CONTROLLER_PATH . PHPMVC_VIEW . PHPMVC_DS ); }
338385 if (!defined ('PHPMVC_CURRENT_VIEW_PATH ' )) { define ('PHPMVC_CURRENT_VIEW_PATH ' , PHPMVC_VIEW_PATH . PHPMVC_VIEW . PHPMVC_DS . PHPMVC_ACTION . '.php ' ); }
339386
387+ $ controllerClassName = '\\' . PHPMVC_APP_NAMESPACE . '\\Controllers \\' . PHPMVC_CONTROLLER . 'Controller ' ;
388+
389+ if (!class_exists ($ controllerClassName )) {
390+ $ response ->setStatusCode (404 );
391+ $ response ->end ();
392+ return null ;
393+ }
394+
340395 // create action context
341396 $ actionContext = new ActionContext ($ httpContext );
342397 InternalHelper::setPropertyValue ($ actionContext , 'actionName ' , PHPMVC_ACTION );
343398
344- // preparing to create an instance of the controller class
345- $ controllerClass = new \ReflectionClass ('\\' . PHPMVC_APP_NAMESPACE . '\\ Controllers \\' . PHPMVC_CONTROLLER . ' Controller ' );
346-
399+ // preparing to create an instance of the controller class
400+ $ controllerClass = new \ReflectionClass ($ controllerClassName );
401+
347402 if (!$ controllerClass ->isSubclassOf ('\\PhpMvc \\Controller ' )) {
348403 throw new \Exception ('The controller type must be the base of " \\PhpMvc \\Controller". ' );
349404 }
@@ -375,17 +430,17 @@ private static function actionContext($route) {
375430 // InternalHelper::setPropertyValue($modelState, 'annotations', $annotations);
376431
377432 // response handling
378- $ httpContext -> getResponse () ->setFlushHandler (function ($ eventArgs ) use ($ actionContext ) {
433+ $ response ->setFlushHandler (function ($ eventArgs ) use ($ actionContext ) {
379434 self ::cachingPartial ($ actionContext , $ eventArgs );
380435 self ::invokeAll (self ::$ appContext ->getFlush (), array ($ actionContext , $ eventArgs ));
381436 });
382437
383- $ httpContext -> getResponse () ->setEndHandler (function () use ($ actionContext ) {
438+ $ response ->setEndHandler (function () use ($ actionContext ) {
384439 self ::caching ($ actionContext );
385440 self ::invokeAll (self ::$ appContext ->getEnd (), array ($ actionContext ));
386441 });
387442
388- $ httpContext -> getResponse () ->setPreSendHandler (function () use ($ actionContext ) {
443+ $ response ->setPreSendHandler (function () use ($ actionContext ) {
389444 self ::cachingClient ($ actionContext );
390445 self ::invokeAll (self ::$ appContext ->getPreSend (), array ($ actionContext ));
391446 });
0 commit comments