From 0fcd84e1617bab1128fd648a82ad8776bec49cab Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Sat, 28 Aug 2010 03:30:33 +0200 Subject: [PATCH] added several improvents for our utilties fixed debugging & exception output --- backend/index.php | 14 ++++----- backend/lib/Dispatcher.php | 16 +++++------ backend/lib/Util/Debug.php | 23 +++++++-------- backend/lib/Util/Definition.php | 50 ++++++++++++++++++--------------- backend/lib/Util/JSON.php | 7 +++-- 5 files changed, 59 insertions(+), 51 deletions(-) diff --git a/backend/index.php b/backend/index.php index 6d728ac..b9cc606 100644 --- a/backend/index.php +++ b/backend/index.php @@ -31,17 +31,17 @@ use Volkszaehler\Controller; // TODO replace by state class define('VZ_VERSION', 0.2); -define('VZ_DIR', '/home/steffen/workspace/volkszaehler.org'); // TODO realpath(__DIR__) -define('BACKEND_DIR', VZ_DIR . '/backend'); +define('VZ_DIR', realpath(__DIR__ . '/..')); +define('VZ_BACKEND_DIR', VZ_DIR . '/backend'); define('DEV_ENV', TRUE); // class autoloading -require BACKEND_DIR . '/lib/Util/ClassLoader.php'; +require VZ_BACKEND_DIR . '/lib/Util/ClassLoader.php'; $classLoaders = array(); -$classLoaders[] = new Util\ClassLoader('Doctrine', BACKEND_DIR . '/lib/vendor/Doctrine'); -$classLoaders[] = new Util\ClassLoader('Symfony', BACKEND_DIR . '/lib/vendor/Symfony'); -$classLoaders[] = new Util\ClassLoader('Volkszaehler', BACKEND_DIR . '/lib'); +$classLoaders[] = new Util\ClassLoader('Doctrine', VZ_BACKEND_DIR . '/lib/vendor/Doctrine'); +$classLoaders[] = new Util\ClassLoader('Symfony', VZ_BACKEND_DIR . '/lib/vendor/Symfony'); +$classLoaders[] = new Util\ClassLoader('Volkszaehler', VZ_BACKEND_DIR . '/lib'); foreach ($classLoaders as $loader) { $loader->register(); // register on SPL autoload stack @@ -50,7 +50,7 @@ foreach ($classLoaders as $loader) { // enable strict error reporting error_reporting(E_ALL); -Util\Configuration::load(BACKEND_DIR . '/volkszaehler.conf'); +Util\Configuration::load(VZ_BACKEND_DIR . '/volkszaehler.conf'); $fc = new Dispatcher; // spawn frontcontroller / dispatcher $fc->run(); // execute controller and sends output diff --git a/backend/lib/Dispatcher.php b/backend/lib/Dispatcher.php index f9a1ac6..246cfe2 100644 --- a/backend/lib/Dispatcher.php +++ b/backend/lib/Dispatcher.php @@ -93,13 +93,12 @@ class Dispatcher { $this->em = Dispatcher::createEntityManager(); // starting debugging - if (($debug = $request->getParameter('debug')) !== FALSE || $debug = Util\Configuration::read('debug')) { + if (($debug = $request->getParameter('debug')) != NULL || $debug = Util\Configuration::read('debug')) { if ($debug > 0) { $this->debug = new Util\Debug($debug); $this->em->getConnection()->getConfiguration()->setSQLLogger($this->debug); } } - // TODO debug controll via configuration file // initialize view switch ($format) { @@ -154,7 +153,7 @@ class Dispatcher { $this->controller->run($action); // run controllers actions (usually CRUD: http://de.wikipedia.org/wiki/CRUD) if (Util\Debug::isActivated()) { - $this->view->addDebug($this->debug); + $this->addDebug($this->debug); } $this->view->sendResponse(); // render view & send http response @@ -164,22 +163,23 @@ class Dispatcher { * Factory for doctrines entitymanager * * @todo create extra singleton class? + * @todo add other caching drivers (memcache, xcache) */ public static function createEntityManager() { $config = new \Doctrine\ORM\Configuration; - if (extension_loaded('apc')) { + if (extension_loaded('apc') && Util\Configuration::read('devmode') == FALSE) { $cache = new \Doctrine\Common\Cache\ApcCache; $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); } - $driverImpl = $config->newDefaultAnnotationDriver(BACKEND_DIR . '/lib/Model'); + $driverImpl = $config->newDefaultAnnotationDriver(VZ_BACKEND_DIR . '/lib/Model'); $config->setMetadataDriverImpl($driverImpl); - $config->setProxyDir(BACKEND_DIR . '/lib/Model/Proxies'); - $config->setProxyNamespace('Volkszaehler\Model\Proxies'); - $config->setAutoGenerateProxyClasses(DEV_ENV == TRUE); + $config->setProxyDir(VZ_BACKEND_DIR . '/lib/Model/Proxy'); + $config->setProxyNamespace('Volkszaehler\Model\Proxy'); + $config->setAutoGenerateProxyClasses(Util\Configuration::read('devmode')); return \Doctrine\ORM\EntityManager::create(Util\Configuration::read('db'), $config); } diff --git a/backend/lib/Util/Debug.php b/backend/lib/Util/Debug.php index 108cea4..e086f0c 100644 --- a/backend/lib/Util/Debug.php +++ b/backend/lib/Util/Debug.php @@ -37,18 +37,14 @@ class Debug implements Logging\SQLLogger { protected $queries = array(); protected $messages = array(); - /** - * @var float holds timestamp of initialization, used later to return time of execution - */ + /** @var float holds timestamp of initialization, used later to return time of execution */ protected $started; - /** - * @var integer the debug level - */ + /** * @var integer the debug level */ protected $level; /** - * constructor + * Constructor * * @param integer $level the debug level */ @@ -68,12 +64,10 @@ class Debug implements Logging\SQLLogger { assert_options(ASSERT_BAIL, FALSE); assert_options(ASSERT_WARNING, FALSE); assert_options(ASSERT_CALLBACK, array($this, 'assertHandler')); - - } /** - * interface for doctrine's dbal sql logger + * interface for Doctrine's DBAL SQLLogger * * @param string $sql the sql query * @param array $parameters optional parameters for prepared queries @@ -134,7 +128,8 @@ class Debug implements Logging\SQLLogger { } /** - * is debugging enabled? + * Is debugging enabled? + * @return boolean */ public static function isActivated() { return isset(self::$instance); } @@ -152,6 +147,12 @@ class Debug implements Logging\SQLLogger { * @return 2 dimensional array with messages */ public function getMessages() { return $this->messages; } + + /** + * @return Debug the Debug instance if available + * @todo OOP? Should we remove this? Replace by State class? + */ + public static function getInstance() { return self::$instance; } } ?> diff --git a/backend/lib/Util/Definition.php b/backend/lib/Util/Definition.php index 515dd53..cb3e42f 100644 --- a/backend/lib/Util/Definition.php +++ b/backend/lib/Util/Definition.php @@ -27,21 +27,23 @@ namespace Volkszaehler\Util; * @author Steffen Vogel * @package util */ -abstract class JSONDefinition { - /** - * Cached json definitions - * - * @var array - */ +abstract class Definition { + /** @var array cached json definitions */ protected static $definitions = NULL; - /** Discriminator for database column */ + /** @var string discriminator for database column */ protected $name; + /** @var string title for UI */ + protected $title; + + /** @var string description for UI */ + protected $description; + /** * Hide default constructor * - * @param array $name + * @param array $object to cast from */ protected function __construct($object) { foreach (get_object_vars($object) as $name => $value) { @@ -49,7 +51,7 @@ abstract class JSONDefinition { $this->$name = $value; } else { - throw new \Exception('unknown definition: ' . $name); + throw new \Exception('unknown definition syntax: ' . $name); } } } @@ -59,31 +61,35 @@ abstract class JSONDefinition { * Factory method for creating new instances * * @param string $name - * @return Model\PropertyDefinition + * @return Util\Definition */ public static function get($name) { - if (is_null(self::$definitions)) { - self::load(); - } - - if (!isset(self::$definitions[$name])) { + if (!self::exists($name)) { throw new \Exception('unknown definition'); } return self::$definitions[$name]; } + /** + * Checks if $name is defined + * @param string $name + */ + public static function exists($name) { + if (is_null(self::$definitions)) { + self::load(); + } + + Debug::log('definitions', self::$definitions); + + return isset(self::$definitions[$name]); + } + /** * Load JSON definitions from file (via lazy loading from get()) */ protected static function load() { - $json = file_get_contents(VZ_DIR . static::FILE); - $json = JSON::strip($json); - $json = json_decode($json); // TODO move to Util\JSON class - - if (!is_array($json) || count($json) == 0) { - throw new \Exception('syntax error in definition'); - } + $json = JSON::decode(file_get_contents(VZ_DIR . static::FILE)); self::$definitions = array(); diff --git a/backend/lib/Util/JSON.php b/backend/lib/Util/JSON.php index 58b4050..59a5394 100644 --- a/backend/lib/Util/JSON.php +++ b/backend/lib/Util/JSON.php @@ -143,14 +143,15 @@ class JSON extends \ArrayObject { class JSONException extends \Exception { /** * @var array errorcodes defined by json_last_error() + * @url http://www.php.net/manual/en/json.constants.php */ protected static $errors = array( JSON_ERROR_NONE => 'No error has occurred', JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', - JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', + JSON_ERROR_CTRL_CHAR => 'Control character error', JSON_ERROR_SYNTAX => 'Syntax error', - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' + JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON' + // JSON_ERROR_UTF8 => 'Malformed UTF-8 characters' // INFO this constant exists since PHP 5.3.3 ); public function __construct($message = NULL, $code = 0) {