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 - 2011 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 a crypto helper
84 private $cryptoInstance = null;
87 * Instance of an Iterator class
89 private $iteratorInstance = null;
92 * Instance of the list
94 private $listInstance = null;
99 private $menuInstance = null;
102 * Instance of the image
104 private $imageInstance = null;
107 * Instance of the stacker
109 private $stackerInstance = null;
112 * A Compressor instance
114 private $compressorInstance = null;
117 * A Parseable instance
119 private $parserInstance = null;
122 * A ProtocolHandler instance
124 private $protocolInstance = null;
127 * A database wrapper instance
129 private $databaseInstance = null;
132 * The real class name
134 private $realClass = 'BaseFrameworkSystem';
137 * Thousands seperator
139 private $thousands = '.'; // German
144 private $decimals = ','; // German
149 private $socketResource = false;
154 private $packageData = array();
156 /***********************
157 * Exception codes.... *
158 ***********************/
159 // @todo Try to clean these constants up
160 const EXCEPTION_IS_NULL_POINTER = 0x001;
161 const EXCEPTION_IS_NO_OBJECT = 0x002;
162 const EXCEPTION_IS_NO_ARRAY = 0x003;
163 const EXCEPTION_MISSING_METHOD = 0x004;
164 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
165 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
166 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
167 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
168 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
169 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
170 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
171 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
172 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
173 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
174 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
175 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
176 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
177 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
178 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
179 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
180 const EXCEPTION_PATH_NOT_FOUND = 0x015;
181 const EXCEPTION_INVALID_PATH_NAME = 0x016;
182 const EXCEPTION_READ_PROTECED_PATH = 0x017;
183 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
184 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
185 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
186 const EXCEPTION_INVALID_RESOURCE = 0x01b;
187 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
188 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
189 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
190 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
191 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
192 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
193 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
194 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
195 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
196 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
197 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
198 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
199 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
200 const EXCEPTION_INVALID_STRING = 0x029;
201 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
202 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
203 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
204 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
205 const EXCEPTION_MISSING_ELEMENT = 0x030;
206 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
207 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
208 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
209 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
210 const EXCEPTION_FATAL_ERROR = 0x035;
211 const EXCEPTION_FILE_NOT_FOUND = 0x036;
212 const EXCEPTION_ASSERTION_FAILED = 0x037;
213 const EXCEPTION_FILE_CANNOT_BE_READ = 0x038;
214 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039;
215 const EXCEPTION_FILTER_CHAIN_INTERCEPTED = 0x040;
218 * Protected super constructor
220 * @param $className Name of the class
223 protected function __construct ($className) {
225 $this->setRealClass($className);
227 // Set configuration instance if no registry ...
228 if (!$this instanceof Register) {
229 // ... because registries doesn't need to be configured
230 $this->setConfigInstance(FrameworkConfiguration::getInstance());
235 * Destructor reached...
239 public function __destruct() {
240 // Flush any updated entries to the database
241 $this->flushPendingUpdates();
243 // Is this object already destroyed?
244 if ($this->__toString() != 'DestructedObject') {
245 // Destroy all informations about this class but keep some text about it alive
246 $this->setRealClass('DestructedObject');
247 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
248 // Already destructed object
249 $this->debugOutput(sprintf("[%s:] The object <span class=\"object_name\">%s</span> is already destroyed.",
257 * The __call() method where all non-implemented methods end up
259 * @param $methodName Name of the missing method
260 * @args $args Arguments passed to the method
263 public final function __call ($methodName, $args) {
264 // Implode all given arguments
268 $argsString = 'NULL';
269 } elseif (is_array($args)) {
270 // Some arguments are there
271 foreach ($args as $arg) {
273 $argsString .= $this->replaceControlCharacters($arg) . ' (' . gettype($arg);
275 // Add length if type is string
276 if (is_string($arg)) {
277 $argsString .= ', '.strlen($arg);
281 $argsString .= '), ';
285 if (substr($argsString, -2, 1) == ',') {
286 $argsString = substr($argsString, 0, -2);
289 // Invalid arguments!
290 $argsString = '!INVALID:' . gettype($args) . '!';
293 // Output stub message
294 $this->debugOutput(sprintf("[%s->%s] Stub! Args: %s",
305 * Getter for $realClass
307 * @return $realClass The name of the real class (not BaseFrameworkSystem)
309 public function __toString () {
310 return $this->realClass;
314 * Setter for database result instance
316 * @param $resultInstance An instance of a database result class
318 * @todo SearchableResult and UpdateableResult shall have a super interface to use here
320 protected final function setResultInstance (SearchableResult $resultInstance) {
321 $this->resultInstance = $resultInstance;
325 * Getter for database result instance
327 * @return $resultInstance An instance of a database result class
329 public final function getResultInstance () {
330 return $this->resultInstance;
334 * Setter for template engine instances
336 * @param $templateInstance An instance of a template engine class
339 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
340 $this->templateInstance = $templateInstance;
344 * Getter for template engine instances
346 * @return $templateInstance An instance of a template engine class
348 protected final function getTemplateInstance () {
349 return $this->templateInstance;
353 * Setter for search instance
355 * @param $searchInstance Searchable criteria instance
358 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
359 $this->searchInstance = $searchInstance;
363 * Getter for search instance
365 * @return $searchInstance Searchable criteria instance
367 public final function getSearchInstance () {
368 return $this->searchInstance;
372 * Setter for update instance
374 * @param $updateInstance Searchable criteria instance
377 public final function setUpdateInstance (LocalUpdateCriteria $updateInstance) {
378 $this->updateInstance = $updateInstance;
382 * Getter for update instance
384 * @return $updateInstance Updateable criteria instance
386 public final function getUpdateInstance () {
387 return $this->updateInstance;
391 * Setter for resolver instance
393 * @param $resolverInstance Instance of a command resolver class
396 public final function setResolverInstance (Resolver $resolverInstance) {
397 $this->resolverInstance = $resolverInstance;
401 * Getter for resolver instance
403 * @return $resolverInstance Instance of a command resolver class
405 public final function getResolverInstance () {
406 return $this->resolverInstance;
410 * Setter for language instance
412 * @param $configInstance The configuration instance which shall
413 * be FrameworkConfiguration
416 public final function setConfigInstance (FrameworkConfiguration $configInstance) {
417 Registry::getRegistry()->addInstance('config', $configInstance);
421 * Getter for configuration instance
423 * @return $configInstance Configuration instance
425 public final function getConfigInstance () {
426 $configInstance = Registry::getRegistry()->getInstance('config');
427 return $configInstance;
431 * Setter for debug instance
433 * @param $debugInstance The instance for debug output class
436 public final function setDebugInstance (DebugMiddleware $debugInstance) {
437 Registry::getRegistry()->addInstance('debug', $debugInstance);
441 * Getter for debug instance
443 * @return $debugInstance Instance to class DebugConsoleOutput or DebugWebOutput
445 public final function getDebugInstance () {
446 // Get debug instance
447 $debugInstance = Registry::getRegistry()->getInstance('debug');
450 return $debugInstance;
454 * Setter for web output instance
456 * @param $webInstance The instance for web output class
459 public final function setWebOutputInstance (OutputStreamer $webInstance) {
460 Registry::getRegistry()->addInstance('web_output', $webInstance);
464 * Getter for web output instance
466 * @return $webOutputInstance - Instance to class WebOutput
468 public final function getWebOutputInstance () {
469 $webOutputInstance = Registry::getRegistry()->getInstance('web_output');
470 return $webOutputInstance;
474 * Setter for database instance
476 * @param $dbInstance The instance for the database connection (forced DatabaseConnection)
479 public final function setDatabaseInstance (DatabaseConnection $dbInstance) {
480 Registry::getRegistry()->addInstance('db_instance', $dbInstance);
484 * Getter for database layer
486 * @return $dbInstance The database layer instance
488 public final function getDatabaseInstance () {
490 $dbInstance = Registry::getRegistry()->getInstance('db_instance');
497 * Setter for compressor channel
499 * @param $compressorInstance An instance of CompressorChannel
502 public final function setCompressorChannel (CompressorChannel $compressorInstance) {
503 Registry::getRegistry()->addInstance('compressor', $compressorInstance);
507 * Getter for compressor channel
509 * @return $compressorInstance The compressor channel
511 public final function getCompressorChannel () {
512 $compressorInstance = Registry::getRegistry()->getInstance('compressor');
513 return $compressorInstance;
517 * Protected getter for a manageable application helper class
519 * @return $applicationInstance An instance of a manageable application helper class
521 protected final function getApplicationInstance () {
522 $applicationInstance = Registry::getRegistry()->getInstance('application');
523 return $applicationInstance;
527 * Setter for a manageable application helper class
529 * @param $applicationInstance An instance of a manageable application helper class
532 public final function setApplicationInstance (ManageableApplication $applicationInstance) {
533 Registry::getRegistry()->addInstance('application', $applicationInstance);
537 * Setter for request instance
539 * @param $requestInstance An instance of a Requestable class
542 public final function setRequestInstance (Requestable $requestInstance) {
543 $this->requestInstance = $requestInstance;
547 * Getter for request instance
549 * @return $requestInstance An instance of a Requestable class
551 public final function getRequestInstance () {
552 return $this->requestInstance;
556 * Setter for response instance
558 * @param $responseInstance An instance of a Responseable class
561 public final function setResponseInstance (Responseable $responseInstance) {
562 $this->responseInstance = $responseInstance;
566 * Getter for response instance
568 * @return $responseInstance An instance of a Responseable class
570 public final function getResponseInstance () {
571 return $this->responseInstance;
575 * Setter for the real class name
577 * @param $realClass Class name (string)
580 public final function setRealClass ($realClass) {
582 $realClass = (string) $realClass;
585 $this->realClass = $realClass;
589 * Checks wether an object equals this object. You should overwrite this
590 * method to implement own equality checks
592 * @param $objectInstance An instance of a FrameworkInterface object
593 * @return $equals Wether both objects equals
595 public function equals (FrameworkInterface $objectInstance) {
598 $this->__toString() == $objectInstance->__toString()
600 $this->hashCode() == $objectInstance->hashCode()
608 * Generates a generic hash code of this class. You should really overwrite
609 * this method with your own hash code generator code. But keep KISS in mind.
611 * @return $hashCode A generic hash code respresenting this whole class
613 public function hashCode () {
615 return crc32($this->__toString());
619 * Formats computer generated price values into human-understandable formats
620 * with thousand and decimal seperators.
622 * @param $value The in computer format value for a price
623 * @param $currency The currency symbol (use HTML-valid characters!)
624 * @param $decNum Number of decimals after commata
625 * @return $price The for the current language formated price string
626 * @throws MissingDecimalsThousandsSeperatorException If decimals or
627 * thousands seperator
630 public function formatCurrency ($value, $currency = '€', $decNum = 2) {
631 // Are all required attriutes set?
632 if ((!isset($this->decimals)) || (!isset($this->thousands))) {
633 // Throw an exception
634 throw new MissingDecimalsThousandsSeperatorException($this, self::EXCEPTION_ATTRIBUTES_ARE_MISSING);
638 $value = (float) $value;
640 // Reformat the US number
641 $price = number_format($value, $decNum, $this->decimals, $this->thousands) . $currency;
643 // Return as string...
648 * Private getter for language instance
650 * @return $langInstance An instance to the language sub-system
652 protected final function getLanguageInstance () {
653 $langInstance = Registry::getRegistry()->getInstance('language');
654 return $langInstance;
658 * Setter for language instance
660 * @param $langInstance An instance to the language sub-system
662 * @see LanguageSystem
664 public final function setLanguageInstance (ManageableLanguage $langInstance) {
665 Registry::getRegistry()->addInstance('language', $langInstance);
669 * Appends a trailing slash to a string
671 * @param $str A string (maybe) without trailing slash
672 * @return $str A string with an auto-appended trailing slash
674 public final function addMissingTrailingSlash ($str) {
675 // Is there a trailing slash?
676 if (substr($str, -1, 1) != '/') {
680 // Return string with trailing slash
685 * Private getter for file IO instance
687 * @return $fileIoInstance An instance to the file I/O sub-system
689 protected final function getFileIoInstance () {
690 return $this->fileIoInstance;
694 * Setter for file I/O instance
696 * @param $fileIoInstance An instance to the file I/O sub-system
699 public final function setFileIoInstance (FileIoHandler $fileIoInstance) {
700 $this->fileIoInstance = $fileIoInstance;
704 * Prepare the template engine (WebTemplateEngine by default) for a given
705 * application helper instance (ApplicationHelper by default).
707 * @param $applicationInstance An application helper instance or
708 * null if we shall use the default
709 * @return $templateInstance The template engine instance
710 * @throws NullPointerException If the discovered application
711 * instance is still null
713 protected function prepareTemplateInstance (ManageableApplication $applicationInstance = null) {
714 // Is the application instance set?
715 if (is_null($applicationInstance)) {
716 // Get the current instance
717 $applicationInstance = $this->getApplicationInstance();
720 if (is_null($applicationInstance)) {
721 // Thrown an exception
722 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
726 // Initialize the template engine
727 $templateInstance = ObjectFactory::createObjectByConfiguredName('web_template_class');
729 // Return the prepared instance
730 return $templateInstance;
734 * Debugs this instance by putting out it's full content
736 * @param $message Optional message to show in debug output
739 public final function debugInstance ($message = '') {
740 // Restore the error handler to avoid trouble with missing array elements or undeclared variables
741 restore_error_handler();
747 if (!empty($message)) {
749 $content = sprintf("<div class=\"debug_message\">Message: %s</div>\n", $message);
752 // Generate the output
753 $content .= sprintf("<pre>%s</pre>",
762 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>",
765 ClassLoader::getInstance()->getPrintableIncludeList()
770 * Replaces control characters with printable output
772 * @param $str String with control characters
773 * @return $str Replaced string
775 protected function replaceControlCharacters ($str) {
778 "\r", '[r]', str_replace(
779 "\n", '[n]', str_replace(
789 * Output a partial stub message for the caller method
791 * @param $message An optional message to display
794 protected function partialStub ($message = '') {
796 $backtrace = debug_backtrace();
798 // Generate the class::method string
799 $methodName = 'UnknownClass->unknownMethod';
800 if ((isset($backtrace[1]['class'])) && (isset($backtrace[1]['function']))) {
801 $methodName = $backtrace[1]['class'] . '->' . $backtrace[1]['function'];
804 // Construct the full message
805 $stubMessage = sprintf("[%s:] Partial stub!",
809 // Is the extra message given?
810 if (!empty($message)) {
811 // Then add it as well
812 $stubMessage .= sprintf(" Message: <span id=\"stub_message\">%s</span>", $message);
815 // Debug instance is there?
816 if (!is_null($this->getDebugInstance())) {
817 // Output stub message
818 $this->debugOutput($stubMessage);
821 trigger_error($stubMessage . "<br />\n");
826 * Outputs a debug backtrace and stops further script execution
830 public function debugBackTrace () {
831 // Sorry, there is no other way getting this nice backtrace
833 debug_print_backtrace();
839 * Outputs a debug message wether to debug instance (should be set!) or dies with or pints the message
841 * @param $message Message we shall send out...
842 * @param $doPrint Wether we shall print or die here which first is the default
845 public function debugOutput ($message, $doPrint = true) {
846 // Get debug instance
847 $debugInstance = $this->getDebugInstance();
849 // Is the debug instance there?
850 if (is_object($debugInstance)) {
851 // Use debug output handler
852 $debugInstance->output($message);
854 if ($doPrint === false) {
855 // Die here if not printed
860 if ($doPrint === true) {
863 // DO NOT REWRITE THIS TO app_die() !!!
870 * Converts e.g. a command from URL to a valid class by keeping out bad characters
872 * @param $str The string, what ever it is needs to be converted
873 * @return $className Generated class name
875 public function convertToClassName ($str) {
879 // Convert all dashes in underscores
880 $str = $this->convertDashesToUnderscores($str);
882 // Now use that underscores to get classname parts for hungarian style
883 foreach (explode('_', $str) as $strPart) {
884 // Make the class name part lower case and first upper case
885 $className .= ucfirst(strtolower($strPart));
893 * Converts dashes to underscores, e.g. useable for configuration entries
895 * @param $str The string with maybe dashes inside
896 * @return $str The converted string with no dashed, but underscores
898 public final function convertDashesToUnderscores ($str) {
900 $str = str_replace('-', '_', $str);
902 // Return converted string
907 * Marks up the code by adding e.g. line numbers
909 * @param $phpCode Unmarked PHP code
910 * @return $markedCode Marked PHP code
912 public function markupCode ($phpCode) {
917 $errorArray = error_get_last();
919 // Init the code with error message
920 if (is_array($errorArray)) {
922 $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>",
923 basename($errorArray['file']),
925 $errorArray['message'],
930 // Add line number to the code
931 foreach (explode("\n", $phpCode) as $lineNo => $code) {
933 $markedCode .= sprintf("<span id=\"code_line\">%s</span>: %s\n",
935 htmlentities($code, ENT_QUOTES)
944 * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
945 * beatiful for web-based front-ends. If null is given a message id
946 * null_timestamp will be resolved and returned.
948 * @param $timestamp Timestamp to prepare (filter) for display
949 * @return $readable A readable timestamp
951 public function doFilterFormatTimestamp ($timestamp) {
952 // Default value to return
955 // Is the timestamp null?
956 if (is_null($timestamp)) {
957 // Get a message string
958 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
960 switch ($this->getLanguageInstance()->getLanguageCode()) {
961 case 'de': // German format is a bit different to default
962 // Split the GMT stamp up
963 $dateTime = explode(' ', $timestamp );
964 $dateArray = explode('-', $dateTime[0]);
965 $timeArray = explode(':', $dateTime[1]);
967 // Construct the timestamp
968 $readable = sprintf($this->getConfigInstance()->getConfigEntry('german_date_time'),
978 default: // Default is pass-through
979 $readable = $timestamp;
989 * Filter a given number into a localized number
991 * @param $value The raw value from e.g. database
992 * @return $localized Localized value
994 public function doFilterFormatNumber ($value) {
995 // Generate it from config and localize dependencies
996 switch ($this->getLanguageInstance()->getLanguageCode()) {
997 case 'de': // German format is a bit different to default
998 $localized = number_format($value, $this->getConfigInstance()->getConfigEntry('decimals'), ',', '.');
1001 default: // US, etc.
1002 $localized = number_format($value, $this->getConfigInstance()->getConfigEntry('decimals'), '.', ',');
1011 * "Getter" for databse entry
1013 * @return $entry An array with database entries
1014 * @throws NullPointerException If the database result is not found
1015 * @throws InvalidDatabaseResultException If the database result is invalid
1017 protected final function getDatabaseEntry () {
1018 // Is there an instance?
1019 if (is_null($this->getResultInstance())) {
1020 // Throw an exception here
1021 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1025 $this->getResultInstance()->rewind();
1027 // Do we have an entry?
1028 if ($this->getResultInstance()->valid() === false) {
1029 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
1033 $this->getResultInstance()->next();
1036 $entry = $this->getResultInstance()->current();
1043 * Getter for field name
1045 * @param $fieldName Field name which we shall get
1046 * @return $fieldValue Field value from the user
1047 * @throws NullPointerException If the result instance is null
1049 public final function getField ($fieldName) {
1050 // Default field value
1053 // Get result instance
1054 $resultInstance = $this->getResultInstance();
1056 // Is this instance null?
1057 if (is_null($resultInstance)) {
1058 // Then the user instance is no longer valid (expired cookies?)
1059 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1062 // Get current array
1063 $fieldArray = $resultInstance->current();
1064 //* DEBUG: */ $this->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
1066 // Does the field exist?
1067 if (isset($fieldArray[$fieldName])) {
1069 $fieldValue = $fieldArray[$fieldName];
1077 * Protected setter for user instance
1079 * @param $userInstance An instance of a user class
1082 protected final function setUserInstance (ManageableAccount $userInstance) {
1083 $this->userInstance = $userInstance;
1087 * Getter for user instance
1089 * @return $userInstance An instance of a user class
1091 public final function getUserInstance () {
1092 return $this->userInstance;
1096 * Setter for controller instance (this surely breaks a bit the MVC patterm)
1098 * @param $controllerInstance An instance of the controller
1101 public final function setControllerInstance (Controller $controllerInstance) {
1102 $this->controllerInstance = $controllerInstance;
1106 * Getter for controller instance (this surely breaks a bit the MVC patterm)
1108 * @return $controllerInstance An instance of the controller
1110 public final function getControllerInstance () {
1111 return $this->controllerInstance;
1115 * Flushs all pending updates to the database layer
1119 public function flushPendingUpdates () {
1120 // Get result instance
1121 $resultInstance = $this->getResultInstance();
1123 // Do we have data to update?
1124 if ((is_object($resultInstance)) && ($resultInstance->ifDataNeedsFlush())) {
1125 // Get wrapper class name config entry
1126 $configEntry = $resultInstance->getUpdateInstance()->getWrapperConfigEntry();
1128 // Create object instance
1129 $wrapperInstance = ObjectFactory::createObjectByConfiguredName($configEntry);
1131 // Yes, then send the whole result to the database layer
1132 $wrapperInstance->doUpdateByResult($this->getResultInstance());
1137 * Outputs a deprecation warning to the developer.
1139 * @param $message The message we shall output to the developer
1141 * @todo Write a logging mechanism for productive mode
1143 public function deprecationWarning ($message) {
1144 // Is developer mode active?
1145 if (defined('DEVELOPER')) {
1146 // Debug instance is there?
1147 if (!is_null($this->getDebugInstance())) {
1148 // Output stub message
1149 $this->debugOutput($message);
1152 trigger_error($message . "<br />\n");
1155 // @TODO Finish this part!
1156 $this->partialStub('Developer mode inactive. Message:' . $message);
1161 * Checks wether the given PHP extension is loaded
1163 * @param $phpExtension The PHP extension we shall check
1164 * @return $isLoaded Wether the PHP extension is loaded
1166 public final function isPhpExtensionLoaded ($phpExtension) {
1168 $isLoaded = in_array($phpExtension, get_loaded_extensions());
1175 * Setter for RNG instance
1177 * @param $rngInstance An instance of a random number generator (RNG)
1180 protected final function setRngInstance (RandomNumberGenerator $rngInstance) {
1181 $this->rngInstance = $rngInstance;
1185 * Getter for RNG instance
1187 * @return $rngInstance An instance of a random number generator (RNG)
1189 public final function getRngInstance () {
1190 return $this->rngInstance;
1194 * Setter for Cryptable instance
1196 * @param $cryptoInstance An instance of a Cryptable class
1199 protected final function setCryptoInstance (Cryptable $cryptoInstance) {
1200 $this->cryptoInstance = $cryptoInstance;
1204 * Getter for Cryptable instance
1206 * @return $cryptoInstance An instance of a Cryptable class
1208 public final function getCryptoInstance () {
1209 return $this->cryptoInstance;
1213 * Setter for Iterator instance
1215 * @param $iteratorInstance An instance of an Iterator
1218 protected final function setIteratorInstance (Iterator $iteratorInstance) {
1219 $this->iteratorInstance = $iteratorInstance;
1223 * Getter for Iterator instance
1225 * @return $iteratorInstance An instance of an Iterator
1227 public final function getIteratorInstance () {
1228 return $this->iteratorInstance;
1232 * "Getter" as a time() replacement but with milliseconds. You should use this
1233 * method instead of the encapsulated getimeofday() function.
1235 * @return $milliTime Timestamp with milliseconds
1237 public function getMilliTime () {
1238 // Get the time of day as float
1239 $milliTime = gettimeofday(true);
1246 * Idles (sleeps) for given milliseconds
1248 * @return $hasSlept Wether it goes fine
1250 public function idle ($milliSeconds) {
1251 // Sleep is fine by default
1254 // Idle so long with found function
1255 if (function_exists('time_sleep_until')) {
1256 // Get current time and add idle time
1257 $sleepUntil = $this->getMilliTime() + abs($milliSeconds) / 1000;
1259 // New PHP 5.1.0 function found
1260 $hasSlept = time_sleep_until($sleepUntil);
1262 // My Sun Station doesn't have that function even with latest PHP
1264 usleep($milliSeconds * 1000);
1272 * Setter for the list instance
1274 * @param $listInstance A list of Listable
1277 protected final function setListInstance (Listable $listInstance) {
1278 $this->listInstance = $listInstance;
1282 * Getter for the list instance
1284 * @return $listInstance A list of Listable
1286 protected final function getListInstance () {
1287 return $this->listInstance;
1291 * Setter for the menu instance
1293 * @param $menuInstance A RenderableMenu instance
1296 protected final function setMenuInstance (RenderableMenu $menuInstance) {
1297 $this->menuInstance = $menuInstance;
1301 * Getter for the menu instance
1303 * @return $menuInstance A RenderableMenu instance
1305 protected final function getMenuInstance () {
1306 return $this->menuInstance;
1310 * Setter for image instance
1312 * @param $imageInstance An instance of an image
1315 public final function setImageInstance (BaseImage $imageInstance) {
1316 $this->imageInstance = $imageInstance;
1320 * Getter for image instance
1322 * @return $imageInstance An instance of an image
1324 public final function getImageInstance () {
1325 return $this->imageInstance;
1329 * Setter for stacker instance
1331 * @param $stackerInstance An instance of an stacker
1334 public final function setStackerInstance (Stackable $stackerInstance) {
1335 $this->stackerInstance = $stackerInstance;
1339 * Getter for stacker instance
1341 * @return $stackerInstance An instance of an stacker
1343 public final function getStackerInstance () {
1344 return $this->stackerInstance;
1348 * Setter for compressor instance
1350 * @param $compressorInstance An instance of an compressor
1353 public final function setCompressorInstance (Compressor $compressorInstance) {
1354 $this->compressorInstance = $compressorInstance;
1358 * Getter for compressor instance
1360 * @return $compressorInstance An instance of an compressor
1362 public final function getCompressorInstance () {
1363 return $this->compressorInstance;
1367 * Setter for Parseable instance
1369 * @param $parserInstance An instance of an Parseable
1372 public final function setParserInstance (Parseable $parserInstance) {
1373 $this->parserInstance = $parserInstance;
1377 * Getter for Parseable instance
1379 * @return $parserInstance An instance of an Parseable
1381 public final function getParserInstance () {
1382 return $this->parserInstance;
1386 * Setter for ProtocolHandler instance
1388 * @param $protocolInstance An instance of an ProtocolHandler
1391 public final function setProtocolInstance (ProtocolHandler $protocolInstance) {
1392 $this->protocolInstance = $protocolInstance;
1396 * Getter for ProtocolHandler instance
1398 * @return $protocolInstance An instance of an ProtocolHandler
1400 public final function getProtocolInstance () {
1401 return $this->protocolInstance;
1405 * Setter for BaseDatabaseWrapper instance
1407 * @param $wrapperInstance An instance of an BaseDatabaseWrapper
1410 public final function setWrapperInstance (BaseDatabaseWrapper $wrapperInstance) {
1411 $this->wrapperInstance = $wrapperInstance;
1415 * Getter for BaseDatabaseWrapper instance
1417 * @return $wrapperInstance An instance of an BaseDatabaseWrapper
1419 public final function getWrapperInstance () {
1420 return $this->wrapperInstance;
1424 * Setter for socket resource
1426 * @param $socketResource A valid socket resource
1429 public final function setSocketResource ($socketResource) {
1430 $this->socketResource = $socketResource;
1434 * Getter for socket resource
1436 * @return $socketResource A valid socket resource
1438 public function getSocketResource () {
1439 return $this->socketResource;
1443 * Setter for raw package Data
1445 * @param $packageData Raw package Data
1448 public final function setPackageData (array $packageData) {
1449 $this->packageData = $packageData;
1453 * Getter for raw package Data
1455 * @return $packageData Raw package Data
1457 public function getPackageData () {
1458 return $this->packageData;