]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/tools/console/class_ConsoleTools.php
Continued:
[core.git] / framework / main / classes / tools / console / class_ConsoleTools.php
index e9d2fe5afaac52dde296d62c8f477a702dc0c74d..f4c48d2f392dc9f7b7831d826b06b13f1ee851ee 100644 (file)
@@ -159,6 +159,7 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        protected function extractHostnameFromRawData (string $rawData) {
                // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: rawData=%s - CALLED!', $rawData));
                if (empty($rawData)) {
                        // Throw IAE
                        throw new InvalidArgumentException('Parameter "rawData" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
@@ -171,13 +172,16 @@ class ConsoleTools extends BaseFrameworkSystem {
                $data = explode(PHP_EOL, $rawData);
 
                // "Walk" through it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: data()=%d', count($data)));
                foreach ($data as $line) {
                        // Trim it
                        $line = trim($line);
 
                        // Begins with a hash (#) = comment?
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: line=%s', $line));
                        if (substr($line, 0, 1) == '#') {
                                // Then skip it
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: line=%s - SKIPPED!', $line));
                                continue;
                        }
 
@@ -187,7 +191,11 @@ class ConsoleTools extends BaseFrameworkSystem {
                                $hostData = explode('=', $line);
 
                                // Make sure only a key=value pair goes through
-                               assert(count($hostData) == 2);
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostData()=%d', count($hostData)));
+                               if (count($hostData) < 2) {
+                                       // Ops, wrong count
+                                       throw new UnexpectedValueException(sprintf('hostData()=%d is unexpected, line=%s', count($hostData), $line), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
+                               }
 
                                // Try to get it and abort
                                $hostname = str_replace(
@@ -195,15 +203,18 @@ class ConsoleTools extends BaseFrameworkSystem {
                                        ['', ''],
                                        $hostData[1]
                                );
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from hostData - BREAK!', $hostname));
                                break;
                        } else {
                                // Use it directly
                                $hostname = $line;
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from line - BREAK!', $line));
                                break;
                        }
                }
 
                // Return it
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - EXIT!', $hostname));
                return $hostname;
        }
 
@@ -218,6 +229,7 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        public static function resolveIpAddress (string $hostname) {
                // Validate parameter
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - CALLED!', $hostname));
                if (empty($hostname)) {
                        // Throw IAE
                        throw new InvalidArgumentException('Parameter "hostname" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
@@ -246,9 +258,11 @@ class ConsoleTools extends BaseFrameworkSystem {
 
                // Resolve it
                // @TODO Here should the cacher be implemented
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s', $hostname));
                $ipResolved = gethostbyname($hostname);
 
                // Was it fine?
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: ipResolved[%s]=%s', gettype($ipResolved), $ipResolved));
                if (($ipResolved !== false) && ($ipResolved != $hostname)) {
                        // Okay, this works!
                        $ipAddress = $ipResolved;
@@ -270,6 +284,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                }
 
                // Return resolved IP
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s - EXIT!', $ipAddress));
                return $ipAddress;
        }
 
@@ -280,7 +295,7 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        public static function acquireHostname () {
                // Is cache there?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!');
                if (!isset(self::$cache[__METHOD__])) {
                        // Get a new instance
                        $toolsInstance = new ConsoleTools();
@@ -289,7 +304,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                        $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hostname_file'));
 
                        // Init host name
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: infoInstance=%s', $infoInstance->__toString()));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: infoInstance=%s', $infoInstance->__toString()));
                        $hostname = 'host.invalid';
 
                        // Try to check /etc/hostname first
@@ -298,18 +313,18 @@ class ConsoleTools extends BaseFrameworkSystem {
                                $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', [$infoInstance]);
 
                                // Read the file
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: fileInstance=%s', $fileInstance->__toString()));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: fileInstance=%s', $fileInstance->__toString()));
                                $rawData = trim($fileInstance->readFromFile());
 
                                // Close the file
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: rawData=%s', $rawData));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: rawData=%s', $rawData));
                                $fileInstance->closeFile();
 
                                // Extract hostname from it
                                $hostname = $toolsInstance->extractHostnameFromRawData($rawData);
 
                                // Debug message
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from /etc/hostname', $hostname));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from /etc/hostname', $hostname));
                        } catch (FileNotFoundException $e) {
                                // Fall-back to 'SESSION_SVR' which found on my Sun Station
                                if (isset($_SERVER['SESSION_SVR'])) {
@@ -326,7 +341,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                                }
 
                                // Debug message
-                               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from _SERVER array', $hostname));
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s from _SERVER array', $hostname));
                        } catch (FrameworkException $e) {
                                // Output debug message
                                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s',
@@ -338,7 +353,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                        }
 
                        // Set cache
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting hostname=%s ...', $hostname));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting hostname=%s ...', $hostname));
                        self::$cache[__METHOD__] = $hostname;
                } else {
                        // Get from cache
@@ -346,7 +361,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                }
 
                // Return it
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - EXIT!', $hostname));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: hostname=%s - EXIT!', $hostname));
                return $hostname;
        }
 
@@ -358,17 +373,17 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        public static function acquireSelfIpAddress () {
                // Is cache there?
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!');
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('CONSOLE-TOOLS: CALLED!');
                if (!isset(self::$cache[__METHOD__])) {
                        // Get host name
                        $hostname = self::acquireHostname();
 
                        // Resolve the IP number
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s', $hostname));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: hostname=%s', $hostname));
                        $ipAddress = self::resolveIpAddress($hostname);
 
                        // Set cache
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting ipAddress=%s ...', $ipAddress));
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('CONSOLE-TOOLS: Setting ipAddress=%s ...', $ipAddress));
                        self::$cache[__METHOD__] = $ipAddress;
                } else {
                        // Get it from cache
@@ -376,7 +391,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                }
 
                // Return it
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s - EXIT!', $ipAddress));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: ipAddress=%s - EXIT!', $ipAddress));
                return $ipAddress;
        }