<?php

class locator
{
    public $data = array();

    function &get($key) {

        if (!isset($this->data[$key])) {

            $this->data[$key] =& $this->create($key);
//			$this->data[$key] = $this->create($key);
        }

        return $this->data[$key];

    }

    public function set($key, &$value)
    {
        $this->data[$key] =& $value;

    }

    public function create($key)
    {
        $method = 'create' . $key;

        if (method_exists($this, $method)) {
            return $this->$method();

        } else {

            exit('Error: Could not create ' . $key . '!');

        }

    }

    public function createAddress()
    {
        require_once(DIR_LIBRARY . 'cart/address.php');

        return new Address($this);

    }

    public function createCache()
    {
        if (!is_writable(DIR_CACHE)) {

              exit('Error: Could not write to cache directory!');

        }

        require_once(DIR_LIBRARY . 'cache/cache.php');

        return new Cache();

    }

    public function createCalculate()
    {
        require_once(DIR_LIBRARY . 'cart/calculate.php');

        return new Calculate($this);

    }

    public function createConfig()
    {
        require_once(DIR_LIBRARY . 'application/config.php');

        return new Config();

    }

    public function createCoupon()
    {
        require_once(DIR_LIBRARY . 'cart/coupon.php');

        return new Coupon($this);

    }

    public function createCurrency()
    {
        require_once(DIR_LIBRARY . 'cart/currency.php');

        return new Currency($this);

    }

    public function createCart()
    {
        require_once(DIR_LIBRARY . 'cart/cart.php');

        return new Cart($this);

    }

    public function createController()
    {
        require_once(DIR_LIBRARY . 'application/controller.php');

        return new Controller($this);

    }

    public function createCustomer()
    {
        require_once(DIR_LIBRARY . 'cart/customer.php');

        return new Customer($this);

    }

    public function createDatabase()
    {
        require_once(DIR_LIBRARY . 'database/database.php');

        $database = new Database($this);

        $database->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

        return $database;

    }

    public function createDownload()
    {
        require_once(DIR_LIBRARY . 'filesystem/download.php');

        return new Download();

    }

    public function createImage()
    {
        if (!is_writable(DIR_IMAGE)) {

            exit('Error: Could not write to image directory!');

        }

        require_once(DIR_LIBRARY . 'image/image.php');

        return new Image($this);

    }

    public function createLanguage()
    {
        require_once(DIR_LIBRARY . 'language/language.php');

        return new Language($this);

    }

    public function createMail()
    {
        require_once(DIR_LIBRARY . 'mail/mail.php');

        return new Mail();

    }

    public function createModel($key)
    {
        require_once(DIR_LIBRARY . 'application/model.php');

        return new Model($this);

    }

    public function createModule()
    {
        require_once(DIR_LIBRARY . 'cart/module.php');

        return new Module($this);

    }

    public function createOrder()
    {
        require_once(DIR_LIBRARY . 'cart/order.php');

        return new Order($this);

    }

    public function createPayment()
    {
        require_once(DIR_LIBRARY . 'cart/payment.php');

        return new Payment($this);

    }

    public function createRequest()
    {
        require_once(DIR_LIBRARY . 'environment/request.php');

        return new Request($this);

    }

    public function createResponse()
    {
        require_once(DIR_LIBRARY . 'environment/response.php');

        return new Response($this);

    }

    public function createRouter()
    {
        require_once(DIR_LIBRARY . 'application/router.php');

        return new Router($this);

    }

    public function createSession()
    {
        require_once(DIR_LIBRARY . 'session/session.php');

        return new Session($this);

    }

    public function createSession_nodb()
    {
        require_once(DIR_LIBRARY . 'session/session_nodb.php');

        return new session_nodb($this);

    }

    public function createShipping()
    {
        require_once(DIR_LIBRARY . 'cart/shipping.php');

        return new Shipping($this);

    }

    public function createTax()
    {
        require_once(DIR_LIBRARY . 'cart/tax.php');

        return new Tax($this);

    }

    public function createUpload()
    {
        require_once(DIR_LIBRARY . 'filesystem/upload.php');

        return new Upload();

    }

    public function createUrl()
    {
        require_once(DIR_LIBRARY . 'environment/url.php');

        return new Url($this);

    }

    public function createUser()
    {
        require_once(DIR_LIBRARY . 'user/user.php');

        return new User($this);

    }

    public function createTemplate()
    {
        require_once(DIR_LIBRARY . 'template/template.php');

        $config =& $this->get('config');

        return new Template($config->get('config_template'));

    }

    public function createWeight()
    {
        require_once(DIR_LIBRARY . 'cart/weight.php');

        return new Weight($this);

    }

}
