Initialize the cart

@param  string  $instance_name
@return mixed
191077
public static function init($instance_name, $save_on_demand){

        $request = app('request');
        $session_id = $request->session()->getId();
        $user_id = config('cart.user_id');
        $app = Application::getInstance();
        $carts = $app->offsetGet("cart_instances");

        if ($user_id instanceof \Closure)
            $user_id = $user_id();

        //if user logged in
        if( $user_id ){
            $user_cart = static::active()->user()->where('name', $instance_name)->first();

            $session_cart_id = $request->session()->get('cart_'.$instance_name);
            $session_cart = is_null($session_cart_id)? null: static::active()->session($session_cart_id)->where('name', $instance_name)->first();

            switch (true) {

                case is_null($user_cart) && is_null($session_cart): //no user cart or session cart
                    $attributes = array(
                        'user_id' => $user_id,
                        'name' => $instance_name,
                        'status' => STATUS_ACTIVE
                    );
                    if($save_on_demand)
                        $cart = new static($attributes);
                    else
                        $cart = static::create($attributes);

                    break;

                case !is_null($user_cart) && is_null($session_cart): //only user cart
                    $cart = $user_cart;
                    break;

                case is_null($user_cart) && !is_null($session_cart): //only session cart
                    $cart = $session_cart;
                    $cart->user_id = $user_id;
                    $cart->session = null;
                    $cart->save();
                    break;

                case !is_null($user_cart) && !is_null($session_cart): //both user cart and session cart exists
                    $session_cart->moveItemsTo($user_cart); //move items from session cart to user cart
                    $session_cart->delete(); //delete session cart
                    $cart = $user_cart;
                    break;
            }

            $request->session()->forget('cart_'.$instance_name); //no longer need it.
            $carts[$instance_name] = $cart;
        }
        //guest user, create cart with session id
        else{
            $attributes = array(
                'session' => $session_id,
                'name' => $instance_name,
                'status' => STATUS_ACTIVE
            );
            $cart = static::firstOrNew($attributes);

            if(!$save_on_demand)
                $cart->save();

            //save current session id, since upon login session id will be regenerated
            //we will use this id to get back the cart before login
            $request->session()->put('cart_'.$instance_name, $session_id);
            $carts[$instance_name] = $cart;
        }
        $app->offsetSet("cart_instances", $carts);
        return $carts[$instance_name];
    }
484054
public static function instance($config_name)
    {
        if (empty(self::$instance[$config_name])) {
            $config = self::$config[$config_name];
            self::$instance[$config_name] = new \Beanbun\Lib\DbConnection($config);
        }
        return self::$instance[$config_name];
    }
1005890
public function newInstance($selfedit = "false", $instanceName=null, $instanceClass=null) {
        //$componentsList = Moufspector::getComponentsList();
        $this->selfedit = $selfedit;
        $componentsList = MoufReflectionProxy::getComponentsList($selfedit=="true");
        sort($componentsList);

        $template = $this->template;
        $template->addContentFunction(array($this, "displayNewInstanceScreen"), $componentsList, $selfedit, $instanceName, $instanceClass);
        //$template->addContentFile(dirname(__FILE__)."/../views/displayNewInstance.php", $this);
        $template->toHtml();
    }
177052
public static function getInstance()
    {
        if (self::$_instance === null) {
            self::$_instance = new Config();
            if (self::$_instance->_store === null) {
                self::$_instance->_store = self::$_instance->_setStore();
            }
        }

        return self::$_instance;
    }
237745
function new_instance()
	{
		if(!($tab = $this->table_name()))
			bors_throw("Try to get new db instance with undefined table");

		if(!$this->create_time(true))
			$this->set_create_time(time());

		if(!$this->modify_time(true))
			$this->set_modify_time(time());

//		$this->set_owner_id(bors()->user_id());
		$this->set_last_editor_id(bors()->user_id());

		$this->storage()->create($this);
	}
397649
protected function setup_instance() {
		switch ( $this->wp_type ) {
			case 'post':
				$wp_post = get_post( $this->get_id() );
				if ( ! is_null( $wp_post ) && get_post_type( $wp_post->ID ) === $this->object_type ) {
					$this->set_instance( $wp_post );
				}
				break;

			case 'term':
				$wp_term = get_term( $this->get_id(), $this->object_type );
				if ( ! is_null( $wp_term ) && ! is_wp_error( $wp_term ) ) {
					$this->set_instance( $wp_term );
				}
				break;
		}
	}
182739
private function instanceCache($name)
    {
        self::$cache = new $name();
        self::$cache->connect($this->getConfig());
    }
831889
public static function &getInstance( $instance = null )
	{
		if ( is_null( self::$instance ) )
		{
			self::$instance = $instance ? $instance : new self();
			// if ( $callback ) $callback( self::$instance );
		}
		return self::$instance;
	}
100355
public static function initialize()
    {
        if (!isset($GLOBALS['_SESSION']) or !($GLOBALS['_SESSION'] instanceof self)) {
            $GLOBALS['_SESSION'] = self::getInstance();
        }

        return self::getInstance();
    }
451870
public static function get_instance( $plugin_name, $plugin_file ) {
		if ( ! isset( self::$_instances[ $plugin_name ] ) ) {
			self::$_instances[ $plugin_name ] = new static( $plugin_name, $plugin_file );
		}

		return self::$_instances[ $plugin_name ];
	}
325570
private function getInstanceCode(Instance $instance) : Code
    {
        $node = ($this->normalizer)($instance->value);

        return new Code(new Node\Stmt\Return_($node), false);
    }
1178119
public static function load($instance_identifier) {
		$args = func_get_args();

		$instance_identifier	= array_shift($args);
		$factory_args			= $args;

		// get factory config
		$instance_identifier = explode(':', $instance_identifier);
		
		// create a fully namespaced class path
		$classname = $instance_identifier[0];
		if ($classname{0} != '\\') {
			$classname = '\\Morrow\\' . $classname;
		}

		// set the instancename we need to get possible parameters from prepare
		$instancename = (isset($instance_identifier[1])) ? $instance_identifier[1] : '_morrow_default';

		// if there were no constructor parameters passed look for prepared parameters
		if (count($factory_args) === 0) {
			if (isset(self::$_params[$classname][$instancename])) {
				$classname = self::$_params[$classname][$instancename]['classname'];
				$factory_args = self::$_params[$classname][$instancename]['args'];
			} else {
				$factory_args = [];
			}
		}
		
		// if the instance was already instantiated return it, otherwise create it
		$instance =& self::$_instances[$classname][$instancename];
		
		if (isset($instance)) {
			if ($instance instanceof $classname) return $instance;
			else {
				throw new \Exception('instance "'.$instancename.'" already defined of class "'.get_class($instance).'"');
				return false;
			}
		}

		// create object
		if (empty($factory_args)) {
			$instance = new $classname;
		} else {
			// check the parameters for instances of class Factoryproxy
			$factory_args = array_map(function($param){
				if (!is_object($param) || get_class($param) !== 'Morrow\\Factory') return $param;
				return call_user_func_array('\\Morrow\\Factory::load', $param->_getProxyParameters());
			}, $factory_args);

			// use reflection class to inject the args as single parameters
			$ref = new \ReflectionClass($classname);
			$instance = $ref->newInstanceArgs($factory_args);
		}

		// first execute onload callback for all instances, then for the specific instance
		if (isset(self::$_onload_callbacks[$classname])) {
			foreach (self::$_onload_callbacks[$classname] as $callback) {
				call_user_func($callback, $instance);
			}
		}
		if (isset(self::$_onload_callbacks[$classname . ':' . $instancename])) {
			foreach (self::$_onload_callbacks[$classname . ':' . $instancename] as $callback) {
				call_user_func($callback, $instance);
			}
		}

		return $instance;
	}
1175297
public static function getInstance($name=null) {
		if( !$name ) {
			$name = 'default';
		}
		if( !isset(static::$instances[$name]) ) {
			static::make($name);
		}
		return static::$instances[$name];
	}
244059
public static function build($component_name){
        if(is_null(self::$instance)){
            self::$instance = new self;
        }
        self::$set['component_name'] = $component_name;
        //组件的唯一id
        self::$uniqid_id = $component_name.'_'.self::$run_time;
        //当前页面的调用的次数
        self::$run_time++;
        return self::$instance;
    }
1044582
static function instance( $module_id ) {
			$key = 'm_' . $module_id;

			if ( ! isset( self::$_instances[ $key ] ) ) {
				self::$_instances[ $key ] = new FS_Plugin_Manager( $module_id );
			}

			return self::$_instances[ $key ];
        }
1234506
public function instance($instance = null)
    {
        $instance = $instance ?: self::DEFAULT_INSTANCE;

        $this->instance = sprintf('%s.%s', 'cart', $instance);

        return $this;
    }
731899
public function newInstance2($selfedit = "false", $instanceName=null, $instanceClass=null) {
        $this->instanceName = $instanceName;
        $this->instanceClass = $instanceClass;
        $this->selfedit = $selfedit;

        $this->contentBlock->addFile(dirname(__FILE__)."/../../views/instances/newInstance.php", $this);
        $this->template->toHtml();
    }