Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
7697f215
Commit
7697f215
authored
Jun 10, 2013
by
Klimov Paul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
"filter" callback option has been added to "yii\helpers\base\FileHelper::findFiles()".
parent
12d94c4a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
27 deletions
+63
-27
FileHelper.php
framework/yii/helpers/base/FileHelper.php
+38
-27
FileHelperTest.php
tests/unit/framework/helpers/FileHelperTest.php
+25
-0
No files found.
framework/yii/helpers/base/FileHelper.php
View file @
7697f215
...
@@ -196,6 +196,10 @@ class FileHelper
...
@@ -196,6 +196,10 @@ class FileHelper
* Returns the files found under the specified directory and subdirectories.
* Returns the files found under the specified directory and subdirectories.
* @param string $dir the directory under which the files will be looked for.
* @param string $dir the directory under which the files will be looked for.
* @param array $options options for file searching. Valid options are:
* @param array $options options for file searching. Valid options are:
* - filter: callback, a PHP callback that is called for each sub-directory or file.
* If the callback returns false, the the sub-directory or file will not be placed to result set.
* The signature of the callback should be: `function ($base, $name, $isFile)`, where `$base` is the name of directory,
* which contains file or sub-directory, `$name` file or sub-directory name, `$isFile` indicates if object is a file or a directory.
* - fileTypes: array, list of file name suffix (without dot). Only files with these suffixes will be returned.
* - fileTypes: array, list of file name suffix (without dot). Only files with these suffixes will be returned.
* - exclude: array, list of directory and file exclusions. Each exclusion can be either a name or a path.
* - exclude: array, list of directory and file exclusions. Each exclusion can be either a name or a path.
* If a file or directory name or path matches the exclusion, it will not be copied. For example, an exclusion of
* If a file or directory name or path matches the exclusion, it will not be copied. For example, an exclusion of
...
@@ -209,11 +213,9 @@ class FileHelper
...
@@ -209,11 +213,9 @@ class FileHelper
*/
*/
public
static
function
findFiles
(
$dir
,
array
$options
=
array
())
public
static
function
findFiles
(
$dir
,
array
$options
=
array
())
{
{
$fileTypes
=
array
();
$level
=
array_key_exists
(
'level'
,
$options
)
?
$options
[
'level'
]
:
-
1
;
$exclude
=
array
();
$filterOptions
=
$options
;
$level
=
-
1
;
$list
=
static
::
findFilesRecursive
(
$dir
,
''
,
$filterOptions
,
$level
);
extract
(
$options
);
$list
=
static
::
findFilesRecursive
(
$dir
,
''
,
$fileTypes
,
$exclude
,
$level
);
sort
(
$list
);
sort
(
$list
);
return
$list
;
return
$list
;
}
}
...
@@ -223,18 +225,17 @@ class FileHelper
...
@@ -223,18 +225,17 @@ class FileHelper
* This method is mainly used by [[findFiles]].
* This method is mainly used by [[findFiles]].
* @param string $dir the source directory.
* @param string $dir the source directory.
* @param string $base the path relative to the original source directory.
* @param string $base the path relative to the original source directory.
* @param array $fileTypes list of file name suffix (without dot). Only files with these suffixes will be returned.
* @param array $filterOptions list of filter options.
* @param array $exclude list of directory and file exclusions. Each exclusion can be either a name or a path.
* - filter: a PHP callback, which results indicates if file will be returned.
* If a file or directory name or path matches the exclusion, it will not be copied. For example, an exclusion of
* - fileTypes: list of file name suffix (without dot). Only files with these suffixes will be returned.
* '.svn' will exclude all files and directories whose name is '.svn'. And an exclusion of '/a/b' will exclude
* - exclude: list of directory and file exclusions. Each exclusion can be either a name or a path.
* file or directory '$src/a/b'. Note, that '/' should be used as separator regardless of the value of the DIRECTORY_SEPARATOR constant.
* @param integer $level recursion depth. It defaults to -1.
* @param integer $level recursion depth. It defaults to -1.
* Level -1 means searching for all directories and files under the directory;
* Level -1 means searching for all directories and files under the directory;
* Level 0 means searching for only the files DIRECTLY under the directory;
* Level 0 means searching for only the files DIRECTLY under the directory;
* level N means searching for those directories that are within N levels.
* level N means searching for those directories that are within N levels.
* @return array files found under the directory.
* @return array files found under the directory.
*/
*/
protected
static
function
findFilesRecursive
(
$dir
,
$base
,
$fileTypes
,
$exclude
,
$level
)
protected
static
function
findFilesRecursive
(
$dir
,
$base
,
array
$filterOptions
,
$level
)
{
{
$list
=
array
();
$list
=
array
();
$handle
=
opendir
(
$dir
);
$handle
=
opendir
(
$dir
);
...
@@ -244,11 +245,11 @@ class FileHelper
...
@@ -244,11 +245,11 @@ class FileHelper
}
}
$path
=
$dir
.
DIRECTORY_SEPARATOR
.
$file
;
$path
=
$dir
.
DIRECTORY_SEPARATOR
.
$file
;
$isFile
=
is_file
(
$path
);
$isFile
=
is_file
(
$path
);
if
(
static
::
validatePath
(
$base
,
$file
,
$isFile
,
$fil
eTypes
,
$exclude
))
{
if
(
static
::
validatePath
(
$base
,
$file
,
$isFile
,
$fil
terOptions
))
{
if
(
$isFile
)
{
if
(
$isFile
)
{
$list
[]
=
$path
;
$list
[]
=
$path
;
}
elseif
(
$level
)
{
}
elseif
(
$level
)
{
$list
=
array_merge
(
$list
,
static
::
findFilesRecursive
(
$path
,
$base
.
DIRECTORY_SEPARATOR
.
$file
,
$fil
eTypes
,
$exclude
,
$level
-
1
));
$list
=
array_merge
(
$list
,
static
::
findFilesRecursive
(
$path
,
$base
.
DIRECTORY_SEPARATOR
.
$file
,
$fil
terOptions
,
$level
-
1
));
}
}
}
}
}
}
...
@@ -259,30 +260,40 @@ class FileHelper
...
@@ -259,30 +260,40 @@ class FileHelper
/**
/**
* Validates a file or directory, checking if it match given conditions.
* Validates a file or directory, checking if it match given conditions.
* @param string $base the path relative to the original source directory
* @param string $base the path relative to the original source directory
* @param string $
fil
e the file or directory name
* @param string $
nam
e the file or directory name
* @param boolean $isFile whether this is a file
* @param boolean $isFile whether this is a file
* @param array $fileTypes list of valid file name suffixes (without dot).
* @param array $filterOptions list of filter options.
* @param array $exclude list of directory and file exclusions. Each exclusion can be either a name or a path.
* - filter: a PHP callback, which results indicates if file will be returned.
* - fileTypes: list of file name suffix (without dot). Only files with these suffixes will be returned.
* - exclude: list of directory and file exclusions. Each exclusion can be either a name or a path.
* If a file or directory name or path matches the exclusion, false will be returned. For example, an exclusion of
* If a file or directory name or path matches the exclusion, false will be returned. For example, an exclusion of
* '.svn' will return false for all files and directories whose name is '.svn'. And an exclusion of '/a/b' will return false for
* '.svn' will return false for all files and directories whose name is '.svn'. And an exclusion of '/a/b' will return false for
* file or directory '$src/a/b'. Note, that '/' should be used as separator regardless of the value of the DIRECTORY_SEPARATOR constant.
* file or directory '$src/a/b'. Note, that '/' should be used as separator regardless of the value of the DIRECTORY_SEPARATOR constant.
* @return boolean whether the file or directory is valid
* @return boolean whether the file or directory is valid
*/
*/
protected
static
function
validatePath
(
$base
,
$
file
,
$isFile
,
$fileTypes
,
$exclude
)
protected
static
function
validatePath
(
$base
,
$
name
,
$isFile
,
array
$filterOptions
)
{
{
foreach
(
$exclude
as
$e
)
{
if
(
isset
(
$filterOptions
[
'filter'
])
&&
!
call_user_func
(
$filterOptions
[
'filter'
],
$base
,
$name
,
$isFile
))
{
if
(
$file
===
$e
||
strpos
(
$base
.
DIRECTORY_SEPARATOR
.
$file
,
$e
)
===
0
)
{
return
false
;
return
false
;
}
}
}
if
(
!
$isFile
||
empty
(
$fileTypes
))
{
if
(
!
empty
(
$filterOptions
[
'exclude'
]))
{
return
true
;
foreach
(
$filterOptions
[
'exclude'
]
as
$e
)
{
if
(
$name
===
$e
||
strpos
(
$base
.
DIRECTORY_SEPARATOR
.
$name
,
$e
)
===
0
)
{
return
false
;
}
}
}
}
if
((
$type
=
pathinfo
(
$file
,
PATHINFO_EXTENSION
))
!==
''
)
{
if
(
!
empty
(
$filterOptions
[
'fileTypes'
]))
{
return
in_array
(
$type
,
$fileTypes
);
if
(
!
$isFile
)
{
}
else
{
return
true
;
return
false
;
}
if
((
$type
=
pathinfo
(
$name
,
PATHINFO_EXTENSION
))
!==
''
)
{
return
in_array
(
$type
,
$filterOptions
[
'fileTypes'
]);
}
else
{
return
false
;
}
}
}
return
true
;
}
}
/**
/**
...
...
tests/unit/framework/helpers/FileHelperTest.php
View file @
7697f215
...
@@ -221,6 +221,31 @@ class FileHelperTest extends TestCase
...
@@ -221,6 +221,31 @@ class FileHelperTest extends TestCase
/**
/**
* @depends testFindFiles
* @depends testFindFiles
*/
*/
public
function
testFindFileFilter
()
{
$dirName
=
'test_dir'
;
$passedFileName
=
'passed.txt'
;
$this
->
createFileStructure
(
array
(
$dirName
=>
array
(
$passedFileName
=>
'passed file content'
,
'declined.txt'
=>
'declined file content'
,
),
));
$basePath
=
$this
->
testFilePath
;
$dirName
=
$basePath
.
DIRECTORY_SEPARATOR
.
$dirName
;
$options
=
array
(
'filter'
=>
function
(
$base
,
$name
,
$isFile
)
use
(
$passedFileName
)
{
return
(
$passedFileName
==
$name
);
}
);
$foundFiles
=
FileHelper
::
findFiles
(
$dirName
,
$options
);
$this
->
assertEquals
(
array
(
$dirName
.
DIRECTORY_SEPARATOR
.
$passedFileName
),
$foundFiles
);
}
/**
* @depends testFindFiles
*/
public
function
testFindFilesExclude
()
public
function
testFindFilesExclude
()
{
{
$dirName
=
'test_dir'
;
$dirName
=
'test_dir'
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment