123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- class Capsule {
-
-
- protected $templatePath;
-
-
- protected $outputDirectory;
-
-
- public $vars = array();
-
-
-
- protected $initialized = false;
-
-
- private $old_include_path;
-
- function __construct() {
- }
-
-
- function clear($which = null) {
- if ($which === null) {
- $this->vars = array();
- } elseif (is_array($which)) {
- foreach($which as $var) {
- unset($this->vars[$var]);
- }
- } else {
- unset($this->vars[$which]);
- }
- }
-
-
- function setTemplatePath($v) {
- $this->templatePath = rtrim($v, DIRECTORY_SEPARATOR.'/');
- }
-
- function getTemplatePath() {
- return $this->templatePath;
- }
-
-
- function setOutputDirectory($v) {
- $this->outputDirectory = rtrim($v, DIRECTORY_SEPARATOR.'/');
- }
-
- function getOutputDirectory() {
- return $this->outputDirectory;
- }
-
-
-
- function display($__template) {
-
-
-
-
-
- $generator = $this;
-
- if (isset($this->vars['this'])) {
- throw new Exception("Assigning a variable named \$this to a context conflicts with class namespace.");
- }
-
-
- extract($this->vars);
-
-
-
- $__old_inc_path = ini_get('include_path');
- ini_set('include_path', $this->templatePath . PATH_SEPARATOR . $__old_inc_path);
-
- @ini_set('track_errors', true);
- include $__template;
- @ini_restore('track_errors');
-
-
- ini_set('include_path', $__old_inc_path);
-
- if (!empty($php_errormsg)) {
- throw new Exception("Unable to parse template " . $__template . ": " . $php_errormsg);
- }
- }
-
-
- function parse($template, $outputFile = null, $append = false) {
-
-
-
- ob_start();
-
- try {
- $this->display($template);
- } catch (Exception $e) {
- ob_end_flush();
- throw $e;
- }
-
- $output = ob_get_contents();
- ob_end_clean();
-
- if ($outputFile !== null) {
- $outputFile = $this->resolvePath($outputFile, $this->outputDirectory);
-
- $flags = null;
- if ($append) $flags = FILE_APPEND;
-
- if (!file_put_contents($outputFile, $output, $flags) && $output != "") {
- throw new Exception("Unable to write output to " . $outputFile);
- }
- }
- return $output;
- }
-
-
- protected function resolvePath($file, $basepath) {
- if ( !($file{0} == DIRECTORY_SEPARATOR || $file{0} == '/')
-
- && !($file{1} == ':' && ($file{2} == DIRECTORY_SEPARATOR || $file{2} == '/'))) {
- if ($basepath != null) {
- $file = $basepath . DIRECTORY_SEPARATOR . $file;
- }
- }
- return $file;
- }
-
- function get($name) {
- if (!isset($this->vars[$name])) return null;
- return $this->vars[$name];
- }
-
-
- function putAll($vars, $recursiveMerge = false) {
- if ($recursiveMerge) {
- $this->vars = array_merge_recursive($this->vars, $vars);
- } else {
- $this->vars = array_merge($this->vars, $vars);
- }
- }
-
-
- function put($name, $value) {
- $this->vars[$name] = $value;
- }
-
-
- function putRef($name, &$value) {
- $this->vars[$name] = &$value;
- }
-
-
- function putCopy($name, $value) {
- if (is_object($value)) {
- $value = clone $value;
- }
- $this->vars[$name] = $value;
- }
- }
|