PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_widgetkit/helpers/asset.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 623 lines | 218 code | 100 blank | 305 comment | 15 complexity | fa74f8b1d41f77dffd6d8244a5f25e6d MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Widgetkit
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: AssetWidgetkitHelper
  10. Asset helper class, to manage assets
  11. */
  12. class AssetWidgetkitHelper extends WidgetkitHelper {
  13. protected $assets;
  14. protected $options;
  15. /*
  16. Function: __construct
  17. Class Constructor.
  18. */
  19. public function __construct($widgetkit) {
  20. parent::__construct($widgetkit);
  21. // init vars
  22. $this->assets = array();
  23. $this->options = array('base_path' => $this['system']->path, 'base_url' => rtrim($this['path']->url('site:'), '/'));
  24. }
  25. /*
  26. Function: get
  27. Get a asset collection
  28. Parameters:
  29. $name - String
  30. Returns:
  31. Mixed
  32. */
  33. public function get($name) {
  34. return isset($this->assets[$name]) ? $this->assets[$name] : null;
  35. }
  36. /*
  37. Function: createString
  38. Create a string asset
  39. Parameters:
  40. $input - String
  41. $options - Array
  42. Returns:
  43. Object
  44. */
  45. public function createString($input, $options = array()) {
  46. return new WidgetkitStringAsset($input, array_merge($options, $this->options));
  47. }
  48. /*
  49. Function: createFile
  50. Create a file asset
  51. Parameters:
  52. $input - String
  53. $options - Array
  54. Returns:
  55. Object
  56. */
  57. public function createFile($input, $options = array()) {
  58. $url = $input;
  59. $path = null;
  60. if (!preg_match('/^(http|https)\:\/\//i', $input)) {
  61. // resource identifier ?
  62. if ($path = $this['path']->path($input)) {
  63. $url = $this['path']->url($input);
  64. }
  65. // absolute/relative path ?
  66. if (!$path) {
  67. $path = realpath($this->options['base_path'].'/'.ltrim(preg_replace('/'.preg_quote(str_replace(DIRECTORY_SEPARATOR, '/', $this->options['base_url']), '/').'/', '', $input, 1), '/'));
  68. }
  69. }
  70. return new WidgetkitFileAsset($url, $path, array_merge($options, $this->options));
  71. }
  72. /*
  73. Function: addString
  74. Add a string asset
  75. Parameters:
  76. $name - String
  77. $input - String
  78. $options - Array
  79. Returns:
  80. Void
  81. */
  82. public function addString($name, $input, $options = array()) {
  83. return $this->addAsset($name, $this->createString($input, $options));
  84. }
  85. /*
  86. Function: addFile
  87. Add a file asset
  88. Parameters:
  89. $name - String
  90. $input - String
  91. $options - Array
  92. Returns:
  93. Mixed
  94. */
  95. public function addFile($name, $input, $options = array()) {
  96. return $this->addAsset($name, $this->createFile($input, $options));
  97. }
  98. /*
  99. Function: addAsset
  100. Add asset object
  101. Parameters:
  102. $name - String
  103. $asset - Object
  104. Returns:
  105. Void
  106. */
  107. protected function addAsset($name, $asset) {
  108. if (!isset($this->assets[$name])) {
  109. $this->assets[$name] = new WidgetkitAssetCollection();
  110. }
  111. $this->assets[$name]->add($asset);
  112. return $asset;
  113. }
  114. /*
  115. Function: cache
  116. Apply filters and cache a asset
  117. Parameters:
  118. $file - String
  119. $asset - Object
  120. $filters - Array
  121. $options - Array
  122. Returns:
  123. Object
  124. */
  125. public function cache($file, $asset, $filters = array(), $options = array()) {
  126. // init vars
  127. $hash = substr($asset->hash(serialize($filters)), 0, 8);
  128. $options = array_merge(array('Gzip' => false), $options);
  129. // copy gzip file, if not exists
  130. if ($options['Gzip'] && !$this['path']->path('cache:gzip.php')) {
  131. @copy($this['path']->path('widgetkit:gzip/gzip.php'), rtrim($this['path']->path('cache:'), '/').'/gzip.php');
  132. }
  133. // append cache file suffix based on hash
  134. if ($extension = pathinfo($file, PATHINFO_EXTENSION)) {
  135. $file = preg_replace('/'.preg_quote('.'.$extension, '/').'$/', sprintf('-%s.%s', $hash, $extension), $file, 1);
  136. } else {
  137. $file .= '-'.$hash;
  138. }
  139. // create cache file, if not exists
  140. if (!$this['path']->path('cache:'.$file)) {
  141. @file_put_contents($this['path']->path('cache:').'/'.ltrim($file, '/'), $asset->getContent($this['assetfilter']->create($filters)));
  142. }
  143. $asset->setUrl($this['path']->url(($options['Gzip'] && $this['path']->path('cache:gzip.php') ? 'cache:gzip.php?' : 'cache:').$file));
  144. return $asset;
  145. }
  146. }
  147. /*
  148. Interface: WidgetkitAssetInterface
  149. Asset interface
  150. */
  151. interface WidgetkitAssetInterface {
  152. public function getUrl();
  153. public function setUrl($url);
  154. public function getContent($filter = null);
  155. public function setContent($content);
  156. public function load($filter = null);
  157. public function hash($salt = '');
  158. }
  159. /*
  160. Class: WidgetkitAssetOptions
  161. Asset options class, provides options implementation
  162. */
  163. abstract class WidgetkitAssetOptions implements ArrayAccess {
  164. protected $options;
  165. /*
  166. Function: __construct
  167. Class Constructor.
  168. */
  169. public function __construct($options = array()) {
  170. $this->options = $options;
  171. }
  172. /* ArrayAccess interface implementation */
  173. public function offsetSet($name, $value) {
  174. $this->options[$name] = $value;
  175. }
  176. public function offsetGet($name) {
  177. return isset($this->options[$name]) ? $this->options[$name] : null;
  178. }
  179. public function offsetExists($name) {
  180. return isset($this->options[$name]);
  181. }
  182. public function offsetUnset($name) {
  183. unset($this->options[$name]);
  184. }
  185. }
  186. /*
  187. Class: WidgetkitAssetBase
  188. Asset base class
  189. */
  190. abstract class WidgetkitAssetBase extends WidgetkitAssetOptions implements WidgetkitAssetInterface {
  191. protected $url;
  192. protected $content;
  193. protected $loaded = false;
  194. /*
  195. Function: getType
  196. Get asset type
  197. Returns:
  198. String
  199. */
  200. public function getType() {
  201. return str_replace(array('Widgetkit', 'Asset'), array('', ''), get_class($this));
  202. }
  203. /*
  204. Function: getUrl
  205. Get asset url
  206. Returns:
  207. String
  208. */
  209. public function getUrl() {
  210. return $this->url;
  211. }
  212. /*
  213. Function: setUrl
  214. Set asset url
  215. Parameters:
  216. $url - String
  217. Returns:
  218. Void
  219. */
  220. public function setUrl($url) {
  221. $this->url = $url;
  222. }
  223. /*
  224. Function: getContent
  225. Get asset content and apply filters
  226. Returns:
  227. String
  228. */
  229. public function getContent($filter = null) {
  230. if (!$this->loaded) {
  231. $this->load($filter);
  232. }
  233. if ($filter) {
  234. $asset = clone $this;
  235. $filter->filterContent($asset);
  236. return $asset->getContent();
  237. }
  238. return $this->content;
  239. }
  240. /*
  241. Function: setContent
  242. Set asset content
  243. Parameters:
  244. $content - String
  245. Returns:
  246. Void
  247. */
  248. public function setContent($content) {
  249. $this->content = $content;
  250. }
  251. /*
  252. Function: doLoad
  253. Load asset and apply filters
  254. Parameters:
  255. $content - String
  256. $filter - Object
  257. Returns:
  258. Void
  259. */
  260. protected function doLoad($content, $filter = null) {
  261. $this->content = $content;
  262. if ($filter) {
  263. $filter->filterLoad($this);
  264. }
  265. $this->loaded = true;
  266. }
  267. }
  268. class WidgetkitStringAsset extends WidgetkitAssetBase {
  269. protected $string;
  270. /*
  271. Function: __construct
  272. Class Constructor.
  273. */
  274. public function __construct($string, $options = array()) {
  275. parent::__construct($options);
  276. $this->string = $string;
  277. }
  278. /*
  279. Function: load
  280. Load asset callback
  281. Parameters:
  282. $filter - Object
  283. Returns:
  284. Void
  285. */
  286. public function load($filter = null) {
  287. $this->doLoad($this->string, $filter);
  288. }
  289. /*
  290. Function: doLoad
  291. Load asset and apply filters
  292. Parameters:
  293. $content - String
  294. $filter - Object
  295. Returns:
  296. Void
  297. */
  298. public function hash($salt = '') {
  299. return md5($this->string.$salt);
  300. }
  301. }
  302. class WidgetkitFileAsset extends WidgetkitAssetBase {
  303. protected $path;
  304. /*
  305. Function: __construct
  306. Class Constructor.
  307. */
  308. public function __construct($url, $path, $options = array()) {
  309. parent::__construct($options);
  310. $this->url = $url;
  311. $this->path = $path;
  312. }
  313. /*
  314. Function: getPath
  315. Get asset file path
  316. Returns:
  317. String
  318. */
  319. public function getPath() {
  320. return $this->path;
  321. }
  322. /*
  323. Function: load
  324. Load asset callback
  325. Parameters:
  326. $filter - Object
  327. Returns:
  328. Void
  329. */
  330. public function load($filter = null) {
  331. if (file_exists($this->path)) {
  332. $this->doLoad(preg_replace('{^\xEF\xBB\xBF|\x1A}', '', file_get_contents($this->path)), $filter); // load with UTF-8 BOM removal
  333. }
  334. }
  335. /*
  336. Function: hash
  337. Get unique asset hash
  338. Parameters:
  339. $salt - String
  340. Returns:
  341. String
  342. */
  343. public function hash($salt = '') {
  344. return md5($this->path.filemtime($this->path).$salt);
  345. }
  346. }
  347. class WidgetkitAssetCollection extends WidgetkitAssetOptions implements WidgetkitAssetInterface, Iterator {
  348. protected $url;
  349. protected $content;
  350. protected $assets;
  351. /*
  352. Function: __construct
  353. Class Constructor.
  354. */
  355. public function __construct($assets = array(), $options = array()) {
  356. parent::__construct($options);
  357. $this->assets = new SplObjectStorage();
  358. if (!is_array($assets)) {
  359. $assets = array($assets);
  360. }
  361. foreach ($assets as $asset) {
  362. $this->add($asset);
  363. }
  364. }
  365. /*
  366. Function: getUrl
  367. Get asset url
  368. Returns:
  369. String
  370. */
  371. public function getUrl() {
  372. return $this->url;
  373. }
  374. /*
  375. Function: setUrl
  376. Set asset url
  377. Parameters:
  378. $url - String
  379. Returns:
  380. Void
  381. */
  382. public function setUrl($url) {
  383. $this->url = $url;
  384. }
  385. /*
  386. Function: getContent
  387. Get asset content and apply filters
  388. Returns:
  389. String
  390. */
  391. public function getContent($filter = null) {
  392. $content = array();
  393. foreach ($this as $asset) {
  394. $content[] = $asset->getContent($filter);
  395. }
  396. return implode("\n", $content);
  397. }
  398. /*
  399. Function: setContent
  400. Set asset content
  401. Parameters:
  402. $content - String
  403. Returns:
  404. Void
  405. */
  406. public function setContent($content) {
  407. $this->content = $content;
  408. }
  409. /*
  410. Function: load
  411. Load asset callback
  412. Parameters:
  413. $filter - Object
  414. Returns:
  415. Void
  416. */
  417. public function load($filter = null) {
  418. $content = array();
  419. foreach ($this as $asset) {
  420. $content[] = $asset->getContent($filter);
  421. }
  422. $this->content = implode("\n", $content);
  423. }
  424. /*
  425. Function: hash
  426. Get unique asset hash
  427. Parameters:
  428. $salt - String
  429. Returns:
  430. String
  431. */
  432. public function hash($salt = '') {
  433. $hashes = array();
  434. foreach ($this as $asset) {
  435. $hashes[] = $asset->hash($salt);
  436. }
  437. return md5(implode(' ', $hashes));
  438. }
  439. /*
  440. Function: add
  441. Add asset to collection
  442. Parameters:
  443. $filter - Object
  444. Returns:
  445. Void
  446. */
  447. public function add($asset) {
  448. $this->assets->attach($asset);
  449. }
  450. /*
  451. Function: remove
  452. Add asset from collection
  453. Parameters:
  454. $filter - Object
  455. Returns:
  456. Void
  457. */
  458. public function remove($asset) {
  459. $this->assets->detach($asset);
  460. }
  461. /* Iterator interface implementation */
  462. public function current() {
  463. return $this->assets->current();
  464. }
  465. public function key() {
  466. return $this->assets->key();
  467. }
  468. public function valid() {
  469. return $this->assets->valid();
  470. }
  471. public function next() {
  472. $this->assets->next();
  473. }
  474. public function rewind() {
  475. $this->assets->rewind();
  476. }
  477. }