ENHANCEMENT: Added a ManifestFileFinder class, which is an extension to SS_FileFinder which takes into account modules, exclude files and lang/test/themes/assets dirs.

This commit is contained in:
ajshort 2011-02-27 22:20:20 +11:00
parent 852920237e
commit ecc6840344
11 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,65 @@
<?php
/**
* An extension to the default file finder with some extra filters to faciliate
* autoload and template manifest generation:
* - Only modules with _config.php files arescanned.
* - If a _manifest_exclude file is present inside a directory it is ignored.
* - Assets and module language directories are ignored.
* - Module tests directories are skipped if the ignore_tests option is not
* set to false.
*
* @package sapphire
* @subpackage manifest
*/
class ManifestFileFinder extends SS_FileFinder {
const CONFIG_FILE = '_config.php';
const EXCLUDE_FILE = '_manifest_exclude';
const LANG_DIR = 'lang';
const TESTS_DIR = 'tests';
public static $default_options = array(
'include_themes' => false,
'ignore_tests' => true,
'min_depth' => 1
);
public function acceptDir($basename, $pathname, $depth) {
// Skip over the assets directory in the site root.
if ($depth == 1 && $basename == ASSETS_DIR) {
return false;
}
// Skip over any lang directories in the top level of the module.
if ($depth == 2 && $basename == self::LANG_DIR) {
return false;
}
// If we're not in testing mode, then skip over the tests directory in
// the module root.
if ($this->getOption('ignore_tests') && $depth == 2 && $basename == self::TESTS_DIR) {
return false;
}
// Ignore any directories which contain a _manifest_exclude file.
if (file_exists($pathname . '/' . self::EXCLUDE_FILE)) {
return false;
}
// Only include top level module directories which have a configuration
// _config.php file. However, if we're in themes mode then include
// the themes dir without a config file.
$lackingConfig = (
$depth == 1
&& !($this->getOption('include_themes') && $basename == THEMES_DIR)
&& !file_exists($pathname . '/' . self::CONFIG_FILE)
);
if ($lackingConfig) {
return false;
}
return parent::acceptDir($basename, $pathname, $depth);
}
}

View File

@ -0,0 +1,61 @@
<?php
/**
* Tests for the {@link ManifestFileFinder} class.
*
* @package sapphire
* @subpackage tests
*/
class ManifestFileFinderTest extends SapphireTest {
protected $base;
public function __construct() {
$this->base = dirname(__FILE__) . '/fixtures/manifestfilefinder';
parent::__construct();
}
public function assertFinderFinds($finder, $expect, $message = null) {
$found = $finder->find($this->base);
foreach ($expect as $k => $file) {
$expect[$k] = "{$this->base}/$file";
}
sort($expect);
sort($found);
$this->assertEquals($expect, $found, $message);
}
public function testBasicOperation() {
$finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/');
$this->assertFinderFinds($finder, array(
'module/module.txt'
));
}
public function testIgnoreTests() {
$finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/');
$finder->setOption('ignore_tests', false);
$this->assertFinderFinds($finder, array(
'module/module.txt',
'module/tests/tests.txt'
));
}
public function testIncludeThemes() {
$finder = new ManifestFileFinder();
$finder->setOption('name_regex', '/\.txt$/');
$finder->setOption('include_themes', true);
$this->assertFinderFinds($finder, array(
'module/module.txt',
'themes/themes.txt'
));
}
}

View File

@ -0,0 +1 @@
<?php

View File

@ -0,0 +1 @@
<?php