PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/www/system/library/ZendY/View/Helper/BundleAbstract.php

https://bitbucket.org/vmihailenco/vladimirwebdev
PHP | 170 lines | 134 code | 25 blank | 11 comment | 7 complexity | 1cd9cd0d0af4a3ffdc483e470268e331 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. abstract class ZendY_View_Helper_BundleAbstract
  3. {
  4. /**
  5. * View object
  6. *
  7. * @var Zend_View_Interface
  8. */
  9. public $view = null;
  10. private $_items = array();
  11. private $_baseUrl = '';
  12. private $_documentRoot = '';
  13. private $_cacheDir = '';
  14. private $_urlPrefix = '';
  15. private $_bundleId = '';
  16. private $_bundleExt = '';
  17. /**
  18. * Set the View object
  19. *
  20. * @param Zend_View_Interface $view
  21. * @return Zend_View_Helper_Abstract
  22. */
  23. public function setView(Zend_View_Interface $view)
  24. {
  25. $this->view = $view;
  26. $this->_baseUrl = $this->view->baseUrl();
  27. return $this;
  28. }
  29. public function getItems()
  30. {
  31. return $this->_items;
  32. }
  33. public function setDocumentRoot($value)
  34. {
  35. $this->_documentRoot = $value;
  36. return $this;
  37. }
  38. public function getDocumentRoot()
  39. {
  40. return $this->_documentRoot;
  41. }
  42. public function setCacheDir($value)
  43. {
  44. $this->_cacheDir = $value;
  45. if ('' === $this->_urlPrefix) {
  46. $this->_urlPrefix = $this->_cacheDir;
  47. }
  48. return $this;
  49. }
  50. public function getCacheDir()
  51. {
  52. return $this->_cacheDir;
  53. }
  54. public function setUrlPrefix($value)
  55. {
  56. $this->_urlPrefix = $value;
  57. return $this;
  58. }
  59. public function getUrlPrefix()
  60. {
  61. return $this->_urlPrefix;
  62. }
  63. public function setBundleExt($value)
  64. {
  65. $this->_bundleExt = $value;
  66. return $this;
  67. }
  68. public function getBundleExt()
  69. {
  70. return $this->_bundleExt;
  71. }
  72. public function __construct()
  73. {
  74. $this->_documentRoot = $_SERVER['DOCUMENT_ROOT'];
  75. }
  76. abstract public function toString();
  77. protected function _addFile($file)
  78. {
  79. $this->_items[] = array(
  80. 'type' => 'file',
  81. 'src' => $this->_cutBaseUrl($file),
  82. );
  83. $this->_bundleId = '';
  84. }
  85. protected function _addSource($source)
  86. {
  87. $this->_items[] = array(
  88. 'type' => 'inline',
  89. 'src' => $source,
  90. );
  91. $this->_bundleId = '';
  92. }
  93. protected function _cutBaseUrl($url)
  94. {
  95. if ($this->_baseUrl
  96. && substr_compare($url, $this->_baseUrl, 0,
  97. strlen($this->_baseUrl)) === 0) {
  98. $url = substr($url, strlen($this->_baseUrl));
  99. }
  100. return $url;
  101. }
  102. protected function _getBundleData()
  103. {
  104. $data = '';
  105. foreach ($this->_items as $item) {
  106. if ($item['type'] == 'file') {
  107. $data .= file_get_contents($this->_documentRoot . '/'
  108. . $item['src']) . PHP_EOL;
  109. } else {
  110. $data .= $item['src'] . PHP_EOL;
  111. }
  112. }
  113. return $data;
  114. }
  115. protected function _createBundle($bundleFile)
  116. {
  117. $data = $this->_getBundleData();
  118. file_put_contents($bundleFile, $data);
  119. }
  120. protected function _getBundleId()
  121. {
  122. if ('' === $this->_bundleId) {
  123. $id = '';
  124. foreach ($this->_items as $item) {
  125. $id .= $item['src'];
  126. }
  127. $this->_bundleId = md5($id);
  128. }
  129. return $this->_bundleId;
  130. }
  131. protected function _getBundlePath()
  132. {
  133. $id = $this->_getBundleId();
  134. return "{$this->_cacheDir}/{$id}{$this->_bundleExt}";
  135. }
  136. protected function _getBundleUrl()
  137. {
  138. $id = $this->_getBundleId();
  139. return "{$this->_baseUrl}/{$this->_urlPrefix}/{$id}{$this->_bundleExt}";
  140. }
  141. public function __toString()
  142. {
  143. return $this->toString();
  144. }
  145. }