Source

php-libs / zf_bundles / library / ZendY / View / Helper / BundleAbstract.php

Full commit
<?php
abstract class ZendY_View_Helper_BundleAbstract
{
    /**
     * View object
     *
     * @var Zend_View_Interface
     */
    public $view = null;

    protected $isEnabled = true;
    protected $items = array();

    protected $baseUrl = '';
    protected $documentRoot = '';
    protected $cacheDir = '';
    protected $urlPrefix = '';

    protected $bundleId = '';
    protected $bundleExt = '';

    public function enable()
    {
        $this->isEnabled = true;
        return $this;
    }

    public function disable()
    {
        $this->isEnabled = false;
        return $this;
    }

    public function getItems()
    {
        return $this->items;
    }

    public function setDocumentRoot($value)
    {
        $this->documentRoot = $value;
        return $this;
    }

    public function getDocumentRoot()
    {
        return $this->documentRoot;
    }

    public function setCacheDir($value)
    {
        $this->cacheDir = $value;
        if ('' === $this->urlPrefix) {
            $this->urlPrefix = $this->cacheDir;
        }
        return $this;
    }

    public function getCacheDir()
    {
        return $this->cacheDir;
    }

    public function setUrlPrefix($value)
    {
        $this->urlPrefix = $value;
        return $this;
    }

    public function getUrlPrefix()
    {
        return $this->urlPrefix;
    }

    public function setBundleExt($value)
    {
        $this->bundleExt = $value;
        return $this;
    }

    public function getBundleExt()
    {
        return $this->bundleExt;
    }

    public function __construct()
    {
        $this->documentRoot = $_SERVER['DOCUMENT_ROOT'];
    }

    abstract public function toString();

    protected function _addFile($file)
    {
        if (!file_exists($file)) {
            return;
        }

        $this->items[] = array(
            'type' => 'file',
            'src' => $this->_cutBaseUrl($file),
        );
        $this->bundleId = '';
    }

    protected function _addSource($source)
    {
        $this->items[] = array(
            'type' => 'inline',
            'src' => $source,
        );
        $this->bundleId = '';
    }

    protected function _cutBaseUrl($url)
    {
        $baseUrl = $this->view->baseUrl();
        if ($baseUrl && strpos($url, $baseUrl) === 0) {
            $url = substr($url, strlen($baseUrl));
        }
        return $url;
    }

    protected function _getBundleData()
    {
        $data = '';
        foreach ($this->items as $item) {
            if ($item['type'] == 'file') {
                $data .= file_get_contents($this->documentRoot . '/'
                        . $item['src']) . PHP_EOL;
            } else {
                $data .= $item['src'] . PHP_EOL;
            }
        }
        return $data;
    }

    protected function _createBundle($bundleFile)
    {
        $data = $this->_getBundleData();
        file_put_contents($bundleFile, $data);
    }

    protected function _getBundleId()
    {
        if ('' === $this->bundleId) {
            $id = '';
            foreach ($this->items as $item) {
                $id .= $item['src'];
            }
            $this->bundleId = md5($id);
        }
        return $this->bundleId;
    }

    protected function _getBundlePath()
    {
        $id = $this->_getBundleId();
        return "{$this->cacheDir}/{$id}{$this->bundleExt}";
    }

    protected function _getBundleUrl()
    {
        $id = $this->_getBundleId();
        return "{$this->urlPrefix}/{$id}{$this->bundleExt}";
    }

    public function __toString()
    {
        return $this->toString();
    }

    /**
     * Set the View object
     *
     * @param  Zend_View_Interface $view
     * @return Zend_View_Helper_Abstract
     */
    public function setView(Zend_View_Interface $view)
    {
        $this->view = $view;
        return $this;
    }
}