Rewrites for client factory, added methods for HttpClient (mostly from ConsoleClient).
[core.git] / inc / classes / main / factories / client / class_ClientFactory.php
index a46749bc985a072c51c981a00519ea3bf50bb92e..880f5cec371b6378d1560b7d3a891d7da74d72c0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * An general object factory
+ * An object factory for clients
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class ObjectFactory extends BaseFactory {
+class ClientFactory extends ObjectFactory {
        /**
         * Protected constructor
         *
-        * @param       $className      Name of this class
         * @return      void
         */
-       protected function __construct ($className = __CLASS__) {
+       protected function __construct () {
                // Call parent constructor
-               parent::__construct($className);
+               parent::__construct(__CLASS__);
        }
 
        /**
-        * Creates a new object given by the name or throws an exception if
-        * the class was not found. No parameters for the object are currently
-        * supported.
+        * Creates a client object for given protocol. This method uses the
+        * registry pattern to cache those instances.
         *
-        * @param       $className                      Name of the class we shall construct
-        * @param       $args                           Arguments in an indexed array
-        * @return      $objectInstance         An instance of the requested object
-        * @throws      NoClassException        If the requested class was not found
+        * @param       $protocol                       Protocol to create a client object for (e.g. 'http' for a HTTP/1.1 client)
+        * @param       $socketResource         A valid socket resource (optional)
+        * @return      $clientInstance         An instance of the requested client
         * @throws      EmptyVariableException  If a variable is empty unexpectly
         */
-       public static final function createObjectByName ($className, array $args=array()) {
-               // First get an instance of this factory
-               $factoryInstance = new ObjectFactory();
-
+       public static final function createClientByProtokol ($protocol, $socketResource = FALSE) {
                // Is the class name valid and is the class there?
-               if (empty($className)) {
+               if (empty($protocol)) {
+                       // Get an instance of this factory
+                       $factoryInstance = new ClientFactory();
+
                        // Throw an exception here
-                       throw new EmptyVariableException(array($factoryInstance, 'className'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!class_exists($className)) {
-                       // Then throw an exception
-                       throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
-               }
+                       throw new EmptyVariableException(array($factoryInstance, 'protocol'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } // END - if
 
-               // Create method name
-               $methodName = sprintf("create%s", $className);
+               // Default is NULL (to initialize variable)
+               $clientInstance = NULL;
 
-               // Run the user function
-               $objectInstance = call_user_func_array(array($className, $methodName), $args);
+               // Generate registry key
+               $registryKey = $protocol . '_client';
 
-               // Count this one up
-               self::countObject($className);
+               // Is the key already in registry?
+               if (Registry::getRegistry()->instanceExists($registryKey)) {
+                       // Then use that instance
+                       $clientInstance = Registry::getRegistry()->getInstance($registryKey);
 
-               // Return the prepared instance
-               return $objectInstance;
-       }
+                       // Set socket resource
+                       $clientInstance->setSocketResource($socketResource);
+               } else {
+                       // Generate object instance
+                       $clientInstance = self::createObjectByConfiguredName($registryKey, array($socketResource));
 
-       /**
-        * Creates an object by it's configured name
-        *
-        * @param       $configEnttry           Configuration entry to read
-        * @param       $args                           Arguments in an indexed array
-        * @return      $objectInstance         An instance of the requested object
-        */
-       public static final function createObjectByConfiguredName ($configEntry, array $args=array()) {
-               // Read the configuration entry
-               $className = FrameworkConfiguration::getSelfInstance()->getConfigEntry($configEntry);
-
-               // Send this to the other factory...
-               $objectInstance = self::createObjectByName($className, $args);
+                       // Set it in registry for later re-use
+                       Registry::getRegistry()->addInstance($registryKey, $clientInstance);
+               }
 
-               // Return the instance
-               return $objectInstance;
+               // Return the prepared instance
+               return $clientInstance;
        }
 }