PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/a10/lib/yii-1.1.10/web/CAssetManager.php

http://chenjin.googlecode.com/
PHP | 304 lines | 133 code | 14 blank | 157 comment | 27 complexity | 4f2a87afc2c985a8948f8a6ee6851eec MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * CAssetManager class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CAssetManager is a Web application component that manages private files (called assets) and makes them accessible by Web clients.
  12. *
  13. * It achieves this goal by copying assets to a Web-accessible directory
  14. * and returns the corresponding URL for accessing them.
  15. *
  16. * To publish an asset, simply call {@link publish()}.
  17. *
  18. * The Web-accessible directory holding the published files is specified
  19. * by {@link setBasePath basePath}, which defaults to the "assets" directory
  20. * under the directory containing the application entry script file.
  21. * The property {@link setBaseUrl baseUrl} refers to the URL for accessing
  22. * the {@link setBasePath basePath}.
  23. *
  24. * @property string $basePath The root directory storing the published asset files. Defaults to 'WebRoot/assets'.
  25. * @property string $baseUrl The base url that the published asset files can be accessed.
  26. * Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'.
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @version $Id: CAssetManager.php 242 2012-03-29 15:18:01Z mole1230 $
  30. * @package system.web
  31. * @since 1.0
  32. */
  33. class CAssetManager extends CApplicationComponent
  34. {
  35. /**
  36. * Default web accessible base path for storing private files
  37. */
  38. const DEFAULT_BASEPATH='assets';
  39. /**
  40. * @var boolean whether to use symbolic link to publish asset files. Defaults to false, meaning
  41. * asset files are copied to public folders. Using symbolic links has the benefit that the published
  42. * assets will always be consistent with the source assets. This is especially useful during development.
  43. *
  44. * However, there are special requirements for hosting environments in order to use symbolic links.
  45. * In particular, symbolic links are supported only on Linux/Unix, and Windows Vista/2008 or greater.
  46. * The latter requires PHP 5.3 or greater.
  47. *
  48. * Moreover, some Web servers need to be properly configured so that the linked assets are accessible
  49. * to Web users. For example, for Apache Web server, the following configuration directive should be added
  50. * for the Web folder:
  51. * <pre>
  52. * Options FollowSymLinks
  53. * </pre>
  54. *
  55. * @since 1.1.5
  56. */
  57. public $linkAssets=false;
  58. /**
  59. * @var array list of directories and files which should be excluded from the publishing process.
  60. * Defaults to exclude '.svn' files only. This option has no effect if {@link linkAssets} is enabled.
  61. * @since 1.1.6
  62. **/
  63. public $excludeFiles=array('.svn');
  64. /**
  65. * @var integer the permission to be set for newly generated asset files.
  66. * This value will be used by PHP chmod function.
  67. * Defaults to 0666, meaning the file is read-writable by all users.
  68. * @since 1.1.8
  69. */
  70. public $newFileMode=0666;
  71. /**
  72. * @var integer the permission to be set for newly generated asset directories.
  73. * This value will be used by PHP chmod function.
  74. * Defaults to 0777, meaning the directory can be read, written and executed by all users.
  75. * @since 1.1.8
  76. */
  77. public $newDirMode=0777;
  78. /**
  79. * @var string base web accessible path for storing private files
  80. */
  81. private $_basePath;
  82. /**
  83. * @var string base URL for accessing the publishing directory.
  84. */
  85. private $_baseUrl;
  86. /**
  87. * @var array published assets
  88. */
  89. private $_published=array();
  90. /**
  91. * @return string the root directory storing the published asset files. Defaults to 'WebRoot/assets'.
  92. */
  93. public function getBasePath()
  94. {
  95. if($this->_basePath===null)
  96. {
  97. $request=Yii::app()->getRequest();
  98. $this->setBasePath(dirname($request->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
  99. }
  100. return $this->_basePath;
  101. }
  102. /**
  103. * Sets the root directory storing published asset files.
  104. * @param string $value the root directory storing published asset files
  105. * @throws CException if the base path is invalid
  106. */
  107. public function setBasePath($value)
  108. {
  109. if(($basePath=realpath($value))!==false && is_dir($basePath) && is_writable($basePath))
  110. $this->_basePath=$basePath;
  111. else
  112. throw new CException(Yii::t('yii','CAssetManager.basePath "{path}" is invalid. Please make sure the directory exists and is writable by the Web server process.',
  113. array('{path}'=>$value)));
  114. }
  115. /**
  116. * @return string the base url that the published asset files can be accessed.
  117. * Note, the ending slashes are stripped off. Defaults to '/AppBaseUrl/assets'.
  118. */
  119. public function getBaseUrl()
  120. {
  121. if($this->_baseUrl===null)
  122. {
  123. $request=Yii::app()->getRequest();
  124. $this->setBaseUrl($request->getBaseUrl().'/'.self::DEFAULT_BASEPATH);
  125. }
  126. return $this->_baseUrl;
  127. }
  128. /**
  129. * @param string $value the base url that the published asset files can be accessed
  130. */
  131. public function setBaseUrl($value)
  132. {
  133. $this->_baseUrl=rtrim($value,'/');
  134. }
  135. /**
  136. * Publishes a file or a directory.
  137. * This method will copy the specified asset to a web accessible directory
  138. * and return the URL for accessing the published asset.
  139. * <ul>
  140. * <li>If the asset is a file, its file modification time will be checked
  141. * to avoid unnecessary file copying;</li>
  142. * <li>If the asset is a directory, all files and subdirectories under it will
  143. * be published recursively. Note, in case $forceCopy is false the method only checks the
  144. * existence of the target directory to avoid repetitive copying.</li>
  145. * </ul>
  146. *
  147. * Note: On rare scenario, a race condition can develop that will lead to a
  148. * one-time-manifestation of a non-critical problem in the creation of the directory
  149. * that holds the published assets. This problem can be avoided altogether by 'requesting'
  150. * in advance all the resources that are supposed to trigger a 'publish()' call, and doing
  151. * that in the application deployment phase, before system goes live. See more in the following
  152. * discussion: http://code.google.com/p/yii/issues/detail?id=2579
  153. *
  154. * @param string $path the asset (file or directory) to be published
  155. * @param boolean $hashByName whether the published directory should be named as the hashed basename.
  156. * If false, the name will be the hash taken from dirname of the path being published and path mtime.
  157. * Defaults to false. Set true if the path being published is shared among
  158. * different extensions.
  159. * @param integer $level level of recursive copying when the asset is a directory.
  160. * Level -1 means publishing all subdirectories and files;
  161. * Level 0 means publishing only the files DIRECTLY under the directory;
  162. * level N means copying those directories that are within N levels.
  163. * @param boolean $forceCopy whether we should copy the asset file or directory even if it is already published before.
  164. * This parameter is set true mainly during development stage when the original
  165. * assets are being constantly changed. The consequence is that the performance
  166. * is degraded, which is not a concern during development, however.
  167. * This parameter has been available since version 1.1.2.
  168. * @return string an absolute URL to the published asset
  169. * @throws CException if the asset to be published does not exist.
  170. */
  171. public function publish($path,$hashByName=false,$level=-1,$forceCopy=false)
  172. {
  173. if(isset($this->_published[$path]))
  174. return $this->_published[$path];
  175. else if(($src=realpath($path))!==false)
  176. {
  177. if(is_file($src))
  178. {
  179. $dir=$this->hash($hashByName ? basename($src) : dirname($src).filemtime($src));
  180. $fileName=basename($src);
  181. $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
  182. $dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName;
  183. if($this->linkAssets)
  184. {
  185. if(!is_file($dstFile))
  186. {
  187. if(!is_dir($dstDir))
  188. {
  189. mkdir($dstDir);
  190. @chmod($dstDir, $this->newDirMode);
  191. }
  192. symlink($src,$dstFile);
  193. }
  194. }
  195. else if(@filemtime($dstFile)<@filemtime($src))
  196. {
  197. if(!is_dir($dstDir))
  198. {
  199. mkdir($dstDir);
  200. @chmod($dstDir, $this->newDirMode);
  201. }
  202. copy($src,$dstFile);
  203. @chmod($dstFile, $this->newFileMode);
  204. }
  205. return $this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName";
  206. }
  207. else if(is_dir($src))
  208. {
  209. $dir=$this->hash($hashByName ? basename($src) : $src.filemtime($src));
  210. $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;
  211. if($this->linkAssets)
  212. {
  213. if(!is_dir($dstDir))
  214. symlink($src,$dstDir);
  215. }
  216. else if(!is_dir($dstDir) || $forceCopy)
  217. {
  218. CFileHelper::copyDirectory($src,$dstDir,array(
  219. 'exclude'=>$this->excludeFiles,
  220. 'level'=>$level,
  221. 'newDirMode'=>$this->newDirMode,
  222. 'newFileMode'=>$this->newFileMode,
  223. ));
  224. }
  225. return $this->_published[$path]=$this->getBaseUrl().'/'.$dir;
  226. }
  227. }
  228. throw new CException(Yii::t('yii','The asset "{asset}" to be published does not exist.',
  229. array('{asset}'=>$path)));
  230. }
  231. /**
  232. * Returns the published path of a file path.
  233. * This method does not perform any publishing. It merely tells you
  234. * if the file or directory is published, where it will go.
  235. * @param string $path directory or file path being published
  236. * @param boolean $hashByName whether the published directory should be named as the hashed basename.
  237. * If false, the name will be the hash taken from dirname of the path being published and path mtime.
  238. * Defaults to false. Set true if the path being published is shared among
  239. * different extensions.
  240. * @return string the published file path. False if the file or directory does not exist
  241. */
  242. public function getPublishedPath($path,$hashByName=false)
  243. {
  244. if(($path=realpath($path))!==false)
  245. {
  246. $base=$this->getBasePath().DIRECTORY_SEPARATOR;
  247. if(is_file($path))
  248. return $base . $this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)) . DIRECTORY_SEPARATOR . basename($path);
  249. else
  250. return $base . $this->hash($hashByName ? basename($path) : $path.filemtime($path));
  251. }
  252. else
  253. return false;
  254. }
  255. /**
  256. * Returns the URL of a published file path.
  257. * This method does not perform any publishing. It merely tells you
  258. * if the file path is published, what the URL will be to access it.
  259. * @param string $path directory or file path being published
  260. * @param boolean $hashByName whether the published directory should be named as the hashed basename.
  261. * If false, the name will be the hash taken from dirname of the path being published and path mtime.
  262. * Defaults to false. Set true if the path being published is shared among
  263. * different extensions.
  264. * @return string the published URL for the file or directory. False if the file or directory does not exist.
  265. */
  266. public function getPublishedUrl($path,$hashByName=false)
  267. {
  268. if(isset($this->_published[$path]))
  269. return $this->_published[$path];
  270. if(($path=realpath($path))!==false)
  271. {
  272. if(is_file($path))
  273. return $this->getBaseUrl().'/'.$this->hash($hashByName ? basename($path) : dirname($path).filemtime($path)).'/'.basename($path);
  274. else
  275. return $this->getBaseUrl().'/'.$this->hash($hashByName ? basename($path) : $path.filemtime($path));
  276. }
  277. else
  278. return false;
  279. }
  280. /**
  281. * Generate a CRC32 hash for the directory path. Collisions are higher
  282. * than MD5 but generates a much smaller hash string.
  283. * @param string $path string to be hashed.
  284. * @return string hashed string.
  285. */
  286. protected function hash($path)
  287. {
  288. return sprintf('%x',crc32($path.Yii::getVersion()));
  289. }
  290. }