2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-08-06 02:14:58 +02:00
|
|
|
/**
|
|
|
|
* Define a constant for the name of the manifest file
|
|
|
|
*/
|
2008-08-12 23:59:32 +02:00
|
|
|
if(!defined('MANIFEST_FILE')) define("MANIFEST_FILE", TEMP_FOLDER . "/manifest-" . str_replace('.php','',basename($_SERVER['SCRIPT_FILENAME'])));
|
2008-02-25 02:06:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The ManifestBuilder class generates the manifest file and keeps it fresh.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* The manifest file is a PHP include that contains global variables that
|
|
|
|
* represent the collected contents of the application:
|
|
|
|
* - all classes ({@link __autoload()})
|
|
|
|
* - all templates ({@link SSViewer})
|
|
|
|
* - all _config.php files
|
|
|
|
*
|
|
|
|
* Traversing the filesystem to collect this information on everypage.
|
|
|
|
* This information is cached so that it need not be regenerated on every
|
|
|
|
* pageview.
|
|
|
|
*
|
|
|
|
* @see main.php, __autoload(), SSViewer, Requirements::themedCSS()
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage core
|
2007-09-15 23:29:52 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class ManifestBuilder {
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $restrict_to_modules = array();
|
2008-02-25 03:10:37 +01:00
|
|
|
static $extendsArray = array();
|
|
|
|
static $classArray = array();
|
|
|
|
static $implementsArray = array();
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2007-09-15 01:03:29 +02:00
|
|
|
/**
|
|
|
|
* @var array $ignore_files Full filenames (without directory-path) which
|
|
|
|
* should be ignored by the manifest.
|
|
|
|
*/
|
|
|
|
public static $ignore_files = array(
|
|
|
|
'main.php',
|
|
|
|
'cli-script.php',
|
|
|
|
'install.php',
|
|
|
|
'index.php',
|
2008-11-06 05:51:25 +01:00
|
|
|
'rewritetest.php',
|
2007-09-15 01:03:29 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array $ignore_folders Foldernames (without path) which
|
|
|
|
* should be ignored by the manifest.
|
|
|
|
*/
|
|
|
|
public static $ignore_folders = array(
|
2008-02-25 03:10:37 +01:00
|
|
|
'mysql',
|
|
|
|
'assets',
|
|
|
|
'shortstat',
|
2007-10-02 06:58:26 +02:00
|
|
|
'HTML',
|
2007-09-15 01:03:29 +02:00
|
|
|
);
|
2008-03-26 10:23:51 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-11-06 05:51:25 +01:00
|
|
|
* Include the manifest, regenerating it if necessary
|
2008-09-28 21:52:06 +02:00
|
|
|
*/
|
2008-11-06 05:51:25 +01:00
|
|
|
static function include_manifest() {
|
|
|
|
if(isset($_REQUEST['usetestmanifest'])) {
|
|
|
|
self::load_test_manifest();
|
|
|
|
} else {
|
2008-12-04 23:38:32 +01:00
|
|
|
// The dev/build reference is some coupling but it solves an annoying bug
|
|
|
|
if(!file_exists(MANIFEST_FILE) || (filemtime(MANIFEST_FILE) < filemtime(BASE_PATH))
|
|
|
|
|| isset($_GET['flush']) || (isset($_REQUEST['url']) && ($_REQUEST['url'] == 'dev/build'
|
|
|
|
|| $_REQUEST['url'] == BASE_URL . '/dev/build'))) {
|
2008-11-06 05:51:25 +01:00
|
|
|
self::create_manifest_file();
|
|
|
|
}
|
|
|
|
require_once(MANIFEST_FILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a copy of the manifest with tests/ folders included.
|
|
|
|
* Only loads the ClassInfo and __autoload() globals; this assumes that _config.php files are already included.
|
|
|
|
*/
|
|
|
|
static function load_test_manifest() {
|
2009-08-05 06:48:58 +02:00
|
|
|
$testManifestFile = MANIFEST_FILE . '-test';
|
|
|
|
|
|
|
|
// The dev/build reference is some coupling but it solves an annoying bug
|
|
|
|
if(!file_exists($testManifestFile)
|
2009-08-21 05:03:16 +02:00
|
|
|
|| (filemtime($testManifestFile) < filemtime(BASE_PATH))
|
2009-08-05 06:48:58 +02:00
|
|
|
|| isset($_GET['flush'])) {
|
2009-08-21 05:03:16 +02:00
|
|
|
|
2009-08-05 06:48:58 +02:00
|
|
|
// Build the manifest, including the tests/ folders
|
|
|
|
$manifestInfo = self::get_manifest_info(BASE_PATH);
|
|
|
|
$manifest = self::generate_php_file($manifestInfo);
|
|
|
|
if($fh = fopen($testManifestFile, "w")) {
|
|
|
|
fwrite($fh, $manifest);
|
|
|
|
fclose($fh);
|
|
|
|
} else {
|
|
|
|
user_error("Cannot write manifest file! Check permissions of " . MANIFEST_FILE, E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require($testManifestFile);
|
2008-11-06 05:51:25 +01:00
|
|
|
}
|
2008-09-28 21:52:06 +02:00
|
|
|
|
|
|
|
/**
|
2008-11-06 05:51:25 +01:00
|
|
|
* Loads all PHP class files - actually opening them and executing them.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-06 05:51:25 +01:00
|
|
|
static function load_all_classes() {
|
|
|
|
global $_CLASS_MANIFEST;
|
|
|
|
foreach($_CLASS_MANIFEST as $classFile) require_once($classFile);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-09-28 21:52:06 +02:00
|
|
|
* Generates a new manifest file and saves it to {@link MANIFEST_FILE}.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-06 21:57:07 +01:00
|
|
|
static function create_manifest_file() {
|
2008-11-06 05:51:25 +01:00
|
|
|
// Build the manifest, ignoring the tests/ folders
|
|
|
|
$manifestInfo = self::get_manifest_info(BASE_PATH, array("tests"));
|
2008-08-20 06:47:48 +02:00
|
|
|
|
|
|
|
$manifest = self::generate_php_file($manifestInfo);
|
2008-11-06 22:01:12 +01:00
|
|
|
if($fh = fopen(MANIFEST_FILE, "w")) {
|
2008-03-26 10:23:51 +01:00
|
|
|
fwrite($fh, $manifest);
|
|
|
|
fclose($fh);
|
|
|
|
} else {
|
2008-11-06 21:57:07 +01:00
|
|
|
user_error("Cannot write manifest file! Check permissions of " . MANIFEST_FILE, E_USER_ERROR);
|
2008-03-26 10:23:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn an array produced by get_manifest_info() into the content of the manifest PHP include
|
|
|
|
*/
|
|
|
|
static function generate_php_file($manifestInfo) {
|
|
|
|
$output = "<?php\n";
|
|
|
|
|
|
|
|
foreach($manifestInfo['globals'] as $globalName => $globalVal) {
|
2008-11-06 05:51:25 +01:00
|
|
|
$output .= "global \$$globalName;\n\$$globalName = " . var_export($globalVal, true) . ";\n\n";
|
2008-03-26 10:23:51 +01:00
|
|
|
}
|
|
|
|
foreach($manifestInfo['require_once'] as $requireItem) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$output .= 'require_once("' . addslashes($requireItem) . "\");\n";
|
2008-03-26 10:23:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
2008-11-06 05:51:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the $manifestInfo array, updating the appropriate globals and loading the appropriate _config files.
|
|
|
|
*/
|
|
|
|
static function process_manifest($manifestInfo) {
|
|
|
|
foreach($manifestInfo['globals'] as $globalName => $globalVal) {
|
|
|
|
global $$globalName;
|
|
|
|
$$globalName = $globalVal;
|
|
|
|
}
|
|
|
|
foreach($manifestInfo['require_once'] as $requireItem) {
|
|
|
|
require_once("$requireItem");
|
|
|
|
}
|
|
|
|
}
|
2008-03-26 10:23:51 +01:00
|
|
|
|
2010-04-13 04:02:52 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2010-04-13 04:13:12 +02:00
|
|
|
* @return array Listing of theme directories
|
2010-04-13 04:02:52 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2010-04-13 04:13:12 +02:00
|
|
|
if($include) $themes[$file] = $file;
|
2010-04-13 04:02:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir($handle);
|
|
|
|
}
|
|
|
|
return $themes;
|
|
|
|
}
|
|
|
|
|
2008-03-26 10:23:51 +01:00
|
|
|
/**
|
|
|
|
* Return an array containing information for the manifest
|
2008-11-06 05:51:25 +01:00
|
|
|
* @param $baseDir The root directory to analyse
|
|
|
|
* @param $excludedFolders An array folder names to exclude. These don't care about where the
|
|
|
|
* folder appears in the hierarchy, so be careful
|
2008-03-26 10:23:51 +01:00
|
|
|
*/
|
2008-11-06 05:51:25 +01:00
|
|
|
static function get_manifest_info($baseDir, $excludedFolders = array()) {
|
2007-09-15 23:29:52 +02:00
|
|
|
// locate and include the exclude files
|
|
|
|
$topLevel = scandir($baseDir);
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($topLevel as $file) {
|
2008-02-25 03:10:37 +01:00
|
|
|
if($file[0] == '.') continue
|
|
|
|
|
2008-10-01 06:27:48 +02:00
|
|
|
$fullPath = '';
|
2007-07-19 12:40:28 +02:00
|
|
|
$fullPath = $baseDir . '/' . $file;
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2008-10-01 06:27:48 +02:00
|
|
|
if(@is_dir($fullPath . '/') && file_exists($fullPath . '/_exclude.php')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
require_once($fullPath . '/_exclude.php');
|
2008-10-01 06:27:48 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-11-06 05:51:25 +01:00
|
|
|
|
|
|
|
// Project - used to give precedence to template files
|
|
|
|
$project = null;
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
// Class, CSS, template manifest
|
2009-08-05 06:48:58 +02:00
|
|
|
$allPhpFiles = array();
|
2008-11-06 05:51:25 +01:00
|
|
|
$templateManifest = array();
|
|
|
|
$cssManifest = array();
|
|
|
|
|
|
|
|
|
2007-09-15 23:29:52 +02:00
|
|
|
if(is_array(self::$restrict_to_modules) && count(self::$restrict_to_modules)) {
|
|
|
|
// $restrict_to_modules is set, so we include only those specified
|
|
|
|
// modules
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach(self::$restrict_to_modules as $module)
|
2009-08-05 06:48:58 +02:00
|
|
|
ManifestBuilder::get_all_php_files($baseDir . '/' . $module, $excludedFolders, $allPhpFiles);
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2007-09-15 23:29:52 +02:00
|
|
|
// Include all directories which have an _config.php file but don't
|
|
|
|
// have an _manifest_exclude file
|
2007-07-19 12:40:28 +02:00
|
|
|
$topLevel = scandir($baseDir);
|
|
|
|
foreach($topLevel as $filename) {
|
2008-02-25 03:10:37 +01:00
|
|
|
if($filename[0] == '.') continue;
|
2008-11-06 05:51:25 +01:00
|
|
|
if($filename == 'themes') continue;
|
2009-05-08 05:29:41 +02:00
|
|
|
if($filename == 'assets') continue;
|
2008-11-06 05:51:25 +01:00
|
|
|
if(in_array($filename, $excludedFolders)) continue;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if(@is_dir("$baseDir/$filename") &&
|
|
|
|
file_exists("$baseDir/$filename/_config.php") &&
|
|
|
|
!file_exists("$baseDir/$filename/_manifest_exclude")) {
|
2008-11-06 05:51:25 +01:00
|
|
|
|
|
|
|
// Get classes, templates, and CSS files
|
2009-08-05 06:48:58 +02:00
|
|
|
ManifestBuilder::get_all_php_files("$baseDir/$filename", $excludedFolders, $allPhpFiles);
|
2008-11-06 05:51:25 +01:00
|
|
|
ManifestBuilder::getTemplateManifest($baseDir, $filename, $excludedFolders, $templateManifest, $cssManifest);
|
|
|
|
|
|
|
|
// List the _config.php files
|
|
|
|
$manifestInfo["require_once"][] = "$baseDir/$filename/_config.php";
|
|
|
|
// Find the $project variable in the relevant config file without having to execute the config file
|
|
|
|
if(preg_match("/\\\$project\s*=\s*[^\n\r]+[\n\r]/", file_get_contents("$baseDir/$filename/_config.php"), $parts)) {
|
|
|
|
eval($parts[0]);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Get themes
|
|
|
|
if(file_exists("$baseDir/themes")) {
|
2010-04-13 04:02:52 +02:00
|
|
|
$themeDirs = self::get_themes("$baseDir/themes", true);
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($themeDirs as $themeDir) {
|
|
|
|
$themeName = strtok($themeDir, '_');
|
2008-11-06 05:51:25 +01:00
|
|
|
ManifestBuilder::getTemplateManifest($baseDir, "themes/$themeDir", $excludedFolders, $templateManifest, $cssManifest, $themeName);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
// Build class-info array from class manifest
|
2009-08-05 06:48:58 +02:00
|
|
|
$allClasses = ManifestBuilder::allClasses($allPhpFiles);
|
|
|
|
|
|
|
|
// Pull the class filenames out
|
|
|
|
$classManifest = $allClasses['file'];
|
|
|
|
unset($allClasses['file']);
|
2008-11-06 05:51:25 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Ensure that any custom templates get favoured
|
2008-11-06 05:51:25 +01:00
|
|
|
if(!$project) user_error("\$project isn't set", E_USER_WARNING);
|
2009-06-27 08:03:30 +02:00
|
|
|
else if(!file_exists("$baseDir/$project")) user_error("\$project is set to '$project' but no such folder exists.", E_USER_WARNING);
|
|
|
|
else ManifestBuilder::getTemplateManifest($baseDir, $project, $excludedFolders, $templateManifest, $cssManifest);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
$manifestInfo["globals"]["_CLASS_MANIFEST"] = $classManifest;
|
|
|
|
$manifestInfo["globals"]["_ALL_CLASSES"] = $allClasses;
|
2008-03-26 10:23:51 +01:00
|
|
|
$manifestInfo["globals"]["_TEMPLATE_MANIFEST"] = $templateManifest;
|
|
|
|
$manifestInfo["globals"]["_CSS_MANIFEST"] = $cssManifest;
|
|
|
|
|
|
|
|
return $manifestInfo;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-08-05 06:48:58 +02:00
|
|
|
* Generates a list of all the PHP files that should be analysed by the manifest builder.
|
2007-09-15 23:29:52 +02:00
|
|
|
*
|
|
|
|
* @param string $folder The folder to traverse (recursively)
|
2008-02-25 03:10:37 +01:00
|
|
|
* @param array $classMap The already built class map
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-08-05 06:48:58 +02:00
|
|
|
private static function get_all_php_files($folder, $excludedFolders, &$allPhpFiles) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = scandir($folder);
|
|
|
|
if($items) foreach($items as $item) {
|
2007-09-15 23:29:52 +02:00
|
|
|
// Skip some specific PHP files
|
2007-09-15 01:03:29 +02:00
|
|
|
if(in_array($item, self::$ignore_files)) continue;
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-09-15 01:03:29 +02:00
|
|
|
// ignore hidden files and folders
|
2007-07-19 12:40:28 +02:00
|
|
|
if(substr($item,0,1) == '.') continue;
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-09-15 01:03:29 +02:00
|
|
|
// ignore files without php-extension
|
2008-02-25 03:10:37 +01:00
|
|
|
if(substr($item,-4) != '.php' && !@is_dir("$folder/$item")) continue;
|
|
|
|
|
2007-09-15 01:03:29 +02:00
|
|
|
// ignore files and folders with underscore-prefix
|
|
|
|
if(substr($item,0,1) == '_') continue;
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
// ignore certain directories
|
|
|
|
if(@is_dir("$folder/$item") && in_array($item, self::$ignore_folders)) continue;
|
2007-09-15 23:29:52 +02:00
|
|
|
|
|
|
|
// ignore directories with _manifest_exlude file
|
2008-02-25 03:10:37 +01:00
|
|
|
if(@is_dir("$folder/$item") && file_exists("$folder/$item/_manifest_exclude")) continue;
|
|
|
|
|
2007-09-15 22:06:42 +02:00
|
|
|
// i18n: ignore language files (loaded on demand)
|
2008-02-25 03:10:37 +01:00
|
|
|
if($item == 'lang' && @is_dir("$folder/$item") && ereg_replace("/[^/]+/\\.\\.","",$folder.'/..') == Director::baseFolder()) continue;
|
|
|
|
|
|
|
|
if(@is_dir("$folder/$item")) {
|
2008-11-06 05:51:25 +01:00
|
|
|
// Folder exclusion - used to skip over tests/ folders
|
|
|
|
if(in_array($item, $excludedFolders)) continue;
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
// recurse into directories (if not in $ignore_folders)
|
2009-08-05 06:48:58 +02:00
|
|
|
ManifestBuilder::get_all_php_files("$folder/$item", $excludedFolders, $allPhpFiles);
|
2007-09-15 01:03:29 +02:00
|
|
|
} else {
|
2009-08-05 06:48:58 +02:00
|
|
|
$allPhpFiles[] = "$folder/$item";
|
2007-09-15 01:03:29 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-15 23:29:52 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-15 23:29:52 +02:00
|
|
|
* Generates the template manifest - a list of all the .SS files in the
|
|
|
|
* application
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-11-06 05:51:25 +01:00
|
|
|
private static function getTemplateManifest($baseDir, $folder, $excludedFolders, &$templateManifest, &$cssManifest, $themeName = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = scandir("$baseDir/$folder");
|
|
|
|
if($items) foreach($items as $item) {
|
|
|
|
if(substr($item,0,1) == '.') continue;
|
|
|
|
if(substr($item,-3) == '.ss') {
|
|
|
|
$templateName = substr($item, 0, -3);
|
|
|
|
$templateType = substr($folder,strrpos($folder,'/')+1);
|
|
|
|
if($templateType == "templates") $templateType = "main";
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($themeName) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$templateManifest[$templateName]['themes'][$themeName][$templateType] = "$baseDir/$folder/$item";
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2008-02-25 03:10:37 +01:00
|
|
|
$templateManifest[$templateName][$templateType] = "$baseDir/$folder/$item";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if(substr($item,-4) == '.css') {
|
|
|
|
$cssName = substr($item, 0, -4);
|
2007-07-20 01:15:05 +02:00
|
|
|
// Debug::message($item);
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($themeName) {
|
2008-02-25 03:10:37 +01:00
|
|
|
$cssManifest[$cssName]['themes'][$themeName] = "$folder/$item";
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2008-02-25 03:10:37 +01:00
|
|
|
$cssManifest[$cssName]['unthemed'] = "$folder/$item";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
} else if(@is_dir("$baseDir/$folder/$item")) {
|
2008-11-06 05:51:25 +01:00
|
|
|
// Folder exclusion - used to skip over tests/ folders
|
|
|
|
if(in_array($item, $excludedFolders)) continue;
|
|
|
|
|
|
|
|
ManifestBuilder::getTemplateManifest($baseDir, "$folder/$item", $excludedFolders, $templateManifest, $cssManifest, $themeName);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-15 23:29:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include everything, so that actually *all* classes are available and
|
2008-11-06 05:51:25 +01:00
|
|
|
* build a map of classes and their subclasses
|
2008-08-20 06:47:48 +02:00
|
|
|
*
|
|
|
|
* @param $classManifest An array of all Sapphire classes; keys are class names and values are filenames
|
2007-09-15 23:29:52 +02:00
|
|
|
*
|
|
|
|
* @return array Returns an array that holds all class relevant
|
|
|
|
* information.
|
|
|
|
*/
|
2008-11-06 05:51:25 +01:00
|
|
|
private static function allClasses($classManifest) {
|
2008-03-26 10:23:51 +01:00
|
|
|
self::$classArray = array();
|
|
|
|
self::$extendsArray = array();
|
|
|
|
self::$implementsArray = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Include everything, so we actually have *all* classes
|
|
|
|
foreach($classManifest as $file) {
|
|
|
|
$b = basename($file);
|
2007-09-15 23:29:52 +02:00
|
|
|
if($b != 'cli-script.php' && $b != 'main.php')
|
2008-02-25 03:10:37 +01:00
|
|
|
self::parse_file($file);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
$allClasses["parents"] = self::find_parents();
|
|
|
|
$allClasses["children"] = self::find_children();
|
|
|
|
$allClasses["implementors"] = self::$implementsArray;
|
|
|
|
|
|
|
|
foreach(self::$classArray as $class => $info) {
|
|
|
|
$allClasses['exists'][$class] = $class;
|
2009-08-05 06:48:58 +02:00
|
|
|
$allClasses['file'][$class] = $info['file'];
|
2007-08-20 01:47:49 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Build a map of classes and their subclasses
|
|
|
|
$_classes = get_declared_classes();
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($_classes as $class) {
|
|
|
|
$allClasses['exists'][$class] = $class;
|
|
|
|
foreach($_classes as $subclass) {
|
2008-02-25 03:10:37 +01:00
|
|
|
if(is_subclass_of($class, $subclass)) $allClasses['parents'][$class][$subclass] = $subclass;
|
|
|
|
if(is_subclass_of($subclass, $class)) $allClasses['children'][$class][$subclass] = $subclass;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-03-26 10:23:51 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $allClasses;
|
|
|
|
}
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2008-11-06 05:51:25 +01:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Parses a php file and adds any class or interface information into self::$classArray
|
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
*/
|
|
|
|
private static function parse_file($filename) {
|
|
|
|
$file = file_get_contents($filename);
|
|
|
|
|
|
|
|
$implements = "";
|
|
|
|
$extends = "";
|
|
|
|
$class="";
|
|
|
|
|
2010-04-13 04:15:37 +02:00
|
|
|
if($file === null) user_error("ManifestBuilder::parse_file(): Couldn't open $filename", E_USER_ERROR);
|
|
|
|
if(!$file) return;
|
2008-04-07 06:42:35 +02:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
// We cache the parse results of each file, since only a few files will have changed between flushings
|
|
|
|
// And, although it's accurate, TokenisedRegularExpression isn't particularly fast
|
|
|
|
$parseCacheFile = TEMP_FOLDER . "/manifestClassParse-" . str_replace(array("/",":", "\\"),"_", realpath($filename));
|
|
|
|
if(!file_exists($parseCacheFile) || filemtime($parseCacheFile) < filemtime($filename)) {
|
|
|
|
$tokens = token_get_all($file);
|
|
|
|
$classes = self::getClassDefParser()->findAll($tokens);
|
|
|
|
$interfaces = self::getInterfaceDefParser()->findAll($tokens);
|
|
|
|
|
|
|
|
$cacheContent = '<?php
|
|
|
|
$classes = ' . var_export($classes,true) . ';
|
|
|
|
$interfaces = ' . var_export($interfaces,true) . ';';
|
|
|
|
if($fh = fopen($parseCacheFile,'w')) {
|
|
|
|
fwrite($fh, $cacheContent);
|
|
|
|
fclose($fh);
|
2008-04-07 06:42:35 +02:00
|
|
|
}
|
2008-04-08 08:17:58 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
include($parseCacheFile);
|
2008-04-07 06:42:35 +02:00
|
|
|
}
|
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
foreach($classes as $class) {
|
|
|
|
$className = $class['className'];
|
|
|
|
unset($class['className']);
|
|
|
|
$class['file'] = $filename;
|
|
|
|
if(!isset($class['extends'])) $class['extends'] = null;
|
|
|
|
|
|
|
|
if($class['extends']) self::$extendsArray[$class['extends']][$className] = $className;
|
|
|
|
if(isset($class['interfaces'])) foreach($class['interfaces'] as $interface) {
|
|
|
|
self::$implementsArray[$interface][$className] = $className;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2008-04-08 08:17:58 +02:00
|
|
|
|
2009-08-05 06:48:58 +02:00
|
|
|
if(isset(self::$classArray[$className])) {
|
|
|
|
$file1 = self::$classArray[$className]['file'];
|
|
|
|
$file2 = $class[$className];
|
|
|
|
user_error("There are two files both containing the same class: '$file1' and " .
|
|
|
|
"'$file2'. This might mean that the wrong code is being used.", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
self::$classArray[$className] = $class;
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
foreach($interfaces as $interface) {
|
|
|
|
$className = $interface['interfaceName'];
|
|
|
|
unset($interface['interfaceName']);
|
|
|
|
$interface['file'] = $filename;
|
|
|
|
if(!isset($interface['extends'])) $interface['extends'] = null;
|
2009-08-05 06:48:58 +02:00
|
|
|
|
|
|
|
if(isset(self::$classArray[$className])) {
|
|
|
|
$file1 = self::$classArray[$className]['file'];
|
|
|
|
$file2 = $interface[$className];
|
|
|
|
user_error("There are two files both containing the same class: '$file1' and " .
|
|
|
|
"'$file2'. This might mean that the wrong code is being used.", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
self::$classArray[$className] = $interface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a {@link TokenisedRegularExpression} object that will parse class definitions
|
|
|
|
* @return TokenisedRegularExpression
|
|
|
|
*/
|
|
|
|
public static function getClassDefParser() {
|
2008-11-06 05:51:25 +01:00
|
|
|
require_once('core/TokenisedRegularExpression.php');
|
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
return new TokenisedRegularExpression(array(
|
|
|
|
0 => T_CLASS,
|
|
|
|
1 => T_WHITESPACE,
|
|
|
|
2 => array(T_STRING, 'can_jump_to' => array(7, 14), 'save_to' => 'className'),
|
|
|
|
3 => T_WHITESPACE,
|
|
|
|
4 => T_EXTENDS,
|
|
|
|
5 => T_WHITESPACE,
|
|
|
|
6 => array(T_STRING, 'save_to' => 'extends', 'can_jump_to' => 14),
|
|
|
|
7 => T_WHITESPACE,
|
|
|
|
8 => T_IMPLEMENTS,
|
|
|
|
9 => T_WHITESPACE,
|
|
|
|
10 => array(T_STRING, 'can_jump_to' => 14, 'save_to' => 'interfaces[]'),
|
|
|
|
11 => array(T_WHITESPACE, 'optional' => true),
|
|
|
|
12 => array(',', 'can_jump_to' => 10),
|
|
|
|
13 => array(T_WHITESPACE, 'can_jump_to' => 10),
|
|
|
|
14 => array(T_WHITESPACE, 'optional' => true),
|
|
|
|
15 => '{',
|
|
|
|
));
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
/**
|
|
|
|
* Returns a {@link TokenisedRegularExpression} object that will parse class definitions
|
|
|
|
* @return TokenisedRegularExpression
|
|
|
|
*/
|
|
|
|
public static function getInterfaceDefParser() {
|
2008-11-06 05:51:25 +01:00
|
|
|
require_once('core/TokenisedRegularExpression.php');
|
|
|
|
|
2008-04-08 08:17:58 +02:00
|
|
|
return new TokenisedRegularExpression(array(
|
|
|
|
0 => T_INTERFACE,
|
|
|
|
1 => T_WHITESPACE,
|
|
|
|
2 => array(T_STRING, 'can_jump_to' => 7, 'save_to' => 'interfaceName'),
|
|
|
|
3 => T_WHITESPACE,
|
|
|
|
4 => T_EXTENDS,
|
|
|
|
5 => T_WHITESPACE,
|
|
|
|
6 => array(T_STRING, 'save_to' => 'extends'),
|
|
|
|
7 => array(T_WHITESPACE, 'optional' => true),
|
|
|
|
8 => '{',
|
|
|
|
));
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2008-04-08 08:17:58 +02:00
|
|
|
|
2007-09-15 23:29:52 +02:00
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Moves through self::$classArray and creates an array containing parent data
|
|
|
|
*
|
|
|
|
* @return array
|
2007-09-15 23:29:52 +02:00
|
|
|
*/
|
2008-02-25 03:10:37 +01:00
|
|
|
private static function find_parents() {
|
|
|
|
$parentArray = array();
|
|
|
|
foreach(self::$classArray as $class => $info) {
|
|
|
|
$extendArray = array();
|
|
|
|
|
|
|
|
$parent = $info["extends"];
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
while($parent) {
|
|
|
|
$extendArray[$parent] = $parent;
|
|
|
|
$parent = isset(self::$classArray[$parent]["extends"]) ? self::$classArray[$parent]["extends"] : null;
|
|
|
|
}
|
|
|
|
$parentArray[$class] = array_reverse($extendArray);
|
|
|
|
}
|
|
|
|
return $parentArray;
|
|
|
|
}
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* Iterates through self::$classArray and returns an array with any descendant data
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function find_children() {
|
|
|
|
$childrenArray = array();
|
|
|
|
foreach(self::$extendsArray as $class => $children) {
|
|
|
|
$allChildren = $children;
|
|
|
|
foreach($children as $childName) {
|
|
|
|
$allChildren = array_merge($allChildren, self::up_children($childName));
|
|
|
|
}
|
|
|
|
$childrenArray[$class] = $allChildren;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
return $childrenArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to find all children of give class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function get_children($class) {
|
|
|
|
return isset(self::$extendsArray[$class]) ? self::$extendsArray[$class] : array();
|
|
|
|
}
|
2008-11-22 04:33:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the Manifest has been included
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
static function has_been_included() {
|
|
|
|
global $_CLASS_MANIFEST, $_TEMPLATE_MANIFEST, $_CSS_MANIFEST, $_ALL_CLASSES;
|
|
|
|
return (bool)(empty($_CLASS_MANIFEST) && empty($_TEMPLATE_MANIFEST) && empty($_CSS_MANIFEST) && empty($_ALL_CLASSES));
|
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a flat array with all children of a given class
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param array $results
|
|
|
|
*/
|
|
|
|
function up_children($class) {
|
|
|
|
$children = self::get_Children($class);
|
|
|
|
$results = $children;
|
|
|
|
foreach($children as $className) {
|
|
|
|
$results = array_merge($results, self::up_children($className));
|
|
|
|
}
|
|
|
|
return $results;;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-15 23:29:52 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|