# util/PearPackageScanner.php

Part of phing, the PHP build tool

PHP version 5

Author
  Christian Weiske
Category
  Util
License
  LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
Link
  https://gitorious.org/phing/pearPackageFileSet
Package
  phing.util
Version
  SVN: $Id: PearPackageScanner.php 1359 2011-11-01 21:52:28Z mrook $

## \PearPackageScanner
jump to top

Package: phing\util

Scans for files in a PEAR package.

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.

Parent(s)
  \DirectoryScanner
Author
  Christian Weiske
Category
  Util
License
  LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
Link
  https://gitorious.org/phing/pearPackageFileSet
Version
  $Revision: 1089 $

### Properties

`$DEFAULTEXCLUDES= 'array( "**/*~"'`
inherited

default set of excludes

Inherited from: \DirectoryScanner::$DEFAULTEXCLUDES
Default value `array( "**/*~"`Details

Type
Inherited_from
  \DirectoryScanner::$DEFAULTEXCLUDES

`$basedir= ''`
inherited

The base directory which should be scanned.

Inherited from: \DirectoryScanner::$basedir
Details

Type
Inherited_from
  \DirectoryScanner::$basedir

`$channel= ''pear.php.net''`
Default value`'pear.php.net'`Details

Type

`$config= ''`
Details

Type

`$dirsDeselected= ''`
inherited Inherited from: \DirectoryScanner::$dirsDeselected
Details

Type
Inherited_from
  \DirectoryScanner::$dirsDeselected

`$dirsExcluded= ''`
inherited

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

Inherited from: \DirectoryScanner::$dirsExcluded
Details

Type
Inherited_from
  \DirectoryScanner::$dirsExcluded

`$dirsIncluded= ''`
inherited

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

Inherited from: \DirectoryScanner::$dirsIncluded
Details

Type
Inherited_from
  \DirectoryScanner::$dirsIncluded

`$dirsNotIncluded= ''`
inherited

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

Inherited from: \DirectoryScanner::$dirsNotIncluded
Details

Type
Inherited_from
  \DirectoryScanner::$dirsNotIncluded

`$everythingIncluded= 'true'`
inherited

if there are no deselected files

Inherited from: \DirectoryScanner::$everythingIncluded
Default value `true` Details

Type
Inherited_from
  \DirectoryScanner::$everythingIncluded

`$excludes= 'null'`
inherited

The patterns for the files that should be excluded.

Inherited from: \DirectoryScanner::$excludes
Default value `null` Details

Type
Inherited_from
  \DirectoryScanner::$excludes

`$expandSymbolicLinks= 'false'`
inherited

Whether to expand/dereference symbolic links, default is false

Inherited from: \DirectoryScanner::$expandSymbolicLinks
Default value `false` Details

Type
Inherited_from
  \DirectoryScanner::$expandSymbolicLinks

`$filesDeselected= ''`
inherited Inherited from: \DirectoryScanner::$filesDeselected
Details

Type
Inherited_from
  \DirectoryScanner::$filesDeselected

`$filesExcluded= ''`
inherited

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

Inherited from: \DirectoryScanner::$filesExcluded
Details

Type
Inherited_from
  \DirectoryScanner::$filesExcluded

`$filesIncluded= ''`
inherited

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

Inherited from: \DirectoryScanner::$filesIncluded
Details

Type
Inherited_from
  \DirectoryScanner::$filesIncluded

`$filesNotIncluded= ''`
inherited

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

Inherited from: \DirectoryScanner::$filesNotIncluded
Details

Type
Inherited_from
  \DirectoryScanner::$filesNotIncluded

`$haveSlowResults= 'false'`
inherited

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

Inherited from: \DirectoryScanner::$haveSlowResults
Default value `false` Details

Type
Inherited_from
  \DirectoryScanner::$haveSlowResults

`$includes= 'null'`
inherited

The patterns for the files that should be included.

Inherited from: \DirectoryScanner::$includes
Default value `null` Details

Type
Inherited_from
  \DirectoryScanner::$includes

`$isCaseSensitive= 'true'`
inherited

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

Inherited from: \DirectoryScanner::$isCaseSensitive
Default value `true` Details

Type
Inherited_from
  \DirectoryScanner::$isCaseSensitive

`$package= ''`
Details

Type

`$packageInfo= ''`
Details

Type

`$role= ''php''`
Default value`'php'`Details

Type

`$selectors= 'null'`
inherited

Selectors

Inherited from: \DirectoryScanner::$selectors
Default value `null` Details

Type
Inherited_from
  \DirectoryScanner::$selectors

### Methods

`addDefaultExcludes( ) : void`
inherited

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

Inherited from: \DirectoryScanner::addDefaultExcludes()
`couldHoldIncluded( \name $_name ) : \<code>true</code>`
inherited

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

Inherited from: \DirectoryScanner::couldHoldIncluded()
Parameters

Name | Type | Description
--- | --- | ---
$_name | \name | the name to match

Returns

Type | Description
--- | ---
\<code>true</code> | when the name matches against at least one include pattern, <code>false</code> otherwise.

`getBasedir( ) : \the`
inherited

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

Inherited from: \DirectoryScanner::getBasedir()
Returns

Type | Description
--- | ---
\the | basedir that is used for scanning

`getDeselectedDirectories( ) : \the`
inherited

Returns the names of the directories which were selected out and therefore not ultimately included.

Inherited from: \DirectoryScanner::getDeselectedDirectories()

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

Returns

Type | Description
--- | ---
\the | names of the directories which were deselected.

Details

See
  \#slowScan

`getDeselectedFiles( ) : \the`
inherited

Returns the names of the files which were selected out and therefore not ultimately included.

Inherited from: \DirectoryScanner::getDeselectedFiles()

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

Returns

Type | Description
--- | ---
\the | names of the files which were deselected.

Details

See
  \#slowScan

`getExcludedDirectories( ) : \the`
inherited

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.

Inherited from: \DirectoryScanner::getExcludedDirectories()

The names are relative to the basedir.

Returns

Type | Description
--- | ---
\the | names of the directories

`getExcludedFiles( ) : \the`
inherited

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.

Inherited from: \DirectoryScanner::getExcludedFiles()

The names are relative to the basedir.

Returns

Type | Description
--- | ---
\the | names of the files

`getIncludedDirectories( ) : \the`
inherited

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

Inherited from: \DirectoryScanner::getIncludedDirectories()

The names are relative to the basedir.

Returns

Type | Description
--- | ---
\the | names of the directories

`getIncludedFiles( ) : \the`
inherited

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

Inherited from: \DirectoryScanner::getIncludedFiles()

The names are relative to the basedir.

Returns

Type | Description
--- | ---
\the | names of the files

`getNotIncludedDirectories( ) : \the`
inherited

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

Inherited from: \DirectoryScanner::getNotIncludedDirectories()

The names are relative to the basedir.

Returns

Type | Description
--- | ---
\the | names of the directories

`getNotIncludedFiles( ) : \the`
inherited

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

Inherited from: \DirectoryScanner::getNotIncludedFiles()

The names are relative to the basedir.

Returns

Type | Description
--- | ---
\the | names of the files

`init( ) : void`

Loads the package information.

Details

Uses
  \$packageInfo

`isEverythingIncluded( ) : \<code>true</code>`
inherited

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

Inherited from: \DirectoryScanner::isEverythingIncluded()
Returns

Type | Description
--- | ---
\<code>true</code> | if all files and directories which have been found so far have been included.

`isExcluded( \name $_name ) : \<code>true</code>`
inherited

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

Inherited from: \DirectoryScanner::isExcluded()
Parameters

Name | Type | Description
--- | --- | ---
$_name | \name | the name to match

Returns

Type | Description
--- | ---
\<code>true</code> | when the name matches against at least one exclude pattern, <code>false</code> otherwise.

`isIncluded( \name $_name ) : \<code>true</code>`
inherited

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

Inherited from: \DirectoryScanner::isIncluded()
Parameters

Name | Type | Description
--- | --- | ---
$_name | \name | the name to match

Returns

Type | Description
--- | ---
\<code>true</code> | when the name matches against at least one include pattern, <code>false</code> otherwise.

`isSelected( string $name, string $file ) : boolean`
inherited

Tests whether a name should be selected.

Inherited from: \DirectoryScanner::isSelected()
Parameters

Name | Type | Description
--- | --- | ---
$name | string | The filename to check for selecting.
$file | string | The full file path.

Returns

Type | Description
--- | ---
boolean | False when the selectors says that the file should not be selected, True otherwise.

`listDir( \src $_dir ) : array`
inherited

Lists contens of a given directory and returns array with entries

Inherited from: \DirectoryScanner::listDir()
Parameters

Name | Type | Description
--- | --- | ---
$_dir | \src | String. Source path and name file to copy.

Returns

Type | Description
--- | ---
array | directory entries

Details

Access
  public
Author
  Albert Lash, alash@plateauinnovation.com

`loadPackageInfo( ) : \PEAR_PackageFile_v2`

Loads and returns the PEAR package information.

Returns

Type | Description
--- | ---
\PEAR_PackageFile_v2 | Package information object

Throws

Exception | Description
--- | ---
\BuildException | When the package does not exist

`match( \pattern $pattern, \str $str, $isCaseSensitive = true ) : boolean`
inherited

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.

Inherited from: \DirectoryScanner::match()
Parameters

Name | Type | Description
--- | --- | ---
$pattern | \pattern | the (non-null) pattern to match against
$str | \str | the (non-null) string that must be matched against the pattern
$isCaseSensitive |  |

Returns

Type | Description
--- | ---
boolean | true when the string matches against the pattern, false otherwise.

Details

Access
  public

`matchPath( \pattern $pattern, \str $str, \isCaseSensitive $isCaseSensitive = true ) : true`
inherited

Matches a path against a pattern. Static

Inherited from: \DirectoryScanner::matchPath()
Parameters

Name | Type | Description
--- | --- | ---
$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?

Returns

Type | Description
--- | ---
true | when the pattern matches against the string. false otherwise.

`matchPatternStart( \pattern $pattern, \str $str, \isCaseSensitive $isCaseSensitive = true ) : boolean`
inherited

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

Inherited from: \DirectoryScanner::matchPatternStart()

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.

Parameters

Name | Type | Description
--- | --- | ---
$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?

Returns

Type | Description
--- | ---
boolean | true if matches, otherwise false

`scan( ) : boolean`

Generates the list of included files and directories

Returns

Type | Description
--- | ---
boolean | True if all went well, false if something was wrong

Details

Uses
  \$filesIncluded
Uses
  \$filesDeselected
Uses
  \$filesNotIncluded
Uses
  \$filesExcluded
Uses
  \$everythingIncluded
Uses
  \$dirsIncluded
Uses
  \$dirsDeselected
Uses
  \$dirsNotIncluded
Uses
  \$dirsExcluded

`setBasedir( \basedir $_basedir ) : void`
inherited

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

Inherited from: \DirectoryScanner::setBasedir()
Parameters

Name | Type | Description
--- | --- | ---
$_basedir | \basedir | the (non-null) basedir for scanning

`setCaseSensitive( \specifies $_isCaseSensitive ) : void`
inherited

Sets the case sensitivity of the file system

Inherited from: \DirectoryScanner::setCaseSensitive()
Parameters

Name | Type | Description
--- | --- | ---
$_isCaseSensitive | \specifies | if the filesystem is case sensitive

`setChannel( string $channel ) : void`

Sets the name of the package channel name

Parameters

Name | Type | Description
--- | --- | ---
$channel | string | package channel name or alias

`setConfig( string $config ) : void`

Sets the full path to the PEAR configuration file

Parameters

Name | Type | Description
--- | --- | ---
$config | string | Configuration file

`setExcludes( \excludes $_excludes = array() ) : void`
inherited

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

Inherited from: \DirectoryScanner::setExcludes()

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

Parameters

Name | Type | Description
--- | --- | ---
$_excludes | \excludes | list of exclude patterns

`setExpandSymbolicLinks( \expandSymbolicLinks $expandSymbolicLinks ) : void`
inherited

Sets whether to expand/dereference symbolic links

Inherited from: \DirectoryScanner::setExpandSymbolicLinks()
Parameters

Name | Type | Description
--- | --- | ---
$expandSymbolicLinks | \expandSymbolicLinks | boolean value

`setIncludes( \includes $_includes = array() ) : void`
inherited

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.

Inherited from: \DirectoryScanner::setIncludes()

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

Parameters

Name | Type | Description
--- | --- | ---
$_includes | \includes | list of include patterns

`setPackage( string $package ) : void`

Sets the name of the PEAR package to get the files from

Parameters

Name | Type | Description
--- | --- | ---
$package | string | Package name without channel

`setRole( string $role ) : void`

Sets the role of files that should be included.

Examples are php,doc,script

Parameters

Name | Type | Description
--- | --- | ---
$role | string | PEAR file role

Details

Internal
  We do not verify the role against a hardcoded list since that would break packages with additional roles.

`setSelectors( \selectors $selectors ) : void`
inherited

Sets the selectors that will select the filelist.

Inherited from: \DirectoryScanner::setSelectors()
Parameters

Name | Type | Description
--- | --- | ---
$selectors | \selectors | specifies the selectors to be invoked on a scan

`slowScan( ) : void`
inherited

Toplevel invocation for the scan.

Inherited from: \DirectoryScanner::slowScan()

Returns immediately if a slow scan has already been requested.

Documentation was generated by DocBlox 0.15.1.