00001 <?php
00025 class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
00029 private static $applicationInstance = null;
00030
00034 private static $langInstance = null;
00035
00039 private static $debugInstance = null;
00040
00044 private $requestInstance = null;
00045
00049 private $responseInstance = null;
00050
00054 private $searchInstance = null;
00055
00059 private $fileIoInstance = null;
00060
00064 private $resolverInstance = null;
00065
00069 private $templateInstance = null;
00070
00074 private $resultInstance = null;
00075
00079 private $userInstance = null;
00080
00084 private $controllerInstance = null;
00085
00089 private $realClass = "FrameworkSystem";
00090
00094 private $thousands = ".";
00095
00099 private $decimals = ",";
00100
00101
00102
00103
00104
00105 const EXCEPTION_IS_NULL_POINTER = 0x001;
00106 const EXCEPTION_IS_NO_OBJECT = 0x002;
00107 const EXCEPTION_IS_NO_ARRAY = 0x003;
00108 const EXCEPTION_MISSING_METHOD = 0x004;
00109 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
00110 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
00111 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
00112 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
00113 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
00114 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
00115 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
00116 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
00117 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
00118 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
00119 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
00120 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
00121 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
00122 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
00123 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
00124 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
00125 const EXCEPTION_PATH_NOT_FOUND = 0x015;
00126 const EXCEPTION_INVALID_PATH_NAME = 0x016;
00127 const EXCEPTION_READ_PROTECED_PATH = 0x017;
00128 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
00129 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
00130 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
00131 const EXCEPTION_INVALID_DIRECTORY_POINTER = 0x01b;
00132 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
00133 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
00134 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
00135 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
00136 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
00137 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
00138 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
00139 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
00140 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
00141 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
00142 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
00143 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
00144 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
00145 const EXCEPTION_INVALID_STRING = 0x029;
00146 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
00147 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
00148 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
00149 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
00150 const EXCEPTION_MISSING_LANGUAGE_HANDLER = 0x02e;
00151 const EXCEPTION_MISSING_FILE_IO_HANDLER = 0x02f;
00152 const EXCEPTION_MISSING_ELEMENT = 0x030;
00153 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
00154 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
00155 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
00156 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
00157 const EXCEPTION_FATAL_ERROR = 0x035;
00158 const EXCEPTION_FILE_NOT_FOUND = 0x036;
00159 const EXCEPTION_ASSERTION_FAILED = 0x037;
00160 const EXCEPTION_FILE_CANNOT_BE_READ = 0x038;
00161 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039;
00162
00172 private $systemClasses = array(
00173 "DebugMiddleware",
00174 "Registry",
00175 "ObjectFactory",
00176 "DebugWebOutput",
00177 "WebOutput",
00178 "CompressorChannel",
00179 "DebugConsoleOutput",
00180 "DebugErrorLogOutput",
00181 "FrameworkDirectoryPointer",
00182 "NullCompressor",
00183 "Bzip2Compressor",
00184 "GzipCompressor",
00185 );
00186
00187
00188
00189
00196 protected function __construct ($className) {
00197
00198 $this->setRealClass($className);
00199
00200
00201 if ((class_exists('Registry')) && (Registry::isInitialized() === false)) {
00202
00203 $this->initInstance();
00204 }
00205 }
00206
00213 public function __destruct() {
00214
00215 if ($this->__toString() != 'DestructedObject') {
00216
00217 $this->setRealClass('DestructedObject');
00218 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
00219
00220 $this->debugOutput(sprintf("[%s:] The object <span class=\"object_name\">%s</span> is already destroyed.",
00221 __CLASS__, $this->__toString()
00222 ));
00223 }
00224 }
00225
00231 public final function __call ($methodName, $args) {
00232
00233 $argsString = "";
00234 if (empty($args)) {
00235
00236 $argsString = "NULL";
00237 } elseif (is_array($args)) {
00238
00239 foreach ($args as $arg) {
00240
00241 $argsString .= $arg." (".gettype($arg);
00242
00243
00244 if (gettype($arg) == 'string') $argsString .= ", ".strlen($arg);
00245
00246
00247 $argsString .= "), ";
00248 }
00249
00250
00251 if (substr($argsString, -2, 1) === ",") {
00252 $argsString = substr($argsString, 0, -2);
00253 }
00254 } else {
00255
00256 $argsString = sprintf("!INVALID:%s!", $args);
00257 }
00258
00259
00260 $this->debugOutput(sprintf("[%s->%s] Stub! Args: %s",
00261 $this->__toString(),
00262 $methodName,
00263 $argsString
00264 ));
00265
00266
00267 return null;
00268 }
00269
00275 private final function initInstance () {
00276
00277 if (!in_array($this->__toString(), $this->systemClasses)) {
00278
00279 $this->setConfigInstance(FrameworkConfiguration::getInstance());
00280
00281
00282 $this->systemclasses[] = $this->getConfigInstance()->readConfig('app_helper_class');
00283
00284
00285 $this->setDebugInstance(DebugMiddleware::createDebugMiddleware($this->getConfigInstance()->readConfig('debug_class')));
00286
00287
00288 $outputInstance = ObjectFactory::createObjectByConfiguredName('web_engine', array($this->getConfigInstance()->readConfig('web_content_type')));
00289 $this->setWebOutputInstance($outputInstance);
00290
00291
00292 $this->setCompressorChannel(CompressorChannel::createCompressorChannel(sprintf("%s%s",
00293 $this->getConfigInstance()->readConfig('base_path'),
00294 $this->getConfigInstance()->readConfig('compressor_base_path')
00295 )));
00296
00297
00298 Registry::isInitialized('OK');
00299 } elseif ($this->__toString() == 'DebugMiddleware') {
00300
00301 $this->setConfigInstance(FrameworkConfiguration::getInstance());
00302 }
00303 }
00304
00312 protected final function setResultInstance (SearchableResult $resultInstance) {
00313 $this->resultInstance = $resultInstance;
00314 }
00315
00321 public final function getResultInstance () {
00322 return $this->resultInstance;
00323 }
00324
00331 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
00332 $this->templateInstance = $templateInstance;
00333 }
00334
00340 protected final function getTemplateInstance () {
00341 return $this->templateInstance;
00342 }
00343
00350 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
00351 $this->searchInstance = $searchInstance;
00352 }
00353
00359 public final function getSearchInstance () {
00360 return $this->searchInstance;
00361 }
00362
00369 public final function setResolverInstance (Resolver $resolverInstance) {
00370 $this->resolverInstance = $resolverInstance;
00371 }
00372
00378 public final function getResolverInstance () {
00379 return $this->resolverInstance;
00380 }
00381
00389 public final function setConfigInstance (FrameworkConfiguration $configInstance) {
00390 Registry::getRegistry()->addInstance('config', $configInstance);
00391 }
00392
00398 public final function getConfigInstance () {
00399 $cfgInstance = Registry::getRegistry()->getInstance('config');
00400 return $cfgInstance;
00401 }
00402
00409 public final function setDebugInstance (DebugMiddleware $debugInstance) {
00410 self::$debugInstance = $debugInstance;
00411 }
00412
00418 public final function getDebugInstance () {
00419 return self::$debugInstance;
00420 }
00421
00428 public final function setWebOutputInstance (OutputStreamer $webInstance) {
00429 Registry::getRegistry()->addInstance('web_output', $webInstance);
00430 }
00431
00437 public final function getWebOutputInstance () {
00438 return Registry::getRegistry()->getInstance('web_output');
00439 }
00440
00448 public final function setDatabaseInstance (DatabaseConnection $dbInstance) {
00449 Registry::getRegistry()->addInstance('dbInstance', $dbInstance);
00450 }
00451
00457 public final function getDatabaseInstance () {
00458
00459 $dbInstance = null;
00460
00461
00462 if ((class_exists('Registry')) && (Registry::isInitialized() === true)) {
00463 $dbInstance = Registry::getRegistry()->getInstance('dbInstance');
00464 }
00465
00466
00467 return $dbInstance;
00468 }
00469
00476 public final function setCompressorChannel (CompressorChannel $compressorChannel) {
00477 Registry::getRegistry()->addInstance('compressor', $compressorChannel);
00478 }
00479
00485 public final function getCompressorChannel () {
00486 return Registry::getRegistry()->getInstance('compressor');
00487 }
00488
00494 protected final function getApplicationInstance () {
00495 return self::$applicationInstance;
00496 }
00497
00504 public final function setApplicationInstance (ManageableApplication $applicationInstance) {
00505 self::$applicationInstance = $applicationInstance;
00506 }
00507
00514 public final function setRequestInstance (Requestable $requestInstance) {
00515 $this->requestInstance = $requestInstance;
00516 }
00517
00523 public final function getRequestInstance () {
00524 return $this->requestInstance;
00525 }
00526
00533 public final function setResponseInstance (Responseable $responseInstance) {
00534 $this->responseInstance = $responseInstance;
00535 }
00536
00542 public final function getResponseInstance () {
00543 return $this->responseInstance;
00544 }
00545
00551 public final function __toString () {
00552 return $this->realClass;
00553 }
00554
00561 public final function setRealClass ($realClass) {
00562
00563 $realClass = (string) $realClass;
00564
00565
00566 $this->realClass = $realClass;
00567 }
00568
00575 public final function isClass ($className) {
00576 return ($this->__toString() == $className);
00577 }
00578
00585 public function isCabin () {
00586 return false;
00587 }
00588
00596 public function isTradeable () {
00597 return false;
00598 }
00599
00612 public function formatCurrency ($value, $currency = "€", $decNum = 2) {
00613
00614 if ((!isset($this->decimals)) || (!isset($this->thousands))) {
00615
00616 throw new MissingDecimalsThousandsSeperatorException($this, self::EXCEPTION_ATTRIBUTES_ARE_MISSING);
00617 }
00618
00619
00620 $value = (float) $value;
00621
00622
00623 $price = sprintf("%s %s",
00624 number_format($value, $decNum, $this->decimals, $this->thousands),
00625 $currency
00626 );
00627
00628
00629 return $price;
00630 }
00631
00637 public final function removeNumberFormaters () {
00638 unset($this->thousands);
00639 unset($this->decimals);
00640 }
00641
00647 protected final function getLanguageInstance () {
00648 return self::$langInstance;
00649 }
00650
00658 public final function setLanguageInstance (ManageableLanguage $langInstance) {
00659 self::$langInstance = $langInstance;
00660 }
00661
00667 public final function removeSystemArray () {
00668 unset($this->systemClasses);
00669 }
00670
00677 public final function addMissingTrailingSlash ($str) {
00678
00679 if (substr($str, -1, 1) != "/") $str .= "/";
00680 return $str;
00681 }
00682
00688 protected final function getFileIoInstance () {
00689 return $this->fileIoInstance;
00690 }
00691
00698 public final function setFileIoInstance (FileIoHandler $fileIoInstance) {
00699 $this->fileIoInstance = $fileIoInstance;
00700 }
00701
00718 protected function prepareTemplateInstance (BaseFrameworkSystem $appInstance=null) {
00719
00720 if (is_null($appInstance)) {
00721
00722 $appInstance = $this->getApplicationInstance();
00723
00724
00725 if (is_null($appInstance)) {
00726
00727 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00728 }
00729 }
00730
00731
00732 $fqfn = sprintf("%s%s/%s",
00733 $this->getConfigInstance()->readConfig('application_path'),
00734 strtolower($appInstance->getAppShortName()),
00735 $this->getConfigInstance()->readConfig('tpl_base_path')
00736 );
00737
00738
00739 if ($appInstance->getLanguageInstance() === null) {
00740
00741 throw new MissingLanguageHandlerException($appInstance, self::EXCEPTION_MISSING_LANGUAGE_HANDLER);
00742 } elseif ($appInstance->getFileIoInstance() === null) {
00743
00744 throw new MissingFileIoHandlerException($appInstance, self::EXCEPTION_MISSING_FILE_IO_HANDLER);
00745 }
00746
00747
00748 $templateInstance = ObjectFactory::createObjectByConfiguredName('template_class', array($fqfn, $appInstance->getLanguageInstance(), $appInstance->getFileIoInstance()));
00749
00750
00751 return $templateInstance;
00752 }
00753
00760 public final function debugInstance ($message = "") {
00761
00762 restore_error_handler();
00763
00764
00765 $content = "";
00766
00767
00768 if (!empty($message)) {
00769
00770 $content = sprintf("<div class=\"debug_message\">Message: %s</div>\n", $message);
00771 }
00772
00773
00774 $content .= sprintf("<pre>%s</pre>",
00775 trim(
00776 htmlentities(
00777 print_r($this, true)
00778 )
00779 )
00780 );
00781
00782
00783 ApplicationEntryPoint::app_die(sprintf("<div class=\"debug_header\">%s debug output:</div><div class=\"debug_content\">%s</div>\nLoaded includes: <div class=\"debug_include_list\">%s</div>",
00784 $this->__toString(),
00785 $content,
00786 ClassLoader::getInstance()->getPrintableIncludeList()
00787 ));
00788 }
00789
00796 protected function partialStub ($message = "") {
00797
00798 $backtrace = debug_backtrace();
00799
00800
00801 $methodName = "UnknownClass->unknownMethod";
00802 if ((isset($backtrace[1]['class'])) && (isset($backtrace[1]['function']))) {
00803 $methodName = $backtrace[1]['class']."->".$backtrace[1]['function'];
00804 }
00805
00806
00807 $stubMessage = sprintf("[%s:] Partial stub!",
00808 $methodName
00809 );
00810
00811
00812 if (!empty($message)) {
00813
00814 $stubMessage .= sprintf(" Message: <span id=\"stub_message\">%s</span>", $message);
00815 }
00816
00817
00818 if (!is_null($this->getDebugInstance())) {
00819
00820 $this->debugOutput($stubMessage);
00821 } else {
00822
00823 trigger_error($stubMessage."<br />\n");
00824 }
00825 }
00826
00832 public function debugBackTrace () {
00833
00834 print("<pre>\n");
00835 debug_print_backtrace();
00836 print("</pre>");
00837 exit();
00838 }
00839
00847 public function debugOutput ($message, $doPrint = true) {
00848
00849 $debugInstance = $this->getDebugInstance();
00850
00851
00852 if (is_object($debugInstance)) {
00853
00854 $debugInstance->output($message);
00855 if ($doPrint === false) die();
00856 } else {
00857
00858 if ($doPrint) {
00859 print($message);
00860 } else {
00861
00862 die($message);
00863 }
00864 }
00865 }
00866
00873 public function convertToClassName ($str) {
00874
00875 $className = "";
00876
00877
00878 $str = str_replace("-", "_", $str);
00879
00880
00881 foreach (explode("_", $str) as $strPart) {
00882
00883 $className .= ucfirst(strtolower($strPart));
00884 }
00885
00886
00887 return $className;
00888 }
00889
00896 public function markupCode ($phpCode) {
00897
00898 $markedCode = "";
00899
00900
00901 $errorArray = error_get_last();
00902
00903
00904 if (is_array($errorArray)) {
00905
00906 $markedCode = sprintf("<div id=\"error_header\">File: <span id=\"error_data\">%s</span>, Line: <span id=\"error_data\">%s</span>, Message: <span id=\"error_data\">%s</span>, Type: <span id=\"error_data\">%s</span></div>",
00907 basename($errorArray['file']),
00908 $errorArray['line'],
00909 $errorArray['message'],
00910 $errorArray['type']
00911 );
00912 }
00913
00914
00915 foreach (explode("\n", $phpCode) as $lineNo => $code) {
00916
00917 $markedCode .= sprintf("<span id=\"code_line\">%s</span>: %s\n",
00918 ($lineNo + 1),
00919 htmlentities($code, ENT_QUOTES)
00920 );
00921 }
00922
00923
00924 return $markedCode;
00925 }
00926
00935 public function doFilterFormatTimestamp ($timestamp) {
00936
00937 $readable = "???";
00938
00939
00940 if (is_null($timestamp)) {
00941
00942 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
00943 } else {
00944 switch ($this->getLanguageInstance()->getLanguageCode()) {
00945 case "de":
00946
00947 $dateTime = explode(" ", $timestamp);
00948 $dateArray = explode("-", $dateTime[0]);
00949 $timeArray = explode(":", $dateTime[1]);
00950
00951
00952 $readable = sprintf($this->getConfigInstance()->readConfig('german_date_time'),
00953 $dateArray[0],
00954 $dateArray[1],
00955 $dateArray[2],
00956 $timeArray[0],
00957 $timeArray[1],
00958 $timeArray[2]
00959 );
00960 break;
00961
00962 default:
00963 $readable = $timestamp;
00964 break;
00965 }
00966 }
00967
00968
00969 return $readable;
00970 }
00971
00979 protected final function getDatabaseEntry () {
00980
00981 if (is_null($this->getResultInstance())) {
00982
00983 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
00984 }
00985
00986
00987 $this->getResultInstance()->rewind();
00988
00989
00990 if ($this->getResultInstance()->valid() === false) {
00991 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
00992 }
00993
00994
00995 $this->getResultInstance()->next();
00996
00997
00998 $entry = $this->getResultInstance()->current();
00999
01000
01001 return $entry;
01002 }
01003
01011 public final function getField ($fieldName) {
01012
01013 $fieldValue = null;
01014
01015
01016 $resultInstance = $this->getResultInstance();
01017
01018
01019 if (is_null($resultInstance)) {
01020
01021 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
01022 }
01023
01024
01025 $fieldArray = $resultInstance->current();
01026
01027
01028 if (isset($fieldArray[$fieldName])) {
01029
01030 $fieldValue = $fieldArray[$fieldName];
01031 }
01032
01033
01034 return $fieldValue;
01035 }
01036
01043 protected final function setUserInstance (ManageableAccount $userInstance) {
01044 $this->userInstance = $userInstance;
01045 }
01046
01052 public final function getUserInstance () {
01053 return $this->userInstance;
01054 }
01055
01062 public final function setControllerInstance (Controller $controllerInstance) {
01063 $this->controllerInstance = $controllerInstance;
01064 }
01065
01071 public final function getControllerInstance () {
01072 return $this->controllerInstance;
01073 }
01074 }
01075
01076
01077 ?>