From 62ff0c614d7a5c62dc21d2acc4fb6dbff96ad8f7 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 7 Jun 2010 00:34:46 +0200 Subject: [PATCH] moved ressources into new directory structure according to the MVC pattern improved class autoloading with preg_replace() --- init.php | 54 +++++++++++-------- lib/{channel => model}/channel.php | 0 .../meter => model/channel}/meter.php | 0 lib/{ => model}/channel/meter/powermeter.php | 0 .../sensor => model/channel}/sensor.php | 0 .../channel/sensor/onewiresensor.php | 0 lib/{db => model}/database.php | 0 lib/{ => model}/databaseobject.php | 0 lib/{ => model}/db/mysql.php | 0 lib/{ => model}/db/pgsql.php | 0 lib/{ => model}/db/sqlite.php | 0 lib/{ => model}/group.php | 0 lib/{ => model}/user.php | 0 lib/{ => view}/http/httphandle.php | 0 lib/{ => view}/http/httprequest.php | 0 lib/{ => view}/http/httpresponse.php | 0 16 files changed, 33 insertions(+), 21 deletions(-) rename lib/{channel => model}/channel.php (100%) rename lib/{channel/meter => model/channel}/meter.php (100%) rename lib/{ => model}/channel/meter/powermeter.php (100%) rename lib/{channel/sensor => model/channel}/sensor.php (100%) rename lib/{ => model}/channel/sensor/onewiresensor.php (100%) rename lib/{db => model}/database.php (100%) rename lib/{ => model}/databaseobject.php (100%) rename lib/{ => model}/db/mysql.php (100%) rename lib/{ => model}/db/pgsql.php (100%) rename lib/{ => model}/db/sqlite.php (100%) rename lib/{ => model}/group.php (100%) rename lib/{ => model}/user.php (100%) rename lib/{ => view}/http/httphandle.php (100%) rename lib/{ => view}/http/httprequest.php (100%) rename lib/{ => view}/http/httpresponse.php (100%) diff --git a/init.php b/init.php index f2795f5..e5c68f4 100644 --- a/init.php +++ b/init.php @@ -24,36 +24,48 @@ */ function __autoload($className) { $libs = __DIR__ . '/lib/'; - + require_once $libs . 'util/exceptions.php'; - - // preg_replace pattern => replace mapping + + // preg_replace pattern class name => inclulde path $mapping = array( - '/.*Exception$/' => 'util/exceptions', - '/^Registry$/' => 'util/registry', - '/^Database$/' => 'db/database', - '/^Channel$/' => 'channel/channel', - '/(.*(Meter|Sensor))/i' => 'channel/$2/$1', - '/(Http.*)/' => 'http/$1', - '/(.*sql.*)/i' => 'db/$1', - '/(.*Controller)/' => 'controller/$1'); + // util classes + '/^.*Exception$/' => 'util/exceptions', + '/^Registry$/' => 'util/registry', + + // model classes + '/^(Channel|User|Group|Database(Object)?)$/'=> 'model/$1', + '/^(MySql|PgSql|SqLite)$/i' => 'model/db/$1', + '/^(.+(Meter|Sensor))$/' => 'model/channel/$2/$1', + '/^(Meter|Sensor)$/' => 'model/channel/$1', + + // view classes + '/^(Http.*)$/' => 'view/http/$1', + '/^(.*View)$/' => 'view/$1', + + // controller classes + '/^(.*Controller)$/' => 'controller/$1' + ); + + $include = preg_replace(array_keys($mapping), array_values($mapping), $className); - foreach ($mapping as $pattern => $replacement) { - $className = preg_replace($pattern, $replacement, $className); - } - - $className = strtolower($className); - - if (file_exists($libs . $className . '.php')) { - require_once $libs . $className . '.php'; + if (!empty($include)) { + $include = $libs . strtolower($include) . '.php'; + + if (file_exists($include)) { + require_once $include; + } + else { + throw new CustomException('Cannot load class ' . $className . '! File does not exist: ' . $include); + } } else { - throw new CustomException('Cannot load class! Name not mapped: ' . $className); + throw new CustomException('Cannot load class ' . $className . '! Name not mapped.'); } } // enable strict error reporting -error_reporting (E_ALL); +error_reporting(E_ALL); // lets handle all php errors as exceptions set_error_handler(array('CustomErrorException', 'errorHandler')); diff --git a/lib/channel/channel.php b/lib/model/channel.php similarity index 100% rename from lib/channel/channel.php rename to lib/model/channel.php diff --git a/lib/channel/meter/meter.php b/lib/model/channel/meter.php similarity index 100% rename from lib/channel/meter/meter.php rename to lib/model/channel/meter.php diff --git a/lib/channel/meter/powermeter.php b/lib/model/channel/meter/powermeter.php similarity index 100% rename from lib/channel/meter/powermeter.php rename to lib/model/channel/meter/powermeter.php diff --git a/lib/channel/sensor/sensor.php b/lib/model/channel/sensor.php similarity index 100% rename from lib/channel/sensor/sensor.php rename to lib/model/channel/sensor.php diff --git a/lib/channel/sensor/onewiresensor.php b/lib/model/channel/sensor/onewiresensor.php similarity index 100% rename from lib/channel/sensor/onewiresensor.php rename to lib/model/channel/sensor/onewiresensor.php diff --git a/lib/db/database.php b/lib/model/database.php similarity index 100% rename from lib/db/database.php rename to lib/model/database.php diff --git a/lib/databaseobject.php b/lib/model/databaseobject.php similarity index 100% rename from lib/databaseobject.php rename to lib/model/databaseobject.php diff --git a/lib/db/mysql.php b/lib/model/db/mysql.php similarity index 100% rename from lib/db/mysql.php rename to lib/model/db/mysql.php diff --git a/lib/db/pgsql.php b/lib/model/db/pgsql.php similarity index 100% rename from lib/db/pgsql.php rename to lib/model/db/pgsql.php diff --git a/lib/db/sqlite.php b/lib/model/db/sqlite.php similarity index 100% rename from lib/db/sqlite.php rename to lib/model/db/sqlite.php diff --git a/lib/group.php b/lib/model/group.php similarity index 100% rename from lib/group.php rename to lib/model/group.php diff --git a/lib/user.php b/lib/model/user.php similarity index 100% rename from lib/user.php rename to lib/model/user.php diff --git a/lib/http/httphandle.php b/lib/view/http/httphandle.php similarity index 100% rename from lib/http/httphandle.php rename to lib/view/http/httphandle.php diff --git a/lib/http/httprequest.php b/lib/view/http/httprequest.php similarity index 100% rename from lib/http/httprequest.php rename to lib/view/http/httprequest.php diff --git a/lib/http/httpresponse.php b/lib/view/http/httpresponse.php similarity index 100% rename from lib/http/httpresponse.php rename to lib/view/http/httpresponse.php