From e0e0ab0aeb7b1f1a4d9f74dacef5f32c729155e4 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 14 Oct 2008 19:26:10 +0000 Subject: [PATCH] Coded the PHP viewer, removed the Debug viewer to be replaced by a debug option that will trigger the debug mode on any viewer. Also added globals for the available viewers to help alleviate any naming inconsistencies. Also updated the pickle script to generate a more reduced index.php file. git-svn-id: http://svn.cleancode.org/svn/pickles@75 4d10bc64-7434-11dc-a737-d2d0f8310089 --- classes/Controller.php | 62 ++++++++++++++++++++++++++++------ classes/Viewer/Common.php | 37 ++++++++++++++++++++ classes/Viewer/Debug.php | 71 --------------------------------------- classes/Viewer/PHP.php | 66 +++++++++++++++++++++--------------- pickles.php | 6 ++++ scripts/pickle | 7 ++-- 6 files changed, 135 insertions(+), 114 deletions(-) delete mode 100644 classes/Viewer/Debug.php diff --git a/classes/Controller.php b/classes/Controller.php index 7cd074b..2f46717 100755 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -42,19 +42,45 @@ class Controller extends Object { * To make life a bit easier when using PICKLES, the Controller logic is * executed automatically via use of a constructor. * - * @param object Config object + * @param mixed Config object or filename (optional) */ - public function __construct(Config $config = null) { + public function __construct($config = null) { parent::__construct(); - - // If no Config object is passed, create a new one from assumptions - if ($config == null) { - $config = new Config(); - } - - // Creates all the other core objects we need to pass around. + + // Creates the core objects that don't need a Config object $logger = new Logger(); $error = new Error($logger); + + // Check the passed config variables object type + if (is_object($config)) { + if ($config instanceof Config === false) { + $error->setWarning('Passed object is not an instance of Config'); + $config = null; + } + } + + // Config filename to be loaded + $filename = null; + + // Checks if the config value is a filename + if (is_string($config)) { + if (file_exists($config)) { + $filename = $config; + } + else { + $error->setWarning('Passed config filename does not exist'); + $config = null; + } + } + + // If no Config object is passed (or it's cleared), create a new one from assumptions + if ($config == null) { + $config = new Config($filename); + } + + unset($filename); + + // Creates all the other core objects we need to pass around $db = new DB($config, $logger, $error); $mailer = new Mailer($config, $error); @@ -153,8 +179,14 @@ class Controller extends Object { } // Creates a new viewer object - $viewer_class = 'Viewer_' . $model->getViewer(); - $viewer = new $viewer_class($config, $error); + $viewer_name = $model->getViewer(); + if (in_array($viewer_name, array('JSON', 'PHP', 'RSS', 'Smarty'))) { + $viewer_class = 'Viewer_' . $viewer_name; + $viewer = new $viewer_class($config, $error); + } + else { + $error->setError('Invalid viewer specified (' . $viewer_name . ')'); + } // Sets the viewers properties $viewer->model_name = $model_name; @@ -164,6 +196,14 @@ class Controller extends Object { // Runs the requested viewer's display function $viewer->display(); + + // Do some cleanup + if (isset($security)) { + unset($security); + } + + unset($model, $viewer); + unset($db, $mailer, $config, $error, $logger) } } } diff --git a/classes/Viewer/Common.php b/classes/Viewer/Common.php index 4314e32..960a67b 100644 --- a/classes/Viewer/Common.php +++ b/classes/Viewer/Common.php @@ -60,6 +60,43 @@ abstract class Viewer_Common extends Object { ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset='); header('Content-type: text/html; charset=UTF-8'); + + if ($this->config->getDebug() === true) { + $superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV); + ?> +
+

PICKLES Debug Console

+ $array) { + ?> +

+ +

$_REQUEST

+
+

$_SESSION

+
+

$_SERVER

+
+
+ . - * - * @author Joshua John Sherman - * @copyright Copyright 2007, 2008 Joshua John Sherman - * @link http://phpwithpickles.org - * @license http://www.gnu.org/copyleft/lesser.html - * @package PICKLES - */ - -/** - * Debugging Viewer - * - * Displays debugging information on the screen. - */ -class Viewer_Debug extends Viewer_Common { - - /** - * Display the debugging information - * - * var_dump()s a few important super globals to aid in debugging. - * - * @todo May want to use contrib/dBug as it has better output. - * @todo Just a thought, perhaps instead of a debug viewer, the debug mode - * would be better served as an option in the model and then the - * regular viewer would be executed as well as displaying the debug - * data. - * @todo May also be cool to add in logic to check for the vi swap file, and - * if that's present, force the model into a debug state, as it will - * indicate it's being worked on. - * @todo Even cooler still, perhaps a debug console should be available when - * the model is marked debug or when a file is open. It would be - * activated with a hot key, would be legendary. - * @todo Perhaps it could be a configuration option to say that if a file is - * open, to now allow the end user to see the page, give them a generic - * "hey we're working on it" message. - */ - public function display() { - echo '

Debug

' . "\n"; - echo '

$_REQUEST

' . "\n"; - echo '
';
-		var_dump($_REQUEST);
-		echo '
'; - echo '

$_SESSION

' . "\n"; - echo '
';
-		var_dump($_SESSION);
-		echo '
'; - echo '

$_SERVER

' . "\n"; - echo '
';
-		var_dump($_SERVER);
-		echo '
'; - } -} - -?> diff --git a/classes/Viewer/PHP.php b/classes/Viewer/PHP.php index f1b915c..45f2c8c 100644 --- a/classes/Viewer/PHP.php +++ b/classes/Viewer/PHP.php @@ -27,59 +27,69 @@ /** * PHP Viewer * - * Displays the associated PHP templates for the Model. This is for all you - * folks that would prefer not to user the Smarty templating engine. Your - * PHP templates are just PHP code, plain and simple. + * Displays the associated PHP templates for the Model. This is + * very similar to the Smarty viewer, but less overhead since it's + * straight PHP. The PHP viewer also utilizes a different caching + * system than Smarty. The general rules around the caching will + * be the same though. */ class Viewer_PHP extends Viewer_Common { + private $template = null; + private $shared_template = null; + /** - * Displays the Smarty generated pages + * Displays the PHP templated pages */ public function display() { - $smarty->template_dir = '../templates/'; + // Establishes the template names + $this->template = SITE_PATH . '../templates/' . $this->model_name . '.php'; + $this->shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.php'; /** - * @todo Resurrect my buffer clean up code + * @todo There's a bug with the store home page since it's a redirect, maybe */ - $smarty->load_filter('output','trimwhitespace'); + if (!file_exists($this->template)) { + if (file_exists($this->shared_template)) { + $this->template = $this->shared_template; + } + } - // Pass all of our controller values to Smarty - $smarty->assign('section', $this->model->section); - $smarty->assign('model', $this->model->name); - $smarty->assign('template', $template); + // Brings these variables to scope + /** + * @todo Section or model needs to go, having both seems dumb. + */ + $section = $this->model->section; + $model = $this->model->name; + $template = $template; // Loads the data from the config - $data = $this->config->getViewerData(); - - if (isset($data) && is_array($data)) { - $smarty->assign('config', $data); - } + $config = $this->config->getPublicData(); // Loads the data from the model $data = $this->model->getData(); - if (isset($data) && is_array($data)) { - foreach ($data as $variable => $value) { - $smarty->assign($variable, $value); - } + // If there's data set, this brings it into scope + if (isset($this->data) && is_array($this->data)) { + extract($this->data); } - // Load it up! - header('Content-type: text/html; charset=UTF-8'); - - // If the index.tpl file is present, load it, else load the template directly + // If the index.php file is present, load it, else load the template directly /** * @todo Should there be additional logic to allow the model or the * template to determine whether or not the index should be loaded? */ - if ($smarty->template_exists('index.tpl')) { - $smarty->display('index.tpl'); + if (file_exists(SITE_PATH . '../templates/index.php')) { + require_once SITE_PATH . '../templates/index.php'; } - else { - $smarty->display($template); + else if (file_exists{$this->template)) { + require_once $this->template; } + + /** + * @todo Resurrect my buffer clean up code + */ } } diff --git a/pickles.php b/pickles.php index 74dd10e..b96591d 100755 --- a/pickles.php +++ b/pickles.php @@ -45,6 +45,12 @@ define('VAR_PATH', PICKLES_PATH . 'var/' . $_SERVER['SERVER_NAME'] . '/'); define('LOG_PATH', VAR_PATH . 'logs/'); define('SMARTY_PATH', VAR_PATH . 'smarty/'); +// Sets up constants for the Viewer names +define('VIEWER_JSON', 'JSON'); +define('VIEWER_PHP', 'PHP'); +define('VIEWER_RSS', 'RSS'); +define('VIEWER_SMARTY', 'Smarty'); + /** * Magic function to automatically load classes * diff --git a/scripts/pickle b/scripts/pickle index 80c999e..1ac210e 100755 --- a/scripts/pickle +++ b/scripts/pickle @@ -81,7 +81,7 @@ XML; // public/.htaccess $htaccess = << PHP; @@ -145,7 +144,7 @@ class home extends Model { // \$this->db->getRow('SELECT ...'); // \$this->db->getArray('SELECT ...'); - \$this->data['message'] = "You have successfully set up a site with PICKLES!"; + \$this->message = "You have successfully set up a site with PICKLES!"; } }