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\Criteria\Local\LocalUpdateCriteria;
10 use Org\Mxchange\CoreFramework\Crypto\Cryptable;
11 use Org\Mxchange\CoreFramework\Database\Frontend\DatabaseWrapper;
12 use Org\Mxchange\CoreFramework\EntryPoint\ApplicationEntryPoint;
13 use Org\Mxchange\CoreFramework\Factory\Database\Wrapper\DatabaseWrapperFactory;
14 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
15 use Org\Mxchange\CoreFramework\Filesystem\PathWriteProtectedException;
16 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
17 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
18 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
19 use Org\Mxchange\CoreFramework\Handler\Handleable;
20 use Org\Mxchange\CoreFramework\Helper\Helper;
21 use Org\Mxchange\CoreFramework\Lists\Listable;
22 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
23 use Org\Mxchange\CoreFramework\Localization\ManageableLanguage;
24 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
25 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
26 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
27 use Org\Mxchange\CoreFramework\Result\Database\CachedDatabaseResult;
28 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
29 use Org\Mxchange\CoreFramework\State\Stateable;
30 use Org\Mxchange\CoreFramework\Stream\Input\InputStream;
31 use Org\Mxchange\CoreFramework\Stream\Output\OutputStreamer;
32 use Org\Mxchange\CoreFramework\Stream\Output\OutputStream;
33 use Org\Mxchange\CoreFramework\User\ManageableAccount;
34 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
38 use \InvalidArgumentException;
44 * The simulator system class is the super class of all other classes. This
45 * class handles saving of games etc.
47 * @author Roland Haeder <webmaster@shipsimu.org>
49 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
50 * @license GNU GPL 3.0 or any newer version
51 * @link http://www.shipsimu.org
53 * This program is free software: you can redistribute it and/or modify
54 * it under the terms of the GNU General Public License as published by
55 * the Free Software Foundation, either version 3 of the License, or
56 * (at your option) any later version.
58 * This program is distributed in the hope that it will be useful,
59 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 * GNU General Public License for more details.
63 * You should have received a copy of the GNU General Public License
64 * along with this program. If not, see <http://www.gnu.org/licenses/>.
66 abstract class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
68 * Length of output from hash()
70 private static $hashLength = NULL;
73 * Self-referencing instance
75 private static $instance = NULL;
80 private $realClass = __CLASS__;
83 * Search criteria instance
85 private $searchInstance = NULL;
88 * Update criteria instance
90 private $updateInstance = NULL;
93 * Database result instance
95 private $resultInstance = NULL;
98 * Instance for user class
100 private $userInstance = NULL;
103 * Instance of a crypto helper
105 private $cryptoInstance = NULL;
108 * Instance of an Iterator class
110 private $iteratorInstance = NULL;
113 * Instance of the list
115 private $listInstance = NULL;
118 * A helper instance for the form
120 private $helperInstance = NULL;
123 * An instance of a InputStream class
125 private $inputStreamInstance = NULL;
128 * An instance of a OutputStream class
130 private $outputStreamInstance = NULL;
135 private $handlerInstance = NULL;
138 * An instance of a database wrapper class
140 private $wrapperInstance = NULL;
145 private $stateInstance = NULL;
150 private $callbackInstance = NULL;
155 private $genericArray = array();
157 /***********************
158 * Exception codes.... *
159 ***********************/
161 // @todo Try to clean these constants up
162 const EXCEPTION_IS_NULL_POINTER = 0x001;
163 const EXCEPTION_IS_NO_OBJECT = 0x002;
164 const EXCEPTION_IS_NO_ARRAY = 0x003;
165 const EXCEPTION_MISSING_METHOD = 0x004;
166 const EXCEPTION_CLASSES_NOT_MATCHING = 0x005;
167 const EXCEPTION_INDEX_OUT_OF_BOUNDS = 0x006;
168 const EXCEPTION_DIMENSION_ARRAY_INVALID = 0x007;
169 const EXCEPTION_ITEM_NOT_TRADEABLE = 0x008;
170 const EXCEPTION_ITEM_NOT_IN_PRICE_LIST = 0x009;
171 const EXCEPTION_GENDER_IS_WRONG = 0x00a;
172 const EXCEPTION_BIRTH_DATE_IS_INVALID = 0x00b;
173 const EXCEPTION_EMPTY_STRUCTURES_ARRAY = 0x00c;
174 const EXCEPTION_HAS_ALREADY_PERSONELL_LIST = 0x00d;
175 const EXCEPTION_NOT_ENOUGTH_UNEMPLOYEES = 0x00e;
176 const EXCEPTION_TOTAL_PRICE_NOT_CALCULATED = 0x00f;
177 const EXCEPTION_HARBOR_HAS_NO_SHIPYARDS = 0x010;
178 const EXCEPTION_CONTRACT_PARTNER_INVALID = 0x011;
179 const EXCEPTION_CONTRACT_PARTNER_MISMATCH = 0x012;
180 const EXCEPTION_CONTRACT_ALREADY_SIGNED = 0x013;
181 const EXCEPTION_UNEXPECTED_EMPTY_STRING = 0x014;
182 const EXCEPTION_PATH_NOT_FOUND = 0x015;
183 const EXCEPTION_INVALID_PATH_NAME = 0x016;
184 const EXCEPTION_READ_PROTECED_PATH = 0x017;
185 const EXCEPTION_WRITE_PROTECED_PATH = 0x018;
186 const EXCEPTION_DIR_POINTER_INVALID = 0x019;
187 const EXCEPTION_FILE_POINTER_INVALID = 0x01a;
188 const EXCEPTION_INVALID_RESOURCE = 0x01b;
189 const EXCEPTION_UNEXPECTED_OBJECT = 0x01c;
190 const EXCEPTION_LIMIT_ELEMENT_IS_UNSUPPORTED = 0x01d;
191 const EXCEPTION_GETTER_IS_MISSING = 0x01e;
192 const EXCEPTION_ARRAY_EXPECTED = 0x01f;
193 const EXCEPTION_ARRAY_HAS_INVALID_COUNT = 0x020;
194 const EXCEPTION_ID_IS_INVALID_FORMAT = 0x021;
195 const EXCEPTION_MD5_CHECKSUMS_MISMATCH = 0x022;
196 const EXCEPTION_UNEXPECTED_STRING_SIZE = 0x023;
197 const EXCEPTION_SIMULATOR_ID_INVALID = 0x024;
198 const EXCEPTION_MISMATCHING_COMPRESSORS = 0x025;
199 const EXCEPTION_CONTAINER_ITEM_IS_NULL = 0x026;
200 const EXCEPTION_ITEM_IS_NO_ARRAY = 0x027;
201 const EXCEPTION_CONTAINER_MAYBE_DAMAGED = 0x028;
202 const EXCEPTION_INVALID_STRING = 0x029;
203 const EXCEPTION_VARIABLE_NOT_SET = 0x02a;
204 const EXCEPTION_ATTRIBUTES_ARE_MISSING = 0x02b;
205 const EXCEPTION_ARRAY_ELEMENTS_MISSING = 0x02c;
206 const EXCEPTION_TEMPLATE_ENGINE_UNSUPPORTED = 0x02d;
207 const EXCEPTION_UNSPPORTED_OPERATION = 0x02e;
208 const EXCEPTION_FACTORY_REQUIRE_PARAMETER = 0x02f;
209 const EXCEPTION_MISSING_ELEMENT = 0x030;
210 const EXCEPTION_HEADERS_ALREADY_SENT = 0x031;
211 const EXCEPTION_DEFAULT_CONTROLLER_GONE = 0x032;
212 const EXCEPTION_CLASS_NOT_FOUND = 0x033;
213 const EXCEPTION_REQUIRED_INTERFACE_MISSING = 0x034;
214 const EXCEPTION_FATAL_ERROR = 0x035;
215 const EXCEPTION_FILE_NOT_FOUND = 0x036;
216 const EXCEPTION_ASSERTION_FAILED = 0x037;
217 const EXCEPTION_FILE_NOT_REACHABLE = 0x038;
218 const EXCEPTION_FILE_CANNOT_BE_READ = 0x039;
219 const EXCEPTION_FILE_CANNOT_BE_WRITTEN = 0x03a;
220 const EXCEPTION_PATH_CANNOT_BE_WRITTEN = 0x03b;
221 const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x03c;
222 const EXCEPTION_FILTER_CHAIN_INTERCEPTED = 0x03d;
223 const EXCEPTION_INVALID_SOCKET = 0x03e;
224 const EXCEPTION_SELF_INSTANCE = 0x03f;
227 * Startup time in miliseconds
229 private static $startupTime = 0;
232 * Protected super constructor
234 * @param $className Name of the class
237 protected function __construct (string $className) {
239 $this->setRealClass($className);
241 // Is the startup time set? (0 cannot be true anymore)
242 if (self::$startupTime == 0) {
244 self::$startupTime = microtime(true);
249 * Destructor for all classes. You should not call this method on your own.
253 public function __destruct () {
254 // Flush any updated entries to the database
255 $this->flushPendingUpdates();
257 // Is this object already destroyed?
258 if ($this->__toString() != 'DestructedObject') {
259 // Destroy all informations about this class but keep some text about it alive
260 $this->setRealClass('DestructedObject');
261 } elseif ((defined('DEBUG_DESTRUCTOR')) && (is_object($this->getDebugInstance()))) {
262 // Already destructed object
263 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('The object <span class="object_name">%s</span> is already destroyed.',
267 // Do not call this twice
268 trigger_error(__METHOD__ . ': Called twice.');
274 * The __call() method where all non-implemented methods end up
276 * @param $methodName Name of the missing method
277 * @args $args Arguments passed to the method
280 public final function __call ($methodName, $args) {
282 self::$instance = $this;
284 // Call static method
285 self::__callStatic($methodName, $args);
287 // Clear self-instance
288 self::$instance = NULL;
292 * The __callStatic() method where all non-implemented static methods end up
294 * @param $methodName Name of the missing method
295 * @param $args Arguments passed to the method
297 * @throws InvalidArgumentException If self::$instance is not a framework's own object
299 public static final function __callStatic ($methodName, $args) {
301 //* PRINT-DEBUG: */ printf('[%s:%d]: methodName=%s,args[]=%s - CALLED!' . PHP_EOL, __METHOD__, __LINE__, $methodName, gettype($args));
303 // Init argument string and class name
305 $className = 'unknown';
307 // Is self-instance set?
308 if (self::$instance instanceof FrameworkInterface) {
309 // Framework's own instance
310 $className = self::$instance->__toString();
311 } elseif (!is_null(self::$instance)) {
313 throw new InvalidArgumentException(sprintf('self::instance[%s] is not expected.', gettype(self::$instance)), self::EXCEPTION_SELF_INSTANCE);
316 // Is it NULL, empty or an array?
317 if (is_null($args)) {
319 $argsString = 'NULL';
320 } elseif (is_array($args)) {
324 // Some arguments are there
325 foreach ($args as $arg) {
326 // Add data about the argument
327 $argsString .= gettype($arg) . ':';
330 // Found a NULL argument
331 $argsString .= 'NULL';
332 } elseif (is_string($arg)) {
333 // Add length for strings
334 $argsString .= strlen($arg);
335 } elseif ((is_int($arg)) || (is_float($arg))) {
338 } elseif (is_array($arg)) {
339 // .. or size if array
340 $argsString .= count($arg);
341 } elseif (is_object($arg)) {
343 $reflection = new ReflectionClass($arg);
345 // Is an other object, maybe no __toString() available
346 $argsString .= $reflection->getName();
347 } elseif ($arg === true) {
348 // ... is boolean 'true'
349 $argsString .= 'true';
350 } elseif ($arg === false) {
351 // ... is boolean 'false'
352 $argsString .= 'false';
355 // Comma for next one
360 if (substr($argsString, -2, 1) == ',') {
362 $argsString = substr($argsString, 0, -2);
368 // Invalid arguments!
369 $argsString = sprintf('!INVALID:%s!', gettype($args));
372 // Output stub message
373 // @TODO __CLASS__ does always return BaseFrameworkSystem but not the extending (=child) class
374 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s::%s]: Stub! Args: %s',
385 * Getter for $realClass
387 * @return $realClass The name of the real class (not BaseFrameworkSystem)
389 public function __toString () {
390 return $this->realClass;
394 * Magic method to catch setting of missing but set class fields/attributes
396 * @param $name Name of the field/attribute
397 * @param $value Value to store
400 public final function __set ($name, $value) {
401 $this->debugBackTrace(sprintf('Tried to set a missing field. name=%s, value[%s]=%s',
404 print_r($value, true)
409 * Magic method to catch getting of missing fields/attributes
411 * @param $name Name of the field/attribute
414 public final function __get ($name) {
415 $this->debugBackTrace(sprintf('Tried to get a missing field. name=%s',
421 * Magic method to catch unsetting of missing fields/attributes
423 * @param $name Name of the field/attribute
426 public final function __unset ($name) {
427 $this->debugBackTrace(sprintf('Tried to unset a missing field. name=%s',
433 * Magic method to catch object serialization
435 * @return $unsupported Unsupported method
436 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
438 public final function __sleep () {
439 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
443 * Magic method to catch object deserialization
445 * @return $unsupported Unsupported method
446 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
448 public final function __wakeup () {
449 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
453 * Magic method to catch calls when an object instance is called
455 * @return $unsupported Unsupported method
456 * @throws UnsupportedOperationException Objects of this framework cannot be serialized
458 public final function __invoke () {
459 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
463 * Setter for the real class name
465 * @param $realClass Class name (string)
468 public final function setRealClass ($realClass) {
470 $this->realClass = (string) $realClass;
474 * Setter for database result instance
476 * @param $resultInstance An instance of a database result class
478 * @todo SearchableResult and UpdateableResult shall have a super interface to use here
480 protected final function setResultInstance (SearchableResult $resultInstance) {
481 $this->resultInstance = $resultInstance;
485 * Getter for database result instance
487 * @return $resultInstance An instance of a database result class
489 public final function getResultInstance () {
490 return $this->resultInstance;
494 * Setter for search instance
496 * @param $searchInstance Searchable criteria instance
499 public final function setSearchInstance (LocalSearchCriteria $searchInstance) {
500 $this->searchInstance = $searchInstance;
504 * Getter for search instance
506 * @return $searchInstance Searchable criteria instance
508 public final function getSearchInstance () {
509 return $this->searchInstance;
513 * Setter for update instance
515 * @param $updateInstance An instance of a LocalUpdateCriteria clase
518 public final function setUpdateInstance (LocalUpdateCriteria $updateInstance) {
519 $this->updateInstance = $updateInstance;
523 * Getter for update instance
525 * @return $updateInstance Updateable criteria instance
527 public final function getUpdateInstance () {
528 return $this->updateInstance;
532 * Setter for debug instance
534 * @param $debugInstance The instance for debug output class
537 public final function setDebugInstance (DebugMiddleware $debugInstance) {
538 GenericRegistry::getRegistry()->addInstance('debug', $debugInstance);
542 * Getter for debug instance
544 * @return $debugInstance Instance to class DebugConsoleOutput or DebugWebOutput
546 public final function getDebugInstance () {
547 // Get debug instance
548 $debugInstance = GenericRegistry::getRegistry()->getInstance('debug');
551 return $debugInstance;
555 * Setter for web output instance
557 * @param $webInstance The instance for web output class
560 public final function setWebOutputInstance (OutputStreamer $webInstance) {
561 GenericRegistry::getRegistry()->addInstance('web_output', $webInstance);
565 * Getter for web output instance
567 * @return $webOutputInstance - Instance to class WebOutput
569 public final function getWebOutputInstance () {
570 $webOutputInstance = GenericRegistry::getRegistry()->getInstance('web_output');
571 return $webOutputInstance;
575 * Private getter for language instance
577 * @return $langInstance An instance to the language sub-system
579 protected final function getLanguageInstance () {
580 $langInstance = GenericRegistry::getRegistry()->getInstance('language');
581 return $langInstance;
585 * Setter for language instance
587 * @param $langInstance An instance to the language sub-system
589 * @see LanguageSystem
591 public final function setLanguageInstance (ManageableLanguage $langInstance) {
592 GenericRegistry::getRegistry()->addInstance('language', $langInstance);
596 * Protected setter for user instance
598 * @param $userInstance An instance of a user class
601 protected final function setUserInstance (ManageableAccount $userInstance) {
602 $this->userInstance = $userInstance;
606 * Getter for user instance
608 * @return $userInstance An instance of a user class
610 public final function getUserInstance () {
611 return $this->userInstance;
615 * Setter for Cryptable instance
617 * @param $cryptoInstance An instance of a Cryptable class
620 protected final function setCryptoInstance (Cryptable $cryptoInstance) {
621 $this->cryptoInstance = $cryptoInstance;
625 * Getter for Cryptable instance
627 * @return $cryptoInstance An instance of a Cryptable class
629 public final function getCryptoInstance () {
630 return $this->cryptoInstance;
634 * Setter for the list instance
636 * @param $listInstance A list of Listable
639 protected final function setListInstance (Listable $listInstance) {
640 $this->listInstance = $listInstance;
644 * Getter for the list instance
646 * @return $listInstance A list of Listable
648 protected final function getListInstance () {
649 return $this->listInstance;
653 * Setter for DatabaseWrapper instance
655 * @param $wrapperInstance An instance of an DatabaseWrapper
658 public final function setWrapperInstance (DatabaseWrapper $wrapperInstance) {
659 $this->wrapperInstance = $wrapperInstance;
663 * Getter for DatabaseWrapper instance
665 * @return $wrapperInstance An instance of an DatabaseWrapper
667 public final function getWrapperInstance () {
668 return $this->wrapperInstance;
672 * Setter for helper instance
674 * @param $helperInstance An instance of a helper class
677 protected final function setHelperInstance (Helper $helperInstance) {
678 $this->helperInstance = $helperInstance;
682 * Getter for helper instance
684 * @return $helperInstance An instance of a helper class
686 public final function getHelperInstance () {
687 return $this->helperInstance;
691 * Getter for a InputStream instance
693 * @param $inputStreamInstance The InputStream instance
695 protected final function getInputStreamInstance () {
696 return $this->inputStreamInstance;
700 * Setter for a InputStream instance
702 * @param $inputStreamInstance The InputStream instance
705 protected final function setInputStreamInstance (InputStream $inputStreamInstance) {
706 $this->inputStreamInstance = $inputStreamInstance;
710 * Getter for a OutputStream instance
712 * @param $outputStreamInstance The OutputStream instance
714 protected final function getOutputStreamInstance () {
715 return $this->outputStreamInstance;
719 * Setter for a OutputStream instance
721 * @param $outputStreamInstance The OutputStream instance
724 protected final function setOutputStreamInstance (OutputStream $outputStreamInstance) {
725 $this->outputStreamInstance = $outputStreamInstance;
729 * Setter for handler instance
731 * @param $handlerInstance An instance of a Handleable class
734 protected final function setHandlerInstance (Handleable $handlerInstance) {
735 $this->handlerInstance = $handlerInstance;
739 * Getter for handler instance
741 * @return $handlerInstance A Handleable instance
743 protected final function getHandlerInstance () {
744 return $this->handlerInstance;
748 * Setter for Iterator instance
750 * @param $iteratorInstance An instance of an Iterator
753 protected final function setIteratorInstance (Iterator $iteratorInstance) {
754 $this->iteratorInstance = $iteratorInstance;
758 * Getter for Iterator instance
760 * @return $iteratorInstance An instance of an Iterator
762 public final function getIteratorInstance () {
763 return $this->iteratorInstance;
767 * Setter for state instance
769 * @param $stateInstance A Stateable instance
772 public final function setStateInstance (Stateable $stateInstance) {
773 $this->stateInstance = $stateInstance;
777 * Getter for state instance
779 * @return $stateInstance A Stateable instance
781 public final function getStateInstance () {
782 return $this->stateInstance;
786 * Setter for call-back instance
788 * @param $callbackInstance An instance of a FrameworkInterface class
791 public final function setCallbackInstance (FrameworkInterface $callbackInstance) {
792 $this->callbackInstance = $callbackInstance;
796 * Getter for call-back instance
798 * @return $callbackInstance An instance of a FrameworkInterface class
800 protected final function getCallbackInstance () {
801 return $this->callbackInstance;
805 * Checks whether an object equals this object. You should overwrite this
806 * method to implement own equality checks
808 * @param $objectInstance An instance of a FrameworkInterface object
809 * @return $equals Whether both objects equals
811 public function equals (FrameworkInterface $objectInstance) {
814 $this->__toString() == $objectInstance->__toString()
816 $this->hashCode() == $objectInstance->hashCode()
824 * Generates a generic hash code of this class. You should really overwrite
825 * this method with your own hash code generator code. But keep KISS in mind.
827 * @return $hashCode A generic hash code respresenting this whole class
829 public function hashCode () {
831 return crc32($this->__toString());
835 * Appends a trailing slash to a string
837 * @param $str A string (maybe) without trailing slash
838 * @return $str A string with an auto-appended trailing slash
840 public final function addMissingTrailingSlash ($str) {
841 // Is there a trailing slash?
842 if (substr($str, -1, 1) != '/') {
846 // Return string with trailing slash
851 * Debugs this instance by putting out it's full content
853 * @param $message Optional message to show in debug output
856 public final function debugInstance ($message = '') {
857 // Restore the error handler to avoid trouble with missing array elements or undeclared variables
858 restore_error_handler();
864 if (!empty($message)) {
866 $content = sprintf('<div class="debug_message">
868 </div>' . PHP_EOL, $message);
871 // Generate the output
872 $content .= sprintf('<pre>%s</pre>',
881 ApplicationEntryPoint::exitApplication(sprintf('<div class="debug_header">
884 <div class="debug_content">
888 <div class="debug_include_list">
893 ClassLoader::getSelfInstance()->getPrintableIncludeList()
898 * Replaces control characters with printable output
900 * @param $str String with control characters
901 * @return $str Replaced string
903 protected function replaceControlCharacters ($str) {
906 chr(13), '[r]', str_replace(
907 chr(10), '[n]', str_replace(
917 * Output a partial stub message for the caller method
919 * @param $message An optional message to display
922 protected function partialStub ($message = '') {
924 $stubMessage = 'Partial stub!';
926 // Is an extra message given?
927 if (!empty($message)) {
928 // Then add it as well
929 $stubMessage .= ' Message: ' . $message;
932 // Debug instance is there?
933 if (!is_null($this->getDebugInstance())) {
934 // Output stub message
935 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($stubMessage);
938 trigger_error($stubMessage);
944 * Outputs a debug backtrace and stops further script execution
946 * @param $message An optional message to output
947 * @param $doExit Whether exit the program (true is default)
950 public function debugBackTrace ($message = '', $doExit = true) {
951 // Sorry, there is no other way getting this nice backtrace
952 if (!empty($message)) {
954 printf('Message: %s<br />' . PHP_EOL, $message);
958 debug_print_backtrace();
962 if ($doExit === true) {
968 * Creates an instance of a debugger instance
970 * @param $className Name of the class (currently unsupported)
971 * @param $lineNumber Line number where the call was made
972 * @return $debugInstance An instance of a debugger class
973 * @deprecated Not fully, as the new Logger facilities are not finished yet.
975 public final static function createDebugInstance ($className, $lineNumber = NULL) {
976 // Is the instance set?
977 if (!GenericRegistry::getRegistry()->instanceExists('debug')) {
978 // Init debug instance
979 $debugInstance = NULL;
983 // Get a debugger instance
984 $debugInstance = DebugMiddleware::createDebugMiddleware(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_class'), $className);
985 } catch (NullPointerException $e) {
986 // Didn't work, no instance there
987 exit(sprintf('Cannot create debugInstance! Exception=%s,message=%s,className=%s,lineNumber=%d' . PHP_EOL, $e->__toString(), $e->getMessage(), $className, $lineNumber));
990 // Empty string should be ignored and used for testing the middleware
991 DebugMiddleware::getSelfInstance()->output('');
993 // Set it in registry
994 GenericRegistry::getRegistry()->addInstance('debug', $debugInstance);
996 // Get instance from registry
997 $debugInstance = GenericRegistry::getRegistry()->getDebugInstance();
1001 return $debugInstance;
1005 * Simple output of a message with line-break
1007 * @param $message Message to output
1010 public function outputLine ($message) {
1012 print($message . PHP_EOL);
1016 * Outputs a debug message whether to debug instance (should be set!) or
1017 * dies with or ptints the message. Do NEVER EVER rewrite the exit() call to
1018 * ApplicationEntryPoint::app_exit(), this would cause an endless loop.
1020 * @param $message Message we shall send out...
1021 * @param $doPrint Whether print or die here (default: print)
1022 * @paran $stripTags Whether to strip tags (default: false)
1025 public function debugOutput ($message, $doPrint = true, $stripTags = false) {
1026 // Set debug instance to NULL
1027 $debugInstance = NULL;
1030 $backtrace = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT);
1032 // Is function partialStub/__callStatic ?
1033 if (in_array($backtrace[1]['function'], array('partialStub', '__call', '__callStatic'))) {
1034 // Prepend class::function:line from 3rd element
1035 $message = sprintf('[%s::%s:%d]: %s',
1036 $backtrace[2]['class'],
1037 $backtrace[2]['function'],
1038 (isset($backtrace[2]['line']) ? $backtrace[2]['line'] : '0'),
1042 // Prepend class::function:line from 2nd element
1043 $message = sprintf('[%s::%s:%d]: %s',
1044 $backtrace[1]['class'],
1045 $backtrace[1]['function'],
1046 (isset($backtrace[1]['line']) ? $backtrace[1]['line'] : '0'),
1053 // Get debug instance
1054 $debugInstance = $this->getDebugInstance();
1055 } catch (NullPointerException $e) {
1056 // The debug instance is not set (yet)
1059 // Is the debug instance there?
1060 if (is_object($debugInstance)) {
1061 // Use debug output handler
1062 $debugInstance->output($message, $stripTags);
1064 if ($doPrint === false) {
1065 // Die here if not printed
1069 // Are debug times enabled?
1070 if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('debug_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_output_timings') == 'Y') {
1072 $message = $this->getPrintableExecutionTime() . $message;
1076 if ($doPrint === true) {
1078 $this->outputLine($message);
1087 * Marks up the code by adding e.g. line numbers
1089 * @param $phpCode Unmarked PHP code
1090 * @return $markedCode Marked PHP code
1092 public function markupCode ($phpCode) {
1097 $errorArray = error_get_last();
1099 // Init the code with error message
1100 if (is_array($errorArray)) {
1102 $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>',
1103 basename($errorArray['file']),
1104 $errorArray['line'],
1105 $errorArray['message'],
1110 // Add line number to the code
1111 foreach (explode(chr(10), $phpCode) as $lineNo => $code) {
1113 $markedCode .= sprintf('<span id="code_line">%s</span>: %s' . PHP_EOL,
1115 htmlentities($code, ENT_QUOTES)
1124 * Filter a given GMT timestamp (non Uni* stamp!) to make it look more
1125 * beatiful for web-based front-ends. If null is given a message id
1126 * null_timestamp will be resolved and returned.
1128 * @param $timestamp Timestamp to prepare (filter) for display
1129 * @return $readable A readable timestamp
1131 public function doFilterFormatTimestamp ($timestamp) {
1132 // Default value to return
1135 // Is the timestamp null?
1136 if (is_null($timestamp)) {
1137 // Get a message string
1138 $readable = $this->getLanguageInstance()->getMessage('null_timestamp');
1140 switch ($this->getLanguageInstance()->getLanguageCode()) {
1141 case 'de': // German format is a bit different to default
1142 // Split the GMT stamp up
1143 $dateTime = explode(' ', $timestamp );
1144 $dateArray = explode('-', $dateTime[0]);
1145 $timeArray = explode(':', $dateTime[1]);
1147 // Construct the timestamp
1148 $readable = sprintf(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('german_date_time'),
1158 default: // Default is pass-through
1159 $readable = $timestamp;
1169 * Filter a given number into a localized number
1171 * @param $value The raw value from e.g. database
1172 * @return $localized Localized value
1174 public function doFilterFormatNumber ($value) {
1175 // Generate it from config and localize dependencies
1176 switch ($this->getLanguageInstance()->getLanguageCode()) {
1177 case 'de': // German format is a bit different to default
1178 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), ',', '.');
1181 default: // US, etc.
1182 $localized = number_format($value, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('decimals'), '.', ',');
1191 * "Getter" for databse entry
1193 * @return $entry An array with database entries
1194 * @throws NullPointerException If the database result is not found
1195 * @throws InvalidDatabaseResultException If the database result is invalid
1197 protected final function getDatabaseEntry () {
1198 // Is there an instance?
1199 if (!$this->getResultInstance() instanceof SearchableResult) {
1200 // Throw an exception here
1201 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1205 $this->getResultInstance()->rewind();
1207 // Do we have an entry?
1208 if ($this->getResultInstance()->valid() === false) {
1209 // @TODO Move the constant to e.g. BaseDatabaseResult when there is a non-cached database result available
1210 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), CachedDatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
1214 $this->getResultInstance()->next();
1217 $entry = $this->getResultInstance()->current();
1224 * Getter for field name
1226 * @param $fieldName Field name which we shall get
1227 * @return $fieldValue Field value from the user
1228 * @throws NullPointerException If the result instance is null
1230 public final function getField (string $fieldName) {
1231 // Default field value
1234 // Get result instance
1235 $resultInstance = $this->getResultInstance();
1237 // Is this instance null?
1238 if (is_null($resultInstance)) {
1239 // Then the user instance is no longer valid (expired cookies?)
1240 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1243 // Get current array
1244 $fieldArray = $resultInstance->current();
1245 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($fieldName.':<pre>'.print_r($fieldArray, true).'</pre>');
1247 // Convert dashes to underscore
1248 $fieldName2 = StringUtils::convertDashesToUnderscores($fieldName);
1250 // Does the field exist?
1251 if ($this->isFieldSet($fieldName)) {
1253 $fieldValue = $fieldArray[$fieldName2];
1254 } elseif (defined('DEVELOPER')) {
1255 // Missing field entry, may require debugging
1256 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldArray<pre>=' . print_r($fieldArray, true) . '</pre>,fieldName=' . $fieldName . ' not found!');
1258 // Missing field entry, may require debugging
1259 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldName=' . $fieldName . ' not found!');
1267 * Checks if given field is set
1269 * @param $fieldName Field name to check
1270 * @return $isSet Whether the given field name is set
1271 * @throws NullPointerException If the result instance is null
1273 public function isFieldSet (string $fieldName) {
1274 // Get result instance
1275 $resultInstance = $this->getResultInstance();
1277 // Is this instance null?
1278 if (is_null($resultInstance)) {
1279 // Then the user instance is no longer valid (expired cookies?)
1280 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
1283 // Get current array
1284 $fieldArray = $resultInstance->current();
1285 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('[' . $this->__toString() . ':' . __LINE__ . '] fieldName=' . $fieldName . ',fieldArray=<pre>'.print_r($fieldArray, true).'</pre>');
1287 // Convert dashes to underscore
1288 $fieldName = StringUtils::convertDashesToUnderscores($fieldName);
1291 $isSet = isset($fieldArray[$fieldName]);
1298 * Flushs all pending updates to the database layer
1302 public function flushPendingUpdates () {
1303 // Get result instance
1304 $resultInstance = $this->getResultInstance();
1306 // Do we have data to update?
1307 if ((is_object($resultInstance)) && ($resultInstance->ifDataNeedsFlush())) {
1308 // Get wrapper class name config entry
1309 $configEntry = $resultInstance->getUpdateInstance()->getWrapperConfigEntry();
1311 // Create object instance
1312 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName($configEntry);
1314 // Yes, then send the whole result to the database layer
1315 $wrapperInstance->doUpdateByResult($this->getResultInstance());
1320 * Outputs a deprecation warning to the developer.
1322 * @param $message The message we shall output to the developer
1324 * @todo Write a logging mechanism for productive mode
1326 public function deprecationWarning ($message) {
1327 // Is developer mode active?
1328 if (defined('DEVELOPER')) {
1329 // Debug instance is there?
1330 if (!is_null($this->getDebugInstance())) {
1331 // Output stub message
1332 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput($message);
1335 trigger_error($message . "<br />\n");
1339 // @TODO Finish this part!
1340 $this->partialStub('Developer mode inactive. Message:' . $message);
1345 * Checks whether the given PHP extension is loaded
1347 * @param $phpExtension The PHP extension we shall check
1348 * @return $isLoaded Whether the PHP extension is loaded
1350 public final function isPhpExtensionLoaded ($phpExtension) {
1352 $isLoaded = in_array($phpExtension, get_loaded_extensions());
1359 * "Getter" as a time() replacement but with milliseconds. You should use this
1360 * method instead of the encapsulated getimeofday() function.
1362 * @return $milliTime Timestamp with milliseconds
1364 public function getMilliTime () {
1365 // Get the time of day as float
1366 $milliTime = gettimeofday(true);
1373 * Idles (sleeps) for given milliseconds
1375 * @return $hasSlept Whether it goes fine
1377 public function idle ($milliSeconds) {
1378 // Sleep is fine by default
1381 // Idle so long with found function
1382 if (function_exists('time_sleep_until')) {
1383 // Get current time and add idle time
1384 $sleepUntil = $this->getMilliTime() + abs($milliSeconds) / 1000;
1386 // New PHP 5.1.0 function found, ignore errors
1387 $hasSlept = @time_sleep_until($sleepUntil);
1390 * My Sun station doesn't have that function even with latest PHP
1393 usleep($milliSeconds * 1000);
1401 * Checks whether the given encoded data was encoded with Base64
1403 * @param $encodedData Encoded data we shall check
1404 * @return $isBase64 Whether the encoded data is Base64
1406 protected function isBase64Encoded ($encodedData) {
1408 $isBase64 = (@base64_decode($encodedData, true) !== false);
1415 * Gets a cache key from Criteria instance
1417 * @param $criteriaInstance An instance of a Criteria class
1418 * @param $onlyKeys Only use these keys for a cache key
1419 * @return $cacheKey A cache key suitable for lookup/storage purposes
1421 protected function getCacheKeyByCriteria (Criteria $criteriaInstance, array $onlyKeys = array()) {
1423 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRAMEWORK-SYSTEM: criteriaInstance=' . $criteriaInstance->__toString() . ',onlyKeys()=' . count($onlyKeys) . ' - CALLED!');
1424 $cacheKey = sprintf('%s@%s',
1425 $this->__toString(),
1426 $criteriaInstance->getCacheKey($onlyKeys)
1430 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-FRAMEWORK-SYSTEM: cacheKey=' . $cacheKey . ' - EXIT!');
1435 * Getter for startup time in miliseconds
1437 * @return $startupTime Startup time in miliseconds
1439 protected function getStartupTime () {
1440 return self::$startupTime;
1444 * "Getter" for a printable currently execution time in nice braces
1446 * @return $executionTime Current execution time in nice braces
1448 protected function getPrintableExecutionTime () {
1449 // Caculate the execution time
1450 $executionTime = microtime(true) - $this->getStartupTime();
1452 // Pack it in nice braces
1453 $executionTime = sprintf('[ %01.5f ] ', $executionTime);
1456 return $executionTime;
1460 * Hashes a given string with a simple but stronger hash function (no salt)
1461 * and hex-encode it.
1463 * @param $str The string to be hashed
1464 * @return $hash The hash from string $str
1466 public static final function hash ($str) {
1467 // Hash given string with (better secure) hasher
1468 $hash = bin2hex(mhash(MHASH_SHA256, $str));
1475 * "Getter" for length of hash() output. This will be "cached" to speed up
1478 * @return $length Length of hash() output
1480 public static final function getHashLength () {
1482 if (is_null(self::$hashLength)) {
1483 // No, then hash a string and save its length.
1484 self::$hashLength = strlen(self::hash('abc123'));
1488 return self::$hashLength;
1492 * Checks whether the given number is really a number (only chars 0-9).
1494 * @param $num A string consisting only chars between 0 and 9
1495 * @param $castValue Whether to cast the value to double. Do only use this to secure numbers from Requestable classes.
1496 * @param $assertMismatch Whether to assert mismatches
1497 * @return $ret The (hopefully) secured numbered value
1499 public function bigintval ($num, $castValue = true, $assertMismatch = false) {
1500 // Filter all numbers out
1501 $ret = preg_replace('/[^0123456789]/', '', $num);
1504 if ($castValue === true) {
1505 // Cast to biggest numeric type
1506 $ret = (double) $ret;
1509 // Assert only if requested
1510 if ($assertMismatch === true) {
1511 // Has the whole value changed?
1512 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
1520 * Checks whether the given hexadecimal number is really a hex-number (only chars 0-9,a-f).
1522 * @param $num A string consisting only chars between 0 and 9
1523 * @param $assertMismatch Whether to assert mismatches
1524 * @return $ret The (hopefully) secured hext-numbered value
1526 public function hexval ($num, $assertMismatch = false) {
1527 // Filter all numbers out
1528 $ret = preg_replace('/[^0123456789abcdefABCDEF]/', '', $num);
1530 // Assert only if requested
1531 if ($assertMismatch === true) {
1532 // Has the whole value changed?
1533 assert(('' . $ret . '' != '' . $num . '') && (!is_null($num)));
1541 * Determines if an element is set in the generic array
1543 * @param $keyGroup Main group for the key
1544 * @param $subGroup Sub group for the key
1545 * @param $key Key to check
1546 * @param $element Element to check
1547 * @return $isset Whether the given key is set
1549 protected final function isGenericArrayElementSet ($keyGroup, $subGroup, $key, $element) {
1551 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
1554 $isset = isset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
1560 * Determines if a key is set in the generic array
1562 * @param $keyGroup Main group for the key
1563 * @param $subGroup Sub group for the key
1564 * @param $key Key to check
1565 * @return $isset Whether the given key is set
1567 protected final function isGenericArrayKeySet ($keyGroup, $subGroup, $key) {
1569 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1572 $isset = isset($this->genericArray[$keyGroup][$subGroup][$key]);
1580 * Determines if a group is set in the generic array
1582 * @param $keyGroup Main group
1583 * @param $subGroup Sub group
1584 * @return $isset Whether the given group is set
1586 protected final function isGenericArrayGroupSet ($keyGroup, $subGroup) {
1588 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
1591 $isset = isset($this->genericArray[$keyGroup][$subGroup]);
1598 * Getter for sub key group
1600 * @param $keyGroup Main key group
1601 * @param $subGroup Sub key group
1602 * @return $array An array with all array elements
1604 protected final function getGenericSubArray ($keyGroup, $subGroup) {
1606 if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
1607 // No, then abort here
1608 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
1613 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',value=' . print_r($this->genericArray[$keyGroup][$subGroup], true));
1616 return $this->genericArray[$keyGroup][$subGroup];
1620 * Unsets a given key in generic array
1622 * @param $keyGroup Main group for the key
1623 * @param $subGroup Sub group for the key
1624 * @param $key Key to unset
1627 protected final function unsetGenericArrayKey ($keyGroup, $subGroup, $key) {
1629 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1632 unset($this->genericArray[$keyGroup][$subGroup][$key]);
1636 * Unsets a given element in generic array
1638 * @param $keyGroup Main group for the key
1639 * @param $subGroup Sub group for the key
1640 * @param $key Key to unset
1641 * @param $element Element to unset
1644 protected final function unsetGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
1646 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
1649 unset($this->genericArray[$keyGroup][$subGroup][$key][$element]);
1653 * Append a string to a given generic array key
1655 * @param $keyGroup Main group for the key
1656 * @param $subGroup Sub group for the key
1657 * @param $key Key to unset
1658 * @param $value Value to add/append
1661 protected final function appendStringToGenericArrayKey ($keyGroup, $subGroup, $key, $value, $appendGlue = '') {
1663 //* 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);
1665 // Is it already there?
1666 if ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1668 $this->genericArray[$keyGroup][$subGroup][$key] .= $appendGlue . (string) $value;
1671 $this->genericArray[$keyGroup][$subGroup][$key] = (string) $value;
1676 * Append a string to a given generic array element
1678 * @param $keyGroup Main group for the key
1679 * @param $subGroup Sub group for the key
1680 * @param $key Key to unset
1681 * @param $element Element to check
1682 * @param $value Value to add/append
1685 protected final function appendStringToGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value, $appendGlue = '') {
1687 //* 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);
1689 // Is it already there?
1690 if ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
1692 $this->genericArray[$keyGroup][$subGroup][$key][$element] .= $appendGlue . (string) $value;
1695 $this->setStringGenericArrayElement($keyGroup, $subGroup, $key, $element, $value);
1700 * Sets a string in a given generic array element
1702 * @param $keyGroup Main group for the key
1703 * @param $subGroup Sub group for the key
1704 * @param $key Key to unset
1705 * @param $element Element to check
1706 * @param $value Value to add/append
1709 protected final function setStringGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value, $appendGlue = '') {
1711 //* 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);
1714 $this->genericArray[$keyGroup][$subGroup][$key][$element] = (string) $value;
1718 * Initializes given generic array group
1720 * @param $keyGroup Main group for the key
1721 * @param $subGroup Sub group for the key
1722 * @param $key Key to use
1723 * @param $forceInit Optionally force initialization
1726 protected final function initGenericArrayGroup ($keyGroup, $subGroup, $forceInit = false) {
1728 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',forceInit=' . intval($forceInit));
1730 // Is it already set?
1731 if (($forceInit === false) && ($this->isGenericArrayGroupSet($keyGroup, $subGroup))) {
1732 // Already initialized
1733 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' already initialized.');
1738 $this->genericArray[$keyGroup][$subGroup] = array();
1742 * Initializes given generic array key
1744 * @param $keyGroup Main group for the key
1745 * @param $subGroup Sub group for the key
1746 * @param $key Key to use
1747 * @param $forceInit Optionally force initialization
1750 protected final function initGenericArrayKey ($keyGroup, $subGroup, $key, $forceInit = false) {
1752 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',forceInit=' . intval($forceInit));
1754 // Is it already set?
1755 if (($forceInit === false) && ($this->isGenericArrayKeySet($keyGroup, $subGroup, $key))) {
1756 // Already initialized
1757 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' already initialized.');
1762 $this->genericArray[$keyGroup][$subGroup][$key] = array();
1766 * Initializes given generic array element
1768 * @param $keyGroup Main group for the key
1769 * @param $subGroup Sub group for the key
1770 * @param $key Key to use
1771 * @param $element Element to use
1772 * @param $forceInit Optionally force initialization
1775 protected final function initGenericArrayElement ($keyGroup, $subGroup, $key, $element, $forceInit = false) {
1777 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ',forceInit=' . intval($forceInit));
1779 // Is it already set?
1780 if (($forceInit === false) && ($this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element))) {
1781 // Already initialized
1782 trigger_error(__METHOD__ . ':keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' already initialized.');
1787 $this->genericArray[$keyGroup][$subGroup][$key][$element] = array();
1791 * Pushes an element to a generic key
1793 * @param $keyGroup Main group for the key
1794 * @param $subGroup Sub group for the key
1795 * @param $key Key to use
1796 * @param $value Value to add/append
1797 * @return $count Number of array elements
1799 protected final function pushValueToGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
1801 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, true));
1804 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1806 $this->initGenericArrayKey($keyGroup, $subGroup, $key);
1810 $count = array_push($this->genericArray[$keyGroup][$subGroup][$key], $value);
1813 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1814 //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
1819 * Pushes an element to a generic array element
1821 * @param $keyGroup Main group for the key
1822 * @param $subGroup Sub group for the key
1823 * @param $key Key to use
1824 * @param $element Element to check
1825 * @param $value Value to add/append
1826 * @return $count Number of array elements
1828 protected final function pushValueToGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
1830 //* 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));
1833 if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
1835 $this->initGenericArrayElement($keyGroup, $subGroup, $key, $element);
1839 $count = array_push($this->genericArray[$keyGroup][$subGroup][$key][$element], $value);
1842 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1843 //* DEBUG: */ print(__METHOD__ . ': count=' . $count . PHP_EOL);
1848 * Pops an element from a generic group
1850 * @param $keyGroup Main group for the key
1851 * @param $subGroup Sub group for the key
1852 * @param $key Key to unset
1853 * @return $value Last "popped" value
1855 protected final function popGenericArrayElement ($keyGroup, $subGroup, $key) {
1857 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1860 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1862 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
1867 $value = array_pop($this->genericArray[$keyGroup][$subGroup][$key]);
1870 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1871 //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, true) . PHP_EOL);
1876 * Shifts an element from a generic group
1878 * @param $keyGroup Main group for the key
1879 * @param $subGroup Sub group for the key
1880 * @param $key Key to unset
1881 * @return $value Last "popped" value
1883 protected final function shiftGenericArrayElement ($keyGroup, $subGroup, $key) {
1885 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1888 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1890 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' not found.');
1895 $value = array_shift($this->genericArray[$keyGroup][$subGroup][$key]);
1898 //* DEBUG: */ print(__METHOD__ . ': genericArray=' . print_r($this->genericArray[$keyGroup][$subGroup][$key], true));
1899 //* DEBUG: */ print(__METHOD__ . ': value[' . gettype($value) . ']=' . print_r($value, true) . PHP_EOL);
1904 * Count generic array group
1906 * @param $keyGroup Main group for the key
1907 * @return $count Count of given group
1909 protected final function countGenericArray ($keyGroup) {
1911 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup);
1914 if (!isset($this->genericArray[$keyGroup])) {
1916 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' not found.');
1921 $count = count($this->genericArray[$keyGroup]);
1924 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',count=' . $count);
1931 * Count generic array sub group
1933 * @param $keyGroup Main group for the key
1934 * @param $subGroup Sub group for the key
1935 * @return $count Count of given group
1937 protected final function countGenericArrayGroup ($keyGroup, $subGroup) {
1939 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
1942 if (!$this->isGenericArrayGroupSet($keyGroup, $subGroup)) {
1944 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
1949 $count = count($this->genericArray[$keyGroup][$subGroup]);
1952 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',count=' . $count);
1959 * Count generic array elements
1961 * @param $keyGroup Main group for the key
1962 * @param $subGroup Sub group for the key
1963 * @para $key Key to count
1964 * @return $count Count of given key
1966 protected final function countGenericArrayElements ($keyGroup, $subGroup, $key) {
1968 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
1971 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
1973 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' not found.');
1975 } elseif (!$this->isValidGenericArrayGroup($keyGroup, $subGroup)) {
1977 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ' is not an array.');
1982 $count = count($this->genericArray[$keyGroup][$subGroup][$key]);
1985 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',count=' . $count);
1992 * Getter for whole generic group array
1994 * @param $keyGroup Key group to get
1995 * @return $array Whole generic array group
1997 protected final function getGenericArray ($keyGroup) {
1999 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup);
2002 if (!isset($this->genericArray[$keyGroup])) {
2004 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ' does not exist.');
2009 return $this->genericArray[$keyGroup];
2013 * Setter for generic array key
2015 * @param $keyGroup Key group to get
2016 * @param $subGroup Sub group for the key
2017 * @param $key Key to unset
2018 * @param $value Mixed value from generic array element
2021 protected final function setGenericArrayKey ($keyGroup, $subGroup, $key, $value) {
2023 //* NOISY-DEBUG: */ if (!is_object($value)) $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',value[' . gettype($value) . ']=' . print_r($value, true));
2026 $this->genericArray[$keyGroup][$subGroup][$key] = $value;
2030 * Getter for generic array key
2032 * @param $keyGroup Key group to get
2033 * @param $subGroup Sub group for the key
2034 * @param $key Key to unset
2035 * @return $value Mixed value from generic array element
2037 protected final function getGenericArrayKey ($keyGroup, $subGroup, $key) {
2039 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
2042 if (!$this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) {
2044 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ' does not exist.');
2049 return $this->genericArray[$keyGroup][$subGroup][$key];
2053 * Sets a value in given generic array key/element
2055 * @param $keyGroup Main group for the key
2056 * @param $subGroup Sub group for the key
2057 * @param $key Key to set
2058 * @param $element Element to set
2059 * @param $value Value to set
2062 protected final function setGenericArrayElement ($keyGroup, $subGroup, $key, $element, $value) {
2064 //* 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));
2067 $this->genericArray[$keyGroup][$subGroup][$key][$element] = $value;
2071 * Getter for generic array element
2073 * @param $keyGroup Key group to get
2074 * @param $subGroup Sub group for the key
2075 * @param $key Key to look for
2076 * @param $element Element to look for
2077 * @return $value Mixed value from generic array element
2079 protected final function getGenericArrayElement ($keyGroup, $subGroup, $key, $element) {
2081 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element);
2084 if (!$this->isGenericArrayElementSet($keyGroup, $subGroup, $key, $element)) {
2086 trigger_error(__METHOD__ . ': keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key . ',element=' . $element . ' does not exist.');
2091 return $this->genericArray[$keyGroup][$subGroup][$key][$element];
2095 * Checks if a given sub group is valid (array)
2097 * @param $keyGroup Key group to get
2098 * @param $subGroup Sub group for the key
2099 * @return $isValid Whether given sub group is valid
2101 protected final function isValidGenericArrayGroup ($keyGroup, $subGroup) {
2103 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup);
2106 $isValid = (($this->isGenericArrayGroupSet($keyGroup, $subGroup)) && (is_array($this->getGenericSubArray($keyGroup, $subGroup))));
2113 * Checks if a given key is valid (array)
2115 * @param $keyGroup Key group to get
2116 * @param $subGroup Sub group for the key
2117 * @param $key Key to check
2118 * @return $isValid Whether given sub group is valid
2120 protected final function isValidGenericArrayKey ($keyGroup, $subGroup, $key) {
2122 //* NOISY-DEBUG: */ $this->outputLine('[' . __METHOD__ . ':' . __LINE__ . '] keyGroup=' . $keyGroup . ',subGroup=' . $subGroup . ',key=' . $key);
2125 $isValid = (($this->isGenericArrayKeySet($keyGroup, $subGroup, $key)) && (is_array($this->getGenericArrayKey($keyGroup, $subGroup, $key))));
2132 * Initializes the web output instance
2136 protected function initWebOutputInstance () {
2137 // Get application instance
2138 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
2140 // Init web output instance
2141 $outputInstance = ObjectFactory::createObjectByConfiguredName('output_class', array($applicationInstance));
2144 $this->setWebOutputInstance($outputInstance);
2148 * Translates boolean true to 'Y' and false to 'N'
2150 * @param $boolean Boolean value
2151 * @return $translated Translated boolean value
2153 public static final function translateBooleanToYesNo (bool $boolean) {
2154 // Make sure it is really boolean
2155 assert(is_bool($boolean));
2158 $translated = ($boolean === true) ? 'Y' : 'N';
2160 // ... and return it
2165 * Creates a full-qualified file name (FQFN) for given file name by adding
2166 * a configured temporary file path to it.
2168 * @param $infoInstance An instance of a SplFileInfo class
2169 * @return $tempInstance An instance of a SplFileInfo class (temporary file)
2170 * @throw PathWriteProtectedException If the path in 'temp_file_path' is write-protected
2171 * @throws FileIoException If the file cannot be written
2173 protected static function createTempPathForFile (SplFileInfo $infoInstance) {
2175 $basePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('temp_file_path');
2177 // Is the path writeable?
2178 if (!is_writable($basePath)) {
2179 // Path is write-protected
2180 throw new PathWriteProtectedException($infoInstance, self::EXCEPTION_PATH_CANNOT_BE_WRITTEN);
2184 $tempInstance = new SplFileInfo($basePath . DIRECTORY_SEPARATOR . $infoInstance->getBasename());
2187 if (!FrameworkBootstrap::isReachableFilePath($tempInstance)) {
2189 throw new FileIoException($tempInstance, self::EXCEPTION_FILE_NOT_REACHABLE);
2193 return $tempInstance;
2197 * "Getter" for a printable state name
2199 * @return $stateName Name of the node's state in a printable format
2201 public final function getPrintableState () {
2202 // Default is 'null'
2203 $stateName = 'null';
2205 // Get the state instance
2206 $stateInstance = $this->getStateInstance();
2208 // Is it an instance of Stateable?
2209 if ($stateInstance instanceof Stateable) {
2210 // Then use that state name
2211 $stateName = $stateInstance->getStateName();