Пример #1
0
 public static function getMime(Charcoal_File $image_file)
 {
     if (!$image_file->exists()) {
         return NULL;
     }
     $data = getimagesize(us($image_file->getPath()));
     if ($data === FALSE) {
         _throw(new Charcoal_ImageGetSizeException($image_file));
     }
     return $data['mime'];
 }
 public function createFile($file_path, $contents, $overwrite = TRUE, $mode = 0777)
 {
     Charcoal_ParamTrait::validateString(1, $file_path);
     Charcoal_ParamTrait::validateString(2, $contents);
     Charcoal_ParamTrait::validateBoolean(3, $overwrite);
     Charcoal_ParamTrait::validateInteger(4, $mode);
     $obj = new Charcoal_File($file_path, $this->_base_dir_obj);
     if ($overwrite) {
         if ($obj->exists() && !$obj->canWrite()) {
             _throw(new Charcoal_FileSystemComponentException('specified file is not writeable.'));
         }
     } elseif ($obj->exists()) {
         _throw(new Charcoal_FileSystemComponentException('specified file is already exists.'));
     }
     try {
         // create file with parent directory
         $obj->makeFile($mode, $contents, TRUE);
         return $obj;
     } catch (Exception $e) {
         _catch($e);
         _throw(new Charcoal_FileSystemComponentException(s('creating file failed.'), $e));
     }
 }
 /**
  *    render buffer
  *
  * @param Charcoal_String|string $buffer    rendering data
  */
 public function render($buffer)
 {
     Charcoal_ParamTrait::validateString(1, $buffer);
     $buffer = us($buffer);
     if (is_string($buffer)) {
         $dir = new Charcoal_File(dirname($this->file_path));
         if (!$dir->exists()) {
             $dir->makeDirectory($this->dir_mode);
         }
         if (!$dir->isWriteable()) {
             _throw(new Charcoal_RenderTargetException("directory not writable: {$dir}"));
         }
         $result = @file_put_contents($this->file_path, $buffer, LOCK_EX);
         if ($result === FALSE) {
             $last_error = print_r(error_get_last(), true);
             _throw(new Charcoal_RenderTargetException("file_put_contents failed: {$this->file_path} last error: {$last_error}"));
         }
     }
 }
 /**
  * create file
  *
  * @param string|Charcoal_String $contents
  * @param Charcoal_File $dir
  * @param string|Charcoal_String $file_name
  *
  * @return Charcoal_File
  */
 public function create($contents, $dir = null, $file_name = null)
 {
     if ($file_name === null) {
         $tmp_filename = Charcoal_System::hash() . '.tmp';
     }
     if ($dir === null) {
         $dir = Charcoal_ResourceLocator::getFile($this->getSandbox()->getEnvironment(), "%TMP_DIR%");
     }
     $tmp_file = new Charcoal_File($file_name, $dir);
     if ($tmp_file->isDirectory()) {
         _throw(new Charcoal_FileSystemComponentException('specified path is directory.'));
     }
     if ($tmp_file->exists()) {
         _throw(new Charcoal_FileSystemComponentException('specified file is already exists.'));
     }
     if ($this->overwrite) {
         if ($tmp_file->exists() && !$tmp_file->canWrite()) {
             _throw(new Charcoal_FileSystemComponentException('specified file is not writeable.'));
         }
     }
     try {
         // create file
         $tmp_file->makeFile($this->mode, $contents, TRUE);
         $this->file = $tmp_file;
         return $tmp_file;
     } catch (Exception $e) {
         _catch($e);
         _throw(new Charcoal_TempFileComponentException(s('creating file failed.'), $e));
     }
     return null;
 }
 /**
  * クリーンアップ
  */
 public function cleanUp($action, $context)
 {
     $action = us($action);
     switch ($action) {
         case "get_empty_data":
         case "get_integer_data":
         case "get_string_data":
         case "get_array_data":
         case "get_boolean_data":
         case "get_float_data":
         case "get_object_data":
         case "set_duration":
         case "delete":
         case "delete_wildcard":
         case "delete_regex":
         case "touch":
         case "touch_wildcard":
         case "touch_regex":
             $cache_files = glob($this->cache_root . '/*');
             if ($cache_files && is_array($cache_files)) {
                 foreach ($cache_files as $cache) {
                     $file = new Charcoal_File(s($cache));
                     $name = $file->getName();
                     $ext = $file->getExtension();
                     if ($file->exists() && $file->isFile() && in_array($ext, array("meta", "data"))) {
                         $file->delete();
                         echo "removed cache file: [{$cache}]" . eol();
                     }
                 }
             }
             break;
         default:
             break;
     }
 }