ホームページ  >  記事  >  バックエンド開発  >  PHPで実装されたCSS更新クラス

PHPで実装されたCSS更新クラス

WBOY
WBOYオリジナル
2016-07-25 08:55:23828ブラウズ
  1. /**css更新クラス、cssファイル内の画像のバージョンを更新します
  2. * 日付: 2013-02-05
  3. * 著者: fdipzone
  4. * バージョン: 1.1
  5. * 編集: bbs.it-home.org
  6. * 機能:
  7. * update() ;
  8. *
  9. * Ver: 1.1 サブフォルダーをトラバースするための search_child パラメーターを追加しました
  10. */
  11. class CSSUpdate{
  12. private $csstmpl_path = null;
  13. プライベート $css_path = null;
  14. プライベート $replacetags = array();
  15. プライベート $search_child = false;
  16. プライベート $convert_num = 0;
  17. プライベート $is_ready = 0;
  18. /**初期化
  19. * @param String $csstmpl_path CSS テンプレート パス
  20. * @param String $css_path CSS ターゲット パス
  21. * @param Array $replacetags 置換される画像タイプ
  22. * @param boolean $search_child サブフォルダーを横断するかどうか、デフォルトは false
  23. */
  24. public function __construct($csstmpl_path, $css_path, $replacetags=array(), $search_child=false){
  25. if(!is_dir($csstmpl_path) || !is_dir($css_path) ) || !$replacetags){
  26. $this->is_ready = 0;
  27. }else{
  28. $this->csstmpl_path = $csstmpl_path;
  29. $this->css_path = $css_path;
  30. $this->replacetags = $replacetags;
  31. $this->search_child = $search_child;
  32. $this->is_ready = 1;
  33. }
  34. }
  35. /**CSSファイルを更新する*/
  36. public function update(){
  37. if($this->is_ready==0){
  38. $this->response('csstmpl または csspath または replacetags エラー');
  39. 「」を返します;
  40. }
  41. $this->traversing($this->csstmpl_path);
  42. $this->response('covert num:'.$this->convert_num);
  43. }
  44. /**フォルダーを走査
  45. * @param String $path ファイル パス
  46. */
  47. プライベート関数 traversing($path){
  48. $handle = opendir($path);
  49. while(($file=readdir($handle))!==false){
  50. if($file!='..' && $file!='.'){
  51. $curfile = $path.'/' .$ファイル;
  52. if(is_dir($curfile)){ // フォルダー
  53. if($this->search_child){ // 必要遍历子文件夹
  54. $this->traversing($curfile);
  55. }
  56. }elseif($this->checkExt($curfile)){ // CSS ファイル
  57. $dfile = str_replace($this->csstmpl_path, $this->css_path, $curfile);
  58. $this->create($curfile, $dfile);
  59. $this->response($curfile.' '.$dfile.' に変換成功');
  60. $this->convert_num ++;
  61. }
  62. }
  63. }
  64. Closedir($handle);
  65. }
  66. /**ファイルの拡張子をチェックする*/
  67. プライベート関数 checkExt($file){
  68. $name = Basename($file);
  69. $namefrag =explode('.', $name);
  70. if(count($namefrag)>=2){
  71. if(strto lower($namefrag[count($namefrag)-1])=='css'){ // css 文件
  72. return true;
  73. }
  74. }
  75. false を返します。
  76. }
  77. /**テンプレートの内容を置き換えて csspath に書き込みます
  78. * @param String $tmplfile テンプレート ファイル
  79. * @param String $dfile ターゲット ファイル
  80. */
  81. プライベート関数 create($tmplfile, $dfile){
  82. $css_content = file_get_contents($tmplfile);
  83. foreach($this->replacetags as $tag){
  84. $css_content = str_replace($tag, $tag."?".date('YmdHis'), $css_content);
  85. }
  86. if(!is_dir(dirname($dfile))){ // 生成目标路径
  87. mkdir(dirname($dfile), 0755, true);
  88. }
  89. file_put_contents($dfile, $css_content, true);
  90. }
  91. /**出力*/
  92. private function response($content){
  93. echo $content."
    ";
  94. }
  95. }
  96. ?>
复制代码

2、演示例demo.php

  1. require_once "CSSUpdate.class.php";
  2. define('ROOT_PATH', dirname(__FILE__));
  3. $css_path = ROOT_PATH.'/css';
  4. $csstmpl_path = ROOT_PATH.'/csstmpl';
  5. $replacetags = array('.png', '.jpg', '.gif');
  6. $cssobj = 新しい CSSUpdate($csstmpl_path, $css_path, $replacetags);
  7. $cssobj->update();
  8. ?>
卷制コード

附、php css更新类的ソースコード下方地址。



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。