/blog/www/system/library/ZendY/View/Helper/BundleAbstract.php
PHP | 170 lines | 134 code | 25 blank | 11 comment | 7 complexity | 1cd9cd0d0af4a3ffdc483e470268e331 MD5 | raw file
Possible License(s): BSD-3-Clause
- <?php
- abstract class ZendY_View_Helper_BundleAbstract
- {
- /**
- * View object
- *
- * @var Zend_View_Interface
- */
- public $view = null;
- private $_items = array();
- private $_baseUrl = '';
- private $_documentRoot = '';
- private $_cacheDir = '';
- private $_urlPrefix = '';
- private $_bundleId = '';
- private $_bundleExt = '';
- /**
- * Set the View object
- *
- * @param Zend_View_Interface $view
- * @return Zend_View_Helper_Abstract
- */
- public function setView(Zend_View_Interface $view)
- {
- $this->view = $view;
- $this->_baseUrl = $this->view->baseUrl();
- 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)
- {
- $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)
- {
- if ($this->_baseUrl
- && substr_compare($url, $this->_baseUrl, 0,
- strlen($this->_baseUrl)) === 0) {
- $url = substr($url, strlen($this->_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->_baseUrl}/{$this->_urlPrefix}/{$id}{$this->_bundleExt}";
- }
- public function __toString()
- {
- return $this->toString();
- }
- }