Skip to content

Commit

Permalink
The autoloader will now search for files inside the directory of any …
Browse files Browse the repository at this point in the history
…loaded coding standard (ref #1430)
  • Loading branch information
gsherwood committed Apr 27, 2017
1 parent 267201e commit 52e9332
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class Autoload
*/
private static $loadedFiles = array();

/**
* A list of additional directories to search during autoloading.
*
* This is typically a list of coding standard directories.
*
* @var string[]
*/
private static $searchPaths = array();


/**
* Loads a class.
Expand Down Expand Up @@ -98,6 +107,18 @@ public static function load($class)
$path = self::$composerAutoloader->findFile($class);
}

// See if the class is inside one of our alternate search paths.
if ($path === false) {
foreach (self::$searchPaths as $searchPath) {
$path = $searchPath.$ds.str_replace('\\', $ds, $class).'.php';
if (is_file($path) === true) {
break;
}

$path = false;
}
}

if ($path !== false && is_file($path) === true) {
self::loadFile($path);
return true;
Expand Down Expand Up @@ -170,6 +191,20 @@ public static function loadFile($path)
}//end loadFile()


/**
* Adds a directory to search during autoloading.
*
* @param string $path The path to the directory to search.
*
* @return void
*/
public static function addSearchPath($path)
{
self::$searchPaths[] = $path;

}//end addSearchPath()


/**
* Gets the class name for the given file path.
*
Expand Down
4 changes: 4 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
-- This allows you to force errors to be shown that would otherwise be ignored by code comments
-- Also stop files being able to change sniff properties mid way through processing
- An error is now reported if no sniffs were registered to be run (request #1129)
- The autoloader will now search for files inside the directory of any loaded coding standard
-- This allows autoloading of any file inside a custom coding standard without manually requiring them
-- Ensure your namespace begins with your coding standard's directory name and follows PSR-4
-- e.g., StandardName\Sniffs\CategoryName\AbstractHelper or StandardName\Helpers\StringSniffHelper
- Fixed an error where STDIN was sometimes not checked when using the --parallel CLI option
- The is_closure index has been removed from the return value of File::getMethodProperties()
-- This value was always false becasue T_FUNCTION tokens are never closures
Expand Down
3 changes: 3 additions & 0 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public function __construct(Config $config)

$this->name .= $standardName;
$this->paths[] = $standard;

// Allow autoloading of custom files inside this standard.
Autoload::addSearchPath(dirname(dirname($standard)));
}

if (PHP_CODESNIFFER_VERBOSITY === 1) {
Expand Down

0 comments on commit 52e9332

Please sign in to comment.