VirtueMart Forum

VirtueMart Dev/Coding Central: VM1 (old version) => Development Projects, Modifications, Hacks & Tweaks. VM1.1 => Topic started by: duccio on January 06, 2012, 18:42:50 pm

Title: helpers/cart.php - VirtueMartCart Dependency injection (sort of)
Post by: duccio on January 06, 2012, 18:42:50 pm
Hi, I made some changes to the VirtueMartCart class and the VirtueMartCart::getCart() to implement the Dependency Injection pattern, so that if somebody wants to extend the cart helper to add or change some behaviors, he just needs to do:

class MyCart extendes VirtueMartCart {
      // new or overloaded functions here
}

VirtueMartCart::setCartClass('MyCart');
$cart = VirtueMartCart::getCart();
/* $cart instanceof MyCart */

I believe it could be useful to some people, and it's small enough that could also be included in the standard distribution.

Duccio


(Code also in file attached)


/*
 *
* Overcomes the lack of Late State Binding in PHP <5.3
*
* Needs to be added to helpers/cart.php
*
*/


//Beginning of helpers/cart.php or other Exception file

class VirtueMart_Helper_Cart_Exception extends Exception {}


class VirtueMartCart {

   private static $_cartClass = 'VirtueMartCart';   
   /* Other variables and functions */
   
   
   
   
   /**
    * Set the name of the class instantiated by getCart
    *
    * Example
    *
    *         class MyCartClass extends VirtueMartCart {
    *             // my code here
    *         }
    *
    *         VirtueMartCart::setCartClass('MyCartClass'); //Or MyCartClass::setCartClass('MyCartClass');
    *         $cart = VirtueCartClass::getCart(); //Or MyCartClass::getCart();
    *
    * @param unknown_type $class
    * @throws Exception
    * @author Duccio Gasparri
    */
   public static function setCartClass($class = 'VirtueMartCart', $exceptionIfDifferent = true)
   {
      if(is_string($class))
         self::$_cartClass = $class;
      elseif(is_object($class))
      self::$_cartClass = get_class($class);
      else
         throw new VirtueMart_Helper_Cart_Exception('Parameter $class must be string or object, '.gettype($class).' provided.');
   
      if($exceptionIfDifferent && is_object(self::$_cart) && self::$_cartClass != get_class(self::$_cart))
         throw new VirtueMart_Helper_Cart_Exception('Cart object already exists of class '.get_class(self::$_cart));
   
   }
   
   /**
    * Get the name of the class instantiated by getCart
    *
    * @return string
    * @author Duccio Gasparri
    */
   public static function getCartClass()
   {
      return self::$_cartClass;
   }
   
   
   // and in function getCart(...)
   public static function getCart($deleteValidation=true,$setCart=true, $options = array()) {
   
      /* ... other code ... */
      
            //Comment here
            //self::$_cart = new VirtueMartCart;
            self::$_cart = new self::$_cartClass; //<-- CHANGE HERE
            
            /* ... other code */
         
         // Comment here
         //self::$_cart = new VirtueMartCart;
         self::$_cart = new self::$_cartClass; //<-- CHANGE HERE
   
   
   }
   

}


[attachment cleanup by admin]
Title: Re: helpers/cart.php - VirtueMartCart Dependency injection (sort of)
Post by: Milbo on January 08, 2012, 12:59:13 pm
interesting