Пример #1
0
 /**
 @param	template	string path to template file relative to the templateFolder.  .php is appended to this path.
 @param	vars	variables to extract and make available to the template file
 @return	output from a template
 
 @note
 	there are 2 ways to refer to an inner parentped template from the outer template
 	1.	use the section creation functions
 	2.	use $template->inner variable created for the parent
 */
 function get_template($template, $vars = [])
 {
     $this->parent_stack[] = '';
     $used_vars = array_merge($this->helpers, $vars);
     ob_start();
     if (substr($template, -4) != '.php') {
         $template = $template . '.php';
     }
     \Grithin\Files::req($this->options['folder'] . $template, $used_vars);
     $output = ob_get_clean();
     $parent = array_pop($this->parent_stack);
     if ($parent) {
         $this->inner = $output;
         $template = $parent;
         $output = $this->get_template($template, $vars);
         unset($this->inner);
         array_pop($this->parent_stack);
     }
     return $output;
 }
Пример #2
0
 function resolveRoutes()
 {
     $this->unparsedTokens = array_merge([''], $this->tokens);
     $this->globals['route'] = $this;
     $i = 0;
     while ($this->unparsedTokens && !$this->stopRouting) {
         $i++;
         if ($i > $this->max_loops) {
             Debug::out('_routing rules are looping');
             Debug::out($this);
             Debug::toss('Route appears to be looping infinitely');
         }
         $this->currentToken = array_shift($this->unparsedTokens);
         if ($this->currentToken) {
             $this->parsedTokens[] = $this->currentToken;
         }
         $path = $this->options['folder'] . implode('/', $this->parsedTokens);
         if (!isset($this->ruleSets[$path])) {
             $this->ruleSets[$path] = (array) Files::inc($path . '/_routing.php', $this->globals, ['extract' => ['rules']])['rules'];
         }
         if (!$this->ruleSets[$path] || $this->stopRouting) {
             continue;
         }
         //note, on match, matehRules resets unparsedTokens (having the effect of loopiing matchRules over again)
         $this->matchRules($path, $this->ruleSets[$path]);
     }
     $this->parsedTokens = [];
 }
Пример #3
0
 static function sendFile($path, $saveAs = null, $exit = true)
 {
     //Might potentially remove ".." from path, but it has already been removed by the time the request gets here by server or browser.  Still removing for precaution
     $path = \Grithin\Files::removeRelative($path);
     if (is_file($path)) {
         $mime = \Grithin\Files::mime($path);
         header('Content-Type: ' . $mime);
         if ($saveAs) {
             header('Content-Description: File Transfer');
             if (strlen($saveAs) > 1) {
                 $fileName = $saveAs;
             } else {
                 $fileName = array_pop(explode('/', $path));
             }
             header('Content-Disposition: attachment; filename="' . self::escapeFilename($fileName) . '"');
         }
         echo file_get_contents($path);
     } else {
         throw new \Exception('Request handler encountered unresolvable file.  Searched at ' . $path);
     }
     if ($exit) {
         exit;
     }
 }