1: <?php
2: /**
3: * Wei Framework
4: *
5: * @copyright Copyright (c) 2008-2013 Twin Huang
6: * @license http://opensource.org/licenses/mit-license.php MIT License
7: */
8:
9: namespace Wei;
10:
11: /**
12: * A service to generate assets' URL
13: *
14: * @author Twin Huang <twinhuang@qq.com>
15: */
16: class Asset extends Base
17: {
18: /**
19: * The base URL of assets
20: *
21: * @var string
22: */
23: protected $baseUrl = '/';
24:
25: /**
26: * The base concat URL of assets
27: *
28: * @var string
29: */
30: protected $concatUrl = '/concat';
31:
32: /**
33: * The version number append to the URL
34: *
35: * @var string
36: */
37: protected $version = '1';
38:
39: /**
40: * Returns the asset or concat URL by specified file
41: *
42: * @param string $file
43: * @param bool $version Whether append version or not
44: * @return string
45: */
46: public function __invoke($file, $version = true)
47: {
48: if (is_array($file)) {
49: return $this->concat($file);
50: } else {
51: $url = $this->baseUrl . $file;
52: if ($version && $this->version) {
53: $url .= ((false === strpos($url, '?')) ? '?' : '&') . 'v=' . $this->version;
54: }
55: return $url;
56: }
57: }
58:
59: /**
60: * Returns the concat URL for list of files
61: *
62: * @param array $files
63: * @return string
64: */
65: public function concat(array $files)
66: {
67: return $this->concatUrl . '?b=' . trim($this->baseUrl, '/') .'&f=' . implode(',', $files);
68: }
69:
70: /**
71: * Returns the base url of assets
72: *
73: * @return string
74: */
75: public function getBaseUrl()
76: {
77: return $this->baseUrl;
78: }
79:
80: /**
81: * Sets the base url of assets
82: *
83: * @param string $baseUrl
84: * @return $this
85: */
86: public function setBaseUrl($baseUrl)
87: {
88: $this->baseUrl = $baseUrl;
89: return $this;
90: }
91: }