3 namespace Org\Mxchange\CoreFramework\Object;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Criteria\Criteria;
8 use Org\Mxchange\CoreFramework\Criteria\Local\LocalSearchCriteria;
9 use Org\Mxchange\CoreFramework\Crypto\Cryptable;
10 use Org\Mxchange\CoreFramework\Database\Frontend\DatabaseWrapper;
11 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
12 use Org\Mxchange\CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
13 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
14 use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
15 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
16 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
17 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
18 use Org\Mxchange\CoreFramework\Handler\Handleable;
19 use Org\Mxchange\CoreFramework\Helper\Helper;
20 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
21 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
22 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
23 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
24 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
25 use Org\Mxchange\CoreFramework\Result\Database\CachedDatabaseResult;
26 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
27 use Org\Mxchange\CoreFramework\State\Stateable;
28 use Org\Mxchange\CoreFramework\Stream\Input\InputStream;
29 use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
30 use Org\Mxchange\CoreFramework\Stream\Output\OutputStream;
31 use Org\Mxchange\CoreFramework\User\ManageableAccount;
32 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
36 use \InvalidArgumentException;
42 * The simulator system class is the super class of all other classes. This
43 * class handles saving of games etc.
45 * @author Roland Haeder <webmaster@shipsimu.org>
47 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
48 * @license GNU GPL 3.0 or any newer version
49 * @link http://www.shipsimu.org
51 * This program is free software: you can redistribute it and/or modify
52 * it under the terms of the GNU General Public License as published by
53 * the Free Software Foundation, either version 3 of the License, or
54 * (at your option) any later version.
56 * This program is distributed in the hope that it will be useful,
57 * but WITHOUT ANY WARRANTY; without even the implied warranty of
58 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59 * GNU General Public License for more details.
61 * You should have received a copy of the GNU General Public License
62 * along with this program. If not, see <http://www.gnu.org/licenses/>.
64 abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
66 * Length of output from hash()
68 private static $hashLength = NULL;
71 * Self-referencing instance
73 private static $instance = NULL;
78 private $realClass = __CLASS__;
81 * Search criteria instance
83 private $searchInstance = NULL;
86 * Database result instance
88 private $resultInstance = NULL;
91 * Instance for user class
93 private $userInstance = NULL;
96 * Instance of a crypto helper
98 private $cryptoInstance = NULL;
101 * Instance of an Iterator class
103 private $iteratorInstance = NULL;
106 * A helper instance for the form
108 private $helperInstance = NULL;
111 * An instance of a InputStream class
113 private $inputStreamInstance = NULL;
116 * An instance of a OutputStream class
118 private $outputStreamInstance = NULL;
123 private $handlerInstance = NULL;
126 * An instance of a database wrapper class
128 private $wrapperInstance = NULL;
133 private $stateInstance = NULL;
138 private $callbackInstance = NULL;
143 private $genericArray = array();
145 /***********************
146 * Exception codes.... *
147 ***********************/
149 // @todo Try to clean these constants up
150 const EXCEPTION_IS_NULL_POINTER = 0x001;
151 const EXCEPTION_IS_NO_OBJECT = 0x002;
152 const EXCEPTION_IS_NO_ARRAY = 0x003;
153 const EXCEPTION_MISSING_METHOD = 0x004;
154 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
155 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
156 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
157 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
158 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
159 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
160 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
161 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
162 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
163 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
164 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
165 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
166 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
167 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
168 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
169 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
170 const EXCEPTION_PATH_NOT_FOUND = 0x015;
171 const EXCEPTION_INVALID_PATH_NAME = 0x016;
172 const EXCEPTION_READ_PROTECED_PATH = 0x017;
173 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
174 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
175 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
176 const EXCEPTION_INVALID_RESOURCE = 0x01b;
177 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
178 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
179 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
180 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
181 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
182 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
183 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
184 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
185 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
186 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
187 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
188 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
189 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
190 const EXCEPTION_INVALID_STRING = 0x029;
191 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
192 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
193 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
194 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
195 const EXCEPTION_UNSPPORTED_OPERATION = 0x02e;
196 const EXCEPTION_FACTORY_REQUIRE_PARAMETER = 0x02f;
197 const EXCEPTION_MISSING_ELEMENT = 0x030;
198 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
199 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
200 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
201 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
202 const EXCEPTION_FATAL_ERROR = 0x035;
203 const EXCEPTION_FILE_NOT_FOUND = 0x036;
204 const EXCEPTION_ASSERTION_FAILED = 0x037;
205 const EXCEPTION_FILE_NOT_REACHABLE = 0x038;
206 const EXCEPTION_FILE_CANNOT_BE_READ = 0x039;
207 const EXCEPTION_FILE_CANNOT_BE_WRITTEN = 0x03a;
208 const EXCEPTION_PATH_CANNOT_BE_WRITTEN = 0x03b;
209 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x03c;
210 const EXCEPTION_FILTER_CHAIN_INTERCEPTED = 0x03d;
211 const EXCEPTION_INVALID_SOCKET = 0x03e;
212 const EXCEPTION_SELF_INSTANCE = 0x03f;
215 * Startup time in miliseconds
217 private static $startupTime = 0;
220 * Protected super constructor
222 * @param $className Name of the class
225 protected function __construct (string $className) {
227 $this->setRealClass($className);
229 // Is the startup time set? (0 cannot be true anymore)
230 if (self::$startupTime == 0) {
232 self::$startupTime = microtime(true);
237 * Destructor for all classes. You should not call this method on your own.
241 public function __destruct () {
242 // Flush any updated entries to the database
243 $this->flushPendingUpdates();
245 // Is this object already destroyed?
246 if ($this->__toString() != 'DestructedObject') {
247 // Destroy all informations about this class but keep some text about it alive
248 $this->setRealClass('DestructedObject');
249 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
250 // Already destructed object
251 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('The object <span class="object_name">%s</span> is already destroyed.',
255 // Do not call this twice
256 trigger_error(__METHOD__ . ': Called twice.');
262 * The __call() method where all non-implemented methods end up
264 * @param $methodName Name of the missing method
265 * @args $args Arguments passed to the method
268 public final function __call ($methodName, $args) {
270 self::$instance = $this;
272 // Call static method
273 self::__callStatic($methodName, $args);
275 // Clear self-instance
276 self::$instance = NULL;
280 * The __callStatic() method where all non-implemented static methods end up
282 * @param $methodName Name of the missing method
283 * @param $args Arguments passed to the method
285 * @throws InvalidArgumentException If self::$instance is not a framework's own object
287 public static final function __callStatic ($methodName, $args) {
289 //* PRINT-DEBUG: */ printf('[%s:%d]: methodName=%s,args[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $methodName, gettype($args));
291 // Init argument string and class name
293 $className = 'unknown';
295 // Is self-instance set?
296 if (self::$instance instanceof FrameworkInterface) {
297 // Framework's own instance
298 $className = self::$instance->__toString();
299 } elseif (!is_null(self::$instance)) {
301 throw new InvalidArgumentException(sprintf('self::instance[%s] is not expected.', gettype(self::$instance)), self::EXCEPTION_SELF_INSTANCE);
304 // Is it NULL, empty or an array?
305 if (is_null($args)) {
307 $argsString = 'NULL';
308 } elseif (is_array($args)) {
312 // Some arguments are there
313 foreach ($args as $arg) {
314 // Add data about the argument
315 $argsString .= gettype($arg) . ':';
318 // Found a NULL argument
319 $argsString .= 'NULL';
320 } elseif (is_string($arg)) {
321 // Add length for strings
322 $argsString .= strlen($arg);
323 } elseif ((is_int($arg)) || (is_float($arg))) {
326 } elseif (is_array($arg)) {
327 // .. or size if array
328 $argsString .= count($arg);
329 } elseif (is_object($arg)) {
331 $reflection = new ReflectionClass($arg);
333 // Is an other object, maybe no __toString() available
334 $argsString .= $reflection->getName();
335 } elseif ($arg === true) {
336 // ... is boolean 'true'
337 $argsString .= 'true';
338 } elseif ($arg === false) {
339 // ... is boolean 'false'
340 $argsString .= 'false';
343 // Comma for next one
348 if (substr($argsString, -2, 1) == ',') {
350 $argsString = substr($argsString, 0, -2);
356 // Invalid arguments!
357 $argsString = sprintf('!INVALID:%s!', gettype($args));
360 // Output stub message
361 // @TODO __CLASS__ does always return BaseFrameworkSystem but not the extending (=child) class
362 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s::%s]: Stub! Args: %s',
373 * Getter for $realClass
375 * @return $realClass The name of the real class (not BaseFrameworkSystem)
377 public function __toString () {
378 return $this->realClass;
382 * Magic method to catch setting of missing but set class fields/attributes
384 * @param $name Name of the field/attribute
385 * @param $value Value to store
388 public final function __set ($name, $value) {
389 $this->debugBackTrace(sprintf('Tried to set a missing field. name=%s, value[%s]=%s',
392 print_r($value, true)
397 * Magic method to catch getting of missing fields/attributes
399 * @param $name Name of the field/attribute
402 public final function __get ($name) {
403 $this->debugBackTrace(sprintf('Tried to get a missing field. name=%s',
409 * Magic method to catch unsetting of missing fields/attributes
411 * @param $name Name of the field/attribute
414 public final function __unset ($name) {
415 $this->debugBackTrace(sprintf('Tried to unset a missing field. name=%s',
421 * Magic method to catch object serialization
423 * @return $unsupported Unsupported method
424 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
426 public final function __sleep () {
427 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
431 * Magic method to catch object deserialization
433 * @return $unsupported Unsupported method
434 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
436 public final function __wakeup () {
437 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
441 * Magic method to catch calls when an object instance is called
443 * @return $unsupported Unsupported method
444 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
446 public final function __invoke () {
447 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
451 * Setter for the real class name
453 * @param $realClass Class name (string)
456 public final function setRealClass ($realClass) {
458 $this->realClass = (string) $realClass;
462 * Setter for database result instance
464 * @param $resultInstance An instance of a database result class
466 * @todo SearchableResult and UpdateableResult shall have a super interface to use here
468 protected final function setResultInstance (SearchableResult $resultInstance) {
469 $this->resultInstance = $resultInstance;
473 * Getter for database result instance
475 * @return $resultInstance An instance of a database result class
477 public final function getResultInstance () {
478 return $this->resultInstance;
482 * Setter for search instance
484 * @param $searchInstance Searchable criteria instance
487 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
488 $this->searchInstance = $searchInstance;
492 * Getter for search instance
494 * @return $searchInstance Searchable criteria instance
496 public final function getSearchInstance () {
497 return $this->searchInstance;
501 * Setter for debug instance
503 * @param $debugInstance The instance for debug output class
506 public final function setDebugInstance (DebugMiddleware $debugInstance) {
507 GenericRegistry::getRegistry()->addInstance('debug', $debugInstance);
511 * Getter for debug instance
513 * @return $debugInstance Instance to class DebugConsoleOutput or DebugWebOutput
515 public final function getDebugInstance () {
516 // Get debug instance
517 $debugInstance = GenericRegistry::getRegistry()->getInstance('debug');
520 return $debugInstance;
524 * Setter for web output instance
526 * @param $webInstance The instance for web output class
529 public final function setWebOutputInstance (OutputStreamer $webInstance) {
530 GenericRegistry::getRegistry()->addInstance('web_output', $webInstance);
534 * Getter for web output instance
536 * @return $webOutputInstance - Instance to class WebOutput
538 public final function getWebOutputInstance () {
539 $webOutputInstance = GenericRegistry::getRegistry()->getInstance('web_output');
540 return $webOutputInstance;
544 * Private getter for language instance
546 * @return $langInstance An instance to the language sub-system
548 protected final function getLanguageInstance () {
549 $langInstance = GenericRegistry::getRegistry()->getInstance('language');
550 return $langInstance;
554 * Setter for language instance
556 * @param $langInstance An instance to the language sub-system
558 * @see LanguageSystem
560 public final function setLanguageInstance (ManageableLanguage $langInstance) {
561 GenericRegistry::getRegistry()->addInstance('language', $langInstance);
565 * Protected setter for user instance
567 * @param $userInstance An instance of a user class
570 protected final function setUserInstance (ManageableAccount $userInstance) {
571 $this->userInstance = $userInstance;
575 * Getter for user instance
577 * @return $userInstance An instance of a user class
579 public final function getUserInstance () {
580 return $this->userInstance;
584 * Setter for Cryptable instance
586 * @param $cryptoInstance An instance of a Cryptable class
589 protected final function setCryptoInstance (Cryptable $cryptoInstance) {
590 $this->cryptoInstance = $cryptoInstance;
594 * Getter for Cryptable instance
596 * @return $cryptoInstance An instance of a Cryptable class
598 public final function getCryptoInstance () {
599 return $this->cryptoInstance;
603 * Setter for DatabaseWrapper instance
605 * @param $wrapperInstance An instance of an DatabaseWrapper
608 public final function setWrapperInstance (DatabaseWrapper $wrapperInstance) {
609 $this->wrapperInstance = $wrapperInstance;
613 * Getter for DatabaseWrapper instance
615 * @return $wrapperInstance An instance of an DatabaseWrapper
617 public final function getWrapperInstance () {
618 return $this->wrapperInstance;
622 * Setter for helper instance
624 * @param $helperInstance An instance of a helper class
627 protected final function setHelperInstance (Helper $helperInstance) {
628 $this->helperInstance = $helperInstance;
632 * Getter for helper instance
634 * @return $helperInstance An instance of a helper class
636 public final function getHelperInstance () {
637 return $this->helperInstance;
641 * Getter for a InputStream instance
643 * @param $inputStreamInstance The InputStream instance
645 protected final function getInputStreamInstance () {
646 return $this->inputStreamInstance;
650 * Setter for a InputStream instance
652 * @param $inputStreamInstance The InputStream instance
655 protected final function setInputStreamInstance (InputStream $inputStreamInstance) {
656 $this->inputStreamInstance = $inputStreamInstance;
660 * Getter for a OutputStream instance
662 * @param $outputStreamInstance The OutputStream instance
664 protected final function getOutputStreamInstance () {
665 return $this->outputStreamInstance;
669 * Setter for a OutputStream instance
671 * @param $outputStreamInstance The OutputStream instance
674 protected final function setOutputStreamInstance (OutputStream $outputStreamInstance) {
675 $this->outputStreamInstance = $outputStreamInstance;
679 * Setter for handler instance
681 * @param $handlerInstance An instance of a Handleable class
684 protected final function setHandlerInstance (Handleable $handlerInstance) {
685 $this->handlerInstance = $handlerInstance;
689 * Getter for handler instance
691 * @return $handlerInstance A Handleable instance
693 protected final function getHandlerInstance () {
694 return $this->handlerInstance;
698 * Setter for Iterator instance
700 * @param $iteratorInstance An instance of an Iterator
703 protected final function setIteratorInstance (Iterator $iteratorInstance) {
704 $this->iteratorInstance = $iteratorInstance;
708 * Getter for Iterator instance
710 * @return $iteratorInstance An instance of an Iterator
712 public final function getIteratorInstance () {
713 return $this->iteratorInstance;
717 * Setter for state instance
719 * @param $stateInstance A Stateable instance
722 public final function setStateInstance (Stateable $stateInstance) {
723 $this->stateInstance = $stateInstance;
727 * Getter for state instance
729 * @return $stateInstance A Stateable instance
731 public final function getStateInstance () {
732 return $this->stateInstance;
736 * Setter for call-back instance
738 * @param $callbackInstance An instance of a FrameworkInterface class
741 public final function setCallbackInstance (FrameworkInterface $callbackInstance) {
742 $this->callbackInstance = $callbackInstance;
746 * Getter for call-back instance
748 * @return $callbackInstance An instance of a FrameworkInterface class
750 protected final function getCallbackInstance () {
751 return $this->callbackInstance;
755 * Checks whether an object equals this object. You should overwrite this
756 * method to implement own equality checks
758 * @param $objectInstance An instance of a FrameworkInterface object
759 * @return $equals Whether both objects equals
761 public function equals (FrameworkInterface $objectInstance) {
764 $this->__toString() == $objectInstance->__toString()
766 $this->hashCode() == $objectInstance->hashCode()
774 * Generates a generic hash code of this class. You should really overwrite
775 * this method with your own hash code generator code. But keep KISS in mind.
777 * @return $hashCode A generic hash code respresenting this whole class
779 public function hashCode () {
781 return crc32($this->__toString());
785 * Appends a trailing slash to a string
787 * @param $str A string (maybe) without trailing slash
788 * @return $str A string with an auto-appended trailing slash
790 public final function addMissingTrailingSlash ($str) {
791 // Is there a trailing slash?
792 if (substr($str, -1, 1) != '/') {
796 // Return string with trailing slash
801 * Debugs this instance by putting out it's full content
803 * @param $message Optional message to show in debug output
806 public final function debugInstance ($message = '') {
807 // Restore the error handler to avoid trouble with missing array elements or undeclared variables
808 restore_error_handler();
814 if (!empty($message)) {
816 $content = sprintf('<div class="debug_message">
818 </div>' . PHP_EOL, $message);
821 // Generate the output
822 $content .= sprintf('<pre>%s</pre>',
831 ApplicationEntryPoint::exitApplication(sprintf('<div class="debug_header">
834 <div class="debug_content">
838 <div class="debug_include_list">
843 ClassLoader::getSelfInstance()->getPrintableIncludeList()
848 * Replaces control characters with printable output
850 * @param $str String with control characters
851 * @return $str Replaced string
853 protected function replaceControlCharacters ($str) {
856 chr(13), '[r]', str_replace(
857 chr(10), '[n]', str_replace(
867 * Output a partial stub message for the caller method
869 * @param $message An optional message to display
872 protected function partialStub ($message = '') {
874 $stubMessage = 'Partial stub!';
876 // Is an extra message given?
877 if (!empty($message)) {
878 // Then add it as well
879 $stubMessage .= ' Message: ' . $message;
882 // Debug instance is there?
883 if (!is_null($this->getDebugInstance())) {
884 // Output stub message
885 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($stubMessage);
888 trigger_error($stubMessage);
894 * Outputs a debug backtrace and stops further script execution
896 * @param $message An optional message to output
897 * @param $doExit Whether exit the program (true is default)
900 public function debugBackTrace ($message = '', $doExit = true) {
901 // Sorry, there is no other way getting this nice backtrace
902 if (!empty($message)) {
904 printf('Message: %s<br />' . PHP_EOL, $message);
908 debug_print_backtrace();
912 if ($doExit === true) {
918 * Creates an instance of a debugger instance
920 * @param $className Name of the class (currently unsupported)
921 * @param $lineNumber Line number where the call was made
922 * @return $debugInstance An instance of a debugger class
923 * @deprecated Not fully, as the new Logger facilities are not finished yet.
925 public final static function createDebugInstance ($className, $lineNumber = NULL) {
926 // Is the instance set?
927 if (!GenericRegistry::getRegistry()->instanceExists('debug')) {
928 // Init debug instance
929 $debugInstance = NULL;
933 // Get a debugger instance
934 $debugInstance = DebugMiddleware::createDebugMiddleware(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_class'), $className);
935 } catch (NullPointerException $e) {
936 // Didn't work, no instance there
937 exit(sprintf('Cannot create debugInstance! Exception=%s,message=%s,className=%s,lineNumber=%d' . PHP_EOL, $e->__toString(), $e->getMessage(), $className, $lineNumber));
940 // Empty string should be ignored and used for testing the middleware
941 DebugMiddleware::getSelfInstance()->output('');
943 // Set it in registry
944 GenericRegistry::getRegistry()->addInstance('debug', $debugInstance);
946 // Get instance from registry
947 $debugInstance = GenericRegistry::getRegistry()->getDebugInstance();
951 return $debugInstance;
955 * Simple output of a message with line-break
957 * @param $message Message to output
960 public function outputLine ($message) {
962 print($message . PHP_EOL);
966 * Outputs a debug message whether to debug instance (should be set!) or
967 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
968 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
970 * @param $message Message we shall send out...
971 * @param $doPrint Whether print or die here (default: print)
972 * @paran $stripTags Whether to strip tags (default: false)
975 public function debugOutput ($message, $doPrint = true, $stripTags = false) {
976 // Set debug instance to NULL
977 $debugInstance = NULL;
980 $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT);
982 // Is function partialStub/__callStatic ?
983 if (in_array($backtrace[1]['function'], array('partialStub', '__call', '__callStatic'))) {
984 // Prepend class::function:line from 3rd element
985 $message = sprintf('[%s::%s:%d]: %s',
986 $backtrace[2]['class'],
987 $backtrace[2]['function'],
988 (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'),
992 // Prepend class::function:line from 2nd element
993 $message = sprintf('[%s::%s:%d]: %s',
994 $backtrace[1]['class'],
995 $backtrace[1]['function'],
996 (isset($backtrace[1]['line']) ? $backtrace[1]['line'] : '0'),
1003 // Get debug instance
1004 $debugInstance = $this->getDebugInstance();
1005 } catch (NullPointerException $e) {
1006 // The debug instance is not set (yet)
1009 // Is the debug instance there?
1010 if (is_object($debugInstance)) {
1011 // Use debug output handler
1012 $debugInstance->output($message, $stripTags);
1014 if ($doPrint === false) {
1015 // Die here if not printed
1019 // Are debug times enabled?
1020 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_output_timings') == 'Y') {
1022 $message = $this->getPrintableExecutionTime() . $message;
1026 if ($doPrint === true) {
1028 $this->outputLine($message);
1037 * Marks up the code by adding e.g. line numbers
1039 * @param $phpCode Unmarked PHP code
1040 * @return $markedCode Marked PHP code
1042 public function markupCode ($phpCode) {
1047 $errorArray = error_get_last();
1049 // Init the code with error message
1050 if (is_array($errorArray)) {
1052 $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>',
1053 basename($errorArray['file']),
1054 $errorArray['line'],
1055 $errorArray['message'],
1060 // Add line number to the code
1061 foreach (explode(chr(10), $phpCode) as $lineNo => $code) {
1063 $markedCode .= sprintf('<span id="code_line">%s</span>: %s' . PHP_EOL,
1065 htmlentities($code, ENT_QUOTES)
1074 * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
1075 * beatiful for web-based front-ends. If null is given a message id
1076 * null_timestamp will be resolved and returned.
1078 * @param $timestamp Timestamp to prepare (filter) for display
1079 * @return $readable A readable timestamp
1081 public function doFilterFormatTimestamp ($timestamp) {
1082 // Default value to return
1085 // Is the timestamp null?
1086 if (is_null($timestamp)) {
1087 // Get a message string
1088 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
1090 switch ($this->getLanguageInstance()->getLanguageCode()) {
1091 case 'de': // German format is a bit different to default
1092 // Split the GMT stamp up
1093 $dateTime = explode(' ', $timestamp );
1094 $dateArray = explode('-', $dateTime[0]);
1095 $timeArray = explode(':', $dateTime[1]);
1097 // Construct the timestamp
1098 $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'),
1108 default: // Default is pass-through
1109 $readable = $timestamp;
1119 * Filter a given number into a localized number
1121 * @param $value The raw value from e.g. database
1122 * @return $localized Localized value
1124 public function doFilterFormatNumber ($value) {
1125 // Generate it from config and localize dependencies
1126 switch ($this->getLanguageInstance()->getLanguageCode()) {
1127 case 'de': // German format is a bit different to default
1128 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), ',', '.');
1131 default: // US, etc.
1132 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ',');
1141 * "Getter" for databse entry
1143 * @return $entry An array with database entries
1144 * @throws NullPointerException If the database result is not found
1145 * @throws InvalidDatabaseResultException If the database result is invalid
1147 protected final function getDatabaseEntry () {
1148 // Is there an instance?
1149 if (!$this->getResultInstance() instanceof SearchableResult) {
1150 // Throw an exception here
1151 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1155 $this->getResultInstance()->rewind();
1157 // Do we have an entry?
1158 if ($this->getResultInstance()->valid() === false) {
1159 // @TODO Move the constant to e.g. BaseDatabaseResult when there is a non-cached database result available
1160 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), CachedDatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
1164 $this->getResultInstance()->next();
1167 $entry = $this->getResultInstance()->current();
1174 * Getter for field name
1176 * @param $fieldName Field name which we shall get
1177 * @return $fieldValue Field value from the user
1178 * @throws NullPointerException If the result instance is null
1180 public final function getField (string $fieldName) {
1181 // Default field value
1184 // Get result instance
1185 $resultInstance = $this->getResultInstance();
1187 // Is this instance null?
1188 if (is_null($resultInstance)) {
1189 // Then the user instance is no longer valid (expired cookies?)
1190 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1193 // Get current array
1194 $fieldArray = $resultInstance->current();
1195 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
1197 // Convert dashes to underscore
1198 $fieldName2 = StringUtils::convertDashesToUnderscores($fieldName);
1200 // Does the field exist?
1201 if ($this->isFieldSet($fieldName)) {
1203 $fieldValue = $fieldArray[$fieldName2];
1204 } elseif (defined('DEVELOPER')) {
1205 // Missing field entry, may require debugging
1206 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldArray<pre>=' . print_r($fieldArray, true) . '</pre>,fieldName=' . $fieldName . ' not found!');
1208 // Missing field entry, may require debugging
1209 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldName=' . $fieldName . ' not found!');
1217 * Checks if given field is set
1219 * @param $fieldName Field name to check
1220 * @return $isSet Whether the given field name is set
1221 * @throws NullPointerException If the result instance is null
1223 public function isFieldSet (string $fieldName) {
1224 // Get result instance
1225 $resultInstance = $this->getResultInstance();
1227 // Is this instance null?
1228 if (is_null($resultInstance)) {
1229 // Then the user instance is no longer valid (expired cookies?)
1230 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1233 // Get current array
1234 $fieldArray = $resultInstance->current();
1235 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . $this->__toString() . ':' . __LINE__ . '] fieldName=' . $fieldName . ',fieldArray=<pre>'.print_r($fieldArray, true).'</pre>');
1237 // Convert dashes to underscore
1238 $fieldName = StringUtils::convertDashesToUnderscores($fieldName);
1241 $isSet = isset($fieldArray[$fieldName]);
1248 * Flushs all pending updates to the database layer
1252 public function flushPendingUpdates () {
1253 // Get result instance
1254 $resultInstance = $this->getResultInstance();
1256 // Do we have data to update?
1257 if ((is_object($resultInstance)) && ($resultInstance->ifDataNeedsFlush())) {
1258 // Get wrapper class name config entry
1259 $configEntry = $resultInstance->getUpdateInstance()->getWrapperConfigEntry();
1261 // Create object instance
1262 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName($configEntry);
1264 // Yes, then send the whole result to the database layer
1265 $wrapperInstance->doUpdateByResult($this->getResultInstance());
1270 * Outputs a deprecation warning to the developer.
1272 * @param $message The message we shall output to the developer
1274 * @todo Write a logging mechanism for productive mode
1276 public function deprecationWarning ($message) {
1277 // Is developer mode active?
1278 if (defined('DEVELOPER')) {
1279 // Debug instance is there?
1280 if (!is_null($this->getDebugInstance())) {
1281 // Output stub message
1282 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($message);
1285 trigger_error($message . "<br />\n");
1289 // @TODO Finish this part!
1290 $this->partialStub('Developer mode inactive. Message:' . $message);
1295 * Checks whether the given PHP extension is loaded
1297 * @param $phpExtension The PHP extension we shall check
1298 * @return $isLoaded Whether the PHP extension is loaded
1300 public final function isPhpExtensionLoaded ($phpExtension) {
1302 $isLoaded = in_array($phpExtension, get_loaded_extensions());
1309 * "Getter" as a time() replacement but with milliseconds. You should use this
1310 * method instead of the encapsulated getimeofday() function.
1312 * @return $milliTime Timestamp with milliseconds
1314 public function getMilliTime () {
1315 // Get the time of day as float
1316 $milliTime = gettimeofday(true);
1323 * Idles (sleeps) for given milliseconds
1325 * @return $hasSlept Whether it goes fine
1327 public function idle ($milliSeconds) {
1328 // Sleep is fine by default
1331 // Idle so long with found function
1332 if (function_exists('time_sleep_until')) {
1333 // Get current time and add idle time
1334 $sleepUntil = $this->getMilliTime() + abs($milliSeconds) / 1000;
1336 // New PHP 5.1.0 function found, ignore errors
1337 $hasSlept = @time_sleep_until($sleepUntil);
1340 * My Sun station doesn't have that function even with latest PHP
1343 usleep($milliSeconds * 1000);
1351 * Checks whether the given encoded data was encoded with Base64
1353 * @param $encodedData Encoded data we shall check
1354 * @return $isBase64 Whether the encoded data is Base64
1356 protected function isBase64Encoded ($encodedData) {
1358 $isBase64 = (@base64_decode($encodedData, true) !== false);
1365 * Gets a cache key from Criteria instance
1367 * @param $criteriaInstance An instance of a Criteria class
1368 * @param $onlyKeys Only use these keys for a cache key
1369 * @return $cacheKey A cache key suitable for lookup/storage purposes
1371 protected function getCacheKeyByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
1373 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRAMEWORK-SYSTEM: criteriaInstance=' . $criteriaInstance->__toString() . ',onlyKeys()=' . count($onlyKeys) . ' - CALLED!');
1374 $cacheKey = sprintf('%s@%s',
1375 $this->__toString(),
1376 $criteriaInstance->getCacheKey($onlyKeys)
1380 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRAMEWORK-SYSTEM: cacheKey=' . $cacheKey . ' - EXIT!');
1385 * Getter for startup time in miliseconds
1387 * @return $startupTime Startup time in miliseconds
1389 protected function getStartupTime () {
1390 return self::$startupTime;
1394 * "Getter" for a printable currently execution time in nice braces
1396 * @return $executionTime Current execution time in nice braces
1398 protected function getPrintableExecutionTime () {
1399 // Caculate the execution time
1400 $executionTime = microtime(true) - $this->getStartupTime();
1402 // Pack it in nice braces
1403 $executionTime = sprintf('[ %01.5f ] ', $executionTime);
1406 return $executionTime;
1410 * Hashes a given string with a simple but stronger hash function (no salt)
1411 * and hex-encode it.
1413 * @param $str The string to be hashed
1414 * @return $hash The hash from string $str
1416 public static final function hash ($str) {
1417 // Hash given string with (better secure) hasher
1418 $hash = bin2hex(mhash(MHASH_SHA256, $str));
1425 * "Getter" for length of hash() output. This will be "cached" to speed up
1428 * @return $length Length of hash() output
1430 public static final function getHashLength () {
1432 if (is_null(self::$hashLength)) {
1433 // No, then hash a string and save its length.
1434 self::$hashLength = strlen(self::hash('abc123'));
1438 return self::$hashLength;
1442 * Checks whether the given number is really a number (only chars 0-9).
1444 * @param $num A string consisting only chars between 0 and 9
1445 * @param $castValue Whether to cast the value to double. Do only use this to secure numbers from Requestable classes.
1446 * @param $assertMismatch Whether to assert mismatches
1447 * @return $ret The (hopefully) secured numbered value
1449 public function bigintval ($num, $castValue = true, $assertMismatch = false) {
1450 // Filter all numbers out
1451 $ret = preg_replace('/[^0123456789]/', '', $num);
1454 if ($castValue === true) {
1455 // Cast to biggest numeric type
1456 $ret = (double) $ret;
1459 // Assert only if requested
1460 if ($assertMismatch === true) {
1461 // Has the whole value changed?
1462 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
1470 * Checks whether the given hexadecimal number is really a hex-number (only chars 0-9,a-f).
1472 * @param $num A string consisting only chars between 0 and 9
1473 * @param $assertMismatch Whether to assert mismatches
1474 * @return $ret The (hopefully) secured hext-numbered value
1476 public function hexval ($num, $assertMismatch = false) {
1477 // Filter all numbers out
1478 $ret = preg_replace('/[^0123456789abcdefABCDEF]/', '', $num);
1480 // Assert only if requested
1481 if ($assertMismatch === true) {
1482 // Has the whole value changed?
1483 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
1491 * Determines if an element is set in the generic array
1493 * @param $keyGroup Main group for the key
1494 * @param $subGroup Sub group for the key
1495 * @param $key Key to check
1496 * @param $element Element to check
1497 * @return $isset Whether the given key is set
1499 protected final function isGenericArrayElementSet ($keyGroup, $subGroup, $key, $element) {
1501 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
1504 $isset = isset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
1510 * Determines if a key is set in the generic array
1512 * @param $keyGroup Main group for the key
1513 * @param $subGroup Sub group for the key
1514 * @param $key Key to check
1515 * @return $isset Whether the given key is set
1517 protected final function isGenericArrayKeySet ($keyGroup, $subGroup, $key) {
1519 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1522 $isset = isset($this->genericArray[$keyGroup][$subGroup][$key]);
1530 * Determines if a group is set in the generic array
1532 * @param $keyGroup Main group
1533 * @param $subGroup Sub group
1534 * @return $isset Whether the given group is set
1536 protected final function isGenericArrayGroupSet ($keyGroup, $subGroup) {
1538 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
1541 $isset = isset($this->genericArray[$keyGroup][$subGroup]);
1548 * Getter for sub key group
1550 * @param $keyGroup Main key group
1551 * @param $subGroup Sub key group
1552 * @return $array An array with all array elements
1554 protected final function getGenericSubArray ($keyGroup, $subGroup) {
1556 if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
1557 // No, then abort here
1558 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
1563 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',value=' . print_r($this->genericArray[$keyGroup][$subGroup], true));
1566 return $this->genericArray[$keyGroup][$subGroup];
1570 * Unsets a given key in generic array
1572 * @param $keyGroup Main group for the key
1573 * @param $subGroup Sub group for the key
1574 * @param $key Key to unset
1577 protected final function unsetGenericArrayKey ($keyGroup, $subGroup, $key) {
1579 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1582 unset($this->genericArray[$keyGroup][$subGroup][$key]);
1586 * Unsets a given element in generic array
1588 * @param $keyGroup Main group for the key
1589 * @param $subGroup Sub group for the key
1590 * @param $key Key to unset
1591 * @param $element Element to unset
1594 protected final function unsetGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
1596 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
1599 unset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
1603 * Append a string to a given generic array key
1605 * @param $keyGroup Main group for the key
1606 * @param $subGroup Sub group for the key
1607 * @param $key Key to unset
1608 * @param $value Value to add/append
1611 protected final function appendStringToGenericArrayKey ($keyGroup, $subGroup, $key, $value, $appendGlue = '') {
1613 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, true) . ',appendGlue=' . $appendGlue);
1615 // Is it already there?
1616 if ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1618 $this->genericArray[$keyGroup][$subGroup][$key] .= $appendGlue . (string) $value;
1621 $this->genericArray[$keyGroup][$subGroup][$key] = (string) $value;
1626 * Append a string to a given generic array element
1628 * @param $keyGroup Main group for the key
1629 * @param $subGroup Sub group for the key
1630 * @param $key Key to unset
1631 * @param $element Element to check
1632 * @param $value Value to add/append
1635 protected final function appendStringToGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value, $appendGlue = '') {
1637 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, true) . ',appendGlue=' . $appendGlue);
1639 // Is it already there?
1640 if ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
1642 $this->genericArray[$keyGroup][$subGroup][$key][$element] .= $appendGlue . (string) $value;
1645 $this->setStringGenericArrayElement($keyGroup, $subGroup, $key, $element, $value);
1650 * Sets a string in a given generic array element
1652 * @param $keyGroup Main group for the key
1653 * @param $subGroup Sub group for the key
1654 * @param $key Key to unset
1655 * @param $element Element to check
1656 * @param $value Value to add/append
1659 protected final function setStringGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value, $appendGlue = '') {
1661 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, true) . ',appendGlue=' . $appendGlue);
1664 $this->genericArray[$keyGroup][$subGroup][$key][$element] = (string) $value;
1668 * Initializes given generic array group
1670 * @param $keyGroup Main group for the key
1671 * @param $subGroup Sub group for the key
1672 * @param $key Key to use
1673 * @param $forceInit Optionally force initialization
1676 protected final function initGenericArrayGroup ($keyGroup, $subGroup, $forceInit = false) {
1678 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',forceInit=' . intval($forceInit));
1680 // Is it already set?
1681 if (($forceInit === false) && ($this->isGenericArrayGroupSet($keyGroup, $subGroup))) {
1682 // Already initialized
1683 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' already initialized.');
1688 $this->genericArray[$keyGroup][$subGroup] = array();
1692 * Initializes given generic array key
1694 * @param $keyGroup Main group for the key
1695 * @param $subGroup Sub group for the key
1696 * @param $key Key to use
1697 * @param $forceInit Optionally force initialization
1700 protected final function initGenericArrayKey ($keyGroup, $subGroup, $key, $forceInit = false) {
1702 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',forceInit=' . intval($forceInit));
1704 // Is it already set?
1705 if (($forceInit === false) && ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key))) {
1706 // Already initialized
1707 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' already initialized.');
1712 $this->genericArray[$keyGroup][$subGroup][$key] = array();
1716 * Initializes given generic array element
1718 * @param $keyGroup Main group for the key
1719 * @param $subGroup Sub group for the key
1720 * @param $key Key to use
1721 * @param $element Element to use
1722 * @param $forceInit Optionally force initialization
1725 protected final function initGenericArrayElement ($keyGroup, $subGroup, $key, $element, $forceInit = false) {
1727 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',forceInit=' . intval($forceInit));
1729 // Is it already set?
1730 if (($forceInit === false) && ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element))) {
1731 // Already initialized
1732 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' already initialized.');
1737 $this->genericArray[$keyGroup][$subGroup][$key][$element] = array();
1741 * Pushes an element to a generic key
1743 * @param $keyGroup Main group for the key
1744 * @param $subGroup Sub group for the key
1745 * @param $key Key to use
1746 * @param $value Value to add/append
1747 * @return $count Number of array elements
1749 protected final function pushValueToGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
1751 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, true));
1754 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1756 $this->initGenericArrayKey($keyGroup, $subGroup, $key);
1760 $count = array_push($this->genericArray[$keyGroup][$subGroup][$key], $value);
1763 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1764 //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
1769 * Pushes an element to a generic array element
1771 * @param $keyGroup Main group for the key
1772 * @param $subGroup Sub group for the key
1773 * @param $key Key to use
1774 * @param $element Element to check
1775 * @param $value Value to add/append
1776 * @return $count Number of array elements
1778 protected final function pushValueToGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
1780 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, true));
1783 if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
1785 $this->initGenericArrayElement($keyGroup, $subGroup, $key, $element);
1789 $count = array_push($this->genericArray[$keyGroup][$subGroup][$key][$element], $value);
1792 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1793 //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
1798 * Pops an element from a generic group
1800 * @param $keyGroup Main group for the key
1801 * @param $subGroup Sub group for the key
1802 * @param $key Key to unset
1803 * @return $value Last "popped" value
1805 protected final function popGenericArrayElement ($keyGroup, $subGroup, $key) {
1807 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1810 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1812 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
1817 $value = array_pop($this->genericArray[$keyGroup][$subGroup][$key]);
1820 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1821 //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, true) . PHP_EOL);
1826 * Shifts an element from a generic group
1828 * @param $keyGroup Main group for the key
1829 * @param $subGroup Sub group for the key
1830 * @param $key Key to unset
1831 * @return $value Last "popped" value
1833 protected final function shiftGenericArrayElement ($keyGroup, $subGroup, $key) {
1835 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1838 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1840 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
1845 $value = array_shift($this->genericArray[$keyGroup][$subGroup][$key]);
1848 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1849 //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, true) . PHP_EOL);
1854 * Count generic array group
1856 * @param $keyGroup Main group for the key
1857 * @return $count Count of given group
1859 protected final function countGenericArray ($keyGroup) {
1861 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup);
1864 if (!isset($this->genericArray[$keyGroup])) {
1866 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' not found.');
1871 $count = count($this->genericArray[$keyGroup]);
1874 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',count=' . $count);
1881 * Count generic array sub group
1883 * @param $keyGroup Main group for the key
1884 * @param $subGroup Sub group for the key
1885 * @return $count Count of given group
1887 protected final function countGenericArrayGroup ($keyGroup, $subGroup) {
1889 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
1892 if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
1894 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
1899 $count = count($this->genericArray[$keyGroup][$subGroup]);
1902 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',count=' . $count);
1909 * Count generic array elements
1911 * @param $keyGroup Main group for the key
1912 * @param $subGroup Sub group for the key
1913 * @para $key Key to count
1914 * @return $count Count of given key
1916 protected final function countGenericArrayElements ($keyGroup, $subGroup, $key) {
1918 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1921 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1923 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
1925 } elseif (!$this->isValidGenericArrayGroup($keyGroup, $subGroup)) {
1927 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' is not an array.');
1932 $count = count($this->genericArray[$keyGroup][$subGroup][$key]);
1935 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',count=' . $count);
1942 * Getter for whole generic group array
1944 * @param $keyGroup Key group to get
1945 * @return $array Whole generic array group
1947 protected final function getGenericArray ($keyGroup) {
1949 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup);
1952 if (!isset($this->genericArray[$keyGroup])) {
1954 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' does not exist.');
1959 return $this->genericArray[$keyGroup];
1963 * Setter for generic array key
1965 * @param $keyGroup Key group to get
1966 * @param $subGroup Sub group for the key
1967 * @param $key Key to unset
1968 * @param $value Mixed value from generic array element
1971 protected final function setGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
1973 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, true));
1976 $this->genericArray[$keyGroup][$subGroup][$key] = $value;
1980 * Getter for generic array key
1982 * @param $keyGroup Key group to get
1983 * @param $subGroup Sub group for the key
1984 * @param $key Key to unset
1985 * @return $value Mixed value from generic array element
1987 protected final function getGenericArrayKey ($keyGroup, $subGroup, $key) {
1989 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1992 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1994 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' does not exist.');
1999 return $this->genericArray[$keyGroup][$subGroup][$key];
2003 * Sets a value in given generic array key/element
2005 * @param $keyGroup Main group for the key
2006 * @param $subGroup Sub group for the key
2007 * @param $key Key to set
2008 * @param $element Element to set
2009 * @param $value Value to set
2012 protected final function setGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
2014 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',value[' . gettype($value) . ']=' . print_r($value, true));
2017 $this->genericArray[$keyGroup][$subGroup][$key][$element] = $value;
2021 * Getter for generic array element
2023 * @param $keyGroup Key group to get
2024 * @param $subGroup Sub group for the key
2025 * @param $key Key to look for
2026 * @param $element Element to look for
2027 * @return $value Mixed value from generic array element
2029 protected final function getGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
2031 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
2034 if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
2036 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' does not exist.');
2041 return $this->genericArray[$keyGroup][$subGroup][$key][$element];
2045 * Checks if a given sub group is valid (array)
2047 * @param $keyGroup Key group to get
2048 * @param $subGroup Sub group for the key
2049 * @return $isValid Whether given sub group is valid
2051 protected final function isValidGenericArrayGroup ($keyGroup, $subGroup) {
2053 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
2056 $isValid = (($this->isGenericArrayGroupSet($keyGroup, $subGroup)) && (is_array($this->getGenericSubArray($keyGroup, $subGroup))));
2063 * Checks if a given key is valid (array)
2065 * @param $keyGroup Key group to get
2066 * @param $subGroup Sub group for the key
2067 * @param $key Key to check
2068 * @return $isValid Whether given sub group is valid
2070 protected final function isValidGenericArrayKey ($keyGroup, $subGroup, $key) {
2072 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
2075 $isValid = (($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) && (is_array($this->getGenericArrayKey($keyGroup, $subGroup, $key))));
2082 * Initializes the web output instance
2086 protected function initWebOutputInstance () {
2087 // Get application instance
2088 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
2090 // Init web output instance
2091 $outputInstance = ObjectFactory::createObjectByConfiguredName('output_class', array($applicationInstance));
2094 $this->setWebOutputInstance($outputInstance);
2098 * Translates boolean true to 'Y' and false to 'N'
2100 * @param $boolean Boolean value
2101 * @return $translated Translated boolean value
2103 public static final function translateBooleanToYesNo (bool $boolean) {
2104 // Make sure it is really boolean
2105 assert(is_bool($boolean));
2108 $translated = ($boolean === true) ? 'Y' : 'N';
2110 // ... and return it
2115 * Creates a full-qualified file name (FQFN) for given file name by adding
2116 * a configured temporary file path to it.
2118 * @param $infoInstance An instance of a SplFileInfo class
2119 * @return $tempInstance An instance of a SplFileInfo class (temporary file)
2120 * @throw PathWriteProtectedException If the path in 'temp_file_path' is write-protected
2121 * @throws FileIoException If the file cannot be written
2123 protected static function createTempPathForFile (SplFileInfo $infoInstance) {
2125 $basePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('temp_file_path');
2127 // Is the path writeable?
2128 if (!is_writable($basePath)) {
2129 // Path is write-protected
2130 throw new PathWriteProtectedException($infoInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
2134 $tempInstance = new SplFileInfo($basePath . DIRECTORY_SEPARATOR . $infoInstance->getBasename());
2137 if (!FrameworkBootstrap::isReachableFilePath($tempInstance)) {
2139 throw new FileIoException($tempInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
2143 return $tempInstance;
2147 * "Getter" for a printable state name
2149 * @return $stateName Name of the node's state in a printable format
2151 public final function getPrintableState () {
2152 // Default is 'null'
2153 $stateName = 'null';
2155 // Get the state instance
2156 $stateInstance = $this->getStateInstance();
2158 // Is it an instance of Stateable?
2159 if ($stateInstance instanceof Stateable) {
2160 // Then use that state name
2161 $stateName = $stateInstance->getStateName();