]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Thu, 26 Dec 2024 05:41:03 +0000 (06:41 +0100)
committerRoland Häder <roland@mxchange.org>
Thu, 26 Dec 2024 05:41:03 +0000 (06:41 +0100)
- starting with PHP 8, sockets are now no longer a scalar resource type, but a
  Socket class

framework/main/classes/client/http/class_HttpClient.php
framework/main/classes/factories/client/class_ClientFactory.php
framework/main/classes/tools/console/class_ConsoleTools.php
framework/main/exceptions/socket/class_InvalidSocketException.php

index fc955edab11c7442ec9c251b3c689288d78b8267..4864ef6d51e8a89adca27f2622a7296ff79c53bb 100644 (file)
@@ -10,6 +10,7 @@ use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 
 // Import SPL stuff
 use \InvalidArgumentException;
+use \Socket;
 use \UnexpectedValueException;
 
 /**
@@ -55,10 +56,10 @@ class HttpClient extends BaseClient implements Client {
        /**
         * Creates an instance of this Client class and prepares it for usage
         *
-        * @param       $socketResource         Resource of a socket (optional)
-        * @return      $clientInstance         An instance of a Client class
+        * @param       $socketResource         An instance of a Socket class
+        * @return      $clientInstance         An instance of a HttpClient class
         */
-       public final static function createHttpClient ($socketResouce = FALSE) {
+       public final static function createHttpClient (Socket $socketResouce = NULL) {
                // Get a new instance
                /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: socketResource[%s]=%s - CALLED!', gettype($socketResource), $socketResource));
                $clientInstance = new HttpClient();
index 3b182f8f7135c9cb99d853fc2014744f615dde34..3b0e3fd4a3f98f7b2705ce8736d0a00b4ae0048e 100644 (file)
@@ -6,6 +6,9 @@ namespace Org\Mxchange\CoreFramework\Factory\Client;
 use Org\Mxchange\CoreFramework\Factory\BaseFactory;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 
+// Import SPL
+use \Socket;
+
 /**
  * An object factory for clients
  *
@@ -44,10 +47,10 @@ class ClientFactory extends BaseFactory {
         * registry pattern to cache those instances.
         *
         * @param       $protocolInstance       An instance of a HandleableProtocol class to create a client object for (e.g. 'http' for a HTTP/1.1 client)
-        * @param       $socketResource         A valid socket resource (optional)
+        * @param       $socketResource         An instance of a Socket class (optional)
         * @return      $clientInstance         An instance of the requested client
         */
-       public static final function createClientByProtocolInstance (HandleableProtocol $protocolInstance, $socketResource = FALSE) {
+       public static final function createClientByProtocolInstance (HandleableProtocol $protocolInstance, Socket $socketResource = NULL) {
                // Default is NULL (to initialize variable)
                $clientInstance = NULL;
 
@@ -63,7 +66,7 @@ class ClientFactory extends BaseFactory {
                        $clientInstance->setSocketResource($socketResource);
                } else {
                        // Generate object instance
-                       $clientInstance = ObjectFactory::createObjectByConfiguredName($registryKey, array($socketResource));
+                       $clientInstance = ObjectFactory::createObjectByConfiguredName($registryKey, [$socketResource]);
 
                        // Set it in registry for later re-use
                        ObjectRegistry::getRegistry('factory')->addInstance($registryKey, $clientInstance);
index 0051dfcc158379aef3dea465451c4ec9f89cb097..ab958a53a830fc5f97850bff57783ae87d0172d0 100644 (file)
@@ -13,6 +13,7 @@ use Org\Mxchange\CoreFramework\Socket\InvalidSocketException;
 
 // Import SPL stuff
 use \InvalidArgumentException;
+use \Socket;
 use \SplFileInfo;
 use \UnexpectedValueException;
 
@@ -95,11 +96,11 @@ class ConsoleTools extends BaseFrameworkSystem {
         *
         * @param       $host                           Host to connect to
         * @param       $port                           Port number to connect to
-        * @param       $socketResource         Resource of a socket
+        * @param       $socketResource         An instance of a Socket class
         * @return      $response                       Response array
         * @throws      InvalidArgumentException        If a parameter is not valid
         */
-       protected function setupProxyTunnel (string $host, int $port, $socketResource) {
+       protected function setupProxyTunnel (string $host, int $port, Socket $socketResource) {
                // Validate parameter
                //* NOISY-DEBUG */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('CONSOLE-TOOLS: host=%s,port=%d,socketResource=%s - CALLED!', $host, $port, $socketResource));
                if (empty($host)) {
@@ -108,9 +109,6 @@ class ConsoleTools extends BaseFrameworkSystem {
                } elseif ($port < 1) {
                        // Throw IAE
                        throw new InvalidArgumentException(sprintf('port=%d is not valid', $port));
-               } elseif (!is_resource($socketResource)) {
-                       // Throw IAE
-                       throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', gettype($socketResource)));
                }
 
                // Initialize array
index 4d23fe9aca9f1e3072818511d84b8448336d5005..b2a98c33891e8bc54a1e6c86ce6ef043d3aa5aa8 100644 (file)
@@ -35,13 +35,19 @@ class InvalidSocketException extends AbstractSocketException {
         */
        public function __construct (array $messageData, int $code) {
                // Construct the message
-               $message = sprintf('[%s:] Invalid socketResource[%s]=%s, errno=%s, errstr=%s',
-                       $messageData[0]->__toString(),
-                       gettype($messageData[0]->getSocketResource()),
-                       $messageData[0]->getSocketResource(),
-                       $messageData[1],
-                       $messageData[2]
-               );
+               if (isset($messageData[1]) && isset($messageData[2])) {
+                       $message = sprintf('[%s:] Invalid socketResource[]=%s, errno=%s, errstr=%s',
+                               $messageData[0]->__toString(),
+                               gettype($messageData[1]),
+                               $messageData[1],
+                               $messageData[2]
+                       );
+               } else {
+                       $message = sprintf('[%s:] Invalid socketResource[]=%s with unknown error.',
+                               $messageData[0]->__toString(),
+                               gettype($messageData[1])
+                       );
+               }
 
                // Call parent exception constructor
                parent::__construct($message, $code);