3 * The simulator system class is the super class of all other classes. This
4 * class handles saving of games etc.
6 * @author Roland Haeder <webmaster@ship-simu.org>
8 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Core Developer Team
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
27 * Instance of a request class
29 private $requestInstance = null;
32 * Instance of a response class
34 private $responseInstance = null;
37 * Search criteria instance
39 private $searchInstance = null;
42 * Update criteria instance
44 private $updateInstance = null;
47 * The file I/O instance for the template loader
49 private $fileIoInstance = null;
54 private $resolverInstance = null;
57 * Template engine instance
59 private $templateInstance = null;
62 * Database result instance
64 private $resultInstance = null;
67 * Instance for user class
69 private $userInstance = null;
72 * A controller instance
74 private $controllerInstance = null;
79 private $rngInstance = null;
82 * Instance of an Iterator class
84 private $iteratorInstance = null;
87 * Instance of the list
89 private $listInstance = null;
94 private $menuInstance = null;
97 * Instance of the image
99 private $imageInstance = null;
102 * Instance of the stacker
104 private $stackerInstance = null;
107 * A Compressor instance
109 private $compressorInstance = null;
112 * A Parseable instance
114 private $parserInstance = null;
117 * The real class name
119 private $realClass = 'BaseFrameworkSystem';
122 * Thousands seperator
124 private $thousands = '.'; // German
129 private $decimals = ','; // German
131 /***********************
132 * Exception codes.... *
133 ***********************/
134 // @todo Try to clean these constants up
135 const EXCEPTION_IS_NULL_POINTER = 0x001;
136 const EXCEPTION_IS_NO_OBJECT = 0x002;
137 const EXCEPTION_IS_NO_ARRAY = 0x003;
138 const EXCEPTION_MISSING_METHOD = 0x004;
139 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
140 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
141 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
142 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
143 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
144 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
145 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
146 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
147 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
148 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
149 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
150 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
151 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
152 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
153 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
154 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
155 const EXCEPTION_PATH_NOT_FOUND = 0x015;
156 const EXCEPTION_INVALID_PATH_NAME = 0x016;
157 const EXCEPTION_READ_PROTECED_PATH = 0x017;
158 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
159 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
160 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
161 const EXCEPTION_INVALID_RESOURCE = 0x01b;
162 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
163 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
164 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
165 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
166 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
167 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
168 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
169 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
170 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
171 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
172 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
173 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
174 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
175 const EXCEPTION_INVALID_STRING = 0x029;
176 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
177 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
178 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
179 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
180 const EXCEPTION_MISSING_LANGUAGE_HANDLER = 0x02e;
181 const EXCEPTION_MISSING_FILE_IO_HANDLER = 0x02f;
182 const EXCEPTION_MISSING_ELEMENT = 0x030;
183 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
184 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
185 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
186 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
187 const EXCEPTION_FATAL_ERROR = 0x035;
188 const EXCEPTION_FILE_NOT_FOUND = 0x036;
189 const EXCEPTION_ASSERTION_FAILED = 0x037;
190 const EXCEPTION_FILE_CANNOT_BE_READ = 0x038;
191 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039;
192 const EXCEPTION_FILTER_CHAIN_INTERCEPTED = 0x040;
195 * Protected super constructor
197 * @param $className Name of the class
200 protected function __construct ($className) {
202 $this->setRealClass($className);
204 // Set configuration instance if no registry
205 if (!$this instanceof Register) {
206 // Because registries doesn't need to be configured
207 $this->setConfigInstance(FrameworkConfiguration::getInstance());
212 * Destructor reached...
215 * @todo This is old code. Do we still need this old lost code?
217 public function __destruct() {
218 // Flush any updated entries to the database
219 $this->flushPendingUpdates();
221 // Is this object already destroyed?
222 if ($this->__toString() != 'DestructedObject') {
223 // Destroy all informations about this class but keep some text about it alive
224 $this->setRealClass('DestructedObject');
225 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
226 // Already destructed object
227 $this->debugOutput(sprintf("[%s:] The object <span class=\"object_name\">%s</span> is already destroyed.",
235 * The call method where all non-implemented methods end up
239 public final function __call ($methodName, $args) {
240 // Implode all given arguments
244 $argsString = 'NULL';
245 } elseif (is_array($args)) {
246 // Some arguments are there
247 foreach ($args as $arg) {
249 $argsString .= $this->replaceControlCharacters($arg) . ' (' . gettype($arg);
251 // Add length if type is string
252 if (gettype($arg) == 'string') $argsString .= ', '.strlen($arg);
255 $argsString .= '), ';
259 if (substr($argsString, -2, 1) == ',') {
260 $argsString = substr($argsString, 0, -2);
263 // Invalid arguments!
264 $argsString = '!INVALID:' . gettype($args) . '!';
267 // Output stub message
268 $this->debugOutput(sprintf("[%s->%s] Stub! Args: %s",
279 * Setter for database result instance
281 * @param $resultInstance An instance of a database result class
283 * @todo SearchableResult and UpdateableResult shall have a super interface to use here
285 protected final function setResultInstance (SearchableResult $resultInstance) {
286 $this->resultInstance = $resultInstance;
290 * Getter for database result instance
292 * @return $resultInstance An instance of a database result class
294 public final function getResultInstance () {
295 return $this->resultInstance;
299 * Setter for template engine instances
301 * @param $templateInstance An instance of a template engine class
304 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
305 $this->templateInstance = $templateInstance;
309 * Getter for template engine instances
311 * @return $templateInstance An instance of a template engine class
313 protected final function getTemplateInstance () {
314 return $this->templateInstance;
318 * Setter for search instance
320 * @param $searchInstance Searchable criteria instance
323 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
324 $this->searchInstance = $searchInstance;
328 * Getter for search instance
330 * @return $searchInstance Searchable criteria instance
332 public final function getSearchInstance () {
333 return $this->searchInstance;
337 * Setter for update instance
339 * @param $updateInstance Searchable criteria instance
342 public final function setUpdateInstance (LocalUpdateCriteria $updateInstance) {
343 $this->updateInstance = $updateInstance;
347 * Getter for update instance
349 * @return $updateInstance Updateable criteria instance
351 public final function getUpdateInstance () {
352 return $this->updateInstance;
356 * Setter for resolver instance
358 * @param $resolverInstance Instance of a command resolver class
361 public final function setResolverInstance (Resolver $resolverInstance) {
362 $this->resolverInstance = $resolverInstance;
366 * Getter for resolver instance
368 * @return $resolverInstance Instance of a command resolver class
370 public final function getResolverInstance () {
371 return $this->resolverInstance;
375 * Setter for language instance
377 * @param $configInstance The configuration instance which shall
378 * be FrameworkConfiguration
381 public final function setConfigInstance (FrameworkConfiguration $configInstance) {
382 Registry::getRegistry()->addInstance('config', $configInstance);
386 * Getter for configuration instance
388 * @return $configInstance Configuration instance
390 public final function getConfigInstance () {
391 $configInstance = Registry::getRegistry()->getInstance('config');
392 return $configInstance;
396 * Setter for debug instance
398 * @param $debugInstance The instance for debug output class
401 public final function setDebugInstance (DebugMiddleware $debugInstance) {
402 Registry::getRegistry()->addInstance('debug', $debugInstance);
406 * Getter for debug instance
408 * @return $debugInstance Instance to class DebugConsoleOutput or DebugWebOutput
410 public final function getDebugInstance () {
411 // Get debug instance
412 $debugInstance = Registry::getRegistry()->getInstance('debug');
415 return $debugInstance;
419 * Setter for web output instance
421 * @param $webInstance The instance for web output class
424 public final function setWebOutputInstance (OutputStreamer $webInstance) {
425 Registry::getRegistry()->addInstance('web_output', $webInstance);
429 * Getter for web output instance
431 * @return $webOutputInstance - Instance to class WebOutput
433 public final function getWebOutputInstance () {
434 $webOutputInstance = Registry::getRegistry()->getInstance('web_output');
435 return $webOutputInstance;
439 * Setter for database instance
441 * @param $dbInstance The instance for the database connection
442 * (forced DatabaseConnection)
445 public final function setDatabaseInstance (DatabaseConnection $dbInstance) {
446 Registry::getRegistry()->addInstance('db_instance', $dbInstance);
450 * Getter for database layer
452 * @return $dbInstance The database layer instance
454 public final function getDatabaseInstance () {
456 $dbInstance = Registry::getRegistry()->getInstance('db_instance');
463 * Setter for compressor channel
465 * @param $compressorInstance An instance of CompressorChannel
468 public final function setCompressorChannel (CompressorChannel $compressorInstance) {
469 Registry::getRegistry()->addInstance('compressor', $compressorInstance);
473 * Getter for compressor channel
475 * @return $compressorInstance The compressor channel
477 public final function getCompressorChannel () {
478 $compressorInstance = Registry::getRegistry()->getInstance('compressor');
479 return $compressorInstance;
483 * Protected getter for a manageable application helper class
485 * @return $applicationInstance An instance of a manageable application helper class
487 protected final function getApplicationInstance () {
488 $applicationInstance = Registry::getRegistry()->getInstance('application');
489 return $applicationInstance;
493 * Setter for a manageable application helper class
495 * @param $applicationInstance An instance of a manageable application helper class
498 public final function setApplicationInstance (ManageableApplication $applicationInstance) {
499 Registry::getRegistry()->addInstance('application', $applicationInstance);
503 * Setter for request instance
505 * @param $requestInstance An instance of a Requestable class
508 public final function setRequestInstance (Requestable $requestInstance) {
509 $this->requestInstance = $requestInstance;
513 * Getter for request instance
515 * @return $requestInstance An instance of a Requestable class
517 public final function getRequestInstance () {
518 return $this->requestInstance;
522 * Setter for response instance
524 * @param $responseInstance An instance of a Responseable class
527 public final function setResponseInstance (Responseable $responseInstance) {
528 $this->responseInstance = $responseInstance;
532 * Getter for response instance
534 * @return $responseInstance An instance of a Responseable class
536 public final function getResponseInstance () {
537 return $this->responseInstance;
541 * Getter for $realClass
543 * @return $realClass The name of the real class (not BaseFrameworkSystem)
545 public final function __toString () {
546 return $this->realClass;
550 * Setter for the real class name
552 * @param $realClass Class name (string)
555 public final function setRealClass ($realClass) {
557 $realClass = (string) $realClass;
560 $this->realClass = $realClass;
564 * Checks wether an object equals this object. You should overwrite this
565 * method to implement own equality checks
567 * @param $objectInstance An instance of a FrameworkInterface object
568 * @return $equals Wether both objects equals
570 public function equals (FrameworkInterface $objectInstance) {
573 $this->__toString() == $objectInstance->__toString()
575 $this->hashCode() == $objectInstance->hashCode()
583 * Formats computer generated price values into human-understandable formats
584 * with thousand and decimal seperators.
586 * @param $value The in computer format value for a price
587 * @param $currency The currency symbol (use HTML-valid characters!)
588 * @param $decNum Number of decimals after commata
589 * @return $price The for the current language formated price string
590 * @throws MissingDecimalsThousandsSeperatorException If decimals or
591 * thousands seperator
594 public function formatCurrency ($value, $currency = '€', $decNum = 2) {
595 // Are all required attriutes set?
596 if ((!isset($this->decimals)) || (!isset($this->thousands))) {
597 // Throw an exception
598 throw new MissingDecimalsThousandsSeperatorException($this, self::EXCEPTION_ATTRIBUTES_ARE_MISSING);
602 $value = (float) $value;
604 // Reformat the US number
605 $price = number_format($value, $decNum, $this->decimals, $this->thousands) . $currency;
607 // Return as string...
612 * Private getter for language instance
614 * @return $langInstance An instance to the language sub-system
616 protected final function getLanguageInstance () {
617 $langInstance = Registry::getRegistry()->getInstance('language');
618 return $langInstance;
622 * Setter for language instance
624 * @param $langInstance An instance to the language sub-system
626 * @see LanguageSystem
628 public final function setLanguageInstance (ManageableLanguage $langInstance) {
629 Registry::getRegistry()->addInstance('language', $langInstance);
633 * Appends a trailing slash to a string
635 * @param $str A string (maybe) without trailing slash
636 * @return $str A string with an auto-appended trailing slash
638 public final function addMissingTrailingSlash ($str) {
639 // Is there a trailing slash?
640 if (substr($str, -1, 1) != '/') $str .= '/';
645 * Private getter for file IO instance
647 * @return $fileIoInstance An instance to the file I/O sub-system
649 protected final function getFileIoInstance () {
650 return $this->fileIoInstance;
654 * Setter for file I/O instance
656 * @param $fileIoInstance An instance to the file I/O sub-system
659 public final function setFileIoInstance (FileIoHandler $fileIoInstance) {
660 $this->fileIoInstance = $fileIoInstance;
664 * Prepare the template engine (WebTemplateEngine by default) for a given
665 * application helper instance (ApplicationHelper by default).
667 * @param $appInstance An application helper instance or
668 * null if we shall use the default
669 * @return $templateInstance The template engine instance
670 * @throws NullPointerException If the template engine could not
672 * @throws UnsupportedTemplateEngineException If $templateInstance is an
673 * unsupported template engine
674 * @throws MissingLanguageHandlerException If the language sub-system
675 * is not yet initialized
676 * @throws NullPointerException If the discovered application
677 * instance is still null
679 protected function prepareTemplateInstance (FrameworkInterface $appInstance=null) {
680 // Is the application instance set?
681 if (is_null($appInstance)) {
682 // Get the current instance
683 $appInstance = $this->getApplicationInstance();
686 if (is_null($appInstance)) {
687 // Thrown an exception
688 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
692 // Are both instances set?
693 if ($appInstance->getLanguageInstance() === null) {
694 // Invalid language instance
695 throw new MissingLanguageHandlerException($appInstance, self::EXCEPTION_MISSING_LANGUAGE_HANDLER);
696 } elseif ($appInstance->getFileIoInstance() === null) {
697 // Invalid language instance
698 throw new MissingFileIoHandlerException($appInstance, self::EXCEPTION_MISSING_FILE_IO_HANDLER);
701 // Initialize the template engine
702 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class', array($appInstance));
704 // Return the prepared instance
705 return $templateInstance;
709 * Debugs this instance by putting out it's full content
711 * @param $message Optional message to show in debug output
714 public final function debugInstance ($message = '') {
715 // Restore the error handler to avoid trouble with missing array elements or undeclared variables
716 restore_error_handler();
722 if (!empty($message)) {
724 $content = sprintf("<div class=\"debug_message\">Message: %s</div>\n", $message);
727 // Generate the output
728 $content .= sprintf("<pre>%s</pre>",
737 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>",
740 ClassLoader::getInstance()->getPrintableIncludeList()
745 * Replaces control characters with printable output
747 * @param $str String with control characters
748 * @return $str Replaced string
750 protected function replaceControlCharacters ($str) {
753 "\r", '[r]', str_replace(
754 "\n", '[n]', str_replace(
764 * Output a partial stub message for the caller method
766 * @param $message An optional message to display
769 protected function partialStub ($message = '') {
771 $backtrace = debug_backtrace();
773 // Generate the class::method string
774 $methodName = 'UnknownClass->unknownMethod';
775 if ((isset($backtrace[1]['class'])) && (isset($backtrace[1]['function']))) {
776 $methodName = $backtrace[1]['class']."->".$backtrace[1]['function'];
779 // Construct the full message
780 $stubMessage = sprintf("[%s:] Partial stub!",
784 // Is the extra message given?
785 if (!empty($message)) {
786 // Then add it as well
787 $stubMessage .= sprintf(" Message: <span id=\"stub_message\">%s</span>", $message);
790 // Debug instance is there?
791 if (!is_null($this->getDebugInstance())) {
792 // Output stub message
793 $this->debugOutput($stubMessage);
796 trigger_error($stubMessage."<br />\n");
801 * Outputs a debug backtrace and stops further script execution
805 public function debugBackTrace () {
806 // Sorry, there is no other way getting this nice backtrace
808 debug_print_backtrace();
814 * Outputs a debug message wether to debug instance (should be set!) or dies with or pints the message
816 * @param $message Message we shall send out...
817 * @param $doPrint Wether we shall print or die here which first is the default
820 public function debugOutput ($message, $doPrint = true) {
821 // Get debug instance
822 $debugInstance = $this->getDebugInstance();
824 // Is the debug instance there?
825 if (is_object($debugInstance)) {
826 // Use debug output handler
827 $debugInstance->output($message);
828 if ($doPrint === false) die(); // Die here if not printed
831 if ($doPrint === true) {
834 // DO NOT REWRITE THIS TO app_die() !!!
841 * Converts e.g. a command from URL to a valid class by keeping out bad characters
843 * @param $str The string, what ever it is needs to be converted
844 * @return $className Generated class name
846 public function convertToClassName ($str) {
850 // Convert all dashes in underscores
851 $str = $this->convertDashesToUnderscores($str);
853 // Now use that underscores to get classname parts for hungarian style
854 foreach (explode('_', $str) as $strPart) {
855 // Make the class name part lower case and first upper case
856 $className .= ucfirst(strtolower($strPart));
864 * Converts dashes to underscores, e.g. useable for configuration entries
866 * @param $str The string with maybe dashes inside
867 * @return $str The converted string with no dashed, but underscores
869 public final function convertDashesToUnderscores ($str) {
871 $str = str_replace('-', '_', $str);
873 // Return converted string
878 * Marks up the code by adding e.g. line numbers
880 * @param $phpCode Unmarked PHP code
881 * @return $markedCode Marked PHP code
883 public function markupCode ($phpCode) {
888 $errorArray = error_get_last();
890 // Init the code with error message
891 if (is_array($errorArray)) {
893 $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>",
894 basename($errorArray['file']),
896 $errorArray['message'],
901 // Add line number to the code
902 foreach (explode("\n", $phpCode) as $lineNo => $code) {
904 $markedCode .= sprintf("<span id=\"code_line\">%s</span>: %s\n",
906 htmlentities($code, ENT_QUOTES)
915 * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
916 * beatiful for web-based front-ends. If null is given a message id
917 * null_timestamp will be resolved and returned.
919 * @param $timestamp Timestamp to prepare (filter) for display
920 * @return $readable A readable timestamp
922 public function doFilterFormatTimestamp ($timestamp) {
923 // Default value to return
926 // Is the timestamp null?
927 if (is_null($timestamp)) {
928 // Get a message string
929 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
931 switch ($this->getLanguageInstance()->getLanguageCode()) {
932 case 'de': // German format is a bit different to default
933 // Split the GMT stamp up
934 $dateTime = explode(' ', $timestamp );
935 $dateArray = explode('-', $dateTime[0]);
936 $timeArray = explode(':', $dateTime[1]);
938 // Construct the timestamp
939 $readable = sprintf($this->getConfigInstance()->getConfigEntry('german_date_time'),
949 default: // Default is pass-through
950 $readable = $timestamp;
960 * Filter a given number into a localized number
962 * @param $value The raw value from e.g. database
963 * @return $localized Localized value
965 public function doFilterFormatNumber ($value) {
966 // Generate it from config and localize dependencies
967 switch ($this->getLanguageInstance()->getLanguageCode()) {
968 case 'de': // German format is a bit different to default
969 $localized = number_format($value, $this->getConfigInstance()->getConfigEntry('decimals'), ',', '.');
973 $localized = number_format($value, $this->getConfigInstance()->getConfigEntry('decimals'), '.', ',');
982 * "Getter" for databse entry
984 * @return $entry An array with database entries
985 * @throws NullPointerException If the database result is not found
986 * @throws InvalidDatabaseResultException If the database result is invalid
988 protected final function getDatabaseEntry () {
989 // Is there an instance?
990 if (is_null($this->getResultInstance())) {
991 // Throw an exception here
992 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
996 $this->getResultInstance()->rewind();
998 // Do we have an entry?
999 if ($this->getResultInstance()->valid() === false) {
1000 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
1004 $this->getResultInstance()->next();
1007 $entry = $this->getResultInstance()->current();
1014 * Getter for field name
1016 * @param $fieldName Field name which we shall get
1017 * @return $fieldValue Field value from the user
1018 * @throws NullPointerException If the result instance is null
1020 public final function getField ($fieldName) {
1021 // Default field value
1024 // Get result instance
1025 $resultInstance = $this->getResultInstance();
1027 // Is this instance null?
1028 if (is_null($resultInstance)) {
1029 // Then the user instance is no longer valid (expired cookies?)
1030 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1033 // Get current array
1034 $fieldArray = $resultInstance->current();
1035 //* DEBUG: */ $this->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
1037 // Does the field exist?
1038 if (isset($fieldArray[$fieldName])) {
1040 $fieldValue = $fieldArray[$fieldName];
1048 * Protected setter for user instance
1050 * @param $userInstance An instance of a user class
1053 protected final function setUserInstance (ManageableAccount $userInstance) {
1054 $this->userInstance = $userInstance;
1058 * Getter for user instance
1060 * @return $userInstance An instance of a user class
1062 public final function getUserInstance () {
1063 return $this->userInstance;
1067 * Setter for controller instance (this surely breaks a bit the MVC patterm)
1069 * @param $controllerInstance An instance of the controller
1072 public final function setControllerInstance (Controller $controllerInstance) {
1073 $this->controllerInstance = $controllerInstance;
1077 * Getter for controller instance (this surely breaks a bit the MVC patterm)
1079 * @return $controllerInstance An instance of the controller
1081 public final function getControllerInstance () {
1082 return $this->controllerInstance;
1086 * Flushs all pending updates to the database layer
1090 public function flushPendingUpdates () {
1091 // Get result instance
1092 $resultInstance = $this->getResultInstance();
1094 // Do we have data to update?
1095 if ((is_object($resultInstance)) && ($resultInstance->ifDataNeedsFlush())) {
1096 // Get wrapper class name config entry
1097 $configEntry = $resultInstance->getUpdateInstance()->getWrapperConfigEntry();
1099 // Create object instance
1100 $wrapperInstance = ObjectFactory::createObjectByConfiguredName($configEntry);
1102 // Yes, then send the whole result to the database layer
1103 $wrapperInstance->doUpdateByResult($this->getResultInstance());
1108 * Outputs a deprecation warning to the developer.
1110 * @param $message The message we shall output to the developer
1112 * @todo Write a logging mechanism for productive mode
1114 public function deprecationWarning ($message) {
1115 // Is developer mode active?
1116 if (defined('DEVELOPER')) {
1117 // Debug instance is there?
1118 if (!is_null($this->getDebugInstance())) {
1119 // Output stub message
1120 $this->debugOutput($message);
1123 trigger_error($message."<br />\n");
1126 // @TODO Finish this part!
1127 $this->partialStub('Developer mode inactive. Message:' . $message);
1132 * Generates a generic hash code of this class. You should really overwrite
1133 * this method with your own hash code generator code. But keep KISS in mind.
1135 * @return $hashCode A generic hash code respresenting this whole class
1137 public function hashCode () {
1139 return crc32($this->__toString());
1143 * Checks wether the given PHP extension is loaded
1145 * @param $phpExtension The PHP extension we shall check
1146 * @return $isLoaded Wether the PHP extension is loaded
1148 public final function isPhpExtensionLoaded ($phpExtension) {
1150 $isLoaded = in_array($phpExtension, get_loaded_extensions());
1157 * Setter for RNG instance
1159 * @param $rngInstance An instance of a random number generator (RNG)
1162 protected final function setRngInstance (RandomNumberGenerator $rngInstance) {
1163 $this->rngInstance = $rngInstance;
1167 * Getter for RNG instance
1169 * @return $rngInstance An instance of a random number generator (RNG)
1171 public final function getRngInstance () {
1172 return $this->rngInstance;
1176 * Setter for Iterator instance
1178 * @param $iteratorInstance An instance of an Iterator
1181 protected final function setIteratorInstance (Iterator $iteratorInstance) {
1182 $this->iteratorInstance = $iteratorInstance;
1186 * Getter for Iterator instance
1188 * @return $iteratorInstance An instance of an Iterator
1190 public final function getIteratorInstance () {
1191 return $this->iteratorInstance;
1195 * "Getter" as a time() replacement but with milliseconds. You should use this
1196 * method instead of the encapsulated getimeofday() function.
1198 * @return $milliTime Timestamp with milliseconds
1200 public function getMilliTime () {
1201 // Get the time of day as float
1202 $milliTime = gettimeofday(true);
1209 * Idles (sleeps) for given milliseconds
1211 * @return $hasSlept Wether it goes fine
1213 public function idle ($milliSeconds) {
1214 // Sleep is fine by default
1217 // Idle so long with found function
1218 if (function_exists('time_sleep_until')) {
1219 // Get current time and add idle time
1220 $sleepUntil = $this->getMilliTime() + abs($milliSeconds) / 1000;
1222 // New PHP 5.1.0 function found
1223 $hasSlept = time_sleep_until($sleepUntil);
1225 // My Sun Station doesn't have that function even with latest PHP
1227 usleep($milliSeconds * 1000);
1235 * Setter for the list instance
1237 * @param $listInstance A list of Listable
1240 protected final function setListInstance (Listable $listInstance) {
1241 $this->listInstance = $listInstance;
1245 * Getter for the list instance
1247 * @return $listInstance A list of Listable
1249 protected final function getListInstance () {
1250 return $this->listInstance;
1254 * Setter for the menu instance
1256 * @param $menuInstance A RenderableMenu instance
1259 protected final function setMenuInstance (RenderableMenu $menuInstance) {
1260 $this->menuInstance = $menuInstance;
1264 * Getter for the menu instance
1266 * @return $menuInstance A RenderableMenu instance
1268 protected final function getMenuInstance () {
1269 return $this->menuInstance;
1273 * Setter for image instanxe
1275 * @param $imageInstance An instance of an image
1278 public final function setImageInstance (BaseImage $imageInstance) {
1279 $this->imageInstance = $imageInstance;
1283 * Getter for image instanxe
1285 * @return $imageInstance An instance of an image
1287 public final function getImageInstance () {
1288 return $this->imageInstance;
1292 * Setter for stacker instanxe
1294 * @param $stackerInstance An instance of an stacker
1297 public final function setStackerInstance (Stackable $stackerInstance) {
1298 $this->stackerInstance = $stackerInstance;
1302 * Getter for stacker instanxe
1304 * @return $stackerInstance An instance of an stacker
1306 public final function getStackerInstance () {
1307 return $this->stackerInstance;
1311 * Setter for compressor instanxe
1313 * @param $compressorInstance An instance of an compressor
1316 public final function setCompressorInstance (Compressor $compressorInstance) {
1317 $this->compressorInstance = $compressorInstance;
1321 * Getter for compressor instanxe
1323 * @return $compressorInstance An instance of an compressor
1325 public final function getCompressorInstance () {
1326 return $this->compressorInstance;
1330 * Setter for Parseable instanxe
1332 * @param $parserInstance An instance of an Parseable
1335 public final function setParserInstance (Parseable $parserInstance) {
1336 $this->parserInstance = $parserInstance;
1340 * Getter for Parseable instanxe
1342 * @return $parserInstance An instance of an Parseable
1344 public final function getParserInstance () {
1345 return $this->parserInstance;