Example #1
0
<?php

// init: 要自動載入 Pix Framework
include __DIR__ . '/../../Pix/Loader.php';
set_include_path(__DIR__ . '/../../' . PATH_SEPARATOR . __DIR__ . '/models');
Pix_Loader::registerAutoload();
// 設定 Pix_Cache server 位置
Pix_Cache::addServer('Pix_Cache_Adapter_Memcache', array('servers' => array(array('host' => 'memcache-server-name', 'port' => 11211))));
$cache = new Pix_Cache();
$cache->save('test-key', 'value');
echo $cache->load('test-key');
Example #2
0
 public static function reset()
 {
     self::$_servers = array();
 }
Example #3
0
 /**
  * partial 印出 $file 的內容,並且將 $data 的參數轉成 $this
  *
  * @param string $file
  * @param array|Pix_Partial|null $data 傳進去的參數
  * @param array|string $options 設定 或 cache id
  * @access public
  * @return string
  */
 public function partial($file, $data = null, $options = null)
 {
     if ($data instanceof Pix_Partial) {
         $data = $data->_data;
     } else {
         $data = (array) $data;
     }
     if (array_key_exists('_data', $data)) {
         $data = array_shift($data);
     }
     if ($this->_path) {
         $path = $this->_path . ltrim($file, '/');
     } else {
         $path = $file;
     }
     if (!is_null($options) and is_scalar($options)) {
         // $options 是 scalar 的話, 就是 cache id
         $cache_id = $options;
     } elseif (is_array($options)) {
         // 是 array 的話, 就從 array 取出設定
         $cache_id = $options['cache_id'];
         $cache = $options['cache'];
     }
     if (!$cache instanceof Pix_Cache) {
         $cache = new Pix_Cache();
     }
     $cache_key = sprintf('Pix_Partial:%s:%s:%s:%s:%s:%s', $this->_cache_prefix, strtolower($_SERVER['HTTP_HOST']), sha1(file_get_contents($path)), $cache_id, self::$_trim_mode ? 1 : 0, self::$_minify_mode ? 1 : 0);
     if (!self::$_nocache and !self::$_write_only_mode and strlen($cache_id) > 0 and $html = $cache->load($cache_key)) {
         return $html;
     }
     // TODO: 這邊要改漂亮一點的寫法
     $old_data = $this->_data;
     $this->_data = $data;
     ob_start();
     try {
         if (!file_exists($path)) {
             throw new Exception("找不到 {$path} 這個路徑的 partial");
         }
         if (preg_match('#.tmpl$#', $path)) {
             $this->jQueryTmpl($path, $this);
         } else {
             require $path;
         }
         $str = ob_get_clean();
     } catch (Pix_Partial_NoRender $e) {
         ob_get_clean();
         $str = '';
     } catch (Pix_Partial_BreakRender $e) {
         $str = ob_get_clean();
     } catch (Exception $e) {
         ob_get_clean();
         throw $e;
     }
     if (self::getCommentMode() and $str) {
         $str = "<!-- Pix_Partial START {$file} -->\n{$str}\n<!-- Pix_Partial END {$file} -->\n";
     }
     $this->_data = $old_data;
     if (self::$_trim_mode) {
         $newstr = '';
         foreach (explode("\n", $str) as $line) {
             $line = trim($line);
             if (!empty($line)) {
                 $newstr .= $line . "\n";
             }
         }
         $str = trim($newstr);
     }
     if (self::$_minify_mode) {
         $str = Minify_HTML::minify($str, ['cssMinifier' => ['Minify_CSS', 'minify'], 'jsMinifier' => ['JSMin', 'minify']]);
     }
     if (!self::$_nocache and strlen($cache_id) > 0) {
         $cache->save($cache_key, $str);
     }
     return $str;
 }