HeadStyle.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @version $Id$
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. /** Zend_View_Helper_Placeholder_Container_Standalone */
  23. require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
  24. /**
  25. * Helper for setting and retrieving stylesheets
  26. *
  27. * @uses Zend_View_Helper_Placeholder_Container_Standalone
  28. * @package Zend_View
  29. * @subpackage Helper
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @method $this appendStyle($content, array $attributes = array())
  33. * @method $this offsetSetStyle($index, $content, array $attributes = array())
  34. * @method $this prependStyle($content, array $attributes = array())
  35. * @method $this setStyle($content, array $attributes = array())
  36. */
  37. class Zend_View_Helper_HeadStyle extends Zend_View_Helper_Placeholder_Container_Standalone
  38. {
  39. /**
  40. * Registry key for placeholder
  41. * @var string
  42. */
  43. protected $_regKey = 'Zend_View_Helper_HeadStyle';
  44. /**
  45. * Allowed optional attributes
  46. * @var array
  47. */
  48. protected $_optionalAttributes = array('lang', 'title', 'media', 'dir');
  49. /**
  50. * Allowed media types
  51. * @var array
  52. */
  53. protected $_mediaTypes = array(
  54. 'all', 'aural', 'braille', 'handheld', 'print',
  55. 'projection', 'screen', 'tty', 'tv'
  56. );
  57. /**
  58. * Capture type and/or attributes (used for hinting during capture)
  59. * @var string
  60. */
  61. protected $_captureAttrs = null;
  62. /**
  63. * Capture lock
  64. * @var bool
  65. */
  66. protected $_captureLock;
  67. /**
  68. * Capture type (append, prepend, set)
  69. * @var string
  70. */
  71. protected $_captureType;
  72. /**
  73. * Constructor
  74. *
  75. * Set separator to PHP_EOL.
  76. *
  77. * @return void
  78. */
  79. public function __construct()
  80. {
  81. parent::__construct();
  82. $this->setSeparator(PHP_EOL);
  83. }
  84. /**
  85. * Return headStyle object
  86. *
  87. * Returns headStyle helper object; optionally, allows specifying
  88. *
  89. * @param string $content Stylesheet contents
  90. * @param string $placement Append, prepend, or set
  91. * @param string|array $attributes Optional attributes to utilize
  92. * @return Zend_View_Helper_HeadStyle
  93. */
  94. public function headStyle($content = null, $placement = 'APPEND', $attributes = array())
  95. {
  96. if ((null !== $content) && is_string($content)) {
  97. switch (strtoupper($placement)) {
  98. case 'SET':
  99. $action = 'setStyle';
  100. break;
  101. case 'PREPEND':
  102. $action = 'prependStyle';
  103. break;
  104. case 'APPEND':
  105. default:
  106. $action = 'appendStyle';
  107. break;
  108. }
  109. $this->$action($content, $attributes);
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Overload method calls
  115. *
  116. * Allows the following method calls:
  117. * - appendStyle($content, $attributes = array())
  118. * - offsetSetStyle($index, $content, $attributes = array())
  119. * - prependStyle($content, $attributes = array())
  120. * - setStyle($content, $attributes = array())
  121. *
  122. * @param string $method
  123. * @param array $args
  124. * @return void
  125. * @throws Zend_View_Exception When no $content provided or invalid method
  126. */
  127. public function __call($method, $args)
  128. {
  129. if (preg_match('/^(?P<action>set|(ap|pre)pend|offsetSet)(Style)$/', $method, $matches)) {
  130. $index = null;
  131. $argc = count($args);
  132. $action = $matches['action'];
  133. if ('offsetSet' == $action) {
  134. if (0 < $argc) {
  135. $index = array_shift($args);
  136. --$argc;
  137. }
  138. }
  139. if (1 > $argc) {
  140. require_once 'Zend/View/Exception.php';
  141. $e = new Zend_View_Exception(sprintf('Method "%s" requires minimally content for the stylesheet', $method));
  142. $e->setView($this->view);
  143. throw $e;
  144. }
  145. $content = $args[0];
  146. $attrs = array();
  147. if (isset($args[1])) {
  148. $attrs = (array) $args[1];
  149. }
  150. $item = $this->createData($content, $attrs);
  151. if ('offsetSet' == $action) {
  152. $this->offsetSet($index, $item);
  153. } else {
  154. $this->$action($item);
  155. }
  156. return $this;
  157. }
  158. return parent::__call($method, $args);
  159. }
  160. /**
  161. * Determine if a value is a valid style tag
  162. *
  163. * @param mixed $value
  164. * @param string $method
  165. * @return boolean
  166. */
  167. protected function _isValid($value)
  168. {
  169. if ((!$value instanceof stdClass)
  170. || !isset($value->content)
  171. || !isset($value->attributes))
  172. {
  173. return false;
  174. }
  175. return true;
  176. }
  177. /**
  178. * Override append to enforce style creation
  179. *
  180. * @param mixed $value
  181. * @return void
  182. */
  183. public function append($value)
  184. {
  185. if (!$this->_isValid($value)) {
  186. require_once 'Zend/View/Exception.php';
  187. $e = new Zend_View_Exception('Invalid value passed to append; please use appendStyle()');
  188. $e->setView($this->view);
  189. throw $e;
  190. }
  191. return $this->getContainer()->append($value);
  192. }
  193. /**
  194. * Override offsetSet to enforce style creation
  195. *
  196. * @param string|int $index
  197. * @param mixed $value
  198. * @return void
  199. */
  200. public function offsetSet($index, $value)
  201. {
  202. if (!$this->_isValid($value)) {
  203. require_once 'Zend/View/Exception.php';
  204. $e = new Zend_View_Exception('Invalid value passed to offsetSet; please use offsetSetStyle()');
  205. $e->setView($this->view);
  206. throw $e;
  207. }
  208. return $this->getContainer()->offsetSet($index, $value);
  209. }
  210. /**
  211. * Override prepend to enforce style creation
  212. *
  213. * @param mixed $value
  214. * @return void
  215. */
  216. public function prepend($value)
  217. {
  218. if (!$this->_isValid($value)) {
  219. require_once 'Zend/View/Exception.php';
  220. $e = new Zend_View_Exception('Invalid value passed to prepend; please use prependStyle()');
  221. $e->setView($this->view);
  222. throw $e;
  223. }
  224. return $this->getContainer()->prepend($value);
  225. }
  226. /**
  227. * Override set to enforce style creation
  228. *
  229. * @param mixed $value
  230. * @return void
  231. */
  232. public function set($value)
  233. {
  234. if (!$this->_isValid($value)) {
  235. require_once 'Zend/View/Exception.php';
  236. $e = new Zend_View_Exception('Invalid value passed to set; please use setStyle()');
  237. $e->setView($this->view);
  238. throw $e;
  239. }
  240. return $this->getContainer()->set($value);
  241. }
  242. /**
  243. * Start capture action
  244. *
  245. * @param mixed $captureType
  246. * @param string $typeOrAttrs
  247. * @return void
  248. */
  249. public function captureStart($type = Zend_View_Helper_Placeholder_Container_Abstract::APPEND, $attrs = null)
  250. {
  251. if ($this->_captureLock) {
  252. require_once 'Zend/View/Helper/Placeholder/Container/Exception.php';
  253. $e = new Zend_View_Helper_Placeholder_Container_Exception('Cannot nest headStyle captures');
  254. $e->setView($this->view);
  255. throw $e;
  256. }
  257. $this->_captureLock = true;
  258. $this->_captureAttrs = $attrs;
  259. $this->_captureType = $type;
  260. ob_start();
  261. }
  262. /**
  263. * End capture action and store
  264. *
  265. * @return void
  266. */
  267. public function captureEnd()
  268. {
  269. $content = ob_get_clean();
  270. $attrs = $this->_captureAttrs;
  271. $this->_captureAttrs = null;
  272. $this->_captureLock = false;
  273. switch ($this->_captureType) {
  274. case Zend_View_Helper_Placeholder_Container_Abstract::SET:
  275. $this->setStyle($content, $attrs);
  276. break;
  277. case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
  278. $this->prependStyle($content, $attrs);
  279. break;
  280. case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
  281. default:
  282. $this->appendStyle($content, $attrs);
  283. break;
  284. }
  285. }
  286. /**
  287. * Convert content and attributes into valid style tag
  288. *
  289. * @param stdClass $item Item to render
  290. * @param string $indent Indentation to use
  291. * @return string
  292. */
  293. public function itemToString(stdClass $item, $indent)
  294. {
  295. $attrString = '';
  296. if (!empty($item->attributes)) {
  297. $enc = 'UTF-8';
  298. if ($this->view instanceof Zend_View_Interface
  299. && method_exists($this->view, 'getEncoding')
  300. ) {
  301. $enc = $this->view->getEncoding();
  302. }
  303. foreach ($item->attributes as $key => $value) {
  304. if (!in_array($key, $this->_optionalAttributes)) {
  305. continue;
  306. }
  307. if ('media' == $key) {
  308. if(false === strpos($value, ',')) {
  309. if (!in_array($value, $this->_mediaTypes)) {
  310. continue;
  311. }
  312. } else {
  313. $media_types = explode(',', $value);
  314. $value = '';
  315. foreach($media_types as $type) {
  316. $type = trim($type);
  317. if (!in_array($type, $this->_mediaTypes)) {
  318. continue;
  319. }
  320. $value .= $type .',';
  321. }
  322. $value = substr($value, 0, -1);
  323. }
  324. }
  325. $attrString .= sprintf(' %s="%s"', $key, htmlspecialchars($value, ENT_COMPAT, $enc));
  326. }
  327. }
  328. $escapeStart = $indent . '<!--'. PHP_EOL;
  329. $escapeEnd = $indent . '-->'. PHP_EOL;
  330. if (isset($item->attributes['conditional'])
  331. && !empty($item->attributes['conditional'])
  332. && is_string($item->attributes['conditional'])
  333. ) {
  334. $escapeStart = null;
  335. $escapeEnd = null;
  336. }
  337. $html = '<style type="text/css"' . $attrString . '>' . PHP_EOL
  338. . $escapeStart . $indent . $item->content . PHP_EOL . $escapeEnd
  339. . '</style>';
  340. if (null == $escapeStart && null == $escapeEnd) {
  341. if (str_replace(' ', '', $item->attributes['conditional']) === '!IE') {
  342. $html = '<!-->' . $html . '<!--';
  343. }
  344. $html = '<!--[if ' . $item->attributes['conditional'] . ']>' . $html . '<![endif]-->';
  345. }
  346. return $html;
  347. }
  348. /**
  349. * Create string representation of placeholder
  350. *
  351. * @param string|int $indent
  352. * @return string
  353. */
  354. public function toString($indent = null)
  355. {
  356. $indent = (null !== $indent)
  357. ? $this->getWhitespace($indent)
  358. : $this->getIndent();
  359. $items = array();
  360. $this->getContainer()->ksort();
  361. foreach ($this as $item) {
  362. if (!$this->_isValid($item)) {
  363. continue;
  364. }
  365. $items[] = $this->itemToString($item, $indent);
  366. }
  367. $return = $indent . implode($this->getSeparator() . $indent, $items);
  368. $return = preg_replace("/(\r\n?|\n)/", '$1' . $indent, $return);
  369. return $return;
  370. }
  371. /**
  372. * Create data item for use in stack
  373. *
  374. * @param string $content
  375. * @param array $attributes
  376. * @return stdClass
  377. */
  378. public function createData($content, array $attributes)
  379. {
  380. if (!isset($attributes['media'])) {
  381. $attributes['media'] = 'screen';
  382. } else if(is_array($attributes['media'])) {
  383. $attributes['media'] = implode(',', $attributes['media']);
  384. }
  385. $data = new stdClass();
  386. $data->content = $content;
  387. $data->attributes = $attributes;
  388. return $data;
  389. }
  390. }