mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	created StringHelper::basename()
In contrast to php function basename() It that always uses \ and / as directory separator. It is needed for use on Classnames that contain namespaces. Fixes #192
This commit is contained in:
		@ -252,7 +252,7 @@ class ActiveRecord extends Model
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public static function tableName()
 | 
						public static function tableName()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		return 'tbl_' . StringHelper::camel2id(basename(get_called_class()), '_');
 | 
							return 'tbl_' . StringHelper::camel2id(StringHelper::basename(get_called_class()), '_');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
 | 
				
			|||||||
@ -43,6 +43,29 @@ class StringHelper
 | 
				
			|||||||
		return function_exists('mb_substr') ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
 | 
							return function_exists('mb_substr') ? mb_substr($string, $start, $length, '8bit') : substr($string, $start, $length);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Returns the trailing name component of a path.
 | 
				
			||||||
 | 
						 * This method does the same as the php function basename() except that it will
 | 
				
			||||||
 | 
						 * always use \ and / as directory separators, independent of the operating system.
 | 
				
			||||||
 | 
						 * Note: basename() operates naively on the input string, and is not aware of the
 | 
				
			||||||
 | 
						 * actual filesystem, or path components such as "..".
 | 
				
			||||||
 | 
						 * @param string $path A path string.
 | 
				
			||||||
 | 
						 * @param string $suffix If the name component ends in suffix this will also be cut off.
 | 
				
			||||||
 | 
						 * @return string the trailing name component of the given path.
 | 
				
			||||||
 | 
						 * @see http://www.php.net/manual/en/function.basename.php
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public static function basename($path, $suffix = '')
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if (($len = mb_strlen($suffix)) > 0 && mb_substr($path, -$len) == $suffix) {
 | 
				
			||||||
 | 
								$path = mb_substr($path, 0, -$len);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							$path = rtrim(str_replace('\\', '/', $path), '/\\');
 | 
				
			||||||
 | 
							if (($pos = mb_strrpos($path, '/')) !== false) {
 | 
				
			||||||
 | 
								return mb_substr($path, $pos + 1);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return $path;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Converts a word to its plural form.
 | 
						 * Converts a word to its plural form.
 | 
				
			||||||
	 * Note that this is for English only!
 | 
						 * Note that this is for English only!
 | 
				
			||||||
 | 
				
			|||||||
@ -70,4 +70,48 @@ class StringHelperTest extends \yii\test\TestCase
 | 
				
			|||||||
		$this->assertEquals('PostTag', StringHelper::id2camel('post-tag'));
 | 
							$this->assertEquals('PostTag', StringHelper::id2camel('post-tag'));
 | 
				
			||||||
		$this->assertEquals('PostTag', StringHelper::id2camel('post_tag', '_'));
 | 
							$this->assertEquals('PostTag', StringHelper::id2camel('post_tag', '_'));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						public function testBasename()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							$this->assertEquals('', StringHelper::basename(''));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('file'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('file.test', '.test2'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('file.test', '.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('/file'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('/file.test', '.test2'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('/file.test', '.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('/path/to/file'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('/path/to/file.test', '.test2'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('/path/to/file.test', '.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('\file'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('\file.test', '.test2'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('\file.test', '.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('C:\file'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('C:\file.test', '.test2'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('C:\file.test', '.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('C:\path\to\file'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('C:\path\to\file.test', '.test2'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('C:\path\to\file.test', '.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// mixed paths
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('/path\to/file.test'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('/path/to\file.test'));
 | 
				
			||||||
 | 
							$this->assertEquals('file.test', StringHelper::basename('\path/to\file.test'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// \ and / in suffix
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('/path/to/filete/st', 'te/st'));
 | 
				
			||||||
 | 
							$this->assertEquals('st', StringHelper::basename('/path/to/filete/st', 'te\st'));
 | 
				
			||||||
 | 
							$this->assertEquals('file', StringHelper::basename('/path/to/filete\st', 'te\st'));
 | 
				
			||||||
 | 
							$this->assertEquals('st', StringHelper::basename('/path/to/filete\st', 'te/st'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// http://www.php.net/manual/en/function.basename.php#72254
 | 
				
			||||||
 | 
							$this->assertEquals('foo', StringHelper::basename('/bar/foo/'));
 | 
				
			||||||
 | 
							$this->assertEquals('foo', StringHelper::basename('\\bar\\foo\\'));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user