Author Topic: helpers/cart.php - VirtueMartCart Dependency injection (sort of)  (Read 3677 times)

duccio

  • Beginner
  • *
  • Posts: 4
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]

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10546
  • VM4.0.7 Eagle Owl
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: helpers/cart.php - VirtueMartCart Dependency injection (sort of)
« Reply #1 on: January 08, 2012, 12:59:13 pm »
interesting
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/