summaryrefslogtreecommitdiff
path: root/framework/Web/TAssetManager.php
blob: 4bc52eed4b7927d64e278dfff62981eee1cc5fec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
 * TAssetManager class
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Revision: $  $Date: $
 * @package System.Web
 */

/**
 * TAssetManager class
 *
 * TAssetManager provides a scheme to allow web clients visiting
 * private files that are normally web-inaccessible.
 *
 * TAssetManager will copy the file to be published into a web-accessible
 * directory. The default base directory for storing the file is "assets", which
 * should be under the application directory. This can be changed by setting
 * the BasePath property together with the BaseUrl property that refers to
 * the URL for accessing the base path.
 *
 * By default, TAssetManager will not publish a file or directory if it already
 * exists in the publishing directory. You may require a timestamp checking by
 * setting CheckTimestamp to true (which is false by default). You may also require
 * so when calling {@link publishFile} or {@link publishDirectory). This is usually
 * very useful during development. In production sites, the timestamp checking
 * should be turned off to improve performance.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Revision: $  $Date: $
 * @package System.Web
 * @since 3.0
 */
class TAssetManager extends TComponent implements IModule
{
	/**
	 * Default web accessible base path for storing private files
	 */
	const DEFAULT_BASEPATH='assets';
	/**
	 * @var string base web accessible path for storing private files
	 */
	private $_basePath=null;
	/**
	 * @var string base URL for accessing the publishing directory.
	 */
	private $_baseUrl=null;
	/**
	 * @var string module ID
	 */
	private $_id;
	/**
	 * @var boolean whether to use timestamp checking to ensure files are published with up-to-date versions.
	 */
	private $_checkTimestamp=false;

	/**
	 * Initializes the module.
	 * This method is required by IModule and is invoked by application.
	 * @param IApplication application
	 * @param TXmlElement module configuration
	 */
	public function init($application,$config)
	{
		if($this->_basePath===null)
			$this->_basePath=dirname($application->getRequest()->getPhysicalApplicationPath()).'/'.self::DEFAULT_BASEPATH;
		if(!is_writable($this->_basePath) || !is_dir($this->_basePath))
			throw new TConfigurationException('assetmanager_basepath_invalid',$this->_basePath);
		if($this->_baseUrl===null)
			$this->_baseUrl=dirname($application->getRequest()->getApplicationPath()).'/'.self::DEFAULT_BASEPATH;
	}

	/**
	 * @return string id of this module
	 */
	public function getID()
	{
		return $this->_id;
	}

	/**
	 * @param string id of this module
	 */
	public function setID($value)
	{
		$this->_id=$value;
	}

	/**
	 * @return string the root directory storing published asset files
	 */
	public function getBasePath()
	{
		return $this->_basePath;
	}

	/**
	 * @param string the root directory storing published asset files
	 * @throws TInvalidOperationException if the service is initialized already
	 */
	public function setBasePath($value)
	{
		if($this->_initialized)
			throw new TInvalidOperationException('assetmanager_basepath_unchangeable');
		else if(is_dir($value))
			$this->_basePath=realpath($value);
		else
			throw new TInvalidDataValueException('assetmanage_basepath_invalid',$value);
	}

	/**
	 * @return string the base url that the published asset files can be accessed
	 */
	public function getBaseUrl()
	{
		return $this->_baseUrl;
	}

	/**
	 * @param string the base url that the published asset files can be accessed
	 * @throws TInvalidOperationException if the service is initialized already
	 */
	public function setBaseUrl($value)
	{
		if($this->_initialized)
			throw new TInvalidOperationException('pageservice_baseurl_unchangeable');
		else
			$this->_baseUrl=$value;
	}

	/**
	 * @return boolean whether file modify time should be used to ensure a published file is latest. Defaults to false.
	 */
	public function getCheckTimestamp()
	{
		return $this->_checkTimestamp;
	}

	/**
	 * @param boolean whether file modify time should be used to ensure a published file is latest. Defaults to false.
	 */
	public function setCheckTimestamp($value)
	{
		if($this->_initialized)
			throw new TInvalidOperationException('pageservice_checktimestamp_unchangeable');
		else
			$this->_checkTimestamp=TPropertyValue::ensureBoolean($value);
	}

	/**
	 * Publishes a directory (recursively).
	 * This method will copy the content in a directory (recursively) to
	 * a web accessible directory and returns the URL for the directory.
	 * @param string the path to be published
	 * @param boolean whether to use file modify time to ensure every published file is latest
	 * @return string an absolute URL to the published directory
	 */
	public function publishDirectory($path,$checkTimestamp=false)
	{
		if(($fullpath=realpath($path))!==false && is_dir($fullpath))
		{
			$dir=md5($fullpath);
			if(!is_dir($this->_basePath.'/'.$dir) || $checkTimestamp || $this->_checkTimestamp)
				$this->copyDirectory($fullpath,$this->_basePath.'/'.$dir);
			return $this->_baseUrl.'/'.$dir;
		}
		else
			throw new TInvalidDataValueException('assetmanager_directory_invalid',$path);
	}

	/**
	 * Copies a directory recursively as another.
	 * If the destination directory does not exist, it will be created.
	 * File modification time is used to ensure the copied files are latest.
	 * @param string the source directory
	 * @param string the destination directory
	 */
	protected function copyDirectory($src,$dst)
	{
		@mkdir($dst);
		$folder=opendir($src);
		while($file=readdir($folder))
		{
			if($file==='.' || $file==='..')
				continue;
			else if(is_file($src.'/'.$file))
			{
				if(@filemtime($dst.'/'.$file)<filemtime($src.'/'.$file))
					copy($src.'/'.$file,$dst.'/'.$file);
			}
			else
				$this->copyDirectory($src.'/'.$file,$dst.'/'.$file);
		}
		closedir($folder);
	}

	/**
	 * Publishes a file.
	 * This method will copy a file to a web accessible directory and
	 * returns the URL for the file.
	 * @param string the file to be published
	 * @param boolean whether to use file modify time to ensure the published file is latest
	 * @return string an absolute URL to the published file
	 */
	public function publishFile($path,$checkTimestamp=false)
	{
		if(($fullpath=realpath($path))!==false && is_file($fullpath))
		{
			$dir=md5(dirname($fullpath));
			$file=$this->_basePath.'/'.$dir.'/'.basename($fullpath);

			if(!is_file($file) || (($checkTimestamp || $this->_checkTimestamp) && filemtime($file)<filemtime($path)))
			{
				@mkdir($this->_basePath.'/'.$dir);
				copy($fullpath,$file);
			}
			return $this->_baseUrl.'/'.$dir.'/'.basename($fullpath);
		}
		else
			throw new TInvalidDataValueException('assetmanager_file_invalid',$path);
	}
}

?>