phing-util
[ class tree: phing-util ] [ index: phing-util ] [ all elements ]

Class: DirectoryScanner

Source Location: /util/DirectoryScanner.php

Class Overview


Class for scanning a directory for files/directories that match a certain criteria.


Author(s):

  • Arnout J. Kuiper, ajkuiper@wxs.nl
  • Magesh Umasankar, umagesh@rediffmail.com
  • Andreas Aderhold, andi@binarycloud.com

Version:

  • $Revision: 552 $

Implements interfaces:

Variables

Methods



Class Details

[line 116]
Class for scanning a directory for files/directories that match a certain criteria.

These criteria consist of a set of include and exclude patterns. With these patterns, you can select which files you want to have included, and which files you want to have excluded.

The idea is simple. A given directory is recursively scanned for all files and directories. Each file/directory is matched against a set of include and exclude patterns. Only files/directories that match at least one pattern of the include pattern list, and don't match a pattern of the exclude pattern list will be placed in the list of files/directories found.

When no list of include patterns is supplied, "**" will be used, which means that everything will be matched. When no list of exclude patterns is supplied, an empty list is used, such that nothing will be excluded.

The pattern matching is done as follows: The name to be matched is split up in path segments. A path segment is the name of a directory or file, which is bounded by DIRECTORY_SEPARATOR ('/' under UNIX, '\' under Windows). E.g. "abc/def/ghi/xyz.php" is split up in the segments "abc", "def", "ghi" and "xyz.php". The same is done for the pattern against which should be matched.

Then the segments of the name and the pattern will be matched against each other. When '**' is used for a path segment in the pattern, then it matches zero or more path segments of the name.

There are special case regarding the use of DIRECTORY_SEPARATOR at the beginning of the pattern and the string to match: When a pattern starts with a DIRECTORY_SEPARATOR, the string to match must also start with a DIRECTORY_SEPARATOR. When a pattern does not start with a DIRECTORY_SEPARATOR, the string to match may not start with a DIRECTORY_SEPARATOR. When one of these rules is not obeyed, the string will not match.

When a name path segment is matched against a pattern path segment, the following special characters can be used: '*' matches zero or more characters, '?' matches one character.

Examples:

"**\*.php" matches all .php files/dirs in a directory tree.

"test\a??.php" matches all files/dirs which start with an 'a', then two more characters and then ".php", in a directory called test.

"**" matches everything in a directory tree.

"**\test\**\XYZ*" matches all files/dirs that start with "XYZ" and where there is a parent directory called test (e.g. "abc\test\def\ghi\XYZ123").

Case sensitivity may be turned off if necessary. By default, it is turned on.

Example of usage: $ds = new DirectroyScanner(); $includes = array("**\*.php"); $excludes = array("modules\*\**"); $ds->SetIncludes($includes); $ds->SetExcludes($excludes); $ds->SetBasedir("test"); $ds->SetCaseSensitive(true); $ds->Scan();

print("FILES:"); $files = ds->GetIncludedFiles(); for ($i = 0; $i < count($files);$i++) { println("$files[$i]\n"); }

This will scan a directory called test for .php files, but excludes all .php files in all directories under a directory called "modules"

This class is complete preg/ereg free port of the Java class org.apache.tools.ant.DirectoryScanner. Even functions that use preg/ereg internally (like split()) are not used. Only the _fast_ string functions and comparison operators (=== !=== etc) are used for matching and tokenizing.




Tags:

author:  Arnout J. Kuiper, ajkuiper@wxs.nl
author:  Magesh Umasankar, umagesh@rediffmail.com
author:  Andreas Aderhold, andi@binarycloud.com
version:  $Revision: 552 $


[ Top ]


Class Variables

$basedir =

[line 137]

The base directory which should be scanned.



Tags:

access:  protected

Type:   mixed


[ Top ]

$DEFAULTEXCLUDES = array(
        "**/*~",
        "**/#*#",
        "**/.#*",
        "**/%*%",
        "**/CVS",
        "**/CVS/**",
        "**/.cvsignore",
        "**/SCCS",
        "**/SCCS/**",
        "**/vssver.scc",
        "**/.svn",
        "**/.svn/**",
        "**/._*",
        "**/.DS_Store",
    )

[line 119]

default set of excludes



Tags:

access:  protected

Type:   mixed


[ Top ]

$dirsDeselected =

[line 188]



Tags:

access:  protected

Type:   mixed


[ Top ]

$dirsExcluded =

[line 176]

The files that where found and matched at least one includes, and also matched at least one excludes.



Tags:

access:  protected

Type:   mixed


[ Top ]

$dirsIncluded =

[line 167]

The directories that where found and matched at least one includes, and matched no excludes.



Tags:

access:  protected

Type:   mixed


[ Top ]

$dirsNotIncluded =

[line 170]

The directories that where found and did not match any includes.



Tags:

access:  protected

Type:   mixed


[ Top ]

$everythingIncluded =  true

[line 191]

if there are no deselected files



Tags:

access:  protected

Type:   mixed


[ Top ]

$excludes =  null

[line 143]

The patterns for the files that should be excluded.



Tags:

access:  protected

Type:   mixed


[ Top ]

$expandSymbolicLinks =  false

[line 146]

Whether to expand/dereference symbolic links, default is false



Tags:

access:  protected

Type:   mixed


[ Top ]

$filesDeselected =

[line 187]



Tags:

access:  protected

Type:   mixed


[ Top ]

$filesExcluded =

[line 161]

The files that where found and matched at least one includes, and also matched at least one excludes. Trie object.



Tags:

access:  protected

Type:   mixed


[ Top ]

$filesIncluded =

[line 152]

The files that where found and matched at least one includes, and matched no excludes.



Tags:

access:  protected

Type:   mixed


[ Top ]

$filesNotIncluded =

[line 155]

The files that where found and did not match any includes. Trie



Tags:

access:  protected

Type:   mixed


[ Top ]

$haveSlowResults =  false

[line 179]

Have the vars holding our results been built by a slow scan?



Tags:

access:  protected

Type:   mixed


[ Top ]

$includes =  null

[line 140]

The patterns for the files that should be included.



Tags:

access:  protected

Type:   mixed


[ Top ]

$isCaseSensitive =  true

[line 182]

Should the file system be treated as a case sensitive one?



Tags:

access:  protected

Type:   mixed


[ Top ]

$selectors =  null

[line 185]

Selectors



Tags:

access:  protected

Type:   mixed


[ Top ]



Class Methods


method addDefaultExcludes [line 695]

void addDefaultExcludes( )

Adds the array with default exclusions to the current exclusions set.



[ Top ]

method couldHoldIncluded [line 563]

true couldHoldIncluded( name $_name)

Tests whether a name matches the start of at least one include pattern.



Tags:

return:  when the name matches against at least one include pattern,
  1. false
otherwise.
access:  protected


Parameters:

name   $_name   the name to match

[ Top ]

method getBasedir [line 262]

the getBasedir( )

Gets the basedir that is used for scanning. This is the directory that is scanned recursively.



Tags:

return:  basedir that is used for scanning


[ Top ]

method getDeselectedDirectories [line 674]

the getDeselectedDirectories( )

<p>Returns the names of the directories which were selected out and therefore not ultimately included.</p>

The names are relative to the base directory. This involves performing a slow scan if one has not already been completed.




Tags:

return:  names of the directories which were deselected.
see:  #slowScan
access:  public



Implementation of:
SelectorScanner::getDeselectedDirectories
[ Top ]

method getDeselectedFiles [line 634]

the getDeselectedFiles( )

<p>Returns the names of the files which were selected out and therefore not ultimately included.</p>

The names are relative to the base directory. This involves performing a slow scan if one has not already been completed.




Tags:

return:  names of the files which were deselected.
see:  #slowScan
access:  public



Implementation of:
SelectorScanner::getDeselectedFiles
[ Top ]

method getExcludedDirectories [line 686]

the getExcludedDirectories( )

Get the names of the directories that matched at least one of the include patterns, an matched also at least one of the exclude patterns.

The names are relative to the basedir.




Tags:

return:  names of the directories


[ Top ]

method getExcludedFiles [line 618]

the getExcludedFiles( )

Get the names of the files that matched at least one of the include patterns, an matched also at least one of the exclude patterns.

The names are relative to the basedir.




Tags:

return:  names of the files


[ Top ]

method getIncludedDirectories [line 647]

the getIncludedDirectories( )

Get the names of the directories that matched at least one of the include patterns, an matched none of the exclude patterns.

The names are relative to the basedir.




Tags:

return:  names of the directories


[ Top ]

method getIncludedFiles [line 595]

the getIncludedFiles( )

Get the names of the files that matched at least one of the include patterns, and matched none of the exclude patterns.

The names are relative to the basedir.




Tags:

return:  names of the files


[ Top ]

method getNotIncludedDirectories [line 658]

the getNotIncludedDirectories( )

Get the names of the directories that matched at none of the include patterns.

The names are relative to the basedir.




Tags:

return:  names of the directories


[ Top ]

method getNotIncludedFiles [line 605]

the getNotIncludedFiles( )

Get the names of the files that matched at none of the include patterns.

The names are relative to the basedir.




Tags:

return:  names of the files


[ Top ]

method isEverythingIncluded [line 720]

true isEverythingIncluded( )

Returns whether or not the scanner has included all the files or directories it has come across so far.



Tags:

return:  if all files and directories which have been found so far have been included.
access:  public


[ Top ]

method isExcluded [line 579]

true isExcluded( name $_name)

Tests whether a name matches against at least one exclude pattern.



Tags:

return:  when the name matches against at least one exclude pattern,
  1. false
otherwise.
access:  protected


Parameters:

name   $_name   the name to match

[ Top ]

method isIncluded [line 547]

true isIncluded( name $_name)

Tests whether a name matches against at least one include pattern.



Tags:

return:  when the name matches against at least one include pattern,
  1. false
otherwise.
access:  protected


Parameters:

name   $_name   the name to match

[ Top ]

method isSelected [line 732]

boolean isSelected( string $name, string $file)

Tests whether a name should be selected.



Tags:

return:  False when the selectors says that the file should not be selected, True otherwise.
access:  protected


Parameters:

string   $name   The filename to check for selecting.
string   $file   The full file path.

[ Top ]

method listDir [line 422]

array listDir( src $_dir)

Lists contens of a given directory and returns array with entries



Tags:

return:  directory entries
author:  Albert Lash, alash@plateauinnovation.com
access:  public


Parameters:

src   $_dir   String. Source path and name file to copy.

[ Top ]

method match [line 239]

boolean match( pattern $pattern, str $str, [ $isCaseSensitive = true])

Matches a string against a pattern. The pattern contains two special characters: '*' which means zero or more characters, '?' which means one and only one character.



Tags:

return:  true when the string matches against the pattern, false otherwise.
access:  public


Parameters:

pattern   $pattern   the (non-null) pattern to match against
str   $str   the (non-null) string that must be matched against the pattern
   $isCaseSensitive  

[ Top ]

method matchPath [line 221]

true matchPath( pattern $pattern, str $str, [isCaseSensitive $isCaseSensitive = true])

Matches a path against a pattern. Static



Tags:

return:  when the pattern matches against the string. false otherwise.


Parameters:

pattern   $pattern   the (non-null) pattern to match against
str   $str   the (non-null) string (path) to match
isCaseSensitive   $isCaseSensitive   must a case sensitive match be done?

[ Top ]

method matchPatternStart [line 207]

boolean matchPatternStart( pattern $pattern, str $str, [isCaseSensitive $isCaseSensitive = true])

Does the path match the start of this pattern up to the first "**".

This is a static mehtod and should always be called static

This is not a general purpose test and should only be used if you can live with false positives.

pattern=**\a and str=b will yield true.




Tags:

return:  true if matches, otherwise false


Parameters:

pattern   $pattern   the (non-null) pattern to match against
str   $str   the (non-null) string (path) to match
isCaseSensitive   $isCaseSensitive   must matches be case sensitive?

[ Top ]

method scan [line 341]

void scan( )

Scans the base directory for files that match at least one include pattern, and don't match any exclude patterns.



[ Top ]

method setBasedir [line 250]

void setBasedir( basedir $_basedir)

Sets the basedir for scanning. This is the directory that is scanned recursively. All '/' and '\' characters are replaced by DIRECTORY_SEPARATOR



Parameters:

basedir   $_basedir   the (non-null) basedir for scanning

[ Top ]

method setCaseSensitive [line 271]

void setCaseSensitive( specifies $_isCaseSensitive)

Sets the case sensitivity of the file system



Parameters:

specifies   $_isCaseSensitive   if the filesystem is case sensitive

[ Top ]

method setExcludes [line 310]

void setExcludes( [excludes $_excludes = array()])

Sets the set of exclude patterns to use. All '/' and '\' characters are replaced by
  1. File.separatorChar
. So the separator used need not match
  1. File.separatorChar
.

When a pattern ends with a '/' or '\', "**" is appended.




Parameters:

excludes   $_excludes   list of exclude patterns

[ Top ]

method setExpandSymbolicLinks [line 331]

void setExpandSymbolicLinks( expandSymbolicLinks $expandSymbolicLinks)

Sets whether to expand/dereference symbolic links



Parameters:

expandSymbolicLinks   $expandSymbolicLinks   boolean value

[ Top ]

method setIncludes [line 284]

void setIncludes( [includes $_includes = array()])

Sets the set of include patterns to use. All '/' and '\' characters are replaced by DIRECTORY_SEPARATOR. So the separator used need not match DIRECTORY_SEPARATOR.

When a pattern ends with a '/' or '\', "**" is appended.




Parameters:

includes   $_includes   list of include patterns

[ Top ]

method setSelectors [line 709]

void setSelectors( selectors $selectors)

Sets the selectors that will select the filelist.



Tags:

access:  public



Implementation of:
SelectorScanner::setSelectors

Parameters:

selectors   $selectors   specifies the selectors to be invoked on a scan

[ Top ]

method slowScan [line 387]

void slowScan( )

Toplevel invocation for the scan.

Returns immediately if a slow scan has already been requested.




Tags:

access:  protected


[ Top ]


Documentation generated on Mon, 19 Oct 2009 10:50:47 +0200 by phpDocumentor 1.4.3