CAssetManager and CFIleHelper improvements

Hi there,

I have found that these two classes (CAssetManager and CFIleHelper) does not allow me to change file/folder rights, It’s very unflexible in some ways therefore I have created 2 classes that extend these ones.

MyAssetManager extends CAssetManager




class MyAssetManager extends CAssetManager

{

  private $_published=array();

  

  private $_folder_rights = 0777;

  private $_file_rights = 0644;

  

  public function setFolderRights($value) {

    $this->_folder_rights = $value;

  }

  

  public function setFileRights($value) {

    $this->_file_rights = $value;

  }

  

  public function publish($path,$hashByName=false,$level=-1)

  {

    if(isset($this->_published[$path]))

      return $this->_published[$path];

    else if(($src=realpath($path))!==false)

    {

      if(is_file($src))

      {

        $dir=$this->hash($hashByName ? basename($src) : dirname($src));

        $fileName=basename($src);

        $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;

        $dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName;


        if(@filemtime($dstFile)<@filemtime($src))

        {

          $oldmask = umask(0);

          if(!is_dir($dstDir))

          {

            @mkdir($dstDir, $this->_folder_rights, true);

          }

          copy($src,$dstFile);

          @chmod($dstFile,$this->_file_rights);

          umask($oldmask);

        }


        return $this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName";

      }

      else if(is_dir($src))

      {

        $dir=$this->hash($hashByName ? basename($src) : $src);

        $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir;


        if(!is_dir($dstDir))

          MyFileHelper::copyDirectory($src,$dstDir,array('exclude'=>array('.svn'),'level'=>$level,'folderRights' => $this->_folder_rights,'fileRights' => $this->_file_rights));


        return $this->_published[$path]=$this->getBaseUrl().'/'.$dir;

      }

    }

    throw new CException(Yii::t('yii','The asset "{asset}" to be published does not exist.',

      array('{asset}'=>$path)));

  }

}



AND

MyFileHelper extends CFileHelper




class MyFileHelper extends CFileHelper

{

  public static function copyDirectory($src,$dst,$options=array())

  {

    $fileTypes=array();

    $exclude=array();

    $level=-1;

    $folderRights = 0755;

    $fileRights = 0644;

    extract($options);

    self::copyDirectoryRecursive($src,$dst,'',$fileTypes,$exclude,$level,$folderRights,$fileRights);

  }

  

  protected static function copyDirectoryRecursive($src,$dst,$base,$fileTypes,$exclude,$level,$_folder_rights = 0777,$_file_rights = 0644)

  {

    $oldmask = umask(0);

    @mkdir($dst, $_folder_rights, true);

    $folder=opendir($src);

    while($file=readdir($folder))

    {

      if($file==='.' || $file==='..')

        continue;

      $path=$src.DIRECTORY_SEPARATOR.$file;

      $isFile=is_file($path);

      if(self::validatePath($base,$file,$isFile,$fileTypes,$exclude))

      {

        if($isFile) {

          copy($path,$dst.DIRECTORY_SEPARATOR.$file);

          @chmod($dst.DIRECTORY_SEPARATOR.$file,$_file_rights);

        } else if($level)

          self::copyDirectoryRecursive($path,$dst.DIRECTORY_SEPARATOR.$file,$base.'/'.$file,$fileTypes,$exclude,$level-1,$_folder_rights,$_file_rights);

      }

    }

    closedir($folder);

    umask($oldmask);

  }

}



And I use these as default behaviour for my assets by small changes in the config:




...

    'assetManager' => array(

      'class' => 'MyAssetManager',

      'folderRights' => 0755,

      'fileRights' => 0755,

    ),

...



I think it would be useful to include this behaviour into original CAssetManager and CFIleHelper. :)