Css Update class implemented by php
- /** Css Update class: updates the image version in the css file.
- * Date: 2013-02-05
- * Author: fdipzone
- * Ver: 1.1
- * Edit: bbs.it-home.org
- * Func:
- * Update ();
- *
- * Ver: 1.1 added the search_child parameter to traverse subfolders.
- */
-
- Class CSSUpdate {
- Private $ cs1_pl_path = null;
- Private $ css_path = null;
- Private $ replacetags = array ();
- Private $ search_child = false;
- Private $ convert_num = 0;
- Private $ is_ready = 0;
-
- /** Initialization
- * @ Param String $ csstmpl_path css template path
- * @ Param String $ css_path css target path
- * @ Param Array $ replacetags the image type to be replaced
- * @ Param boolean $ search_child: whether to traverse subfolders. the default value is false.
- */
- Public function _ construct ($ cs1_pl_path, $ css_path, $ replacetags = array (), $ search_child = false ){
- If (! Is_dir ($ cs1_pl_path) |! Is_dir ($ css_path) |! $ Replacetags ){
- $ This-> is_ready = 0;
- } Else {
- $ This-> cs1_pl_path = $ cs1_pl_path;
- $ This-> css_path = $ css_path;
- $ This-> replacetags = $ replacetags;
- $ This-> search_child = $ search_child;
- $ This-> is_ready = 1;
- }
- }
-
- /** UPDATE The css file */
- Public function update (){
- If ($ this-> is_ready = 0 ){
- $ This-> response ('csstmpl or csspath or replacetags error ');
- Return '';
- }
- $ This-> traversing ($ this-> cs1_pl_path );
- $ This-> response ('covert num: '. $ this-> convert_num );
- }
-
- /** Traverse folders
- * @ Param String $ path file path
- */
- Private function traversing ($ path ){
- $ Handle = opendir ($ path );
- While ($ file = readdir ($ handle ))! = False ){
- If ($ file! = '..' & $ File! = '.'){
- $ Curfile = $ path. '/'. $ file;
-
- If (is_dir ($ curfile) {// folder
- If ($ this-> search_child) {// you need to traverse subfolders.
- $ This-> traversing ($ curfile );
- }
- } Elseif ($ this-> checkExt ($ curfile) {// css file
- $ Dfile = str_replace ($ this-> csstmpl_path, $ this-> css_path, $ curfile );
- $ This-> create ($ curfile, $ dfile );
- $ This-> response ($ curfile. 'convert to '. $ dfile. 'success ');
- $ This-> convert_num ++;
- }
- }
- }
- Closedir ($ handle );
- }
-
- /** Check the file suffix */
- Private function checkExt ($ file ){
- $ Name = basename ($ file );
- $ Namefrag = explode ('.', $ name );
- If (count ($ namefrag)> = 2 ){
- If (strtolower ($ namefrag [count ($ namefrag)-1]) = 'css ') {// css file
- Return true;
- }
- }
- Return false;
- }
-
- /** Replace the template content and write it to csspath
- * @ Param String $ tmplfile template file
- * @ Param String $ dfile target file
- */
- Private function create ($ tmplfile, $ dfile ){
- $ Css_content = file_get_contents ($ tmplfile );
-
- Foreach ($ this-> replacetags as $ tag ){
- $ Css_content = str_replace ($ tag, $ tag ."? ". Date ('ymdhis '), $ css_content );
- }
-
- If (! Is_dir (dirname ($ dfile) {// Generate the target path
- Mkdir (dirname ($ dfile), 0755, true );
- }
-
- File_put_contents ($ dfile, $ css_content, true );
- }
-
- /** Output */
- Private function response ($ content ){
- Echo $ content ."
";
- }
- }
- ?>
2. demo. php
- Require_once "CSSUpdate. class. php ";
-
- Define ('root _ path', dirname (_ FILE __));
-
- $ Css_path = ROOT_PATH. '/css ';
- $ Cs1_pl_path = ROOT_PATH. '/csstmpl ';
- $ Replacetags = array('.png ', '.jpg', '.gif ');
-
- $ Cssobj = new CSSUpdate ($ cs1_pl_path, $ css_path, $ replacetags );
- $ Cssobj-> update ();
- ?>
-
Appendix: Source code of the php css Update class. |