mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Convert 'caseless' option into 'caseSensitive' option at yii\helpers\BaseFileHelper::findFiles()
				
					
				
			This commit is contained in:
		@ -185,7 +185,7 @@ Yii Framework 2 Change Log
 | 
			
		||||
- Enh #4636: Added `yii\web\Response::setDownloadHeaders()` (pawzar)
 | 
			
		||||
- Enh #4644: Added `yii\db\Schema::createColumnSchema()` to be able to customize column schema used (mcd-php)
 | 
			
		||||
- Enh #4656: HtmlPurifier helper config can now be a closure to change the purifier config object after it was created (Alex-Code)
 | 
			
		||||
- Enh #4062: Added 'caseless' option to `yii\helpers\BaseFileHelper::findFiles()` (klimov-paul)
 | 
			
		||||
- Enh #4062: Added 'caseSensitive' option to `yii\helpers\BaseFileHelper::findFiles()` (klimov-paul)
 | 
			
		||||
- Enh #4691: Encoding on `ActiveForm` and `ActiveField` validation errors is now configurable (Alex-Code)
 | 
			
		||||
- Enh #4740: Added `yii\web\Session::addFlash()` (restyler)
 | 
			
		||||
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
 | 
			
		||||
 | 
			
		||||
@ -26,7 +26,7 @@ class BaseFileHelper
 | 
			
		||||
    const PATTERN_ENDSWITH = 4;
 | 
			
		||||
    const PATTERN_MUSTBEDIR = 8;
 | 
			
		||||
    const PATTERN_NEGATIVE = 16;
 | 
			
		||||
    const PATTERN_CASELESS = 32;
 | 
			
		||||
    const PATTERN_CASE_INSENSITIVE = 32;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@ -228,7 +228,7 @@ class BaseFileHelper
 | 
			
		||||
     *   apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b';
 | 
			
		||||
     *   and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches
 | 
			
		||||
     *   both '/' and '\' in the paths.
 | 
			
		||||
     * - caseless: boolean, whether patterns specified at "only" or "except" should be case insensitive. Defaults to false.
 | 
			
		||||
     * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.
 | 
			
		||||
     * - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true.
 | 
			
		||||
     * - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file.
 | 
			
		||||
     *   If the callback returns false, the copy operation for the sub-directory or file will be cancelled.
 | 
			
		||||
@ -346,7 +346,7 @@ class BaseFileHelper
 | 
			
		||||
     * - only: array, list of patterns that the file paths should match if they are to be returned. Directory paths are not checked against them.
 | 
			
		||||
     *   Same pattern matching rules as in the "except" option are used.
 | 
			
		||||
     *   If a file path matches a pattern in both "only" and "except", it will NOT be returned.
 | 
			
		||||
     * - caseless: boolean, whether patterns specified at "only" or "except" should be case insensitive. Defaults to false.
 | 
			
		||||
     * - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true.
 | 
			
		||||
     * - recursive: boolean, whether the files under the subdirectories should also be looked for. Defaults to true.
 | 
			
		||||
     * @return array files found under the directory. The file list is sorted.
 | 
			
		||||
     * @throws InvalidParamException if the dir is invalid.
 | 
			
		||||
@ -478,7 +478,7 @@ class BaseFileHelper
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $fnmatchFlags = 0;
 | 
			
		||||
        if ($flags & self::PATTERN_CASELESS) {
 | 
			
		||||
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
 | 
			
		||||
            $fnmatchFlags |= FNM_CASEFOLD;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -532,7 +532,7 @@ class BaseFileHelper
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $fnmatchFlags = FNM_PATHNAME;
 | 
			
		||||
        if ($flags & self::PATTERN_CASELESS) {
 | 
			
		||||
        if ($flags & self::PATTERN_CASE_INSENSITIVE) {
 | 
			
		||||
            $fnmatchFlags |= FNM_CASEFOLD;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -584,11 +584,11 @@ class BaseFileHelper
 | 
			
		||||
    /**
 | 
			
		||||
     * Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead.
 | 
			
		||||
     * @param string $pattern
 | 
			
		||||
     * @param boolean $caseLess
 | 
			
		||||
     * @param boolean $caseSensitive
 | 
			
		||||
     * @throws \yii\base\InvalidParamException
 | 
			
		||||
     * @return array with keys: (string) pattern, (int) flags, (int|boolean)firstWildcard
 | 
			
		||||
     */
 | 
			
		||||
    private static function parseExcludePattern($pattern, $caseLess)
 | 
			
		||||
    private static function parseExcludePattern($pattern, $caseSensitive)
 | 
			
		||||
    {
 | 
			
		||||
        if (!is_string($pattern)) {
 | 
			
		||||
            throw new InvalidParamException('Exclude/include pattern must be a string.');
 | 
			
		||||
@ -600,8 +600,8 @@ class BaseFileHelper
 | 
			
		||||
            'firstWildcard' => false,
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        if ($caseLess) {
 | 
			
		||||
            $result['flags'] |= self::PATTERN_CASELESS;
 | 
			
		||||
        if (!$caseSensitive) {
 | 
			
		||||
            $result['flags'] |= self::PATTERN_CASE_INSENSITIVE;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (!isset($pattern[0])) {
 | 
			
		||||
@ -651,17 +651,20 @@ class BaseFileHelper
 | 
			
		||||
     */
 | 
			
		||||
    private static function normalizeOptions(array $options)
 | 
			
		||||
    {
 | 
			
		||||
        if (!array_key_exists('caseSensitive', $options)) {
 | 
			
		||||
            $options['caseSensitive'] = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (isset($options['except'])) {
 | 
			
		||||
            foreach ($options['except'] as $key => $value) {
 | 
			
		||||
                if (is_string($value)) {
 | 
			
		||||
                    $options['except'][$key] = self::parseExcludePattern($value, !empty($options['caseless']));
 | 
			
		||||
                    $options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (isset($options['only'])) {
 | 
			
		||||
            foreach ($options['only'] as $key => $value) {
 | 
			
		||||
                if (is_string($value)) {
 | 
			
		||||
                    $options['only'][$key] = self::parseExcludePattern($value, !empty($options['caseless']));
 | 
			
		||||
                    $options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -410,7 +410,7 @@ class FileHelperTest extends TestCase
 | 
			
		||||
    /**
 | 
			
		||||
     * @depends testFindFilesExclude
 | 
			
		||||
     */
 | 
			
		||||
    public function testFindFilesCaseLess()
 | 
			
		||||
    public function testFindFilesCaseSensitive()
 | 
			
		||||
    {
 | 
			
		||||
        $dirName = 'test_dir';
 | 
			
		||||
        $this->createFileStructure([
 | 
			
		||||
@ -424,14 +424,14 @@ class FileHelperTest extends TestCase
 | 
			
		||||
 | 
			
		||||
        $options = [
 | 
			
		||||
            'except' => ['*.txt'],
 | 
			
		||||
            'caseless' => true
 | 
			
		||||
            'caseSensitive' => false
 | 
			
		||||
        ];
 | 
			
		||||
        $foundFiles = FileHelper::findFiles($dirName, $options);
 | 
			
		||||
        $this->assertCount(0, $foundFiles);
 | 
			
		||||
 | 
			
		||||
        $options = [
 | 
			
		||||
            'only' => ['*.txt'],
 | 
			
		||||
            'caseless' => true
 | 
			
		||||
            'caseSensitive' => false
 | 
			
		||||
        ];
 | 
			
		||||
        $foundFiles = FileHelper::findFiles($dirName, $options);
 | 
			
		||||
        $this->assertCount(2, $foundFiles);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user