Browse Source

add a function to simulate the php basename()

saeid 8 years ago
parent
commit
fb8d26480b
1 changed files with 18 additions and 3 deletions
  1. 18 3
      src/Filesystem/File.php

+ 18 - 3
src/Filesystem/File.php

@@ -86,7 +86,7 @@ class File
         $splInfo = new SplFileInfo($path);
         $this->Folder = new Folder($splInfo->getPath(), $create, $mode);
         if (!is_dir($path)) {
-            $this->name = ltrim($splInfo->getFilename(), '/');
+            $this->name = ltrim($splInfo->getFilename(), DS);
         }
         $this->pwd();
         $create && !$this->exists() && $this->safe($path) && $this->create();
@@ -345,7 +345,7 @@ class File
             $this->info();
         }
         if (isset($this->info['extension'])) {
-            return basename($this->name, '.' . $this->info['extension']);
+            return $this->_basename($this->name, '.' . $this->info['extension']);
         }
         if ($this->name) {
             return $this->name;
@@ -355,6 +355,21 @@ class File
     }
 
     /**
+     * Returns the file basename. simulate the php basename().
+     *
+     * @return string the file basename.
+     */
+    protected function _basename($name, $ext=null)
+    {
+        $splInfo = new SplFileInfo($name);
+        $name = ltrim($splInfo->getFilename(), DS);
+        if($ext===null || rtrim($name,$ext)===''){
+            return $name;
+        }
+        return rtrim($name,$ext);
+    }
+
+    /**
      * Makes file name safe for saving
      *
      * @param string|null $name The name of the file to make safe if different from $this->name
@@ -370,7 +385,7 @@ class File
             $ext = $this->ext();
         }
 
-        return preg_replace("/(?:[^\w\.-]+)/", '_', basename($name, $ext));
+        return preg_replace("/(?:[^\w\.-]+)/", '_', $this->_basename($name, $ext));
     }
 
     /**