From d2d6c47682a509196842e644eeb6ae195c27a3b7 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 14 Oct 2008 13:27:26 +0000 Subject: [PATCH] Changed the core Object to not require any params in the constructor, and now we rely on polymorphism in the classes themselves to override that. git-svn-id: http://svn.cleancode.org/svn/pickles@74 4d10bc64-7434-11dc-a737-d2d0f8310089 --- classes/Config.php | 6 +- classes/Controller.php | 14 +++-- classes/DB.php | 9 ++- classes/Error.php | 4 +- classes/Image.php | 115 ++++++++++++++++++++++++++++++++++++++ classes/Logger.php | 4 +- classes/Mailer.php | 2 +- classes/Model.php | 7 +-- classes/Object.php | 31 +--------- classes/Security.php | 9 +++ classes/Viewer/Common.php | 5 +- classes/Viewer/Smarty.php | 8 +++ 12 files changed, 166 insertions(+), 48 deletions(-) create mode 100755 classes/Image.php diff --git a/classes/Config.php b/classes/Config.php index e7c3122..77be6f5 100755 --- a/classes/Config.php +++ b/classes/Config.php @@ -41,9 +41,13 @@ class Config extends Object { /** * Constructor + * + * Calls the parent constructor and loads the pass file + * + * @param string $file Filename of the config file (optional) */ public function __construct($file = '../config.xml') { - parent::__construct($this); + parent::__construct(); $this->load($file); } diff --git a/classes/Controller.php b/classes/Controller.php index 74fd2c4..7cd074b 100755 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -44,12 +44,18 @@ class Controller extends Object { * * @param object Config object */ - public function __construct(Config $config) { - parent::__construct($config); + public function __construct(Config $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. $logger = new Logger(); - $error = new Error($config, $logger); - $db = new DB($config, $error); + $error = new Error($logger); + $db = new DB($config, $logger, $error); $mailer = new Mailer($config, $error); // Generate a generic "site down" message diff --git a/classes/DB.php b/classes/DB.php index 32412d0..298ebfc 100755 --- a/classes/DB.php +++ b/classes/DB.php @@ -50,6 +50,7 @@ class DB extends Object { private $database; private $error; + protected $logger; /** * Private MySQL resources @@ -57,10 +58,11 @@ class DB extends Object { private $connection; private $results; - public function __construct(Config $config, Error $error = null) { - parent::__construct($config); + public function __construct(Config $config, Logger $logger, Error $error) { + parent::__construct(); - $this->error = isset($error) ? $error : new Error($config); + $this->logger = $logger; + $this->error = $error; $this->hostname = $config->database->hostname; $this->username = $config->database->username; @@ -139,6 +141,7 @@ class DB extends Object { $this->open(); if (trim($sql) != '') { + $this->logger->write('sql', trim(str_replace(array("\t", "\n"), array('', ' '), $sql))); $this->results = mysql_query($sql, $this->connection); if (empty($this->results)) { $this->error->addError('There was an error executing the SQL'); diff --git a/classes/Error.php b/classes/Error.php index f55ceff..f106179 100755 --- a/classes/Error.php +++ b/classes/Error.php @@ -46,8 +46,8 @@ class Error extends Object { /** * Constructor */ - public function __construct(Config $config, Logger $logger = null) { - $this->logger = isset($logger) ? $logger : new Logger(); + public function __construct(Logger $logger) { + $this->logger = $logger; } /** diff --git a/classes/Image.php b/classes/Image.php new file mode 100755 index 0000000..5314c39 --- /dev/null +++ b/classes/Image.php @@ -0,0 +1,115 @@ +. + * + * @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 + */ + +/** + * Incomplete collection of image manipulation utilities + * + * I'm not entirely sure if this class is being utilized anywhere, especially + * since a lot of the functions are not finished, or var_dump() stuff. The + * functions are (or eventually will be) wrappers for ImageMagick commands. The + * other route would be to code it all in pure PHP as to not have to add another + * dependency to PICKLES. After checking the PHP documentation on image + * processing, it seems there's an ImageMagick module that can utilized, that may + * end up being the route that is taken. Until then, this class is useless shit. + * + * @todo Make the class usable. + */ +class Image extends Object { + + public function __construct() { + parent::__construct(); + } + + /** + * Checks an image format + * + * This is basically just a wrapper for in_array(). The passed image type is + * checked against the passed array of types (else it will use the default + * list). + * + * @param string $type The type of being being checked + * @param array $types An array of valid image types, defaults to the common + * web formats of jpg, gif and png + * @return boolean If the passed type is in the list of valid types + */ + public function check($type, $types = array('image/jpeg', 'image/gif', 'image/png')) { + if (!is_array($types)) { + $types[0] = $types; + } + + return in_array($type, $types); + } + + /** + * Moves an uploaded file + * + * Handles not only moving an uploaded file to another location, but removes + * the original file once it's been moved. It's ashame that the function + * move_uploaded_file() just copies a file, and doesn't actually move it. + * + * @param string $origin The uploaded file that is to be moved + * @param string $destination The destination for the origin file + * @todo This function needs to return the status of the move + * @todo Currently if the move fails, the origin file will still be removed. + */ + public function move($origin, $destination) { + move_uploaded_file($origin, $destination); + imagedestroy($origin); + } + + public function resize() { + + } + + public function convert($original, $destination, $keep_original = true) { + var_dump('convert ' . $original . ' ' . $destination); + + var_dump( exec('convert ' . $original . ' ' . $destination) ); + } + + /* + + + if ($_FILES['image']['type'] == 'image/jpeg') { + $original = $directory . 'original.jpg'; + + $source = imagecreatefromjpeg($original); + list($width, $height) = getimagesize($original); + + $sizes = array('small' => 75, 'medium' => 150, 'large' => 500); + foreach ($sizes as $name => $size) { + $temp = imagecreatetruecolor($size, $size); + imagecopyresampled($temp, $source, 0, 0, 0, 0, $size, $size, $width, $height); + imagejpeg($temp, "{$directory}{$name}.jpg", 85); + + imagedestroy($temp); + } + + imagedestroy($source); + */ +} + +?> diff --git a/classes/Logger.php b/classes/Logger.php index 796857b..8fa8cc8 100644 --- a/classes/Logger.php +++ b/classes/Logger.php @@ -29,7 +29,9 @@ */ class Logger extends Object { - public function __construct() { } + public function __construct() { + parent::__construct(); + } public function write($type, $message) { if (!file_exists(LOG_PATH)) { mkdir(LOG_PATH, 0777, true); } diff --git a/classes/Mailer.php b/classes/Mailer.php index 879fc7c..4b628ff 100644 --- a/classes/Mailer.php +++ b/classes/Mailer.php @@ -37,7 +37,7 @@ class Mailer extends Object { public function __construct(Config $config, Error $error) { - parent::__construct($config); + parent::__construct(); $this->config = $config; $this->error = $error; } diff --git a/classes/Model.php b/classes/Model.php index 20b8936..b501b2f 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -69,13 +69,12 @@ class Model extends Object { * Handles calling the parent constructor and sets up the model's * internal config and database object */ - public function __construct(Config $config, DB $db, Mailer $mailer = null) { - parent::__construct($config); + public function __construct(Config $config, DB $db, Mailer $mailer) { + parent::__construct(); $this->config = $config; $this->db = $db; - - $this->mailer = isset($mailer) ? $mailer : new Mailer($config); + $this->mailer = $mailer; } /** diff --git a/classes/Object.php b/classes/Object.php index a2f52b6..9a39623 100644 --- a/classes/Object.php +++ b/classes/Object.php @@ -35,39 +35,10 @@ */ class Object { - /** - * Protected instance of the Config class - */ - protected $config = null; - - /** - * Protected instance of the DB class - */ - protected $db = null; - - protected $logger = null; - /** * Constructor - * - * Handles getting an instance of the Config class. - * - * @param object Config object */ - public function __construct(Config $config, DB $db = null) { - if (isset($config)) { - $this->config = $config; - } - - if (isset($db)) { - $this->db = $db; - } - - $parents = class_parents($this); - - $logger = new Logger(); - $logger->write('object', get_class($this) . (is_array($parents) ? ' -> ' . implode(' -> ', $parents) : '')); - } + public function __construct() { } /** * Destructor diff --git a/classes/Security.php b/classes/Security.php index fb0948d..e6a85cc 100644 --- a/classes/Security.php +++ b/classes/Security.php @@ -37,6 +37,15 @@ */ class Security extends Object { + private $config; + private $db; + + public function __construct(Config $config, DB $db) { + parent::__construct(); + $this->config = $config; + $this->db = $db; + } + /** * Authenticates the user * diff --git a/classes/Viewer/Common.php b/classes/Viewer/Common.php index f6035b3..4314e32 100644 --- a/classes/Viewer/Common.php +++ b/classes/Viewer/Common.php @@ -39,9 +39,10 @@ abstract class Viewer_Common extends Object { * @param object $model Object for the model we're loading */ public function __construct(Config $config, Error $error) { - parent::__construct($config); + parent::__construct(); - $this->error = $error; + $this->config = $config; + $this->error = $error; /** * @todo This may need to be flipped on only for Smarty and PHP templates diff --git a/classes/Viewer/Smarty.php b/classes/Viewer/Smarty.php index 0746bfa..45802b1 100644 --- a/classes/Viewer/Smarty.php +++ b/classes/Viewer/Smarty.php @@ -52,6 +52,14 @@ class Viewer_Smarty extends Viewer_Common { $smarty->cache_dir = $cache_dir ; $smarty->compile_dir = $compile_dir; + /** + * @todo move this to the config + */ + // Enables caching + //$smarty->caching = 1; + //$smarty->compile_check = true; + //$smarty->cache_lifetime = 3600; + // Loads the trim whitespace filter $smarty->load_filter('output','trimwhitespace');