예제 #1
0
파일: middleware.php 프로젝트: boltphp/core
 public final function matchRoute(\bolt\browser\route $route, \bolt\browser\request $req)
 {
     $collection = b::browser('route\\collection\\create', [$route]);
     $match = new UrlMatcher($collection, $req->getContext());
     // we're going to try and match our request
     // if not we fall back to error
     try {
         return $match->matchRequest($req);
     } catch (ResourceNotFoundException $e) {
         return false;
     }
 }
예제 #2
0
파일: assets.php 프로젝트: boltphp/core
 public function handle($req, $res)
 {
     $route = b::browser('route\\create', ['path' => $this->config->value('route', '/a/{path}'), 'require' => ['path' => '.*']]);
     // match a route
     if (($params = $this->matchRoute($route, $req)) === false) {
         return false;
     }
     // content
     $content = "";
     // paths
     $paths = $req->assets->getDirs();
     // explode out the path
     foreach (explode('&', $params['path']) as $path) {
         $info = pathinfo($path);
         // get our path
         $dir = $info['dirname'];
         $file = $info['basename'];
         $ext = $info['extension'];
         // find this template
         $find = new Finder();
         // loop through each path
         foreach ($paths as $path) {
             // find the files
             $files = $find->files()->in(b::path($path, $dir))->name($file);
             if (iterator_count($files)) {
                 $it = iterator_to_array($files);
                 $first = array_shift($it);
                 $real = $first->getRealPath();
                 // rel
                 $rel = str_replace($path, '', $real);
                 // process a file and append it's content
                 $content .= \bolt\browser\assets::instance()->processFile($real, ['rel' => $rel, 'url' => 'http://localhost/' . str_replace('{path}', '', $route->getPath()), 'filterOnly' => $req->query->get('filterOnly')]);
             }
         }
     }
     // figureo ut
     $res->headers->set('Content-Type', $this->_mapContentTypeFromExt($ext));
     // set our content
     $res->setContent($content);
     return $res;
 }
예제 #3
0
파일: browser.php 프로젝트: boltphp/core
 public function run()
 {
     // loop through and add any engines not already
     foreach ($this->_engines as $ext => $ref) {
         b::render('setEngine', $ext, $ref);
     }
     // collect to backfill
     b::render('engine\\collect');
     // add routes from controller classes
     b::browser('route\\collection\\fromControllers', $this->_routes);
     // before
     $this->_runMiddleware('before', [$this->_request]);
     // no response
     $resp = false;
     // loop through our middleware
     // and see if anyone returns a response
     // if they do, we switch a controller
     foreach ($this->_middleware as $mid) {
         $_resp = $mid['instance']->handle($this->_request, $this->_response);
         if ($_resp and $_resp instanceof \bolt\browser\response) {
             $resp = $_resp;
             break;
         }
     }
     // if no response
     if (!$resp) {
         // match our route from the request
         $match = new browser\route\match($this->_routes, $this->_request->getContext());
         // we're going to try and match our request
         // if not we fall back to error
         try {
             $params = $match->matchRequest($this->_request);
         } catch (ResourceNotFoundException $e) {
             var_dump('bad');
             die;
         }
         // bind our params to request::attributes
         $this->_request->attributes->replace($params);
         // run middle before we run the controller
         $this->_runMiddleware('before', [$this->_request]);
         // we can get started
         $controller = new $params['_controller']($this->_request, $this->_response);
         // check if we can plugin to the controller
         if (property_exists($controller, 'isPlugable') and $controller->isPlugable) {
             $controller->inherit($this);
         }
         // build the controller
         $resp = $controller->run($this->_request->attributes->all());
     }
     if (!$resp) {
         $resp = $this->_response;
     }
     // run middle before we run the controller
     $this->_runMiddleware('handle', [$this->_request, $this->_response]);
     // prepare base on request
     $resp->prepare($this->_request);
     // figure out if content is callable
     if (is_callable($resp->getContent())) {
         while (is_callable($resp->getContent())) {
             $resp->setContent(call_user_func($resp->getContent()));
         }
     }
     // and done
     $resp->send();
 }