diff --git a/core/ManifestBuilder.php b/core/ManifestBuilder.php index 5f19c2732..edf5c26c0 100644 --- a/core/ManifestBuilder.php +++ b/core/ManifestBuilder.php @@ -148,6 +148,34 @@ class ManifestBuilder { } } + /** + * Get themes from a particular directory. + * + * @param string $baseDir Optional: Absolute path to theme directory for testing e.g. "/Users/sharvey/Sites/test24/themes" + * @param boolean $includeSubThemes If set to TRUE, sub-themes such as "blackcandy_blog" are included too + * @return array indexed array of theme directories + */ + public static function get_themes($baseDir = null, $includeSubThemes = false) { + // If no base directory specified, the default is the project root + if(!$baseDir) $baseDir = BASE_PATH . DIRECTORY_SEPARATOR . THEMES_DIR; + $themes = array(); + $handle = opendir($baseDir); + if($handle) { + while(false !== ($file = readdir($handle))) { + $fullPath = $baseDir . DIRECTORY_SEPARATOR . $file; + if(strpos($file, '.') === false && is_dir($fullPath)) { + $include = $includeSubThemes ? true : false; + if(strpos($file, '_') === false) { + $include = true; + } + if($include) $themes[] = $file; + } + } + closedir($handle); + } + return $themes; + } + /** * Return an array containing information for the manifest * @param $baseDir The root directory to analyse @@ -206,17 +234,14 @@ class ManifestBuilder { if(preg_match("/\\\$project\s*=\s*[^\n\r]+[\n\r]/", file_get_contents("$baseDir/$filename/_config.php"), $parts)) { eval($parts[0]); } - } } } // Get themes if(file_exists("$baseDir/themes")) { - $themeDirs = scandir("$baseDir/themes"); + $themeDirs = self::get_themes("$baseDir/themes", true); foreach($themeDirs as $themeDir) { - if(substr($themeDir,0,1) == '.') continue; - // The theme something_forum is understood as being a part of the theme something $themeName = strtok($themeDir, '_'); ManifestBuilder::getTemplateManifest($baseDir, "themes/$themeDir", $excludedFolders, $templateManifest, $cssManifest, $themeName); } diff --git a/tests/ManifestBuilderTest.php b/tests/ManifestBuilderTest.php index 3b9dc16b5..3edf5059b 100644 --- a/tests/ManifestBuilderTest.php +++ b/tests/ManifestBuilderTest.php @@ -127,6 +127,55 @@ class ManifestBuilderTest extends SapphireTest { global $project; } + function testThemeRetrieval() { + $ds = DIRECTORY_SEPARATOR; + $testThemeBaseDir = TEMP_FOLDER . $ds . 'test-themes'; + + // If the test directory somehow exists after a failed test, remove all the files and directories inside + if(file_exists($testThemeBaseDir)) { + $handle = opendir($testThemeBaseDir); + while(false !== ($file = readdir($handle))) { + $fullPath = $testThemeBaseDir . $ds . $file; + if(strpos($file, '.') === false) { + if(is_dir($fullPath)) rmdir($fullPath); + else unlink($fullPath); + } + } + closedir($handle); + rmdir($testThemeBaseDir); + } + + mkdir($testThemeBaseDir); + mkdir($testThemeBaseDir . $ds . 'blackcandy'); + mkdir($testThemeBaseDir . $ds . 'blackcandy_blog'); + mkdir($testThemeBaseDir . $ds . 'darkshades'); + mkdir($testThemeBaseDir . $ds . 'darkshades_blog'); + + $this->assertEquals(array( + 'blackcandy', + 'darkshades' + ), ManifestBuilder::get_themes($testThemeBaseDir), 'Our test theme directory contains 2 themes'); + + $this->assertEquals(array( + 'blackcandy', + 'blackcandy_blog', + 'darkshades', + 'darkshades_blog' + ), ManifestBuilder::get_themes($testThemeBaseDir, true), 'Our test theme directory contains 2 themes and 2 sub-themes'); + + // Remove all the test themes we created + $handle = opendir($testThemeBaseDir); + while(false !== ($file = readdir($handle))) { + $fullPath = $testThemeBaseDir . $ds . $file; + if(strpos($file, '.') === false) { + if(is_dir($fullPath)) rmdir($fullPath); + else unlink($fullPath); + } + } + closedir($handle); + rmdir($testThemeBaseDir); + } + function tearDown() { global $_CLASS_MANIFEST, $_ALL_CLASSES, $project;