Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Delicious;
- /**
- * This class provides the template rendering functions.
- *
- * @author Michael
- * @copyright (c) 2013, Michael Beers | Online Media en Design
- * @link http://michaelbeers.nl
- * @since 1.0
- */
- class View {
- private $_module = '';
- private $_controller = '';
- private $_action = '';
- private $_theme = null; // The theme name.
- private $_layout = false; // The layout where the body should be wrapped in.
- private $_title = '';
- private $_titleSeparator = ' - ';
- private $_metadata = array();
- private $_scripts = array();
- private $_partials = array();
- private $_breadcrumbs = array();
- private $_cacheLifetime = 0;
- private $_data = array();
- private $_output = null;
- /**
- * Constructor.
- */
- public function __construct(array $config) {
- $this->_titleSeparator = $config['title_separator'];
- $this->_theme = $config['theme'];
- $this->_cacheLifetime = $config['cache_lifetime'];
- // Setup module, controller and action.
- $mc = explode("\\Controller\\", Dispatch::$_controller);
- $this->_module = $mc[0];
- $this->_controller = $mc[1];
- $this->_action = Dispatch::$_action;
- }
- /**
- * Magic get function for getting view data.
- *
- * @param string $name
- * @return mixed
- */
- public function __get($name) {
- return $this->_data[$name];
- }
- /**
- * Magic set function for setting view data.
- *
- * @param string $name
- * @param mixed $value
- */
- public function __set($name, $value) {
- $this->_data[$name] = $value;
- }
- /**
- * Magic function for rendering the view data.
- */
- public function __toString() {
- $view = strtolower($this->_controller) . '-' . strtolower($this->_action);
- return $this->render($view, array(), true);
- }
- // ----------------------------------------------------------------------
- /**
- * Render the HTML output.
- *
- * @param string $view
- * @param array $data
- * @param boolean $return
- */
- public function render($view, array $data = array(), $return = false) {
- // Merge all the given data to use it in all view files.
- is_array($data) || $data = (array) $data;
- $this->_data = array_merge($this->_data, $data);
- // Check if the data is cached or not.
- if ($this->_cacheLifetime > 0 && $this->_inCache($view)) {
- // The template is cached so it will loaded from the cache.
- $this->_output = $this->_loadCachedView($view);
- } else {
- // The template is not cached so it will be loaded by searching.
- if (empty($this->_title)) {
- // Guess the title whether its empty.
- $this->_title = $this->_guessTitle();
- }
- // Setup the default template variable.
- $template = array();
- $template['title'] = $this->_title;
- $template['breadcrumbs'] = $this->_breadcrumbs;
- $template['metadata'] = implode("\n\t\t", $this->_metadata) . "\n";
- $template['scripts'] = implode("\n\t\t", $this->_scripts) . "\n";
- $template['partials'] = array();
- $this->_data['template'] = &$template; // Make an reference for later updates.
- // Load all partial data.
- foreach ($this->_partials as $name => $partial) {
- // Fix data array.
- is_array($partial['data']) || $partial['data'] = (array) $partial['data'];
- if (isset($partial['view'])) {
- $template['partials'][$name] = $this->_loadView($partial['view'], $partial['data']);
- } else {
- $template['partials'][$name] = $partial['string'];
- }
- }
- // Load basic view.
- $this->_output = $this->_loadView($view, array());
- // Wrap the output into a template file.
- // Cache the view?
- if ($this->_cacheLifetime > 0) {
- $this->_setCache($view, $this->_output);
- }
- }
- // Return or print the output.
- if (!$return) {
- print $this->_output;
- return;
- }
- return $this->_output;
- }
- // ----------------------------------------------------------------------
- /**
- * Set the page title.
- *
- * @param string Each parameter will be a segment.
- */
- public function setTitle() {
- if ($segments = func_get_args()) {
- $this->_title = implode($this->_titleSeparator, $segments);
- } else {
- $this->_title = $this->_guessTitle();
- }
- }
- /**
- * Guess the title based on the module, controller and action.
- *
- * @return string
- */
- private function _guessTitle() {
- $title_parts = array();
- // Check whether the action isn't the default index.
- if ($this->_action != 'index') {
- $title_parts[] = $this->_action;
- }
- // Make sure the controller name does not exists as title part.
- if (!in_array($this->_controller, $title_parts)) {
- $title_parts[] = $this->_controller;
- }
- // Make sure the module name does not exists as title part.
- if (!in_array($this->_module, $title_parts)) {
- $title_parts[] = $this->_module;
- }
- $title = humanize(implode($this->_titleSeparator, $title_parts));
- return $title;
- }
- // ----------------------------------------------------------------------
- /**
- * Add new breadcrumb to the page.
- *
- * @param type $name
- * @param type $url
- */
- public function addBreadcrumb($name, $url = '') {
- $this->_breadcrumbs[] = array('name' => $name, 'url' => $url);
- }
- /**
- * Add new metadata to the page.
- *
- * @param string $name Name of the metadata.
- * @param string $content Content of the metadata.
- * @param string $type Metadata type (meta, link or charset).
- */
- public function addMeta($name, $content, $type = 'meta') {
- $name = h(strip_tags($name));
- $content = h(strip_tags($content));
- // Fix for keywords.
- if ($name === 'keywords' && !strpos($content, ', ')) {
- $content = preg_replace('/[\s]+/', ', ', trim($content));
- }
- // Fix for html5 charset
- if ($name === 'charset') {
- $type = 'charset';
- }
- switch ($type) {
- case 'meta':
- $this->_metadata[$name] = '<meta name="' . $name . '" content="' . $content .
- '" />';
- break;
- case 'link':
- $this->_metadata[$content] = '<link rel="' . $name . '" href="' . $content .
- '" />';
- break;
- case 'charset':
- $this->_metadata[$name] = '<meta charset="' . $content . '" />';
- break;
- }
- }
- /**
- * Add new script to the page.
- *
- * @param string $name Name of the script.
- * @param string $url Url of the script.
- */
- public function addScript($name, $url, $type = 'text/javascript') {
- $this->_scripts[$name] = '<script type="' . $type . '" src="' . $url . '"></script>';
- }
- // ----------------------------------------------------------------------
- /**
- * Return cache id.
- *
- * @param string $view
- * @return string
- */
- private function _getCacheId($view) {
- $parts = array(
- strtolower($this->_module),
- strtolower($this->_controller),
- strtolower($this->_action),
- $view
- );
- return md5(implode('-', $parts));
- }
- /**
- * Return whether the view is already in the cache or not.
- *
- * @param string $view
- * @return boolean
- */
- private function _inCache($view) {
- $id = $this->_getCacheId($view);
- $filename = PUBLIC_PATH . 'data' . DS . 'cache' . DS . $id . '.cache';
- if (is_file($filename)) {
- clearstatcache();
- if (filemtime($filename) > (time() - $this->_cacheLifetime)) {
- $isCached = true;
- }
- }
- return isset($isCached) ? true : false;
- }
- /**
- * Sets a file to the cache.
- *
- * @param string $view
- * @param mixed $content
- */
- private function _setCache($view, $content) {
- $id = $this->_getCacheId($view);
- $filename = PUBLIC_PATH . 'data' . DS . 'cache' . DS . $id . '.cache';
- $directory = PUBLIC_PATH . 'data' . DS . 'cache' . DS;
- // Make directory if not exists.
- @mkdir($directory, 0775);
- // Write the content to the cache.
- if ($fp = fopen($filename, 'wb')) {
- fwrite($fp, $content);
- fclose($fp);
- }
- }
- /**
- * Return a cached view.
- *
- * @param string $view
- * @return mixed
- */
- private function _loadCachedView($view) {
- $id = $this->_getCacheId($view);
- $filename = PUBLIC_PATH . 'data' . DS . 'cache' . DS . $id . '.cache';
- if ($fp = fopen($filename, 'rb')) {
- $output = fread($fp, filesize($filename));
- fclose($fp);
- }
- return isset($output) ? $output : false;
- }
- /**
- * Return a normal view.
- *
- * @param string $view
- * @param array $data
- * @param string $override_path
- * @return mixed
- */
- private function _loadView($view, array $data = array(), $override_path = false) {
- $path = PACKAGE_PATH . ucfirst($this->_module) . DS . 'View' . DS . $view . EXT;
- // Override the default path.
- if ($override_path) {
- $path = $override_path . $view . EXT;
- }
- // Check if the file exists.
- if (!file_exists($path)) {
- $output = 'Error loading view "' . $view . '" in "' . ucfirst($this->_module) . DS . 'View' . '".';
- log_message($output);
- return $output;
- }
- // Start rendering (gzip compression supported)
- if (ob_start("ob_gzhandler") === false) {
- ob_start();
- }
- extract($this->_data);
- require ($path);
- $output = ob_get_clean();
- // End rendering...
- return $output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment