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
This commit is contained in:
Josh Sherman 2008-10-14 13:27:26 +00:00
parent 3fc3f74e75
commit d2d6c47682
12 changed files with 166 additions and 48 deletions

View file

@ -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);
}

View file

@ -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

View file

@ -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');

View file

@ -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;
}
/**

115
classes/Image.php Executable file
View file

@ -0,0 +1,115 @@
<?php
/**
* Image Utilities Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @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);
*/
}
?>

View file

@ -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); }

View file

@ -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;
}

View file

@ -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;
}
/**

View file

@ -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

View file

@ -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
*

View file

@ -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

View file

@ -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');