Shit ton of updates.

git-svn-id: http://svn.cleancode.org/svn/pickles@81 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-10-20 01:03:00 +00:00
parent 5238e3f80d
commit 42e316fc05
9 changed files with 281 additions and 148 deletions

View file

@ -103,6 +103,27 @@ class Config extends Object {
return false;
}
/**
* Gets the caching value
*
* @return mixed The module's cache life span setting or true or false
*/
public function getCaching() {
if (isset($this->modules->caching)) {
if ($this->modules->caching == 'true') {
return true;
}
else if ($this->modules->caching == 'false') {
return false;
}
else {
return (int)$this->modules->caching;
}
}
return false;
}
/**
* Gets the authentication value
*

View file

@ -132,7 +132,7 @@ class Controller extends Object {
require_once $module_file;
if (class_exists($class)) {
$module = new $class($config, $db, $mailer);
$module = new $class($config, $db, $mailer, $error);
}
}
// Tries to load the shared module
@ -145,12 +145,12 @@ class Controller extends Object {
}
if (class_exists($class)) {
$module = new $class($config, $db, $mailer);
$module = new $class($config, $db, $mailer, $error);
}
}
// Loads the stock module
else {
$module = new Module($config, $db, $mailer);
$module = new Module($config, $db, $mailer, $error);
}
// Checks if we loaded a module file and no class was present
@ -171,10 +171,36 @@ class Controller extends Object {
$security->authenticate();
}
// Creates a new viewer object
$display_type = $module->getDisplay();
$display_class = 'Display_' . $display_type;
$display = new $display_class($config, $error);
// Sets the display's properties
$display->module_name = $module_name;
$display->shared_name = $shared_module_name;
$display->section = $section;
// Potentially establishes caching
$caching = $module->getCaching();
if ($caching) {
$display->caching = $caching;
if ($display_type == DISPLAY_SMARTY) {
$module->setSmartyObject($display->getSmartyObject());
}
}
$display->prepare();
// Potentially executes the module's logic
if (method_exists($module, '__default')) {
$module->__default();
if ($module->getCacheID()) {
$display->cache_id = $module->getCacheID();
}
if (isset($mailer->message)) {
$status = $mailer->send();
$module->type = $status['type'];
@ -182,21 +208,11 @@ class Controller extends Object {
}
}
// Creates a new viewer object
$display_name = $module->getDisplay();
if (in_array($display_name, array('JSON', 'PHP', 'RSS', 'Smarty'))) {
$display_class = 'Display_' . $display_name;
$display = new $display_class($config, $error);
}
else {
$error->addError('Invalid display specified (' . $viewer_name . ')');
}
// Sets the displays properties
$display->module_name = $module_name;
$display->shared_name = $shared_module_name;
$display->section = $section;
$display->data = $module->getData();
// Loads the module data into the display to be rendered
/**
* @todo perhaps make this a passed variable
*/
$display->data = $module->getData();
// Runs the requested rendering function
$display->render();

View file

@ -149,8 +149,8 @@ class DB extends Object {
}
else {
// Grabs the table name
$explain = mysql_query('EXPLAIN ' . $sql, $this->connection);
$results = mysql_fetch_array($explain, MYSQL_ASSOC);
//$explain = mysql_query('EXPLAIN ' . $sql, $this->connection);
//$results = mysql_fetch_array($explain, MYSQL_ASSOC);
// Grabs the model's name that made the call
$backtrace = debug_backtrace();
@ -359,7 +359,7 @@ class DB extends Object {
if (is_array($conditions)) {
foreach ($conditions as $key => $value) {
$where = ($where == null) ? 'WHERE ' : ' AND ';
$where = ($where == null) ? ' WHERE ' : ' AND ';
if ($value == null) {
$where .= $key . ' IS NULL';

View file

@ -60,25 +60,30 @@ abstract class Display_Common extends Object {
header('Content-type: text/html; charset=UTF-8');
if ($this->config->getDebug() === true) {
$superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV);
?>
<div class="debug" style="border: 2px solid black; padding: 5px; margin: 10px;">
<h1>PICKLES Debug Console</h1>
<?php
/*
foreach ($superglobals as $superglobal => $array) {
?>
<h2><h2>
<?php
<style>
div.debug {
border: 2px solid #000;
padding: 5px;
margin: 10px;
background-color: #FFF;
color: #000;
}
</style>
<div class="debug">
<h1>PICKLES Debug Console</h1><br />
<?php
foreach ($GLOBALS as $name => $array) {
if (count($array) > 0 && $name != 'GLOBALS') {
?>
<h2>$<?=$name;?></h2>
<?php
var_dump($array);
echo '<br />';
}
}
*/
?>
<h2>$_REQUEST</h2>
<pre><?php var_dump($_REQUEST); ?></pre>
<h2>$_SESSION</h2>
<pre><?php var_dump($_SESSION); ?></pre>
<h2>$_SERVER</h2>
<pre><?php var_dump($_SERVER); ?></pre>
</div>
<?php
@ -100,7 +105,9 @@ abstract class Display_Common extends Object {
/**
* Abstract rendering function that is overloaded within the loaded viewer
*/
abstract public function render();
public abstract function render();
public function prepare() { }
}
?>

View file

@ -35,18 +35,35 @@
*/
class Display_PHP extends Display_Common {
private $template_path = null;
private $template = null;
private $shared_template = null;
public function __construct(Config $config, Error $error) {
parent::__construct($config, $error);
// Establishes the template path
$this->template_path = SITE_PATH . '../templates/';
}
public function prepare() {
// Enables caching
if ($this->caching == true) {
if (is_numeric($this->caching)) {
//$this->smarty->cache_lifetime = $this->caching;
}
}
$this->template = $this->template_path . $this->module_name . '.php';
$this->shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.php';
}
/**
* Renders the PHP templated pages
*/
public function render() {
// Establishes the template names
$this->template = SITE_PATH . '../templates/' . $this->module_name . '.php';
$this->shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.php';
//if (filemtime($this->template)) {
// readfile('/var/www/josh/pickles/var/joshtronic.localhost/smarty/cache/home.html');
//}
@ -84,8 +101,8 @@ class Display_PHP extends Display_Common {
* @todo Should there be additional logic to allow the module or the
* template to determine whether or not the index should be loaded?
*/
if (file_exists(SITE_PATH . '../templates/index.php')) {
require_once SITE_PATH . '../templates/index.php';
if (file_exists($this->template_path . 'index.php')) {
require_once $this->template_path . 'index.php';
}
else if (file_exists($this->template)) {
require_once $this->template;

View file

@ -33,15 +33,15 @@
*/
class Display_Smarty extends Display_Common {
/**
* Render the Smarty generated pages
*/
public function render() {
private $smarty = null;
$smarty = new Smarty();
public function __construct(Config $config, Error $error) {
parent::__construct($config, $error);
$this->smarty = new Smarty();
// Establishes our paths
$smarty->template_dir = SITE_PATH . '../templates/';
$this->smarty->template_dir = SITE_PATH . '../templates/';
$cache_dir = SMARTY_PATH . 'cache';
$compile_dir = SMARTY_PATH . 'compile';
@ -49,21 +49,24 @@ class Display_Smarty extends Display_Common {
if (!file_exists($cache_dir)) { mkdir($cache_dir, 0777, true); }
if (!file_exists($compile_dir)) { mkdir($compile_dir, 0777, true); }
$smarty->cache_dir = $cache_dir ;
$smarty->compile_dir = $compile_dir;
$this->smarty->cache_dir = $cache_dir ;
$this->smarty->compile_dir = $compile_dir;
}
/**
* @todo move this to the config
*/
// Enables caching
$smarty->caching = 1;
$smarty->compile_check = true;
$smarty->cache_lifetime = 3600;
public function prepare() {
var_dump($smarty->is_cached('index.tpl', $this->model_name));
// Enables caching
if ($this->caching == true) {
$this->smarty->caching = 1;
$this->smarty->compile_check = true;
if (is_numeric($this->caching)) {
$this->smarty->cache_lifetime = $this->caching;
}
}
// Loads the trim whitespace filter
$smarty->load_filter('output','trimwhitespace');
$this->smarty->load_filter('output', 'trimwhitespace');
// Includes the PICKLES custom Smarty functions
$directory = PICKLES_PATH . 'functions/smarty/';
@ -74,7 +77,7 @@ class Display_Smarty extends Display_Common {
if (!preg_match('/^\./', $file)) {
list($type, $name, $ext) = split('\.', $file);
require_once $directory . $file;
$smarty->register_function($name, "smarty_{$type}_{$name}");
$this->smarty->register_function($name, "smarty_{$type}_{$name}");
}
}
closedir($handle);
@ -82,7 +85,7 @@ class Display_Smarty extends Display_Common {
}
// Establishes the template names
$template = SITE_PATH . '../templates/' . $this->model_name . '.tpl';
$template = SITE_PATH . '../templates/' . $this->module_name . '.tpl';
$shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.tpl';
/**
@ -94,52 +97,45 @@ class Display_Smarty extends Display_Common {
}
}
// Pass all of our controller values to Smarty
$smarty->assign('section', $this->section);
$smarty->assign('model', $this->model_name);
$smarty->assign('template', $template);
$this->template = $template;
}
// Loads the data from the config
$data = $this->config->getPublicData();
/**
* Render the Smarty generated pages
*/
public function render() {
$cache_id = isset($this->cache_id) ? $this->cache_id : $this->module_name;
$template = $this->smarty->template_exists('index.tpl') ? 'index.tpl' : $this->template;
if (isset($data) && is_array($data)) {
$smarty->assign('config', $data);
}
if (!$this->smarty->is_cached($template, $cache_id)) {
// Loads the model's data
if (isset($this->data) && is_array($this->data)) {
foreach ($this->data as $variable => $value) {
$smarty->assign($variable, $value);
// Pass all of our controller values to Smarty
$this->smarty->assign('section', $this->section);
$this->smarty->assign('module', $this->module_name);
$this->smarty->assign('template', $this->template);
// Loads the data from the config
$data = $this->config->getPublicData();
if (isset($data) && is_array($data)) {
$this->smarty->assign('config', $data);
}
// Loads the module's data
if (isset($this->data) && is_array($this->data)) {
foreach ($this->data as $variable => $value) {
$this->smarty->assign($variable, $value);
}
}
}
/**
* @todo There's no error checking for the index... should it be shared,
* and should the error checking occur anyway since any shit could
* happen?
*/
/*
$template = '../templates/index.tpl';
$shared_template = str_replace('../', '../../pickles/', $template);
$this->smarty->display($template, $cache_id);
}
if (!file_exists($template)) {
if (file_exists($shared_template)) {
$template = $shared_template;
}
}
*/
// If the index.tpl 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', $this->model_name);
}
else {
$smarty->display($template);
}
public function getSmartyObject() {
return $this->smarty;
}
}

View file

@ -55,7 +55,7 @@ class Mailer extends Object {
$defaults = $this->config->contact;
// Pulls the recipients from the config
if (!isset($this->recipients)) {
if (!isset($this->recipients) && isset($defaules->recipients->recipient)) {
$this->recipients = $defaults->recipients->recipient;
}
@ -102,6 +102,8 @@ class Mailer extends Object {
$message = 'An unexpected error has occurred';
}
Logger::write('mailer', '[' . $type . ']');
// Builds the status array to be returned
$return = array(
'type' => $type,

View file

@ -35,54 +35,60 @@
class Module extends Object {
/**
* Data array used by the viewer
* Data array used by the display
*/
protected $data = array();
/**
* Config object
* Passed objects
*/
protected $config = null;
/**
* Database object
*/
protected $db = null;
protected $db = null;
protected $mailer = null;
protected $error = null;
/**
* Name of the module
*/
protected $name = null;
/**
* Mailer object
* Module defaults
*/
protected $mailer = null;
protected $authentication = false;
protected $viewer = DISPLAY_PHP;
protected $caching = false;
protected $display = false;
protected $session = false;
private $smarty;
private $cache_id;
/**
* Constructor
*
* Handles calling the parent constructor and sets up the module's
* internal config and database object
*
* @param object $config Instance of the Config class
* @param object $db Instance of the DB class
* @param object $mailer Instance of the Mailer class
*/
public function __construct(Config $config, DB $db, Mailer $mailer) {
public function __construct(Config $config, DB $db, Mailer $mailer, Error $error) {
parent::__construct();
$this->config = $config;
$this->db = $db;
$this->mailer = $mailer;
$this->error = $error;
}
/**
* Gets the authenticate value
* Gets the authentication value
*
* Order of precedence: Module, Config, Guess (guess is always false)
* Order of precedence:
* Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not the module requires user authentication
* @return boolean Whether or not user authentication is required
*/
public function getAuthentication() {
if ($this->authentication != null) {
@ -94,11 +100,36 @@ class Module extends Object {
return false;
}
/**
* Gets the caching value
*
* Order of precedence:
* POSTed, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not user authentication is required
*/
public function getCaching() {
/*
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
return false;
}
*/
if ($this->caching != null) {
return $this->caching;
}
else if ($this->config->getCaching()) {
return $this->config->getCaching();
}
return false;
}
/**
* Gets the session value
*
* Order of precedence: Auth On, Module, Config, Guess (guess is always false)
* Order of precedence:
* Auth On, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not the session needs to be started
*/
@ -119,18 +150,22 @@ class Module extends Object {
/**
* Gets the requested Display
*
* Order of precedence: Module, Config, Guess (guess is always Smarty)
* Order of precedence:
* Module, Config, Guess (guess is always Smarty)
*
* @return string The viewer that the module has requested to be used
* @todo Guess shouldn't be Smarty, it should be the dummy PHP template.
* @todo Use the config override value to help determine.
* @return string The display that the module has requested to be used
*/
public function getDisplay() {
if ($this->display == null) {
return isset($argv) ? 'CLI' : 'Smarty';
if (in_array($this->display, array(DISPLAY_JSON, DISPLAY_PHP, DISPLAY_RSS, DISPLAY_SMARTY))) {
return $this->display;
}
else if (isset($this->config->modules->display)) {
return (string)$this->config->modules->display;
}
else {
return $this->display;
$this->error->addWarning('Invalid display specified, DISPLAY_PHP used by default (' . $this->display . ')');
return DISPLAY_PHP;
}
}
@ -147,17 +182,54 @@ class Module extends Object {
return null;
}
/**
* Sets the variable in the data array
*
* Overrides the built-in functionality to set an object's property with
* logic to place that data inside the data array for easier interaction
* later on.
*
* @param string $variable Name of the variable to be set
* @param mixed $value Data to be set
*/
public function __set($variable, $value) {
$this->data[$variable] = $value;
if ($variable != 'cache_id') {
$this->data[$variable] = $value;
}
}
/**
* Destructor
*
* Handles calling the parent's constructor, nothing else.
*/
public function __destruct() {
parent::__destruct();
public function setSmartyObject(Smarty $smarty) {
$this->smarty = $smarty;
}
public function isCached($id = null) {
if ($id == null) {
$id = get_class($this);
}
switch ($this->getDisplay()) {
case DISPLAY_PHP:
break;
case DISPLAY_SMARTY:
if ($this->smarty->template_exists('index.tpl')) {
return $this->smarty->is_cached('index.tpl', $id);
}
else {
return $this->smarty->is_cached($template, $id);
}
break;
}
return false;
}
public function setCacheID($id) {
$this->cache_id = $id;
}
public function getCacheID() {
return $this->cache_id;
}
/**
@ -170,7 +242,9 @@ class Module extends Object {
* checked without running code it's potentially not supposed to have
* been executed.
*/
public function __default() { }
public function __default() {
}
}
?>

View file

@ -45,17 +45,17 @@ 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');
// Sets up constants for the Display names
define('DISPLAY_JSON', 'JSON');
define('DISPLAY_PHP', 'PHP');
define('DISPLAY_RSS', 'RSS');
define('DISPLAY_SMARTY', 'Smarty');
/**
* Magic function to automatically load classes
*
* Determines if the system needs to load a PICKLES core class or
* a PICKLES shared model (not to be confused with site level models).
* a PICKLES shared module (not to be confused with site level modules).
*
* @param string $class Name of the class to be loaded
* @return boolean Return value of require_once() or false (default)
@ -63,16 +63,16 @@ define('VIEWER_SMARTY', 'Smarty');
function __autoload($class) {
$filename = str_replace('_', '/', $class) . '.php';
$class_file = PICKLES_PATH . 'classes/' . $filename;
$model_file = PICKLES_PATH . 'models/' . $filename;
$class_file = PICKLES_PATH . 'classes/' . $filename;
$module_file = PICKLES_PATH . 'modules/' . $filename;
// Loads the class file
if (file_exists($class_file)) {
return require_once $class_file;
}
// Loads the shared model
else if (file_exists($model_file)) {
return require_once $model_file;
// Loads the shared module
else if (file_exists($module_file)) {
return require_once $module_file;
}
// Loads Smarty
else if ($class == 'Smarty') {