/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/pagefactory.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2013, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');

require_once "classes{$sep}site.php";
require_once "classes{$sep}theme.php";
require_once "classes{$sep}viewer.php";

/**
 * Creates Web pages
 *
 * @access public
 * @package Showkase
 */
Class PageFactory
{
    /**
     * @var object instance of ThemeSet
     */
    private $themeSet;
    
    /**
     * @var object instance of Theme
     */
    private $theme;
    
    /**
     * @var array of Viewer objects for cacheing
     */
    private $viewers = array();
    
    /**
     * Constructor
     *
     * @param object ThemeSet
     * @param object Theme (as at instantiation time!)
     */
    public function __construct(ThemeSet $themeSet, Theme $theme=null)
    {
        //$trace = debug_backtrace();
        //echo "called by {$trace[1]['class']} :: {$trace[1]['function']}";
        $this->themeSet = $themeSet;
        $this->theme = $theme;
        if (is_null($this->theme)) {
           $this->theme = new Theme(
              new Site(
                  $this->themeSet
              )
           );
        }
    }
   
    /**
     * Creates page object
     *
     * @access public
     * @param string page type (template type)
     * @param string page reference. This is the permanent reference not the index.
     * @param string page path, empty string indicates new page
     * @return object page object
     */
    public function make($pageType, $ref, $pagePath='')
    {
        $layer = $this->theme;
        $pluginDirectoryPath = PLUGINS_DIRECTORY.DIRECTORY_SEPARATOR.$pageType;
        foreach (glob($pluginDirectoryPath.DIRECTORY_SEPARATOR.'*.inc.php') as $path) {
            require_once $path;
            if ($pageType == 'library') continue;
            if (
                strpos($path, $pageType.'gallery.inc.php') !== false
            ) {
                if (isset($this->viewers[$pageType])) {
                    $layer = $this->viewers[$pageType];
                } else {
                    $layer = $this->viewers[$pageType] = new Viewer($this->theme, $pageType);
                }
            }
        }
        $class = ucfirst($pageType).'Page';
        if (!class_exists($class)) throw new Exception('Cannot find plugin for the <i>'.$class.'</i> page type');
        return new $class($layer, $ref, $pageType, $pagePath, $this->themeSet);
    }
}